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 54 johnjud gateway image endopint #22

Merged
merged 34 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
8fa42dc
chore
macgeargear Jan 10, 2024
0571338
chore
macgeargear Jan 10, 2024
65c4ce2
fix: image dto
macgeargear Jan 10, 2024
94a7e3b
fix: image response dto
macgeargear Jan 10, 2024
4b3f1ee
chore
macgeargear Jan 10, 2024
555e4e6
chore
macgeargear Jan 10, 2024
e9f88a6
feat: image handler
macgeargear Jan 10, 2024
4cfea26
feat: image service
macgeargear Jan 10, 2024
7b586b0
feat: image utils
macgeargear Jan 10, 2024
652b462
gen mock
macgeargear Jan 10, 2024
fe86843
fix: image service interface
macgeargear Jan 10, 2024
30deead
chore
macgeargear Jan 10, 2024
8107862
chore
macgeargear Jan 10, 2024
d0af5f2
chore
macgeargear Jan 10, 2024
fa33d73
chore
macgeargear Jan 10, 2024
68426de
chore
macgeargear Jan 10, 2024
5ac8f92
chore
macgeargear Jan 10, 2024
d969b96
feat: get image path
macgeargear Jan 10, 2024
865c66c
chore
macgeargear Jan 10, 2024
a2d2a7a
feat: image handler
macgeargear Jan 10, 2024
30d9e28
chore
macgeargear Jan 10, 2024
54568df
feat: dto
macgeargear Jan 10, 2024
22344f0
feat: assign pet handler
macgeargear Jan 10, 2024
d290e89
feat: assign pet service
macgeargear Jan 10, 2024
896e248
feat: admin path
macgeargear Jan 10, 2024
60d433c
mock gen
macgeargear Jan 10, 2024
a49b478
feat: image service interface
macgeargear Jan 10, 2024
31ccce7
chore
macgeargear Jan 11, 2024
d7505da
feat: assign pet
macgeargear Jan 11, 2024
5b5d0c2
fix: invalidArgument error
macgeargear Jan 11, 2024
d58594d
feat: assign pet path
macgeargear Jan 11, 2024
e6cc73e
chore
macgeargear Jan 11, 2024
2644a3c
chore
macgeargear Jan 11, 2024
ade717f
chore
macgeargear Jan 11, 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"
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) {
macgeargear marked this conversation as resolved.
Show resolved Hide resolved
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
Loading