Skip to content

Commit

Permalink
Merge pull request #13 from isd-sgcu/feature/adopt-pet
Browse files Browse the repository at this point in the history
feature/adopt pet
  • Loading branch information
Sunioatm authored Jan 3, 2024
2 parents c9989d9 + b695aa9 commit 5ab389a
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 4 deletions.
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ 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/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.0.8 h1:nIQBZgK2OFVrLjVtpeDgwows8poA7LhsIVE4hlbBC1o=
github.com/isd-sgcu/johnjud-go-proto v0.0.8/go.mod h1:HP0w9gC30b5WNnqeFBM9JJZud+pvyikz0+pGFSI/Wjw=
github.com/isd-sgcu/johnjud-go-proto v0.0.9 h1:cFfZ2JSpW0jg94Iv5zHQJGnoekj0eCQe42SJaTpnp3c=
github.com/isd-sgcu/johnjud-go-proto v0.0.9/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
Expand Down Expand Up @@ -109,8 +107,6 @@ 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/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.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
19 changes: 19 additions & 0 deletions src/app/service/pet/pet.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,25 @@ func (s *Service) Create(_ context.Context, req *proto.CreatePetRequest) (res *p
return &proto.CreatePetResponse{Pet: RawToDto(raw, imgUrls)}, nil
}

func (s *Service) AdoptPet(ctx context.Context, req *proto.AdoptPetRequest) (res *proto.AdoptPetResponse, err error) {
dtoPet, err := s.FindOne(context.Background(), &proto.FindOnePetRequest{Id: req.PetId})
if err != nil {
return nil, status.Error(codes.NotFound, "pet not found")
}
pet, err := DtoToRaw(dtoPet.Pet)
if err != nil {
return nil, status.Error(codes.Internal, "error converting dto to raw")
}
pet.AdoptBy = req.UserId

err = s.repository.Update(req.PetId, pet)
if err != nil {
return nil, status.Error(codes.NotFound, "pet not found")
}

return &proto.AdoptPetResponse{Success: true}, nil
}

func RawToDtoList(in *[]*pet.Pet, imageUrls [][]string) ([]*proto.Pet, error) {
var result []*proto.Pet
if len(*in) != len(imageUrls) {
Expand Down
88 changes: 88 additions & 0 deletions src/app/service/pet/pet.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type PetServiceTest struct {
ImageUrls []string
ImagesList [][]*img_proto.Image
ImageUrlsList [][]string
ChangeAdoptBy *pet.Pet
AdoptByReq *proto.AdoptPetRequest
}

func TestPetService(t *testing.T) {
Expand Down Expand Up @@ -71,6 +73,7 @@ func (t *PetServiceTest) SetupTest() {
Background: faker.Paragraph(),
Address: faker.Paragraph(),
Contact: faker.Paragraph(),
AdoptBy: "",
}
var images []*img_proto.Image
var imageUrls []string
Expand Down Expand Up @@ -216,6 +219,36 @@ func (t *PetServiceTest) SetupTest() {
Visible: false,
}

t.ChangeAdoptBy = &pet.Pet{
Base: model.Base{
ID: t.Pet.Base.ID,
CreatedAt: t.Pet.Base.CreatedAt,
UpdatedAt: t.Pet.Base.UpdatedAt,
DeletedAt: t.Pet.Base.DeletedAt,
},
Type: t.Pet.Type,
Species: t.Pet.Species,
Name: t.Pet.Name,
Birthdate: t.Pet.Birthdate,
Gender: t.Pet.Gender,
Habit: t.Pet.Habit,
Caption: t.Pet.Caption,
Status: t.Pet.Status,
IsSterile: t.Pet.IsSterile,
IsVaccinated: t.Pet.IsVaccinated,
IsVisible: t.Pet.IsVisible,
IsClubPet: t.Pet.IsClubPet,
Background: t.Pet.Background,
Address: t.Pet.Address,
Contact: t.Pet.Contact,
AdoptBy: faker.UUIDDigit(),
}

t.AdoptByReq = &proto.AdoptPetRequest{
PetId: t.ChangeAdoptBy.ID.String(),
UserId: t.ChangeAdoptBy.AdoptBy,
}

}
func (t *PetServiceTest) TestDeleteSuccess() {
want := &proto.DeletePetResponse{Success: true}
Expand Down Expand Up @@ -520,3 +553,58 @@ func (t *PetServiceTest) TestChangeViewNotFound() {
assert.Nil(t.T(), actual)
assert.Equal(t.T(), codes.NotFound, st.Code())
}

func (t *PetServiceTest) TestAdoptBySuccess() {
want := &proto.AdoptPetResponse{Success: true}
repo := &mock.RepositoryMock{}

repo.On("FindOne", t.AdoptByReq.PetId, &pet.Pet{}).Return(t.Pet, nil)
repo.On("Update", t.AdoptByReq.PetId, t.ChangeAdoptBy).Return(t.ChangeAdoptBy, nil)

imgSrv := new(img_mock.ServiceMock)
imgSrv.On("FindByPetId", t.Pet.ID.String()).Return(t.Images, nil)

srv := NewService(repo, imgSrv)

actual, err := srv.AdoptPet(context.Background(), t.AdoptByReq)

assert.Nil(t.T(), err)
assert.Equal(t.T(), want, actual)
}

func (t *PetServiceTest) TestAdoptByPetNotFound() {
wantError := status.Error(codes.NotFound, "pet not found")
repo := &mock.RepositoryMock{}

repo.On("FindOne", t.AdoptByReq.PetId, &pet.Pet{}).Return(nil, wantError)

imgSrv := new(img_mock.ServiceMock)
srv := NewService(repo, imgSrv)

actual, err := srv.AdoptPet(context.Background(), t.AdoptByReq)

assert.NotNil(t.T(), err)
assert.Equal(t.T(), wantError, err)
assert.Nil(t.T(), actual)

repo.AssertNotCalled(t.T(), "Update", t.AdoptByReq.PetId, t.ChangeAdoptBy)
}

func (t *PetServiceTest) TestAdoptByUpdateError() {
wantError := status.Error(codes.NotFound, "pet not found")
repo := &mock.RepositoryMock{}

repo.On("FindOne", t.AdoptByReq.PetId, &pet.Pet{}).Return(t.Pet, nil)
repo.On("Update", t.AdoptByReq.PetId, t.ChangeAdoptBy).Return(nil, errors.New("update error"))

imgSrv := new(img_mock.ServiceMock)
imgSrv.On("FindByPetId", t.Pet.ID.String()).Return(nil, errors.New("pet not found"))

srv := NewService(repo, imgSrv)

actual, err := srv.AdoptPet(context.Background(), t.AdoptByReq)

assert.NotNil(t.T(), err)
assert.Equal(t.T(), wantError, err)
assert.Nil(t.T(), actual)
}

0 comments on commit 5ab389a

Please sign in to comment.