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

[JOH-27] User endpoints and Admin middleware #11

Merged
merged 38 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
cd3b103
feat: usersvc findone
bookpanda Jan 5, 2024
ce0e52f
fix: typo
bookpanda Jan 5, 2024
dc54903
feat: usersvc update
bookpanda Jan 5, 2024
59baa48
fix: remove validation in resp
bookpanda Jan 5, 2024
3ac9671
feat: user svc delete
bookpanda Jan 5, 2024
3059615
fix: pet svc delete return nil
bookpanda Jan 5, 2024
ba9a37e
fix: auth config
bookpanda Jan 6, 2024
24856ea
Merge branch 'dev' into JOH-27/user-endpoints
bookpanda Jan 7, 2024
0c1b397
feat: pr template
bookpanda Jan 7, 2024
5876f32
feat: pr template
bookpanda Jan 7, 2024
6bead97
fix: remove user old svc mock
bookpanda Jan 7, 2024
898a26c
fix: update user add password, email
bookpanda Jan 7, 2024
97a2beb
chore: gen mock
bookpanda Jan 7, 2024
5714a97
feat: user client mock
bookpanda Jan 7, 2024
ba8edcc
feat: user delete add unavailable
bookpanda Jan 7, 2024
fd4c0cf
feat: user svc test
bookpanda Jan 7, 2024
2cae2d9
feat: findone swagger
bookpanda Jan 7, 2024
6e11f07
fix: merge update, findone dto -> dto.User
bookpanda Jan 7, 2024
d2d291c
feat: user handler update
bookpanda Jan 8, 2024
746e744
feat: add context Role()
bookpanda Jan 8, 2024
915c17b
chore: gen mock
bookpanda Jan 8, 2024
8f8f8bd
feat: role const
bookpanda Jan 8, 2024
35e2242
feat: add admin path
bookpanda Jan 8, 2024
e31bf92
feat: user handler delete
bookpanda Jan 8, 2024
81b3501
fix: handler pass by ref
bookpanda Jan 9, 2024
fb02398
feat: user handler test
bookpanda Jan 9, 2024
0c82d9b
fix: remove validate in resp
bookpanda Jan 9, 2024
974c192
fix: user handler lighter sucess respo
bookpanda Jan 9, 2024
71c3175
fix: pet handler lighter sucess resp
bookpanda Jan 9, 2024
797e5a4
fix: like handler use lighter success resp
bookpanda Jan 9, 2024
88d4f6d
fix: pet create url
bookpanda Jan 9, 2024
b625e8d
feat: add admin paths
bookpanda Jan 9, 2024
4e497d0
fix: auth config
bookpanda Jan 9, 2024
4de877f
fix: url endpoints not ending in /
bookpanda Jan 9, 2024
44cd89f
fix: auth middleware detect :id
bookpanda Jan 9, 2024
6fb9623
fix: paths
bookpanda Jan 9, 2024
46f26d8
fix: auth cache
bookpanda Jan 9, 2024
d0a1854
fix: admin paths
bookpanda Jan 9, 2024
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
13 changes: 13 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Change made

- [ ]  New features
- [ ]  Bug fixes
- [ ]  Breaking changes
## Describe what you have done
-
### New Features
-
### Fix
-
### Others
-
2 changes: 1 addition & 1 deletion config/auth/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ app:
secret: <secret>

database:
host: localhost
host: local-db
port: 5432
name: johnjud_db
username: root
Expand Down
4 changes: 2 additions & 2 deletions src/app/dto/pet.dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type ChangeViewPetRequest struct {
}

type ChangeViewPetResponse struct {
Success bool `json:"success" validate:"required"`
Success bool `json:"success"`
}

type UpdatePetRequest struct {
Expand All @@ -81,5 +81,5 @@ type DeleteRequest struct {
Id string `json:"id" validate:"required"`
}
type DeleteResponse struct {
Success bool `json:"success" validate:"required"`
Success bool `json:"success"`
}
15 changes: 10 additions & 5 deletions src/app/dto/user.dto.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package dto

type UserDto struct {
type User struct {
Id string `json:"id"`
Email string `json:"email"`
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}

type UpdateUserRequest struct {
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"required,gte=6,lte=30"`
Firstname string `json:"firstname" validate:"required"`
Lastname string `json:"lastname" validate:"required"`
}

type UpdateUserDto struct {
Password string `json:"password" validate:"required,gte=6,lte=30"`
Firstname string `json:"firstname" validate:"required"`
Lastname string `json:"lastname" validate:"required"`
type DeleteUserResponse struct {
Success bool `json:"success" validate:"required"`
}
115 changes: 103 additions & 12 deletions src/app/handler/user/user.handler.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package user

import (
"net/http"
"strings"

"github.com/isd-sgcu/johnjud-gateway/src/app/constant"
"github.com/isd-sgcu/johnjud-gateway/src/app/dto"
"github.com/isd-sgcu/johnjud-gateway/src/app/router"
"github.com/isd-sgcu/johnjud-gateway/src/app/validator"
userconst "github.com/isd-sgcu/johnjud-gateway/src/constant/user"
"github.com/isd-sgcu/johnjud-gateway/src/pkg/service/user"
"net/http"
)

type Handler struct {
Expand All @@ -17,12 +21,24 @@ func NewHandler(service user.Service, validate validator.IDtoValidator) *Handler
return &Handler{service, validate}
}

func (h *Handler) FindOne(c *router.FiberCtx) {
// FindOne is a function that returns a user by id from database
// @Summary finds one user
// @Description Returns the data of user if successful
// @Param id path string true "user id"
// @Tags user
// @Accept json
// @Produce json
// @Success 200 {object} dto.User
// @Failure 400 {object} dto.ResponseBadRequestErr "Invalid request body"
// @Failure 500 {object} dto.ResponseInternalErr "Internal service error"
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
// @Router /v1/users/{id} [get]
func (h *Handler) FindOne(c router.IContext) {
id, err := c.ID()
if err != nil {
c.JSON(http.StatusInternalServerError, dto.ResponseErr{
StatusCode: http.StatusInternalServerError,
Message: "Invalid ID",
c.JSON(http.StatusBadRequest, &dto.ResponseErr{
StatusCode: http.StatusBadRequest,
Message: err.Error(),
Data: nil,
})
return
Expand All @@ -34,27 +50,102 @@ func (h *Handler) FindOne(c *router.FiberCtx) {
return
}

c.JSON(http.StatusCreated, user)
c.JSON(http.StatusOK, &dto.ResponseSuccess{
StatusCode: http.StatusOK,
Message: userconst.FindOneUserSuccessMessage,
Data: user,
})
return
}

func (h *Handler) Update(c *router.FiberCtx) {
// Update is a function that updates user in database
// @Summary updates user
// @Description Returns the data of user if successfully
// @Param update body dto.UpdateUserRequest true "update user dto"
// @Tags auth
// @Accept json
// @Produce json
// @Success 201 {object} dto.User
// @Failure 400 {object} dto.ResponseBadRequestErr "Invalid request body"
// @Failure 500 {object} dto.ResponseInternalErr "Internal service error"
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
// @Router /v1/users [put]
func (h *Handler) Update(c router.IContext) {
usrId := c.UserID()

usrDto := dto.UpdateUserDto{}
request := &dto.UpdateUserRequest{}

err := c.Bind(&usrDto)
err := c.Bind(request)
if err != nil {
c.JSON(http.StatusBadRequest, err)
c.JSON(http.StatusBadRequest, &dto.ResponseErr{
StatusCode: http.StatusBadRequest,
Message: constant.BindingRequestErrorMessage + err.Error(),
Data: nil,
})
return
}

if err := h.validate.Validate(request); err != nil {
var errorMessage []string
for _, reqErr := range err {
errorMessage = append(errorMessage, reqErr.Message)
}
c.JSON(http.StatusBadRequest, &dto.ResponseErr{
StatusCode: http.StatusBadRequest,
Message: constant.InvalidRequestBodyMessage + strings.Join(errorMessage, ", "),
Data: nil,
})
return
}

user, errRes := h.service.Update(usrId, request)
if errRes != nil {
c.JSON(errRes.StatusCode, errRes)
return
}

c.JSON(http.StatusOK, &dto.ResponseSuccess{
StatusCode: http.StatusOK,
Message: userconst.UpdateUserSuccessMessage,
Data: user,
})
return
}

// Delete is a function that deletes user in database
// @Summary deletes user
// @Description Returns successful status if user is successfully deleted
// @Param id path string true "user id"
// @Tags user
// @Accept json
// @Produce json
// @Success 201 {object} bool
// @Success 201 {object} dto.DeleteUserResponse
// @Failure 400 {object} dto.ResponseBadRequestErr "Invalid request body"
// @Failure 500 {object} dto.ResponseInternalErr "Internal service error"
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
// @Router /v1/users/{id} [delete]
func (h *Handler) Delete(c router.IContext) {
id, err := c.ID()
if err != nil {
c.JSON(http.StatusBadRequest, &dto.ResponseErr{
StatusCode: http.StatusBadRequest,
Message: err.Error(),
Data: nil,
})
return
}

user, errRes := h.service.Update(usrId, &usrDto)
res, errRes := h.service.Delete(id)
if errRes != nil {
c.JSON(errRes.StatusCode, errRes)
return
}

c.JSON(http.StatusOK, user)
c.JSON(http.StatusOK, &dto.ResponseSuccess{
StatusCode: http.StatusOK,
Message: userconst.DeleteUserSuccessMessage,
Data: res,
})
return
}
Loading
Loading