feat(repo): soft-delete User with status filter on queries
This commit is contained in:
@@ -19,6 +19,7 @@ func isDuplicateKeyError(err error) bool {
|
||||
type UserRepository interface {
|
||||
Create(ctx context.Context, user *model.User) error
|
||||
FindByID(ctx context.Context, id string) (*model.User, error)
|
||||
FindByIDIncludeDeleted(ctx context.Context, id string) (*model.User, error)
|
||||
FindByEmail(ctx context.Context, email string) (*model.User, error)
|
||||
FindByUsername(ctx context.Context, username string) (*model.User, error)
|
||||
Update(ctx context.Context, user *model.User) error
|
||||
@@ -47,6 +48,19 @@ func (r *userRepository) Create(ctx context.Context, user *model.User) error {
|
||||
}
|
||||
|
||||
func (r *userRepository) FindByID(ctx context.Context, id string) (*model.User, error) {
|
||||
var user model.User
|
||||
result := r.db.WithContext(ctx).First(&user, "id = ? AND status = ?", id, model.StatusActive)
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return nil, model.ErrNotFound
|
||||
}
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
// FindByIDIncludeDeleted finds a user by ID regardless of status.
|
||||
func (r *userRepository) FindByIDIncludeDeleted(ctx context.Context, id string) (*model.User, error) {
|
||||
var user model.User
|
||||
result := r.db.WithContext(ctx).First(&user, "id = ?", id)
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
@@ -60,7 +74,7 @@ func (r *userRepository) FindByID(ctx context.Context, id string) (*model.User,
|
||||
|
||||
func (r *userRepository) FindByEmail(ctx context.Context, email string) (*model.User, error) {
|
||||
var user model.User
|
||||
result := r.db.WithContext(ctx).First(&user, "email = ?", email)
|
||||
result := r.db.WithContext(ctx).First(&user, "email = ? AND status = ?", email, model.StatusActive)
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return nil, model.ErrNotFound
|
||||
}
|
||||
@@ -72,7 +86,7 @@ func (r *userRepository) FindByEmail(ctx context.Context, email string) (*model.
|
||||
|
||||
func (r *userRepository) FindByUsername(ctx context.Context, username string) (*model.User, error) {
|
||||
var user model.User
|
||||
result := r.db.WithContext(ctx).First(&user, "username = ?", username)
|
||||
result := r.db.WithContext(ctx).First(&user, "username = ? AND status = ?", username, model.StatusActive)
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return nil, model.ErrNotFound
|
||||
}
|
||||
@@ -94,7 +108,7 @@ func (r *userRepository) Update(ctx context.Context, user *model.User) error {
|
||||
}
|
||||
|
||||
func (r *userRepository) Delete(ctx context.Context, id string) error {
|
||||
result := r.db.WithContext(ctx).Delete(&model.User{}, "id = ?", id)
|
||||
result := r.db.WithContext(ctx).Model(&model.User{}).Where("id = ?", id).Update("status", model.StatusAdminDeleted)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
@@ -105,11 +119,11 @@ func (r *userRepository) List(ctx context.Context, offset, limit int) ([]model.U
|
||||
var users []model.User
|
||||
var total int64
|
||||
|
||||
if err := r.db.WithContext(ctx).Model(&model.User{}).Count(&total).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Model(&model.User{}).Where("status = ?", model.StatusActive).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
result := r.db.WithContext(ctx).Offset(offset).Limit(limit).Find(&users)
|
||||
result := r.db.WithContext(ctx).Where("status = ?", model.StatusActive).Offset(offset).Limit(limit).Find(&users)
|
||||
if result.Error != nil {
|
||||
return nil, 0, result.Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user