mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
- Core Go application with GraphQL API using gqlgen - Comprehensive data models for literary works, authors, translations - Repository pattern with caching layer - Authentication and authorization system - Linguistics analysis capabilities with multiple adapters - Vector search integration with Weaviate - Docker containerization support - Python data migration and analysis scripts - Clean architecture with proper separation of concerns - Production-ready configuration and middleware - Proper .gitignore excluding vendor/, database files, and build artifacts
63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
package repositories
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"gorm.io/gorm"
|
|
"tercul/models"
|
|
)
|
|
|
|
// UserRepository defines CRUD methods specific to User.
|
|
type UserRepository interface {
|
|
BaseRepository[models.User]
|
|
FindByUsername(ctx context.Context, username string) (*models.User, error)
|
|
FindByEmail(ctx context.Context, email string) (*models.User, error)
|
|
ListByRole(ctx context.Context, role models.UserRole) ([]models.User, error)
|
|
}
|
|
|
|
type userRepository struct {
|
|
BaseRepository[models.User]
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewUserRepository creates a new UserRepository.
|
|
func NewUserRepository(db *gorm.DB) UserRepository {
|
|
return &userRepository{
|
|
BaseRepository: NewBaseRepositoryImpl[models.User](db),
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
// FindByUsername finds a user by username
|
|
func (r *userRepository) FindByUsername(ctx context.Context, username string) (*models.User, error) {
|
|
var user models.User
|
|
if err := r.db.WithContext(ctx).Where("username = ?", username).First(&user).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrEntityNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
// FindByEmail finds a user by email
|
|
func (r *userRepository) FindByEmail(ctx context.Context, email string) (*models.User, error) {
|
|
var user models.User
|
|
if err := r.db.WithContext(ctx).Where("email = ?", email).First(&user).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrEntityNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
// ListByRole lists users by role
|
|
func (r *userRepository) ListByRole(ctx context.Context, role models.UserRole) ([]models.User, error) {
|
|
var users []models.User
|
|
if err := r.db.WithContext(ctx).Where("role = ?", role).Find(&users).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return users, nil
|
|
}
|