Skip to content

Commit

Permalink
Merge pull request #33 from isd-sgcu/pet-findall/image
Browse files Browse the repository at this point in the history
Pet findall/image
  • Loading branch information
bookpanda authored Feb 16, 2024
2 parents e028125 + 7831831 commit 233bfc4
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 52 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ require (
github.com/gofiber/fiber/v2 v2.52.0
github.com/golang/mock v1.6.0
github.com/google/uuid v1.5.0
github.com/isd-sgcu/johnjud-go-proto v0.5.2
github.com/isd-sgcu/johnjud-go-proto v0.6.0
github.com/rs/zerolog v1.31.0
github.com/spf13/viper v1.18.1
github.com/stretchr/testify v1.8.4
github.com/swaggo/swag v1.16.2
google.golang.org/grpc v1.60.1
google.golang.org/grpc v1.61.0
)

require (
Expand Down
14 changes: 6 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,15 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/isd-sgcu/johnjud-go-proto v0.5.0 h1:GgqRzWjya5p1yhfU/kpX8i4WL42+qT2TkyXZmssH6B4=
github.com/isd-sgcu/johnjud-go-proto v0.5.0/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4=
github.com/isd-sgcu/johnjud-go-proto v0.5.2 h1:LWhi7zaEeEOJ60nyCxxMeAr7Bvl9stWvQbaw0RWz/Cs=
github.com/isd-sgcu/johnjud-go-proto v0.5.2/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4=
github.com/isd-sgcu/johnjud-go-proto v0.6.0 h1:sOWGjsqXwzpSaweSlNqPlttExZryp8mV76ob95LppjM=
github.com/isd-sgcu/johnjud-go-proto v0.6.0/go.mod h1:Yfajs+jSTcuVAKK9xLcVbZkUvCBO3exZV8bOGdelvW0=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
Expand Down Expand Up @@ -246,8 +244,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f h1:ultW7fxlIvee4HYrtnaRPon9HpEgFk5zYpmfMgtKB5I=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f/go.mod h1:L9KNLi232K1/xB6f7AlSX692koaRnKaWSR0stBki0Yc=
google.golang.org/grpc v1.60.1 h1:26+wFr+cNqSGFcOXcabYC0lUVJVRa2Sb2ortSK7VrEU=
google.golang.org/grpc v1.60.1/go.mod h1:OlCHIeLYqSSsLi6i49B5QGdzaMZK9+M7LXN2FKz4eGM=
google.golang.org/grpc v1.61.0 h1:TOvOcuXn30kRao+gfcvsebNEa5iZIiLkisYEkf7R7o0=
google.golang.org/grpc v1.61.0/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
Expand Down
1 change: 1 addition & 0 deletions src/app/dto/image.dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type DecomposedFile struct {

type ImageResponse struct {
Id string `json:"id"`
PetId string `json:"pet_id"`
Url string `json:"url"`
ObjectKey string `json:"object_key"`
}
Expand Down
18 changes: 15 additions & 3 deletions src/app/handler/pet/pet.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,29 @@ type PetHandlerTest struct {
ServiceDownErr *dto.ResponseErr
InternalErr *dto.ResponseErr
Images []*imgProto.Image
ImagesList [][]*imgProto.Image
ImagesList map[string][]*imgProto.Image
}

func TestPetHandler(t *testing.T) {
suite.Run(t, new(PetHandlerTest))
}

func (t *PetHandlerTest) SetupTest() {
imagesList := utils.MockImageList(3)
petIds := []string{faker.UUIDDigit(), faker.UUIDDigit(), faker.UUIDDigit(), faker.UUIDDigit()}
imagesList := make(map[string][]*imgProto.Image)
for i := 0; i <= 3; i++ {
for j := 0; j <= 3; j++ {
img := &imgProto.Image{
Id: faker.UUIDDigit(),
PetId: petIds[i],
ImageUrl: faker.URL(),
ObjectKey: faker.Word(),
}
imagesList[petIds[i]] = append(imagesList[petIds[i]], img)
}
}
t.ImagesList = imagesList
t.Images = imagesList[0]
t.Images = imagesList[petIds[0]]
var pets []*petProto.Pet
genders := []petConst.Gender{petConst.MALE, petConst.FEMALE}
statuses := []petConst.Status{petConst.ADOPTED, petConst.FINDHOME}
Expand Down
2 changes: 1 addition & 1 deletion src/app/service/auth/auth.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type AuthServiceTest struct {
token string
refreshTokenRequest *dto.RefreshTokenRequest
forgotPasswordRequest *dto.ForgotPasswordRequest
resetPasswordRequest *dto.ResetPasswordRequest
resetPasswordRequest *dto.ResetPasswordRequest
}

func TestAuthService(t *testing.T) {
Expand Down
29 changes: 29 additions & 0 deletions src/app/service/image/image.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,35 @@ func NewService(client proto.ImageServiceClient) *Service {
}
}

func (s *Service) FindAll() ([]*dto.ImageResponse, *dto.ResponseErr) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

res, errRes := s.client.FindAll(ctx, &proto.FindAllImageRequest{})
if errRes != nil {
st, _ := status.FromError(errRes)
log.Error().
Str("service", "image").
Str("module", "find all").
Msg(st.Message())
switch st.Code() {
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) FindByPetId(petId string) ([]*dto.ImageResponse, *dto.ResponseErr) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
Expand Down
17 changes: 14 additions & 3 deletions src/app/service/pet/pet.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ func (s *Service) FindAll(in *dto.FindAllPetRequest) (result *dto.FindAllPetResp
Data: nil,
}
}
imagesList := utils.MockImageList(len(res.Pets))

images, errSvc := s.imageService.FindAll()
if errSvc != nil {
return nil, errSvc
}

imagesList := utils.ImageList(images)
findAllDto := utils.ProtoToDtoList(res.Pets, imagesList)
metaData := utils.MetadataProtoToDto(res.Metadata)

Expand Down Expand Up @@ -202,8 +208,13 @@ func (s *Service) Update(id string, in *dto.UpdatePetRequest) (result *dto.PetRe
}
}
}
images := utils.MockImageList(1)[0]
updatePetResponse := utils.ProtoToDto(res.Pet, utils.ImageProtoToDto(images))

images, errSvc := s.imageService.FindByPetId(res.Pet.Id)
if errSvc != nil {
return nil, errSvc
}

updatePetResponse := utils.ProtoToDto(res.Pet, images)
return updatePetResponse, nil
}

Expand Down
36 changes: 31 additions & 5 deletions src/app/service/pet/pet.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type PetServiceTest struct {
MetadataProto *petproto.FindAllPetMetaData
PetNotVisible *petproto.Pet
FindAllPetReq *petproto.FindAllPetRequest
FindAllImageReq *imgproto.FindAllImageRequest
UpdatePetReq *petproto.UpdatePetRequest
CreatePetReq *petproto.CreatePetRequest
ChangeViewPetReq *petproto.ChangeViewPetRequest
Expand All @@ -46,7 +47,8 @@ type PetServiceTest struct {
AdoptDto *dto.AdoptByRequest

Images []*imgproto.Image
ImagesList [][]*imgproto.Image
AllImages []*imgproto.Image
ImagesList map[string][]*imgproto.Image

AssignPetReq *imgproto.AssignPetRequest
AssignPetDto *dto.AssignPetRequest
Expand All @@ -59,16 +61,30 @@ func TestPetService(t *testing.T) {
}

func (t *PetServiceTest) SetupTest() {
imagesList := utils.MockImageList(3)
petIds := []string{faker.UUIDDigit(), faker.UUIDDigit(), faker.UUIDDigit(), faker.UUIDDigit()}
t.AllImages = []*imgproto.Image{}
imagesList := make(map[string][]*imgproto.Image)
for i := 0; i <= 3; i++ {
for j := 0; j <= 3; j++ {
img := &imgproto.Image{
Id: faker.UUIDDigit(),
PetId: petIds[i],
ImageUrl: faker.URL(),
ObjectKey: faker.Word(),
}
imagesList[petIds[i]] = append(imagesList[petIds[i]], img)
t.AllImages = append(t.AllImages, img)
}
}
t.ImagesList = imagesList
t.Images = imagesList[0]
t.Images = imagesList[petIds[0]]
genders := []pet.Gender{pet.MALE, pet.FEMALE}
statuses := []pet.Status{pet.ADOPTED, pet.FINDHOME}

var pets []*petproto.Pet
for i := 0; i <= 3; i++ {
pet := &petproto.Pet{
Id: faker.UUIDDigit(),
Id: petIds[i],
Type: faker.Word(),
Name: faker.Name(),
Birthdate: faker.Word(),
Expand All @@ -77,7 +93,7 @@ func (t *PetServiceTest) SetupTest() {
Pattern: faker.Word(),
Habit: faker.Paragraph(),
Caption: faker.Paragraph(),
Images: imagesList[i],
Images: imagesList[petIds[i]],
Status: string(statuses[rand.Intn(2)]),
IsSterile: true,
IsVaccinated: true,
Expand Down Expand Up @@ -206,6 +222,8 @@ func (t *PetServiceTest) SetupTest() {
PetID: t.Pet.Id,
}

t.FindAllImageReq = &imgproto.FindAllImageRequest{}

t.AssignPetReq = &imgproto.AssignPetRequest{
Ids: []string{},
PetId: t.Pet.Id,
Expand Down Expand Up @@ -262,7 +280,11 @@ func (t *PetServiceTest) TestFindAllSuccess() {
client := petmock.PetClientMock{}
client.On("FindAll", t.FindAllPetReq).Return(protoResp, nil)

findAllImageResp := &imgproto.FindAllImageResponse{
Images: t.AllImages,
}
imageClient := imagemock.ImageClientMock{}
imageClient.On("FindAll", t.FindAllImageReq).Return(findAllImageResp, nil)

imageSvc := imageSvc.NewService(&imageClient)
svc := NewService(&client, imageSvc)
Expand Down Expand Up @@ -468,7 +490,11 @@ func (t *PetServiceTest) TestUpdateSuccess() {
client := &petmock.PetClientMock{}
client.On("Update", protoReq).Return(protoResp, nil)

findByPetIdResp := &imgproto.FindImageByPetIdResponse{
Images: t.Images,
}
imageClient := imagemock.ImageClientMock{}
imageClient.On("FindByPetId", t.FindByPetIdReq).Return(findByPetIdResp, nil)

imageSvc := imageSvc.NewService(&imageClient)
svc := NewService(client, imageSvc)
Expand Down
25 changes: 11 additions & 14 deletions src/app/utils/image/image.utils.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package image

import (
"fmt"

"github.com/isd-sgcu/johnjud-gateway/src/app/dto"
imageProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1"
)

func ProtoToDto(in *imageProto.Image) *dto.ImageResponse {
return &dto.ImageResponse{
Id: in.Id,
PetId: in.PetId,
Url: in.ImageUrl,
ObjectKey: in.ObjectKey,
}
Expand All @@ -20,6 +19,7 @@ func ProtoToDtoList(in []*imageProto.Image) []*dto.ImageResponse {
for _, i := range in {
res = append(res, &dto.ImageResponse{
Id: i.Id,
PetId: i.PetId,
Url: i.ImageUrl,
ObjectKey: i.ObjectKey,
})
Expand All @@ -35,19 +35,16 @@ func CreateDtoToProto(in *dto.UploadImageRequest) *imageProto.UploadImageRequest
}
}

func MockImageList(n int) [][]*imageProto.Image {
var imagesList [][]*imageProto.Image
for i := 0; i <= n; i++ {
var images []*imageProto.Image
for j := 0; j <= 3; j++ {
images = append(images, &imageProto.Image{
Id: fmt.Sprintf("%v%v", i, j),
PetId: fmt.Sprintf("%v%v", i, j),
ImageUrl: fmt.Sprintf("%v%v", i, j),
ObjectKey: fmt.Sprintf("%v%v", i, j),
})
func ImageList(in []*dto.ImageResponse) map[string][]*imageProto.Image {
imagesList := make(map[string][]*imageProto.Image)
for _, image := range in {
img := &imageProto.Image{
Id: image.Id,
PetId: image.PetId,
ImageUrl: image.Url,
ObjectKey: image.ObjectKey,
}
imagesList = append(imagesList, images)
imagesList[image.PetId] = append(imagesList[image.PetId], img)
}

return imagesList
Expand Down
30 changes: 14 additions & 16 deletions src/app/utils/pet/pet.utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pet

import (
"errors"
"fmt"
"strconv"

"github.com/isd-sgcu/johnjud-gateway/src/app/dto"
Expand All @@ -11,19 +10,16 @@ import (
imgproto "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1"
)

func MockImageList(n int) [][]*imgproto.Image {
var imagesList [][]*imgproto.Image
for i := 0; i <= n; i++ {
var images []*imgproto.Image
for j := 0; j <= 3; j++ {
images = append(images, &imgproto.Image{
Id: fmt.Sprintf("%v%v", i, j),
PetId: fmt.Sprintf("%v%v", i, j),
ImageUrl: fmt.Sprintf("%v%v", i, j),
ObjectKey: fmt.Sprintf("%v%v", i, j),
})
func ImageList(in []*dto.ImageResponse) map[string][]*imgproto.Image {
imagesList := make(map[string][]*imgproto.Image)
for _, image := range in {
img := &imgproto.Image{
Id: image.Id,
PetId: image.PetId,
ImageUrl: image.Url,
ObjectKey: image.ObjectKey,
}
imagesList = append(imagesList, images)
imagesList[image.PetId] = append(imagesList[image.PetId], img)
}

return imagesList
Expand All @@ -34,6 +30,7 @@ func ImageProtoToDto(images []*imgproto.Image) []*dto.ImageResponse {
for _, image := range images {
imageDto = append(imageDto, &dto.ImageResponse{
Id: image.Id,
PetId: image.PetId,
Url: image.ImageUrl,
ObjectKey: image.ObjectKey,
})
Expand All @@ -48,6 +45,7 @@ func ImageListProtoToDto(imagesList [][]*imgproto.Image) [][]*dto.ImageResponse
for _, image := range images {
imageDto = append(imageDto, &dto.ImageResponse{
Id: image.Id,
PetId: image.PetId,
Url: image.ImageUrl,
ObjectKey: image.ObjectKey,
})
Expand Down Expand Up @@ -129,9 +127,9 @@ func UpdateDtoToProto(id string, in *dto.UpdatePetRequest) *petproto.UpdatePetRe
return req
}

func ProtoToDtoList(in []*petproto.Pet, imagesList [][]*imgproto.Image) []*dto.PetResponse {
func ProtoToDtoList(in []*petproto.Pet, imagesList map[string][]*imgproto.Image) []*dto.PetResponse {
var resp []*dto.PetResponse
for i, p := range in {
for _, p := range in {
pet := &dto.PetResponse{
Id: p.Id,
Type: p.Type,
Expand All @@ -150,7 +148,7 @@ func ProtoToDtoList(in []*petproto.Pet, imagesList [][]*imgproto.Image) []*dto.P
Address: p.Address,
Contact: p.Contact,
AdoptBy: p.AdoptBy,
Images: ImageProtoToDto(imagesList[i]),
Images: ImageProtoToDto(imagesList[p.Id]),
}
resp = append(resp, pet)
}
Expand Down
Loading

0 comments on commit 233bfc4

Please sign in to comment.