diff --git a/core/service/user.go b/core/service/user.go index 3e9e6bd..adc96a8 100644 --- a/core/service/user.go +++ b/core/service/user.go @@ -155,10 +155,6 @@ func (sv *userService) UpdateUserByID(ctx context.Context, return dto.UserResponse{}, err } - if reflect.DeepEqual(user, entity.User{}) { - return dto.UserResponse{}, errs.ErrUserNotFound - } - if req.Email != "" && req.Email != user.Email { us, err := sv.userRepository.GetUserByPrimaryKey(ctx, nil, constant.DBAttrEmail, req.Email) if err != nil && err != errs.ErrUserNotFound { @@ -191,15 +187,11 @@ func (sv *userService) UpdateUserByID(ctx context.Context, } func (sv *userService) DeleteUserByID(ctx context.Context, id string) error { - userCheck, err := sv.userRepository.GetUserByPrimaryKey(ctx, nil, constant.DBAttrID, id) + _, err := sv.userRepository.GetUserByPrimaryKey(ctx, nil, constant.DBAttrID, id) if err != nil { return err } - if reflect.DeepEqual(userCheck, entity.User{}) { - return errs.ErrUserNotFound - } - err = sv.userRepository.DeleteUserByID(ctx, nil, id) if err != nil { return err @@ -214,10 +206,6 @@ func (sv *userService) ChangePicture(ctx context.Context, return dto.UserResponse{}, err } - if reflect.DeepEqual(user, entity.User{}) { - return dto.UserResponse{}, errs.ErrUserNotFound - } - if user.Picture != nil && *user.Picture != "" { if err := util.DeleteFile(*user.Picture); err != nil { return dto.UserResponse{}, err @@ -252,10 +240,6 @@ func (sv *userService) DeletePicture(ctx context.Context, userID string) error { return err } - if reflect.DeepEqual(user, entity.User{}) { - return errs.ErrUserNotFound - } - if user.Picture == nil || *user.Picture == "" { return errs.ErrUserNoPicture } diff --git a/infrastructure/repository/generic.go b/infrastructure/repository/generic.go new file mode 100644 index 0000000..98f72cc --- /dev/null +++ b/infrastructure/repository/generic.go @@ -0,0 +1,49 @@ +package repository + +import ( + "context" + "errors" + + "gorm.io/gorm" +) + +func useDB(tx *gorm.DB, defaultDB *gorm.DB) *gorm.DB { + if tx == nil { + return defaultDB + } + return tx +} + +func Create[T any](ctx context.Context, tx *gorm.DB, defaultDB *gorm.DB, entity T) (T, error) { + if err := useDB(tx, defaultDB).WithContext(ctx).Debug().Create(&entity).Error; err != nil { + return entity, err + } + return entity, nil +} + +func GetByID[T any](ctx context.Context, tx *gorm.DB, defaultDB *gorm.DB, + id string, notFoundErr error, includes ...string) (T, error) { + var entity T + + stmt := useDB(tx, defaultDB).WithContext(ctx).Debug().Model(&entity) + for _, include := range includes { + stmt = stmt.Preload(include) + } + + err := stmt.Where("id = ?", id).Take(&entity).Error + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return entity, notFoundErr + } + return entity, err + } + return entity, nil +} + +func Update[T any](ctx context.Context, tx *gorm.DB, defaultDB *gorm.DB, entity *T) error { + return useDB(tx, defaultDB).WithContext(ctx).Debug().Updates(entity).Error +} + +func Delete[T any](ctx context.Context, tx *gorm.DB, defaultDB *gorm.DB, id string) error { + return useDB(tx, defaultDB).WithContext(ctx).Debug().Delete(new(T), &id).Error +} diff --git a/infrastructure/repository/user.go b/infrastructure/repository/user.go index 978829c..7b16ad5 100644 --- a/infrastructure/repository/user.go +++ b/infrastructure/repository/user.go @@ -22,23 +22,16 @@ func (rp *userRepository) DB() *gorm.DB { return rp.db } -func (rp *userRepository) CreateNewUser(ctx context.Context, tx *gorm.DB, user entity.User) (entity.User, error) { - if tx == nil { - tx = rp.db - } - - if err := tx.WithContext(ctx).Debug().Create(&user).Error; err != nil { - return entity.User{}, err - } - return user, nil +func (ur *userRepository) CreateNewUser(ctx context.Context, tx *gorm.DB, user entity.User) (entity.User, error) { + return Create(ctx, tx, ur.DB(), user) } -func (rp *userRepository) GetUserByPrimaryKey(ctx context.Context, +func (ur *userRepository) GetUserByPrimaryKey(ctx context.Context, tx *gorm.DB, key string, val string) (entity.User, error) { var user entity.User if tx == nil { - tx = rp.db + tx = ur.db } err := tx.WithContext(ctx).Debug().Where(key+" = $1", val).Take(&user).Error @@ -51,24 +44,10 @@ func (rp *userRepository) GetUserByPrimaryKey(ctx context.Context, return user, nil } -func (rp *userRepository) UpdateUser(ctx context.Context, tx *gorm.DB, user entity.User) error { - if tx == nil { - tx = rp.db - } - - if err := tx.WithContext(ctx).Debug().Updates(&user).Error; err != nil { - return err - } - return nil +func (ur *userRepository) UpdateUser(ctx context.Context, tx *gorm.DB, user entity.User) error { + return Update(ctx, tx, ur.DB(), &user) } -func (rp *userRepository) DeleteUserByID(ctx context.Context, tx *gorm.DB, id string) error { - if tx == nil { - tx = rp.db - } - - if err := tx.WithContext(ctx).Debug().Delete(&entity.User{}, &id).Error; err != nil { - return err - } - return nil +func (ur *userRepository) DeleteUserByID(ctx context.Context, tx *gorm.DB, id string) error { + return Delete[entity.User](ctx, tx, ur.DB(), id) }