Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions core/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
49 changes: 49 additions & 0 deletions infrastructure/repository/generic.go
Original file line number Diff line number Diff line change
@@ -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
}
37 changes: 8 additions & 29 deletions infrastructure/repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Loading