mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 00:31:35 +00:00
40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
package repositories
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"gorm.io/gorm"
|
|
"tercul/internal/models"
|
|
)
|
|
|
|
// UserProfileRepository defines CRUD methods specific to UserProfile.
|
|
type UserProfileRepository interface {
|
|
BaseRepository[models.UserProfile]
|
|
GetByUserID(ctx context.Context, userID uint) (*models.UserProfile, error)
|
|
}
|
|
|
|
type userProfileRepository struct {
|
|
BaseRepository[models.UserProfile]
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewUserProfileRepository creates a new UserProfileRepository.
|
|
func NewUserProfileRepository(db *gorm.DB) UserProfileRepository {
|
|
return &userProfileRepository{
|
|
BaseRepository: NewBaseRepositoryImpl[models.UserProfile](db),
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
// GetByUserID finds a user profile by user ID
|
|
func (r *userProfileRepository) GetByUserID(ctx context.Context, userID uint) (*models.UserProfile, error) {
|
|
var profile models.UserProfile
|
|
if err := r.db.WithContext(ctx).Where("user_id = ?", userID).First(&profile).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrEntityNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &profile, nil
|
|
}
|