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 28 pet and like endpoints #18

Closed
wants to merge 7 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/app/dto/pet.dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type PetResponse struct {
Contact string `json:"contact"`
AdoptBy string `json:"adopt_by"`
Images []ImageResponse `json:"images"`
IsLike *bool `json:"is_like"`
}

type CreatePetRequest struct {
Expand Down
36 changes: 36 additions & 0 deletions src/app/handler/like/like.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ func NewHandler(service likeSvc.Service, validate validator.IDtoValidator) *Hand
return &Handler{service, validate}
}

// FindByUserId is a function that return all petID and userID that user liked.
// @Summary find likes by user id
// @Description Return dto.ResponseSuccess
// @Param id path string true "user id"
// @Tags like
// @Accept json
// @Produce json
// @Success 200 {object} dto.ResponseSuccess
// @Failure 404 {object} dto.ResponseNotfoundErr "user not found"
// @Failure 500 {object} dto.ResponseInternalErr "Internal service error"
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
// @Router /v1/likes/ [get]
func (h *Handler) FindByUserId(c router.IContext) {
id, err := c.Param("id")
if err != nil {
Expand All @@ -46,6 +58,18 @@ func (h *Handler) FindByUserId(c router.IContext) {
return
}

// Create is a function for creating a `like` for a pet that a user is interested
// @Summary create like
// @Description Return dto.ResponseSuccess
// @Param create body dto.CreateLikeRequest true "create like request"
// @Tags like
// @Accept json
// @Produce json
// @Success 200 {object} dto.ResponseSuccess
// @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/likes/ [post]
func (h *Handler) Create(c router.IContext) {
request := &dto.CreateLikeRequest{}
err := c.Bind(request)
Expand Down Expand Up @@ -85,6 +109,18 @@ func (h *Handler) Create(c router.IContext) {
return
}

// Create is a function for delete like in database
// @Summary delete like
// @Description Return dto.ResponseSuccess if like is successfully deleted
// @Param id path string true "user id"
// @Tags like
// @Accept json
// @Produce json
// @Success 200 {object} dto.ResponseSuccess
// @Failure 404 {object} dto.ResponseNotfoundErr "like not found"
// @Failure 500 {object} dto.ResponseInternalErr "Internal service error"
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
// @Router /v1/likes/ [delete]
func (h *Handler) Delete(c router.IContext) {
id, err := c.Param("id")
if err != nil {
Expand Down
68 changes: 48 additions & 20 deletions src/app/handler/pet/pet.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,59 @@ import (
"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"
petUtils "github.com/isd-sgcu/johnjud-gateway/src/app/utils/pet"
"github.com/isd-sgcu/johnjud-gateway/src/app/validator"
petconst "github.com/isd-sgcu/johnjud-gateway/src/constant/pet"
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"
)

type Handler struct {
service petSvc.Service
likeService likeSvc.Service
imageService imageSvc.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: service,
imageService: imageService,
likeService: likeService,
}
}

// FindAll is a function that return all pets in database
// @Summary find all pets
// @Description Return the data of pets if successfully
// @Tags auth
// @Tags pet
// @Accept json
// @Produce json
// @Success 200 {object} dto.PetDto
// @Success 200 {object} dto.ResponseSuccess
// @Failure 500 {object} dto.ResponseInternalErr "Internal service error"
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
// @Router /v1/pets/ [get]
func (h *Handler) FindAll(c router.IContext) {
userId := c.UserID()
response, respErr := h.service.FindAll()
if respErr != nil {
c.JSON(respErr.StatusCode, respErr)
return
}

likeResponse, likeRespErr := h.likeService.FindByUserId(userId)
if likeRespErr != nil {
c.JSON(respErr.StatusCode, likeRespErr)
return
}

petResponse := petUtils.MapIsLikeToPets(likeResponse, response)

c.JSON(http.StatusOK, dto.ResponseSuccess{
StatusCode: http.StatusOK,
Message: petconst.FindAllPetSuccessMessage,
Data: response,
Data: petResponse,
})
return
}
Expand All @@ -55,12 +71,13 @@ func (h *Handler) FindAll(c router.IContext) {
// @Tags auth
// @Accept json
// @Produce json
// @Success 200 {object} dto.PetDto
// @Failure 400 {object} dto.ResponseBadRequestErr "Invalid request body"
// @Success 200 {object} dto.ResponseSuccess
// @Failure 404 {object} dto.ResponseNotfoundErr "Pet not found"
// @Failure 500 {object} dto.ResponseInternalErr "Internal service error"
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
// @Router /v1/pets/{id} [get]
func (h *Handler) FindOne(c router.IContext) {
userId := c.UserID()
id, err := c.Param("id")
if err != nil {
c.JSON(http.StatusInternalServerError, dto.ResponseErr{
Expand All @@ -77,6 +94,14 @@ func (h *Handler) FindOne(c router.IContext) {
return
}

likeResponse, likeRespErr := h.likeService.FindByUserId(userId)
if likeRespErr != nil {
c.JSON(respErr.StatusCode, likeRespErr)
return
}

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

c.JSON(http.StatusOK, dto.ResponseSuccess{
StatusCode: http.StatusOK,
Message: petconst.FindOnePetSuccessMessage,
Expand All @@ -89,10 +114,10 @@ func (h *Handler) FindOne(c router.IContext) {
// @Summary create pet
// @Description Return the data of pet if successfully
// @Param create body dto.CreatePetRequest true "pet dto"
// @Tags auth
// @Tags pet
// @Accept json
// @Produce json
// @Success 201 {object} dto.PetDto
// @Success 201 {object} dto.ResponseSuccess
// @Failure 400 {object} dto.ResponseBadRequestErr "Invalid request body"
// @Failure 500 {object} dto.ResponseInternalErr "Internal service error"
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
Expand Down Expand Up @@ -140,12 +165,13 @@ func (h *Handler) Create(c router.IContext) {
// @Summary update pet
// @Description Return the data of pet if successfully
// @Param update body dto.UpdatePetRequest true "update pet dto"
// @Param id path stirng true "pet id"
// @Tags auth
// @Param id path string true "pet id"
// @Tags pet
// @Accept json
// @Produce json
// @Success 201 {object} dto.PetDto
// @Success 201 {object} dto.ResponseSuccess
// @Failure 400 {object} dto.ResponseBadRequestErr "Invalid request body"
// @Failure 404 {object} dto.ResponseNotfoundErr "Pet not found"
// @Failure 500 {object} dto.ResponseInternalErr "Internal service error"
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
// @Router /v1/pets/{id} [put]
Expand Down Expand Up @@ -202,13 +228,14 @@ func (h *Handler) Update(c router.IContext) {
// Change is a function that change visibility of pet in database
// @Summary change view pet
// @Description Return the status true of pet if successfully else false
// @Param change view body dto.ChangeViewPetRequest true "change view pet dto"
// @Param id string true "pet id"
// @Tags auth
// @Param changeView body dto.ChangeViewPetRequest true "change view pet dto"
// @Param id path string true "pet id"
// @Tags pet
// @Accept json
// @Produce json
// @Success 201 {object} bool
// @Success 201 {object} dto.ResponseSuccess
// @Failure 400 {object} dto.ResponseBadRequestErr "Invalid request body"
// @Failure 404 {object} dto.ResponseNotfoundErr "Pet not found"
// @Failure 500 {object} dto.ResponseInternalErr "Internal service error"
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
// @Router /v1/pets/ [put]
Expand Down Expand Up @@ -265,11 +292,11 @@ func (h *Handler) ChangeView(c router.IContext) {
// Delete is a function that delete pet in database
// @Summary delete pet
// @Description Return the status true of pet if successfully else false
// @Param id string true "pet id"
// @Tags auth
// @Param id path string true "pet id"
// @Tags pet
// @Accept json
// @Produce json
// @Success 201 {object} bool
// @Success 201 {object} dto.ResponseSuccess
// @Failure 400 {object} dto.ResponseBadRequestErr "Invalid request body"
// @Failure 500 {object} dto.ResponseInternalErr "Internal service error"
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
Expand Down Expand Up @@ -308,8 +335,9 @@ func (h *Handler) Delete(c router.IContext) {
// @Tags pet
// @Accept json
// @Produce json
// @Success 201 {object} bool
// @Success 201 {object} dto.ResponseSuccess
// @Failure 400 {object} dto.ResponseBadRequestErr "Invalid request body"
// @Failure 404 {object} dto.ResponseNotfoundErr "Pet not found"
// @Failure 500 {object} dto.ResponseInternalErr "Internal service error"
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
// @Router /v1/pets/{id}/adopt [put]
Expand Down
Loading
Loading