mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
commonrepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
type UserRepository interface {
|
|
commonrepo.BaseRepository[entity.User]
|
|
GetByIdUser(ctx context.Context, idUser int64, modifier func(*gorm.DB) *gorm.DB) (*entity.User, error)
|
|
UpsertByIdUser(ctx context.Context, user *entity.User) error
|
|
SoftDeleteByIdUser(ctx context.Context, idUser int64) error
|
|
}
|
|
|
|
type UserRepositoryImpl struct {
|
|
*commonrepo.BaseRepositoryImpl[entity.User]
|
|
}
|
|
|
|
func NewUserRepository(db *gorm.DB) UserRepository {
|
|
return &UserRepositoryImpl{
|
|
BaseRepositoryImpl: commonrepo.NewBaseRepository[entity.User](db),
|
|
}
|
|
}
|
|
|
|
func (r *UserRepositoryImpl) GetByIdUser(
|
|
ctx context.Context,
|
|
idUser int64,
|
|
modifier func(*gorm.DB) *gorm.DB,
|
|
) (*entity.User, error) {
|
|
return r.BaseRepositoryImpl.First(ctx, func(db *gorm.DB) *gorm.DB {
|
|
return db.Where("id_user = ?", idUser)
|
|
})
|
|
}
|
|
|
|
func (r *UserRepositoryImpl) UpsertByIdUser(ctx context.Context, user *entity.User) error {
|
|
if user == nil {
|
|
return gorm.ErrInvalidData
|
|
}
|
|
|
|
conflict := []clause.Column{{Name: "id_user"}}
|
|
user.DeletedAt = gorm.DeletedAt{}
|
|
user.UpdatedAt = time.Now()
|
|
|
|
return r.BaseRepositoryImpl.Upsert(ctx, user, conflict, func(db *gorm.DB) *gorm.DB {
|
|
return db.Omit("id", "created_at")
|
|
})
|
|
}
|
|
|
|
func (r *UserRepositoryImpl) SoftDeleteByIdUser(ctx context.Context, idUser int64) error {
|
|
query := r.DB().WithContext(ctx).Where("id_user = ?", idUser)
|
|
result := query.Delete(&entity.User{})
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
return gorm.ErrRecordNotFound
|
|
}
|
|
return nil
|
|
}
|