Skip to content

Commit

Permalink
Merge pull request #10 from isd-sgcu/gear/joh-28-pet-and-like-endpoints
Browse files Browse the repository at this point in the history
Gear/joh 28 pet and like endpoints
  • Loading branch information
macgeargear authored Jan 5, 2024
2 parents cbecf21 + e4bc7ea commit 98d1ea1
Show file tree
Hide file tree
Showing 10 changed files with 561 additions and 305 deletions.
6 changes: 6 additions & 0 deletions src/app/dto/common.dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,9 @@ type ResponseGatewayTimeoutErr struct {
Message string `json:"message" example:"Connection timeout"`
Data interface{} `json:"data"`
}

type ResponseSuccess struct {
StatusCode int `json:"status_code" example:"200"`
Message string `json:"message" example:"success"`
Data interface{} `json:"data"`
}
60 changes: 52 additions & 8 deletions src/app/dto/pet.dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,33 @@ import (
"github.com/isd-sgcu/johnjud-gateway/src/constant/pet"
)

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

type PetResponse struct {
Id string `json:"id"`
Type string `json:"type"`
Species string `json:"species"`
Name string `json:"name"`
Birthdate string `json:"birthdate"`
Gender pet.Gender `json:"gender"`
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"`
Background string `json:"background"`
Address string `json:"address"`
Contact string `json:"contact"`
AdoptBy string `json:"adopt_by"`
Images []ImageResponse `json:"images"`
}

type CreatePetRequest struct {
Type string `json:"type" validate:"required"`
Species string `json:"species" validate:"required"`
Name string `json:"name" validate:"required"`
Expand All @@ -22,20 +47,39 @@ type PetDto struct {
Address string `json:"address"`
Contact string `json:"contact"`
AdoptBy string `json:"adopt_by"`
}

type CreatePetRequest struct {
Pet *PetDto `json:"pet" validate:"required"`
Images []string `json:"images"`
}

type ChangeViewPetRequest struct {
Visible bool `json:"visible" validate:"required"`
}

type UpdatePetRequest struct {
Pet *PetDto `json:"pet" validate:"required"`
type ChangeViewPetResponse struct {
Success bool `json:"success" validate:"required"`
}

type UpdatePetRequest struct {
Type string `json:"type"`
Species string `json:"species"`
Name string `json:"name"`
Birthdate string `json:"birthdate"`
Gender pet.Gender `json:"gender"`
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"`
Background string `json:"background"`
Address string `json:"address"`
Contact string `json:"contact"`
AdoptBy string `json:"adopt_by"`
Images []string `json:"images"`
}
type DeleteRequest struct {
Id string `json:"id" validate:"required"`
}
type DeleteResponse struct {
Success bool `json:"success" validate:"required"`
}
64 changes: 42 additions & 22 deletions src/app/handler/pet/pet.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
imageSrv "github.com/isd-sgcu/johnjud-gateway/src/app/handler/image"
"github.com/isd-sgcu/johnjud-gateway/src/app/router"
"github.com/isd-sgcu/johnjud-gateway/src/app/validator"
pet_proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1"
petconst "github.com/isd-sgcu/johnjud-gateway/src/constant/pet"
)

type Handler struct {
Expand All @@ -19,12 +19,12 @@ type Handler struct {
}

type Service interface {
FindAll() ([]*pet_proto.Pet, *dto.ResponseErr)
FindOne(string) (*pet_proto.Pet, *dto.ResponseErr)
Create(*dto.CreatePetRequest) (*pet_proto.Pet, *dto.ResponseErr)
Update(string, *dto.UpdatePetRequest) (*pet_proto.Pet, *dto.ResponseErr)
ChangeView(string, *dto.ChangeViewPetRequest) (bool, *dto.ResponseErr)
Delete(string) (bool, *dto.ResponseErr)
FindAll() ([]*dto.PetResponse, *dto.ResponseErr)
FindOne(string) (*dto.PetResponse, *dto.ResponseErr)
Create(*dto.CreatePetRequest) (*dto.PetResponse, *dto.ResponseErr)
Update(string, *dto.UpdatePetRequest) (*dto.PetResponse, *dto.ResponseErr)
ChangeView(string, *dto.ChangeViewPetRequest) (*dto.ChangeViewPetResponse, *dto.ResponseErr)
Delete(string) (*dto.DeleteResponse, *dto.ResponseErr)
}

func NewHandler(service Service, imageService imageSrv.Service, validate validator.IDtoValidator) *Handler {
Expand All @@ -48,7 +48,11 @@ func (h *Handler) FindAll(c router.IContext) {
return
}

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

Expand All @@ -67,7 +71,7 @@ func (h *Handler) FindAll(c router.IContext) {
func (h *Handler) FindOne(c router.IContext) {
id, err := c.Param("id")
if err != nil {
c.JSON(http.StatusInternalServerError, &dto.ResponseErr{
c.JSON(http.StatusInternalServerError, dto.ResponseErr{
StatusCode: http.StatusInternalServerError,
Message: "Invalid ID",
Data: nil,
Expand All @@ -81,7 +85,11 @@ func (h *Handler) FindOne(c router.IContext) {
return
}

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

Expand All @@ -98,9 +106,7 @@ func (h *Handler) FindOne(c router.IContext) {
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
// @Router /v1/pets/create [post]
func (h *Handler) Create(c router.IContext) {
request := &dto.CreatePetRequest{
Pet: &dto.PetDto{},
}
request := &dto.CreatePetRequest{}
err := c.Bind(request)
if err != nil {
c.JSON(http.StatusBadRequest, dto.ResponseErr{
Expand Down Expand Up @@ -130,7 +136,11 @@ func (h *Handler) Create(c router.IContext) {
return
}

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

Expand Down Expand Up @@ -158,9 +168,7 @@ func (h *Handler) Update(c router.IContext) {
return
}

request := &dto.UpdatePetRequest{
Pet: &dto.PetDto{},
}
request := &dto.UpdatePetRequest{}

err = c.Bind(request)
if err != nil {
Expand Down Expand Up @@ -191,7 +199,11 @@ func (h *Handler) Update(c router.IContext) {
return
}

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

Expand All @@ -211,7 +223,7 @@ func (h *Handler) Update(c router.IContext) {
func (h *Handler) ChangeView(c router.IContext) {
id, err := c.Param("id")
if err != nil {
c.JSON(http.StatusBadRequest, &dto.ResponseErr{
c.JSON(http.StatusBadRequest, dto.ResponseErr{
StatusCode: http.StatusBadRequest,
Message: err.Error(),
Data: nil,
Expand Down Expand Up @@ -250,7 +262,11 @@ func (h *Handler) ChangeView(c router.IContext) {
return
}

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

Expand All @@ -269,7 +285,7 @@ func (h *Handler) ChangeView(c router.IContext) {
func (h *Handler) Delete(c router.IContext) {
id, err := c.Param("id")
if err != nil {
c.JSON(http.StatusBadRequest, &dto.ResponseErr{
c.JSON(http.StatusBadRequest, dto.ResponseErr{
StatusCode: http.StatusBadRequest,
Message: err.Error(),
Data: nil,
Expand All @@ -283,6 +299,10 @@ func (h *Handler) Delete(c router.IContext) {
return
}

c.JSON(http.StatusOK, res)
c.JSON(http.StatusOK, dto.ResponseSuccess{
StatusCode: http.StatusOK,
Message: petconst.DeletePetSuccessMessage,
Data: res,
})
return
}
Loading

0 comments on commit 98d1ea1

Please sign in to comment.