108 lines
2.9 KiB
Go
108 lines
2.9 KiB
Go
package userprofile
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"rijig/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type UserProfileRepository interface {
|
|
GetByID(ctx context.Context, userID string) (*model.User, error)
|
|
GetByRoleName(ctx context.Context, roleName string) ([]*model.User, error)
|
|
// GetIdentityCardsByUserRegStatus(ctx context.Context, userRegStatus string) ([]model.IdentityCard, error)
|
|
// GetCompanyProfileByUserRegStatus(ctx context.Context, userRegStatus string) ([]model.IdentityCard, error)
|
|
Update(ctx context.Context, userID string, user *model.User) error
|
|
}
|
|
|
|
type userProfileRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewUserProfileRepository(db *gorm.DB) UserProfileRepository {
|
|
return &userProfileRepository{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (r *userProfileRepository) GetByID(ctx context.Context, userID string) (*model.User, error) {
|
|
var user model.User
|
|
|
|
err := r.db.WithContext(ctx).
|
|
Preload("Role").
|
|
Where("id = ?", userID).
|
|
First(&user).Error
|
|
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrUserNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *userProfileRepository) GetByRoleName(ctx context.Context, roleName string) ([]*model.User, error) {
|
|
var users []*model.User
|
|
|
|
err := r.db.WithContext(ctx).
|
|
Preload("Role").
|
|
Joins("JOIN roles ON users.role_id = roles.id").
|
|
Where("roles.role_name = ?", roleName).
|
|
Find(&users).Error
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return users, nil
|
|
}
|
|
|
|
/* func (r *userProfileRepository) GetIdentityCardsByUserRegStatus(ctx context.Context, userRegStatus string) ([]model.IdentityCard, error) {
|
|
var identityCards []model.IdentityCard
|
|
|
|
if err := r.db.WithContext(ctx).
|
|
Joins("JOIN users ON identity_cards.user_id = users.id").
|
|
Where("users.registration_status = ?", userRegStatus).
|
|
Preload("User").
|
|
Find(&identityCards).Error; err != nil {
|
|
log.Printf("Error fetching identity cards by user registration status: %v", err)
|
|
return nil, fmt.Errorf("error fetching identity cards by user registration status: %w", err)
|
|
}
|
|
|
|
return identityCards, nil
|
|
}
|
|
|
|
func (r *userProfileRepository) GetCompanyProfileByUserRegStatus(ctx context.Context, userRegStatus string) ([]model.IdentityCard, error) {
|
|
var identityCards []model.IdentityCard
|
|
|
|
if err := r.db.WithContext(ctx).
|
|
Joins("JOIN users ON company_profiles.user_id = users.id").
|
|
Where("users.registration_status = ?", userRegStatus).
|
|
Preload("User").
|
|
Find(&identityCards).Error; err != nil {
|
|
log.Printf("Error fetching identity cards by user registration status: %v", err)
|
|
return nil, fmt.Errorf("error fetching identity cards by user registration status: %w", err)
|
|
}
|
|
return identityCards, nil
|
|
} */
|
|
|
|
func (r *userProfileRepository) Update(ctx context.Context, userID string, user *model.User) error {
|
|
result := r.db.WithContext(ctx).
|
|
Model(&model.User{}).
|
|
Where("id = ?", userID).
|
|
Updates(user)
|
|
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
|
|
if result.RowsAffected == 0 {
|
|
return ErrUserNotFound
|
|
}
|
|
|
|
return nil
|
|
}
|