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

Gear/joh 35 image endpoints #21

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3a2cf32
fix: add `IsLike` to PetResponse
macgeargear Jan 9, 2024
fcbbd59
feat: return `IsLike` when there're `bearer token`
macgeargear Jan 9, 2024
3c1d771
chore
macgeargear Jan 9, 2024
3e665d9
add utils
macgeargear Jan 9, 2024
8449b72
fix: adoptdto in userdto file
bookpanda Jan 7, 2024
1e67dee
fix: pet swagger docs
bookpanda Jan 7, 2024
0d53fb6
chore: gen docs
bookpanda Jan 7, 2024
a5244df
chore: fix dto
macgeargear Jan 9, 2024
c1c44a1
fix: add mock context
macgeargear Jan 9, 2024
c60e730
chore: fix test
macgeargear Jan 9, 2024
331cb01
feat: user `c.IsAuth()` for check if user is authenticated
macgeargear Jan 9, 2024
3e7f795
feat: skip auth middleware for [GET] `/pets/` and `/pets/:id`
macgeargear Jan 9, 2024
85dbb48
feat: isAuth for check if user authenticated
macgeargear Jan 9, 2024
292a82a
chore
macgeargear Jan 9, 2024
9bc862b
feat: CanSkipAuth check
macgeargear Jan 9, 2024
26230c6
fix: remove `GET /pet/` use `CanSkipAuth` instead
macgeargear Jan 9, 2024
ca107a2
Merge remote-tracking branch 'origin/dev' into gear/joh-35-image-endp…
macgeargear Jan 10, 2024
65f9e0d
Revert "Merge remote-tracking branch 'origin/dev' into gear/joh-35-im…
macgeargear Jan 10, 2024
90c060f
chore
macgeargear Jan 10, 2024
2d8cd53
fix: dto
macgeargear Jan 10, 2024
cd25c51
chore
macgeargear Jan 10, 2024
87da57f
feat: GET image endpoint
macgeargear Jan 10, 2024
c6cf7e0
feat: image utils
macgeargear Jan 10, 2024
e5a40f3
chore
macgeargear Jan 10, 2024
d3c7cc7
chore: image mock
macgeargear Jan 10, 2024
50bc17c
feat: image endpoint
macgeargear Jan 10, 2024
dec3428
fix: image service interface
macgeargear Jan 10, 2024
730a318
gen mock
macgeargear Jan 10, 2024
ea2a736
test: setup etst
macgeargear Jan 10, 2024
edf6765
feat: iamge service
macgeargear Jan 10, 2024
dceec6a
chore
macgeargear Jan 10, 2024
f64d628
Merge branch 'dev' into gear/joh-35-image-endpoints
macgeargear Jan 10, 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
1 change: 1 addition & 0 deletions src/app/constant/error.constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ const InvalidArgumentMessage = "Invalid Argument"

const PetNotFoundMessage = "Pet not found"
const UserNotFoundMessage = "User not found"
const ImageNotFoundMessage = "Image not found"
6 changes: 5 additions & 1 deletion src/app/dto/image.dto.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package dto

type ImageDto struct {
type UploadImageRequest struct {
Filename string `json:"filename" validate:"required"`
Data []byte `json:"data" validate:"required"`
PetId string `json:"pet_id" validate:"required"`
}

type DeleteImageResponse struct {
Success bool `json:"success"`
}
37 changes: 20 additions & 17 deletions src/app/dto/pet.dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
)

type ImageResponse struct {
Id string `json:"id"`
Url string `json:"url"`
Id string `json:"id"`
Url string `json:"url"`
ObjectKey string `json:"object_key"`
}

type PetResponse struct {
Expand All @@ -21,15 +22,16 @@ type PetResponse struct {
Habit string `json:"habit"`
Caption string `json:"caption"`
Status pet.Status `json:"status"`
IsSterile *bool `json:"is_sterile"`
IsVaccinated *bool `json:"is_vaccinated"`
IsVisible *bool `json:"is_visible"`
IsClubPet *bool `json:"is_club_pet"`
Origin string `json:"origin"`
IsSterile bool `json:"is_sterile"`
IsVaccinated bool `json:"is_vaccinated"`
IsVisible bool `json:"is_visible"`
IsClubPet bool `json:"is_club_pet"`
Background string `json:"background"`
Address string `json:"address"`
Contact string `json:"contact"`
AdoptBy string `json:"adopt_by"`
Images []ImageResponse `json:"images"`
IsLike bool `json:"is_like"`
}

type CreatePetRequest struct {
Expand All @@ -43,11 +45,12 @@ type CreatePetRequest struct {
Habit string `json:"habit" validate:"required"`
Caption string `json:"caption"`
Status pet.Status `json:"status" validate:"required" example:"findhome"`
IsSterile *bool `json:"is_sterile" validate:"required"`
IsVaccinated *bool `json:"is_vaccinated" validate:"required"`
IsVisible *bool `json:"is_visible" validate:"required"`
IsClubPet *bool `json:"is_club_pet" validate:"required"`
Origin string `json:"origin" validate:"required"`
IsSterile bool `json:"is_sterile" validate:"required"`
IsVaccinated bool `json:"is_vaccinated" validate:"required"`
IsVisible bool `json:"is_visible" validate:"required"`
IsClubPet bool `json:"is_club_pet" validate:"required"`
Background string `json:"background"`

Address string `json:"address"`
Contact string `json:"contact"`
AdoptBy string `json:"adopt_by"`
Expand Down Expand Up @@ -82,11 +85,11 @@ type UpdatePetRequest struct {
Habit string `json:"habit"`
Caption string `json:"caption"`
Status pet.Status `json:"status"`
IsSterile *bool `json:"is_sterile"`
IsVaccinated *bool `json:"is_vaccinated"`
IsVisible *bool `json:"is_visible"`
IsClubPet *bool `json:"is_club_pet"`
Origin string `json:"origin"`
IsSterile bool `json:"is_sterile"`
IsVaccinated bool `json:"is_vaccinated"`
IsVisible bool `json:"is_visible"`
IsClubPet bool `json:"is_club_pet"`
Background string `json:"background"`
Address string `json:"address"`
Contact string `json:"contact"`
AdoptBy string `json:"adopt_by"`
Expand Down
89 changes: 87 additions & 2 deletions src/app/handler/image/image.handler.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,113 @@
package auth

import (
"fmt"
"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"
imageConst "github.com/isd-sgcu/johnjud-gateway/src/constant/image"
imageSvc "github.com/isd-sgcu/johnjud-gateway/src/pkg/service/image"
)

type Handler struct {
service imageSvc.Service
validate *validator.DtoValidator
validate validator.IDtoValidator
}

func NewHandler(service imageSvc.Service, validate *validator.DtoValidator) *Handler {
func NewHandler(service imageSvc.Service, validate validator.IDtoValidator) *Handler {
return &Handler{service, validate}
}

func (h *Handler) FindByPetId(c *router.FiberCtx) {
id, err := c.ID()
if err != nil {
c.JSON(http.StatusBadRequest, dto.ResponseErr{
StatusCode: http.StatusInternalServerError,
Message: constant.InvalidIDMessage,
Data: nil,
})
return
}

response, respErr := h.service.FindByPetId(id)
if respErr != nil {
c.JSON(respErr.StatusCode, respErr)
return
}

c.JSON(http.StatusOK, dto.ResponseSuccess{

Check failure on line 42 in src/app/handler/image/image.handler.go

View workflow job for this annotation

GitHub Actions / build

undefined: dto.ResponseSuccess
StatusCode: http.StatusOK,
Message: imageConst.FindImageSuccessMessage,
Data: response,
})
return
}

func (h *Handler) Upload(c *router.FiberCtx) {
request := &dto.UploadImageRequest{}
err := c.Bind(request)
fmt.Println("request: ", request)
if err != nil {
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
}

response, respErr := h.service.Upload(request)
if respErr != nil {
c.JSON(respErr.StatusCode, respErr)
return
}

c.JSON(http.StatusCreated, dto.ResponseSuccess{

Check failure on line 82 in src/app/handler/image/image.handler.go

View workflow job for this annotation

GitHub Actions / build

undefined: dto.ResponseSuccess
StatusCode: http.StatusCreated,
Message: imageConst.UploadImageSuccessMessage,
Data: response,
})
return
}

func (h *Handler) Delete(c *router.FiberCtx) {
id, err := c.ID()
if err != nil {
c.JSON(http.StatusBadRequest, dto.ResponseErr{
StatusCode: http.StatusBadRequest,
Message: err.Error(),
Data: nil,
})
return
}

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

c.JSON(http.StatusOK, dto.ResponseSuccess{

Check failure on line 107 in src/app/handler/image/image.handler.go

View workflow job for this annotation

GitHub Actions / build

undefined: dto.ResponseSuccess
StatusCode: http.StatusOK,
Message: imageConst.DelteImageSuccessMessage,
Data: res,
})
return
}
4 changes: 2 additions & 2 deletions src/app/handler/like/like.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewHandler(service likeSvc.Service, validate validator.IDtoValidator) *Hand
}

func (h *Handler) FindByUserId(c router.IContext) {
id, err := c.Param("id")
id, err := c.ID()
if err != nil {
c.JSON(http.StatusBadRequest, dto.ResponseErr{
StatusCode: http.StatusInternalServerError,
Expand Down Expand Up @@ -77,7 +77,7 @@ func (h *Handler) Create(c router.IContext) {
}

func (h *Handler) Delete(c router.IContext) {
id, err := c.Param("id")
id, err := c.ID()
if err != nil {
c.JSON(http.StatusBadRequest, dto.ResponseErr{
StatusCode: http.StatusBadRequest,
Expand Down
59 changes: 54 additions & 5 deletions src/app/handler/pet/pet.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ import (
"github.com/isd-sgcu/johnjud-gateway/src/app/router"
"github.com/isd-sgcu/johnjud-gateway/src/app/validator"
imageSvc "github.com/isd-sgcu/johnjud-gateway/src/pkg/service/image"
likeSvc "github.com/isd-sgcu/johnjud-gateway/src/pkg/service/like"
petSvc "github.com/isd-sgcu/johnjud-gateway/src/pkg/service/pet"

petUtils "github.com/isd-sgcu/johnjud-gateway/src/app/utils/pet"
)

type Handler struct {
service petSvc.Service
imageService imageSvc.Service
likeService likeSvc.Service
validate validator.IDtoValidator
}

func NewHandler(service petSvc.Service, imageService imageSvc.Service, validate validator.IDtoValidator) *Handler {
return &Handler{service, imageService, validate}
func NewHandler(service petSvc.Service, imageService imageSvc.Service, likeService likeSvc.Service, validate validator.IDtoValidator) *Handler {
return &Handler{service, imageService, likeService, validate}
}

// FindAll is a function that returns all pets in database
Expand All @@ -33,13 +37,36 @@ func NewHandler(service petSvc.Service, imageService imageSvc.Service, validate
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
// @Router /v1/pets/ [get]
func (h *Handler) FindAll(c router.IContext) {
isAuth := c.IsAuth()
response, respErr := h.service.FindAll()
if respErr != nil {
c.JSON(respErr.StatusCode, respErr)
return
}

c.JSON(http.StatusOK, response)
if !isAuth {
c.JSON(http.StatusOK, dto.ResponseSuccess{
StatusCode: http.StatusOK,
Message: petconst.FindAllPetSuccessMessage,
Data: response,
})
return
}

userID := c.UserID()
likeResponse, likeErr := h.likeService.FindByUserId(userID)
if likeErr != nil {
c.JSON(likeErr.StatusCode, likeErr)
return

}
petWithLikeResponse := petUtils.MapIsLikeToPets(likeResponse, response)

c.JSON(http.StatusOK, dto.ResponseSuccess{
StatusCode: http.StatusOK,
Message: petconst.FindAllPetSuccessMessage,
Data: petWithLikeResponse,
})
return
}

Expand All @@ -56,7 +83,8 @@ func (h *Handler) FindAll(c router.IContext) {
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
// @Router /v1/pets/{id} [get]
func (h *Handler) FindOne(c router.IContext) {
id, err := c.Param("id")
id, err := c.ID()
isAuth := c.IsAuth()
if err != nil {
c.JSON(http.StatusInternalServerError, dto.ResponseErr{
StatusCode: http.StatusInternalServerError,
Expand All @@ -72,7 +100,28 @@ func (h *Handler) FindOne(c router.IContext) {
return
}

c.JSON(http.StatusOK, response)
if !isAuth {
c.JSON(http.StatusOK, dto.ResponseSuccess{
StatusCode: http.StatusOK,
Message: petconst.FindOnePetSuccessMessage,
Data: response,
})
return
}

userId := c.UserID()
likeResponse, likeErr := h.likeService.FindByUserId(userId)
if likeErr != nil {
c.JSON(likeErr.StatusCode, likeErr)
}

response.IsLike = petUtils.IsLike(response.Id, likeResponse)

c.JSON(http.StatusOK, dto.ResponseSuccess{
StatusCode: http.StatusOK,
Message: petconst.FindOnePetSuccessMessage,
Data: response,
})
return
}

Expand Down
Loading
Loading