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

Jjnavanon/sucu 46 user get user by #15

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ tmp_dir = "tmp"

[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ./cmd/main.go"
bin = "./tmp/main.exe"
cmd = "go build -o ./tmp/main.exe ./cmd/main.go"
Comment on lines +7 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ฝากแก้ให้เหมือนเดิมด้วย bro ใน macos มันไม่มี .exe @JJnvn

delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
Expand Down
1 change: 1 addition & 0 deletions cmd/server/fiber_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (s *FiberHttpServer) initUserRouter(router fiber.Router, httpHandler handle
userRouter := router.Group("/users")

userRouter.Get("/", httpHandler.User().GetAllUsers)
userRouter.Get("/:user_id", httpHandler.Middleware().IsLogin, httpHandler.Middleware().SuperAdmin, httpHandler.User().GetUserByID)
userRouter.Post("/", httpHandler.Middleware().IsLogin, httpHandler.Middleware().SuperAdmin, httpHandler.User().CreateUser)
userRouter.Patch("/", httpHandler.Middleware().IsLogin, httpHandler.User().UpdateProfile)
userRouter.Put("/:user_id", httpHandler.Middleware().IsLogin, httpHandler.Middleware().SuperAdmin, httpHandler.User().UpdateUserByID)
Expand Down
25 changes: 24 additions & 1 deletion internal/domain/usecases/user_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,30 @@ func (u *userUsecase) GetAllUsers(req *dtos.UserDTO) (*[]dtos.UserDTO, *apperror
}

func (u *userUsecase) GetUserByID(req *dtos.UserDTO, userID string) (*dtos.UserDTO, *apperror.AppError) {
return nil, nil
role, err := utils.GetRole(req.Role)
if err != nil {
u.logger.Named("GetUserByID").Error(constant.ErrInvalidRole, zap.String("role", req.Role), zap.Error(err))
return nil, apperror.BadRequestError(constant.ErrInvalidRole)
}
res, err := u.userRepository.FindUserByID(userID)
if err != nil {
u.logger.Named("GetUserByID").Error(constant.ErrFindUserByID, zap.String("userID", req.ID), zap.Error(err))
return nil, apperror.NotFoundError(constant.ErrFindUserByID)
}
if res.RoleID != role {
u.logger.Named("GetUserByID").Error(constant.ErrInvalidRole, zap.String("role", req.Role), zap.Error(err))
return nil, apperror.ForbiddenError(constant.ErrInvalidRole)
}
res_return := dtos.UserDTO{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

แก้ชื่อตัวแปรให้เป็น camel ด้วยคับบ like resReturn

ID: res.ID,
FirstName: res.FirstName,
LastName: res.LastName,
Role: res.RoleID,
CreatedAt: res.CreatedAt,
UpdatedAt: res.UpdatedAt,
}
return &res_return, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

นี่ด้วย


}

func (u *userUsecase) CreateUser(req *dtos.UserDTO, createUserDTO *dtos.CreateUserDTO) *apperror.AppError {
Expand Down
8 changes: 7 additions & 1 deletion internal/interface/handlers/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ func (h *UserHandler) GetAllUsers(c *fiber.Ctx) error {
// @Failure 500 {object} response.Response
// @Router /users/{user_id} [get]
func (h *UserHandler) GetUserByID(c *fiber.Ctx) error {
return nil
userID := c.Params("user_id") // how to get this?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment plss

req := c.Locals("user").(*dtos.UserDTO)
res, err := h.userUsecase.GetUserByID(req, userID)
if err != nil {
return err
}
return c.JSON(res)
Comment on lines +49 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ใช้ package responses เวลา return นะะ

}

// CreateUser godoc
Expand Down
1 change: 1 addition & 0 deletions utils/constant/error_constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var (
// user error
ErrUserAlreadyExists = "user already exists"
ErrUserNotFound = "user not found"
ErrRoleNotFound = "role not found"
ErrHashPasswordFailed = "failed to hash password"
ErrInsertUserFailed = "failed to insert user"
ErrFindUserByID = "failed to find user by ID"
Expand Down