Skip to content

Commit

Permalink
Merge pull request #24 from isd-sgcu/JOH-82/pet-create
Browse files Browse the repository at this point in the history
[JOH-82] Pet service's create assign images
  • Loading branch information
bookpanda authored Jan 28, 2024
2 parents 1160b70 + dff4767 commit 6aba816
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 24 deletions.
2 changes: 1 addition & 1 deletion docker-compose-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ services:
- BUCKET_ACCESS_KEY=BUCKET_ACCESS_KEY
- BUCKET_SECRET_KEY=BUCKET_SECRET_KEY
- BUCKET_NAME=johnjud-pet-images
- BUCKET_USE_SSL=false
- BUCKET_USE_SSL=true
ports:
- "3004:3004"
networks:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
- BUCKET_ACCESS_KEY=BUCKET_ACCESS_KEY
- BUCKET_SECRET_KEY=BUCKET_SECRET_KEY
- BUCKET_NAME=johnjud-pet-images
- BUCKET_USE_SSL=false
- BUCKET_USE_SSL=true
ports:
- "3004:3004"
networks:
Expand Down
17 changes: 17 additions & 0 deletions src/app/service/image/image.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,20 @@ func (s *Service) FindByPetId(petId string) ([]*proto.Image, error) {
return res.Images, nil

}

func (s *Service) AssignPet(petId string, imageIds []string) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

_, err := s.client.AssignPet(ctx, &proto.AssignPetRequest{PetId: petId, Ids: imageIds})
if err != nil {
log.Error().
Err(err).
Str("service", "image").
Str("module", "assign pet").
Msg("Error while connecting to service")
return err
}

return nil
}
32 changes: 29 additions & 3 deletions src/app/service/image/image.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (

type ImageServiceTest struct {
suite.Suite
petId string
images []*proto.Image
petId string
images []*proto.Image
imageIds []string
}

func TestImageService(t *testing.T) {
Expand All @@ -37,6 +38,7 @@ func (t *ImageServiceTest) SetupTest() {
ImageUrl: faker.URL(),
},
}
t.imageIds = []string{t.images[0].Id, t.images[1].Id}
}

func (t *ImageServiceTest) TestFindByPetIdSuccess() {
Expand All @@ -56,7 +58,7 @@ func (t *ImageServiceTest) TestFindByPetIdSuccess() {
func (t *ImageServiceTest) TestFindByPetIdError() {
c := mock.ClientMock{}
c.On("FindByPetId", &proto.FindImageByPetIdRequest{PetId: t.petId}).
Return(&proto.FindImageByPetIdResponse{Images: t.images}, status.Error(codes.Unavailable, "Connection Timeout"))
Return(nil, status.Error(codes.Unavailable, "Connection Timeout"))

srv := NewService(&c)
actual, err := srv.FindByPetId(t.petId)
Expand All @@ -66,3 +68,27 @@ func (t *ImageServiceTest) TestFindByPetIdError() {
assert.Nil(t.T(), actual)
assert.Equal(t.T(), codes.Unavailable, st.Code())
}

func (t *ImageServiceTest) TestAssignPetSuccess() {
c := mock.ClientMock{}
c.On("AssignPet", &proto.AssignPetRequest{PetId: t.petId, Ids: t.imageIds}).
Return(&proto.AssignPetResponse{Success: true}, nil)

srv := NewService(&c)
err := srv.AssignPet(t.petId, t.imageIds)

assert.Nil(t.T(), err)
}

func (t *ImageServiceTest) TestAssignPetError() {
c := mock.ClientMock{}
c.On("AssignPet", &proto.AssignPetRequest{PetId: t.petId, Ids: t.imageIds}).
Return(nil, status.Error(codes.Unavailable, "Connection Timeout"))

srv := NewService(&c)
err := srv.AssignPet(t.petId, t.imageIds)

st, ok := status.FromError(err)
assert.True(t.T(), ok)
assert.Equal(t.T(), codes.Unavailable, st.Code())
}
14 changes: 12 additions & 2 deletions src/app/service/pet/pet.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type IRepository interface {

type ImageService interface {
FindByPetId(petId string) ([]*image_proto.Image, error)
AssignPet(petId string, imageIds []string) error
}

func NewService(repository IRepository, imageService ImageService) *Service {
Expand Down Expand Up @@ -138,13 +139,22 @@ func (s *Service) Create(_ context.Context, req *proto.CreatePetRequest) (res *p
return nil, status.Error(codes.Internal, "error converting dto to raw: "+err.Error())
}

images := []*image_proto.Image{}

err = s.repository.Create(raw)
if err != nil {
return nil, status.Error(codes.Internal, "failed to create pet")
}

imageIds := petUtils.ExtractImageIDs(req.Pet.Images)
err = s.imageService.AssignPet(raw.ID.String(), imageIds)
if err != nil {
return nil, status.Error(codes.Internal, "failed to assign pet to images")
}

images, err := s.imageService.FindByPetId(raw.ID.String())
if err != nil {
return nil, status.Error(codes.Internal, "error finding images by pet id")
}

return &proto.CreatePetResponse{Pet: petUtils.RawToDto(raw, images)}, nil
}

Expand Down
7 changes: 6 additions & 1 deletion src/app/service/pet/pet.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func (t *PetServiceTest) createPetsDto(in []*pet.Pet, imagesList [][]*img_proto.

func (t *PetServiceTest) TestCreateSuccess() {
want := &proto.CreatePetResponse{Pet: t.PetDto}
want.Pet.Images = []*img_proto.Image{} // when pet is first created, it has no images
want.Pet.Images = t.Images

repo := &mock.RepositoryMock{}

Expand All @@ -445,6 +445,11 @@ func (t *PetServiceTest) TestCreateSuccess() {
repo.On("Create", in).Return(t.Pet, nil)
imgSrv := new(img_mock.ServiceMock)

imageIds := []string{t.CreatePetReqMock.Pet.Images[0].Id, t.CreatePetReqMock.Pet.Images[1].Id, t.CreatePetReqMock.Pet.Images[2].Id}
imgSrv.On("AssignPet", t.Pet.ID.String(), imageIds).Return(nil)

imgSrv.On("FindByPetId", t.Pet.ID.String()).Return(t.Images, nil)

srv := NewService(repo, imgSrv)

actual, err := srv.Create(context.Background(), t.CreatePetReqMock)
Expand Down
8 changes: 8 additions & 0 deletions src/app/utils/pet/pet.utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ func ExtractImageUrls(in []*imageProto.Image) []string {
return result
}

func ExtractImageIDs(in []*imageProto.Image) []string {
var result []string
for _, e := range in {
result = append(result, e.Id)
}
return result
}

func parseDate(dateStr string) (time.Time, error) {
parsedTime, err := time.Parse(time.RFC3339, dateStr)
if err != nil {
Expand Down
32 changes: 16 additions & 16 deletions src/database/seeds/constant.seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var pets = []pet.Pet{
{
Type: "แมว",
Name: "เต้าเจี้ยว",
Birthdate: "2023-07-04 00:00:00.000000",
Birthdate: "2023-07-04T00:00:00.000Z",
Gender: constant.MALE,
Color: "ครีม",
Habit: "ขี้อ้อน ชอบนั่งตัก ติดคน",
Expand All @@ -25,7 +25,7 @@ var pets = []pet.Pet{
}, {
Type: "แมว",
Name: "ซีอิ๊ว",
Birthdate: "2023-07-04 00:00:00.000000",
Birthdate: "2023-07-04T00:00:00.000Z",
Gender: constant.FEMALE,
Color: "ดำ",
Habit: "ขี้อ้อน ขี้กลัว",
Expand All @@ -41,7 +41,7 @@ var pets = []pet.Pet{
}, {
Type: "แมว",
Name: "พัดพา",
Birthdate: "2023-06-18 00:00:00.000000",
Birthdate: "2023-06-18T00:00:00.000Z",
Gender: constant.FEMALE,
Color: "เทา",
Habit: "ติดคน ชอบคน ขี้อ้อนมาก ชอบนั่งตัก",
Expand All @@ -57,7 +57,7 @@ var pets = []pet.Pet{
}, {
Type: "แมว",
Name: "โอริโอ้",
Birthdate: "2022-12-20 00:00:00.000000",
Birthdate: "2022-12-20T00:00:00.000Z",
Gender: constant.MALE,
Color: "ดำ",
Habit: "ซน เล่นแรง กอดได้",
Expand All @@ -73,7 +73,7 @@ var pets = []pet.Pet{
}, {
Type: "แมว",
Name: "แม่พลอย",
Birthdate: "2017-07-18 00:00:00.000000",
Birthdate: "2017-07-18T00:00:00.000Z",
Gender: constant.FEMALE,
Color: "เทา",
Habit: "เข้ากับแมวตัวอื่นได้ดี เรียบร้อย",
Expand All @@ -89,7 +89,7 @@ var pets = []pet.Pet{
}, {
Type: "แมว",
Name: "แมงมุม",
Birthdate: "2023-09-06 00:00:00.000000",
Birthdate: "2023-09-06T00:00:00.000Z",
Gender: constant.MALE,
Color: "น้ำตาล",
Habit: "ขี้เล่น ร่าเริง ชอบของเล่น",
Expand All @@ -105,7 +105,7 @@ var pets = []pet.Pet{
}, {
Type: "แมว",
Name: "ลูกหม่อน",
Birthdate: "2023-08-18 00:00:00.000000",
Birthdate: "2023-08-18T00:00:00.000Z",
Gender: constant.FEMALE,
Color: "น้ำตาล",
Habit: "ขี้อ้อนมาก ติดคน ชอบร้องเรียกคน ฉลาด ซนตามวัย",
Expand All @@ -121,7 +121,7 @@ var pets = []pet.Pet{
}, {
Type: "แมว",
Name: "ซาบะ",
Birthdate: "2023-06-18 00:00:00.000000",
Birthdate: "2023-06-18T00:00:00.000Z",
Gender: constant.FEMALE,
Color: "เทา",
Habit: "เรียบร้อย นิ่ง ขี้อ้อน ไม่โวยวาย",
Expand All @@ -137,7 +137,7 @@ var pets = []pet.Pet{
}, {
Type: "แมว",
Name: "มีจ้า",
Birthdate: "2023-09-04 00:00:00.000000",
Birthdate: "2023-09-04T00:00:00.000Z",
Gender: constant.MALE,
Color: "น้ำตาล",
Habit: "ขี้เล่น ขี้สงสัย ชอบกระโดด ทำหน้าตลกๆ",
Expand All @@ -153,7 +153,7 @@ var pets = []pet.Pet{
}, {
Type: "แมว",
Name: "หวานเจี๊ยบ",
Birthdate: "2023-09-04 00:00:00.000000",
Birthdate: "2023-09-04T00:00:00.000Z",
Gender: constant.FEMALE,
Color: "ดำ",
Habit: "แมวเด็กติดคน ชอบปีนขึ้นที่สูง ให้กระโดดขึ้นตักได้ ชอบเล่นซ่อนแอบแต่ก็วิ่งมาหาอยู่ดี",
Expand All @@ -169,7 +169,7 @@ var pets = []pet.Pet{
}, {
Type: "แมว",
Name: "วินนี่",
Birthdate: "2023-10-04 00:00:00.000000",
Birthdate: "2023-10-04T00:00:00.000Z",
Gender: constant.FEMALE,
Color: "ดำ",
Habit: "หนูค่อนข้างซน ชอบแกล้งพี่ๆทีมงาน ชอบงับๆนิ้วเพราะหนูหมั่นเขี้ยว😆 ใครได้หนูไป รับรองว่าไม่มีทางเหงาแน่นอน เพราะหนูก็อยากหาเพื่อนเล่นด้วยเหมือนกัน",
Expand All @@ -185,7 +185,7 @@ var pets = []pet.Pet{
}, {
Type: "แมว",
Name: "มะกอก & มะกรูด",
Birthdate: "2023-06-18 00:00:00.000000",
Birthdate: "2023-06-18T00:00:00.000Z",
Gender: constant.MALE,
Color: "เทา",
Habit: "ขี้เล่น สุขภาพแข็งแรงดี",
Expand All @@ -201,7 +201,7 @@ var pets = []pet.Pet{
}, {
Type: "แมว",
Name: "น้องมอม",
Birthdate: "2022-01-18 00:00:00.000000",
Birthdate: "2022-01-18T00:00:00.000Z",
Gender: constant.FEMALE,
Color: "เทา",
Habit: "ขี้อ้อน ขี้กลัว",
Expand All @@ -217,7 +217,7 @@ var pets = []pet.Pet{
}, {
Type: "แมว",
Name: "น้องเด็กหรือแมวเด็ก",
Birthdate: "2023-06-04 00:00:00.000000",
Birthdate: "2023-06-04T00:00:00.000Z",
Gender: constant.FEMALE,
Color: "น้ำตาล",
Habit: "ไม่ได้ระบุนิสัยมาให้",
Expand All @@ -233,7 +233,7 @@ var pets = []pet.Pet{
}, {
Type: "แมว",
Name: "น้องเล็ก",
Birthdate: "2023-06-04 00:00:00.000000",
Birthdate: "2023-06-04T00:00:00.000Z",
Gender: constant.MALE,
Color: "เทา",
Habit: "เชื่อง ยอมให้จับ ให้ลูบเล่นและอุ้มได้ แต่ตอนนี้น้องอยู่แต่ในกรง ทำให้ไม่ได้ใช้ชีวิตเท่าที่ควร น้องอยากออกจากกรงมาก ถ้าได้บ้านคงจะติดคนและขี้อ้อนน่าดู",
Expand All @@ -249,7 +249,7 @@ var pets = []pet.Pet{
}, {
Type: "แมว",
Name: "หนิงหนิง",
Birthdate: "2023-08-18 00:00:00.000000",
Birthdate: "2023-08-18T00:00:00.000Z",
Gender: constant.MALE,
Color: "นวล",
Habit: "ยังกลัวๆอยู่ ขี้อ้อน เรียบร้อย ไม่ซน ไม่กัด",
Expand Down
6 changes: 6 additions & 0 deletions src/mocks/image/image.mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ func (c *ServiceMock) FindByPetId(petId string) (res []*proto.Image, err error)

return res, args.Error(1)
}

func (c *ServiceMock) AssignPet(petId string, imageIds []string) (err error) {
args := c.Called(petId, imageIds)

return args.Error(0)
}

0 comments on commit 6aba816

Please sign in to comment.