Skip to content

Commit

Permalink
Merge pull request #95 from Kos-Kita/fix/userRespon
Browse files Browse the repository at this point in the history
fix error respon user
  • Loading branch information
elfandor2 authored Feb 13, 2024
2 parents 13d6f0c + 6af68ed commit 7df0b22
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 24 deletions.
9 changes: 9 additions & 0 deletions features/user/data/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ func CoreToModel(input user.Core) User {
}
}

func CoreToModelUpdate(input user.CoreUpdate) User {
return User{
Name: input.Name,
UserName: input.UserName,
Email: input.Email,
PhotoProfile: input.PhotoProfile,
}
}

func (u User) ModelToCore() user.Core {
return user.Core{
ID: u.ID,
Expand Down
9 changes: 5 additions & 4 deletions features/user/data/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func (repo *userQuery) SelectById(userId int) (*user.Core, error) {
}

// Update implements user.UserDataInterface.
func (repo *userQuery) Update(userId int, input user.Core) error {
dataGorm := CoreToModel(input)
func (repo *userQuery) Update(userId int, input user.CoreUpdate) error {
dataGorm := CoreToModelUpdate(input)
tx := repo.db.Model(&User{}).Where("id = ?", userId).Updates(dataGorm)
if tx.Error != nil {
return tx.Error
Expand Down Expand Up @@ -75,7 +75,8 @@ func (repo *userQuery) Login(email string, password string) (data *user.Core, er
var userGorm User
tx := repo.db.Where("email = ?", email).First(&userGorm)
if tx.Error != nil {
return nil, tx.Error
// return nil, tx.Error
return nil, errors.New(" Invalid email or password")
}
result := userGorm.ModelToCore()
return &result, nil
Expand Down Expand Up @@ -103,4 +104,4 @@ func (repo *userQuery) GetTotalUser() (int, error) {
return 0, tx.Error
}
return int(count), nil
}
}
15 changes: 11 additions & 4 deletions features/user/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@ type Core struct {
UserName string `validate:"required"`
Email string `validate:"required,email"`
Password string `validate:"required"`
Gender string `validate:"required"`
Role string `validate:"required"`
Gender string
Role string
PhotoProfile string
CreatedAt time.Time
UpdatedAt time.Time
}

type CoreUpdate struct {
Name string `validate:"required"`
UserName string `validate:"required"`
Email string `validate:"required,email"`
PhotoProfile string
}

// interface untuk Data Layer
type UserDataInterface interface {
Insert(input Core) error
SelectById(userId int) (*Core, error)
Update(userId int, input Core) error
Update(userId int, input CoreUpdate) error
Delete(userId int) error
Login(email, password string) (data *Core, err error)
ChangePassword(userId int, oldPassword, newPassword string) error
Expand All @@ -30,7 +37,7 @@ type UserDataInterface interface {
type UserServiceInterface interface {
Create(input Core) error
GetById(userId int) (*Core, error)
Update(userId int, input Core) error
Update(userId int, input CoreUpdate) error
Delete(userId int) error
Login(email, password string) (data *Core, token string, err error)
ChangePassword(userId int, oldPassword, newPassword string) error
Expand Down
14 changes: 7 additions & 7 deletions features/user/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func (handler *UserHandler) RegisterUser(c echo.Context) error {
userCore := RequestToCore(newUser)
errInsert := handler.userService.Create(userCore)
if errInsert != nil {
return c.JSON(http.StatusInternalServerError, responses.WebResponse("error insert data. "+errInsert.Error(), nil))
return c.JSON(http.StatusBadRequest, responses.WebResponse("error insert data. "+errInsert.Error(), nil))
}

return c.JSON(http.StatusOK, responses.WebResponse("Successful Operation", nil))
return c.JSON(http.StatusOK, responses.WebResponse("success insert user", nil))
}

func (handler *UserHandler) Login(c echo.Context) error {
Expand All @@ -46,7 +46,7 @@ func (handler *UserHandler) Login(c echo.Context) error {
}
result, token, err := handler.userService.Login(reqData.Email, reqData.Password)
if err != nil {
return c.JSON(http.StatusInternalServerError, responses.WebResponse("error login. "+err.Error(), nil))
return c.JSON(http.StatusBadRequest, responses.WebResponse("error login. "+err.Error(), nil))
}
responseData := map[string]any{
"token": token,
Expand Down Expand Up @@ -90,10 +90,10 @@ func (handler *UserHandler) UpdateUser(c echo.Context) error {
}
}

userCore := UpdateRequestToCore(userData, imageURL)
userCore := UpdateRequestToCoreUpdate(userData, imageURL)
errUpdate := handler.userService.Update(userIdLogin, userCore)
if errUpdate != nil {
return c.JSON(http.StatusInternalServerError, responses.WebResponse("error update data. "+errUpdate.Error(), nil))
return c.JSON(http.StatusBadRequest, responses.WebResponse("error update data. "+errUpdate.Error(), nil))
}

return c.JSON(http.StatusOK, responses.WebResponse("success update data", nil))
Expand Down Expand Up @@ -121,8 +121,8 @@ func (handler *UserHandler) ChangePassword(c echo.Context) error {

errChange := handler.userService.ChangePassword(userIdLogin, passwords.OldPassword, passwords.NewPassword)
if errChange != nil {
return c.JSON(http.StatusInternalServerError, responses.WebResponse("error update data. "+errChange.Error(), nil))
return c.JSON(http.StatusBadRequest, responses.WebResponse("error change password. "+errChange.Error(), nil))
}

return c.JSON(http.StatusOK, responses.WebResponse("success update data", nil))
return c.JSON(http.StatusOK, responses.WebResponse("success change password", nil))
}
9 changes: 9 additions & 0 deletions features/user/handler/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ func UpdateRequestToCore(input UserRequest, imageURL string) user.Core {
PhotoProfile: imageURL,
}
}

func UpdateRequestToCoreUpdate(input UserRequest, imageURL string) user.CoreUpdate {
return user.CoreUpdate{
Name: input.Name,
UserName: input.UserName,
Email: input.Email,
PhotoProfile: imageURL,
}
}
19 changes: 10 additions & 9 deletions features/user/service/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,15 @@ func (service *userService) GetById(userId int) (*user.Core, error) {
}

// Update implements user.UserServiceInterface.
func (service *userService) Update(userId int, input user.Core) error {
func (service *userService) Update(userId int, input user.CoreUpdate) error {
errValidate := service.validate.Struct(input)
if errValidate != nil {
return errValidate
}
if userId <= 0 {
return errors.New("invalid id.")
}

if input.Password != "" {
hashedPass, errHash := service.hashService.HashPassword(input.Password)
if errHash != nil {
return errors.New("Error hash password.")
}
input.Password = hashedPass
}

err := service.userData.Update(userId, input)
return err
}
Expand Down Expand Up @@ -124,6 +120,11 @@ func (service *userService) ChangePassword(userId int, oldPassword, newPassword
return errors.New("current password not match")
}

checkNewPassword := service.hashService.CheckPasswordHash(user.Password, newPassword)
if checkNewPassword {
return errors.New("password cannot be the same")
}

hashedNewPass, errHash := service.hashService.HashPassword(newPassword)
if errHash != nil {
return errors.New("Error hash password.")
Expand Down
Binary file added utils/images/bapak kos.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7df0b22

Please sign in to comment.