meti-backend/internal/repository/user_profile_repository.go
Aditya Siregar 9e95e8ee5e Init Eslogad
2025-08-09 15:09:43 +07:00

46 lines
1.4 KiB
Go

package repository
import (
"context"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"eslogad-be/internal/entities"
"github.com/google/uuid"
)
type UserProfileRepository struct {
db *gorm.DB
}
func NewUserProfileRepository(db *gorm.DB) *UserProfileRepository {
return &UserProfileRepository{db: db}
}
func (r *UserProfileRepository) GetByUserID(ctx context.Context, userID uuid.UUID) (*entities.UserProfile, error) {
var p entities.UserProfile
if err := r.db.WithContext(ctx).First(&p, "user_id = ?", userID).Error; err != nil {
return nil, err
}
return &p, nil
}
func (r *UserProfileRepository) Create(ctx context.Context, profile *entities.UserProfile) error {
return r.db.WithContext(ctx).Create(profile).Error
}
func (r *UserProfileRepository) Upsert(ctx context.Context, profile *entities.UserProfile) error {
return r.db.WithContext(ctx).Clauses(
clause.OnConflict{
Columns: []clause.Column{{Name: "user_id"}},
DoUpdates: clause.AssignmentColumns([]string{"full_name", "display_name", "phone", "avatar_url", "job_title", "employee_no", "bio", "timezone", "locale", "preferences", "notification_prefs"}),
},
).Create(profile).Error
}
func (r *UserProfileRepository) Update(ctx context.Context, profile *entities.UserProfile) error {
return r.db.WithContext(ctx).Model(&entities.UserProfile{}).Where("user_id = ?", profile.UserID).Updates(profile).Error
}