diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index 310f6f9..4b021b5 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -91,13 +91,29 @@ func (s *Service) ChangeView(_ context.Context, req *proto.ChangeViewPetRequest) func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res *proto.FindAllPetResponse, err error) { var pets []*pet.Pet + var imageUrlsList [][]string err = s.repository.FindAll(&pets) if err != nil { log.Error().Err(err).Str("service", "event").Str("module", "find all").Msg("Error while querying all events") return nil, status.Error(codes.Unavailable, "Internal error") } - return &proto.FindAllPetResponse{Pets: RawToDtoList(&pets)}, nil + + for _, pet := range pets { + images, err := s.imageService.FindByPetId(pet.ID.String()) + if err != nil { + return nil, status.Error(codes.Internal, "error querying image service") + } + imageUrls := ExtractImageUrls(images) + imageUrlsList = append(imageUrlsList, imageUrls) + } + + petWithImageUrls, err := RawToDtoList(&pets, imageUrlsList) + if err != nil { + return nil, status.Error(codes.Internal, "error converting raw to dto list") + } + + return &proto.FindAllPetResponse{Pets: petWithImageUrls}, nil } func (s Service) FindOne(_ context.Context, req *proto.FindOnePetRequest) (res *proto.FindOnePetResponse, err error) { @@ -109,7 +125,14 @@ func (s Service) FindOne(_ context.Context, req *proto.FindOnePetRequest) (res * Str("service", "pet").Str("module", "find one").Str("id", req.Id).Msg("Not found") return nil, status.Error(codes.NotFound, err.Error()) } - return &proto.FindOnePetResponse{Pet: RawToDto(&pet, []string{})}, err + + images, err := s.imageService.FindByPetId(req.Id) + if err != nil { + return nil, status.Error(codes.Internal, "error querying image service") + } + imageUrls := ExtractImageUrls(images) + + return &proto.FindOnePetResponse{Pet: RawToDto(&pet, imageUrls)}, err } func (s *Service) Create(_ context.Context, req *proto.CreatePetRequest) (res *proto.CreatePetResponse, err error) { @@ -128,12 +151,16 @@ func (s *Service) Create(_ context.Context, req *proto.CreatePetRequest) (res *p return &proto.CreatePetResponse{Pet: RawToDto(raw, imgUrls)}, nil } -func RawToDtoList(in *[]*pet.Pet) []*proto.Pet { +func RawToDtoList(in *[]*pet.Pet, imageUrls [][]string) ([]*proto.Pet, error) { var result []*proto.Pet - for _, e := range *in { - result = append(result, RawToDto(e, []string{})) + if len(*in) != len(imageUrls) { + return nil, errors.New("length of in and imageUrls have to be the same") } - return result + + for i, e := range *in { + result = append(result, RawToDto(e, imageUrls[i])) + } + return result, nil } func RawToDto(in *pet.Pet, imgUrl []string) *proto.Pet { diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index 4235945..664d1b6 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -38,6 +38,8 @@ type PetServiceTest struct { ChangeViewPetReqMock *proto.ChangeViewPetRequest Images []*img_proto.Image ImageUrls []string + ImagesList [][]*img_proto.Image + ImageUrlsList [][]string } func TestPetService(t *testing.T) { @@ -45,48 +47,59 @@ func TestPetService(t *testing.T) { } func (t *PetServiceTest) SetupTest() { - t.Pet = &pet.Pet{ - Base: model.Base{ - ID: uuid.New(), - CreatedAt: time.Time{}, - UpdatedAt: time.Time{}, - DeletedAt: gorm.DeletedAt{}, - }, - Type: faker.Word(), - Species: faker.Word(), - Name: faker.Name(), - Birthdate: faker.Word(), - Gender: petConst.Gender(rand.Intn(1) + 1), - Habit: faker.Paragraph(), - Caption: faker.Paragraph(), - Status: petConst.Status(rand.Intn(1) + 1), - IsSterile: true, - IsVaccinated: true, - IsVisible: true, - IsClubPet: true, - Background: faker.Paragraph(), - Address: faker.Paragraph(), - Contact: faker.Paragraph(), + var pets []*pet.Pet + for i := 0; i <= 3; i++ { + pet := &pet.Pet{ + Base: model.Base{ + ID: uuid.New(), + CreatedAt: time.Time{}, + UpdatedAt: time.Time{}, + DeletedAt: gorm.DeletedAt{}, + }, + Type: faker.Word(), + Species: faker.Word(), + Name: faker.Name(), + Birthdate: faker.Word(), + Gender: petConst.Gender(rand.Intn(1) + 1), + Habit: faker.Paragraph(), + Caption: faker.Paragraph(), + Status: petConst.Status(rand.Intn(1) + 1), + IsSterile: true, + IsVaccinated: true, + IsVisible: true, + IsClubPet: true, + Background: faker.Paragraph(), + Address: faker.Paragraph(), + Contact: faker.Paragraph(), + } + var images []*img_proto.Image + var imageUrls []string + for i := 0; i < 3; i++ { + url := faker.URL() + images = append(images, &img_proto.Image{ + Id: faker.UUIDDigit(), + PetId: pet.ID.String(), + ImageUrl: url, + }) + imageUrls = append(imageUrls, url) + } + t.ImagesList = append(t.ImagesList, images) + t.ImageUrlsList = append(t.ImageUrlsList, imageUrls) + pets = append(pets, pet) } - t.Images = []*img_proto.Image{ - { - Id: faker.UUIDDigit(), - PetId: t.Pet.ID.String(), - ImageUrl: faker.URL(), - }, - { - Id: faker.UUIDDigit(), - PetId: t.Pet.ID.String(), - ImageUrl: faker.URL(), - }, - } + t.Pets = pets + t.Pet = pets[0] - t.ImageUrls = []string{ - t.Images[0].ImageUrl, - t.Images[1].ImageUrl, + for _, images := range t.ImagesList { + for _, image := range images { + t.ImageUrls = append(t.ImageUrls, image.ImageUrl) + } } + t.Images = t.ImagesList[0] + t.ImageUrls = t.ImageUrlsList[0] + t.PetDto = &proto.Pet{ Id: t.Pet.ID.String(), Type: t.Pet.Type, @@ -202,6 +215,7 @@ func (t *PetServiceTest) SetupTest() { Id: t.Pet.ID.String(), Visible: false, } + } func (t *PetServiceTest) TestDeleteSuccess() { want := &proto.DeletePetResponse{Success: true} @@ -259,8 +273,6 @@ func (t *PetServiceTest) TestDeleteWithUnexpectedError() { } func (t *PetServiceTest) TestFindOneSuccess() { - t.PetDto.ImageUrls = []string{} - want := &proto.FindOnePetResponse{Pet: t.PetDto} repo := &mock.RepositoryMock{} @@ -276,14 +288,18 @@ func (t *PetServiceTest) TestFindOneSuccess() { } func (t *PetServiceTest) TestFindAllSuccess() { - var pets []*pet.Pet - want := &proto.FindAllPetResponse{Pets: createPetsDto(t.Pets)} + want := &proto.FindAllPetResponse{Pets: t.createPetsDto(t.Pets, t.ImageUrlsList)} + + var petsIn []*pet.Pet repo := &mock.RepositoryMock{} - repo.On("FindAll", pets).Return(&t.Pets, nil) + repo.On("FindAll", petsIn).Return(&t.Pets, nil) + imgSrv := new(img_mock.ServiceMock) - imgSrv.On("FindByPetId", t.Pet.ID.String()).Return(t.Images, nil) + for i, pet := range t.Pets { + imgSrv.On("FindByPetId", pet.ID.String()).Return(t.ImagesList[i], nil) + } srv := NewService(repo, imgSrv) @@ -309,10 +325,43 @@ func (t *PetServiceTest) TestFindOneNotFound() { assert.Equal(t.T(), codes.NotFound, st.Code()) } -func createPetsDto(in []*pet.Pet) []*proto.Pet { +func createPets() []*pet.Pet { + var result []*pet.Pet + + for i := 0; i < rand.Intn(4)+1; i++ { + r := &pet.Pet{ + Base: model.Base{ + ID: uuid.New(), + CreatedAt: time.Time{}, + UpdatedAt: time.Time{}, + DeletedAt: gorm.DeletedAt{}, + }, + Type: faker.Word(), + Species: faker.Word(), + Name: faker.Name(), + Birthdate: faker.Word(), + Gender: petConst.Gender(rand.Intn(1) + 1), + Habit: faker.Paragraph(), + Caption: faker.Paragraph(), + Status: petConst.Status(rand.Intn(1) + 1), + IsSterile: true, + IsVaccinated: true, + IsVisible: true, + IsClubPet: true, + Background: faker.Paragraph(), + Address: faker.Paragraph(), + Contact: faker.Paragraph(), + } + result = append(result, r) + } + + return result +} + +func (t *PetServiceTest) createPetsDto(in []*pet.Pet, imageUrlsList [][]string) []*proto.Pet { var result []*proto.Pet - for _, p := range in { + for i, p := range in { r := &proto.Pet{ Id: p.ID.String(), Type: p.Type, @@ -322,8 +371,8 @@ func createPetsDto(in []*pet.Pet) []*proto.Pet { Gender: proto.Gender(p.Gender), Habit: p.Habit, Caption: p.Caption, - Status: 0, - ImageUrls: []string{}, + Status: proto.PetStatus(p.Status), + ImageUrls: imageUrlsList[i], IsSterile: p.IsSterile, IsVaccinated: p.IsVaccinated, IsVisible: p.IsVisible, @@ -447,6 +496,7 @@ func (t *PetServiceTest) TestChangeViewSuccess() { repo.On("FindOne", t.Pet.ID.String(), &pet.Pet{}).Return(t.Pet, nil) repo.On("Update", t.Pet.ID.String(), t.ChangeViewPet).Return(t.ChangeViewPet, nil) imgSrv := new(img_mock.ServiceMock) + imgSrv.On("FindByPetId", t.Pet.ID.String()).Return(t.Images, nil) srv := NewService(repo, imgSrv) actual, err := srv.ChangeView(context.Background(), t.ChangeViewPetReqMock)