mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 21:41:55 +00:00
feat/login crud in users sync with sso
This commit is contained in:
@@ -1,21 +1,64 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
"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 {
|
||||
repository.BaseRepository[entity.User]
|
||||
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 {
|
||||
*repository.BaseRepositoryImpl[entity.User]
|
||||
*commonrepo.BaseRepositoryImpl[entity.User]
|
||||
}
|
||||
|
||||
func NewUserRepository(db *gorm.DB) UserRepository {
|
||||
return &UserRepositoryImpl{
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.User](db),
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user