Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Allow super admin update user names #124

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
35 changes: 35 additions & 0 deletions backend/internal/controller/http/v1/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func newUserRoutes(handler *gin.RouterGroup, t usecase.UserUseCase, a usecase.Au
{
allowedRoute.GET("/approve-new-accounts", r.detail)
allowedRoute.POST("/edit-users-role", r.editUsersRole)
allowedRoute.PUT("/:id/update-name", r.updateName)
}
}
}
Expand Down Expand Up @@ -275,3 +276,37 @@ func (r *usersRoutes) getProfile(c *gin.Context) {
// @Router /users [get]
func (r *usersRoutes) forgetPassword(c *gin.Context) {
}



type updateNameRequest struct {
Name string `json:"name" binding:"required"`
}

// @Summary Update user name
// @Description Update user name
// @ID updateName
// @Tags user
// @Accept json
// @Produce json
// @Success 200 {object}
// @Failure 500 {object} response
// @Router /users/{id}/update-name [put]
func (r *usersRoutes) updateName(c *gin.Context) {
var req updateNameRequest
if err := c.ShouldBindJSON(&req); err != nil {
r.l.Error(err, "http - v1 - editUserRole - ShouldBindJSON")
errorResponse(c, http.StatusBadRequest, "invalid request body", err)
return
}
userID := c.Param("id")
if err := r.t.UpdateName(userID, req.Name); err != nil {
r.l.Error(err, "http - v1 - updateName - UpdateName")
errorResponse(c, http.StatusInternalServerError, "database problems", err)
return
}

c.JSON(http.StatusOK, gin.H{
"message": "Name updated sucessfully",
})
}
1 change: 1 addition & 0 deletions backend/internal/usecase/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type (
EditUsersRole(id_user string, id_role string) error
GetProfile(token string) (entity.UserResponse, error)
GetSuperAdminUsers() ([]entity.UserResponse, error)
UpdateName(id string, name string) error
}

// User -.
Expand Down
14 changes: 14 additions & 0 deletions backend/internal/usecase/mocks/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions backend/internal/usecase/repo/user_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,14 @@ func (r UserRepo) GetSuperAdminUsers() ([]entity.UserResponse, error) {
}

return entities, nil
}

func (r UserRepo) UpdateName(id string, name string) error {
// insert into if not exists if exist replace
stmt := `UPDATE UserAccount SET name=$2 WHERE id = $1`
_, err := r.Db.Exec(stmt, id, name)
if err != nil {
return fmt.Errorf("UserRepo - UpdateName - db.Exec: %w", err)
}
return nil
}
7 changes: 7 additions & 0 deletions backend/internal/usecase/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,10 @@ func (uc *UserUseCase) GetSuperAdminUsers() ([]entity.UserResponse, error) {
return users, nil
}

func (uc *UserUseCase) UpdateName(userID string, name string) error {
err := uc.repo.UpdateName(userID, name)
if err != nil {
return err
}
return nil
}
Loading