Skip to content

Commit

Permalink
Merge pull request #22 from isd-sgcu/gear/joh-54-johnjud-gateway-imag…
Browse files Browse the repository at this point in the history
…e-endopint

Gear/joh 54 johnjud gateway image endopint
  • Loading branch information
macgeargear authored Jan 11, 2024
2 parents b36ef41 + ade717f commit ee1704b
Show file tree
Hide file tree
Showing 14 changed files with 407 additions and 61 deletions.
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"
21 changes: 20 additions & 1 deletion src/app/dto/image.dto.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
package dto

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

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"`
}

type AssignPetRequest struct {
Ids []string `json:"ids" validate:"required"`
PetId string `json:"pet_id" validate:"required"`
}

type AssignPetResponse struct {
Success bool `json:"success"`
}
5 changes: 0 additions & 5 deletions src/app/dto/pet.dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import (
"github.com/isd-sgcu/johnjud-gateway/src/constant/pet"
)

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

type PetResponse struct {
Id string `json:"id"`
Type string `json:"type"`
Expand Down
111 changes: 108 additions & 3 deletions src/app/handler/image/image.handler.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,133 @@
package auth
package image

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"
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, response)
return
}

func (h *Handler) Upload(c *router.FiberCtx) {
request := &dto.UploadImageRequest{}
err := c.Bind(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, 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, res.Success)
return
}

func (h *Handler) AssignPet(c *router.FiberCtx) {
request := &dto.AssignPetRequest{}
err := c.Bind(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.AssignPet(request)
if respErr != nil {
c.JSON(respErr.StatusCode, respErr)
return
}

c.JSON(http.StatusOK, response)
return
}
1 change: 1 addition & 0 deletions src/app/handler/pet/pet.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

errConst "github.com/isd-sgcu/johnjud-gateway/src/app/constant"
utils "github.com/isd-sgcu/johnjud-gateway/src/app/utils/pet"
petConst "github.com/isd-sgcu/johnjud-gateway/src/constant/pet"
petProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1"
imgProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1"

Expand Down
7 changes: 7 additions & 0 deletions src/app/router/image.router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ func (r *FiberRouter) DeleteImage(path string, h func(ctx *FiberCtx)) {
return nil
})
}

func (r *FiberRouter) GetImage(path string, h func(ctx *FiberCtx)) {
r.image.Delete(path, func(c *fiber.Ctx) error {
h(NewFiberCtx(c))
return nil
})
}
2 changes: 1 addition & 1 deletion src/app/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewFiberRouter(authGuard IGuard, conf config.App) *FiberRouter {
user := GroupWithAuthMiddleware(r, "/user", authGuard.Use)
pet := GroupWithAuthMiddleware(r, "/pets", authGuard.Use)

image := GroupWithAuthMiddleware(r, "/image", authGuard.Use)
image := GroupWithAuthMiddleware(r, "/images", authGuard.Use)
like := GroupWithAuthMiddleware(r, "/likes", authGuard.Use)

return &FiberRouter{r, auth, user, pet, image, like}
Expand Down
172 changes: 166 additions & 6 deletions src/app/service/image/image.service.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package image

import (
"context"
"net/http"
"time"

"github.com/isd-sgcu/johnjud-gateway/src/app/constant"
"github.com/isd-sgcu/johnjud-gateway/src/app/dto"
utils "github.com/isd-sgcu/johnjud-gateway/src/app/utils/image"
proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1"
"github.com/rs/zerolog/log"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type Service struct {
Expand All @@ -15,14 +24,165 @@ func NewService(client proto.ImageServiceClient) *Service {
}
}

func (s *Service) FindByPetId(string) ([]*proto.Image, *dto.ResponseErr) {
return nil, nil
func (s *Service) FindByPetId(petId string) ([]*dto.ImageResponse, *dto.ResponseErr) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

res, errRes := s.client.FindByPetId(ctx, &proto.FindImageByPetIdRequest{PetId: petId})
if errRes != nil {
st, _ := status.FromError(errRes)
log.Error().
Str("service", "image").
Str("module", "find by pet id").
Msg(st.Message())
switch st.Code() {
case codes.NotFound:
return nil, &dto.ResponseErr{
StatusCode: http.StatusNotFound,
Message: constant.PetNotFoundMessage,
Data: nil,
}
case codes.Unavailable:
return nil, &dto.ResponseErr{
StatusCode: http.StatusServiceUnavailable,
Message: constant.UnavailableServiceMessage,
Data: nil,
}
default:
return nil, &dto.ResponseErr{
StatusCode: http.StatusInternalServerError,
Message: constant.InternalErrorMessage,
Data: nil,
}
}
}
return utils.ProtoToDtoList(res.Images), nil
}

func (s *Service) Upload(in *dto.ImageDto) (*proto.Image, *dto.ResponseErr) {
return nil, nil
func (s *Service) Upload(in *dto.UploadImageRequest) (*dto.ImageResponse, *dto.ResponseErr) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

request := utils.CreateDtoToProto(in)
res, errRes := s.client.Upload(ctx, request)
if errRes != nil {
st, _ := status.FromError(errRes)
log.Error().
Err(errRes).
Str("service", "image").
Str("module", "upload").
Msg(st.Message())
switch st.Code() {
case codes.InvalidArgument:
return nil, &dto.ResponseErr{
StatusCode: http.StatusBadRequest,
Message: constant.InvalidArgumentMessage,
Data: nil,
}
case codes.Unavailable:
return nil, &dto.ResponseErr{
StatusCode: http.StatusServiceUnavailable,
Message: constant.UnavailableServiceMessage,
Data: nil,
}
default:
return nil, &dto.ResponseErr{
StatusCode: http.StatusInternalServerError,
Message: constant.InternalErrorMessage,
Data: nil,
}
}
}
return utils.ProtoToDto(res.Image), nil
}

func (s *Service) Delete(id string) (bool, *dto.ResponseErr) {
return false, nil
func (s *Service) Delete(id string) (*dto.DeleteImageResponse, *dto.ResponseErr) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

request := &proto.DeleteImageRequest{
Id: id,
}

res, errRes := s.client.Delete(ctx, request)
if errRes != nil {
st, _ := status.FromError(errRes)
log.Error().
Err(errRes).
Str("service", "image").
Str("module", "delete").
Msg(st.Message())
switch st.Code() {
case codes.NotFound:
return nil, &dto.ResponseErr{
StatusCode: http.StatusNotFound,
Message: constant.ImageNotFoundMessage,
Data: nil,
}
case codes.Unavailable:
return nil, &dto.ResponseErr{
StatusCode: http.StatusServiceUnavailable,
Message: constant.UnavailableServiceMessage,
Data: nil,
}
default:
return nil, &dto.ResponseErr{
StatusCode: http.StatusInternalServerError,
Message: constant.InternalErrorMessage,
Data: nil,
}
}
}
return &dto.DeleteImageResponse{
Success: res.Success,
}, nil
}

func (s *Service) AssignPet(in *dto.AssignPetRequest) (*dto.AssignPetResponse, *dto.ResponseErr) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

request := &proto.AssignPetRequest{
Ids: in.Ids,
PetId: in.PetId,
}

res, errRes := s.client.AssignPet(ctx, request)
if errRes != nil {
st, _ := status.FromError(errRes)
log.Error().
Err(errRes).
Str("service", "image").
Str("module", "assign pet").
Msg(st.Message())
switch st.Code() {
case codes.InvalidArgument:
return nil, &dto.ResponseErr{
StatusCode: http.StatusBadRequest,
Message: constant.InvalidArgumentMessage,
Data: nil,
}
case codes.NotFound:
return nil, &dto.ResponseErr{
StatusCode: http.StatusNotFound,
Message: constant.PetNotFoundMessage,
Data: nil,
}
case codes.Unavailable:
return nil, &dto.ResponseErr{
StatusCode: http.StatusServiceUnavailable,
Message: constant.UnavailableServiceMessage,
Data: nil,
}
default:
return nil, &dto.ResponseErr{
StatusCode: http.StatusInternalServerError,
Message: constant.InternalErrorMessage,
Data: nil,
}
}
}
return &dto.AssignPetResponse{
Success: res.Success,
}, nil
}
Loading

0 comments on commit ee1704b

Please sign in to comment.