Skip to content

Commit

Permalink
Merge pull request #34 from isd-sgcu/pet-svc/delete-cascade-img
Browse files Browse the repository at this point in the history
Pet svc/delete cascade img
  • Loading branch information
bookpanda authored Feb 17, 2024
2 parents 76fc556 + 932ff3b commit d32347c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
42 changes: 42 additions & 0 deletions src/app/service/image/image.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,48 @@ func (s *Service) Delete(id string) (*dto.DeleteImageResponse, *dto.ResponseErr)
}, nil
}

func (s *Service) DeleteByPetId(petId string) (*dto.DeleteImageResponse, *dto.ResponseErr) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

request := &proto.DeleteByPetIdRequest{
PetId: petId,
}

res, errRes := s.client.DeleteByPetId(ctx, request)
if errRes != nil {
st, _ := status.FromError(errRes)
log.Error().
Err(errRes).
Str("service", "image").
Str("module", "delete by pet id").
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) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
Expand Down
6 changes: 6 additions & 0 deletions src/app/service/pet/pet.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ func (s *Service) Delete(id string) (result *dto.DeleteResponse, err *dto.Respon
}
}
}

_, errSvc := s.imageService.DeleteByPetId(id)
if errSvc != nil {
return nil, errSvc
}

return &dto.DeleteResponse{
Success: res.Success,
}, nil
Expand Down
13 changes: 10 additions & 3 deletions src/app/service/pet/pet.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,19 +543,26 @@ func (t *PetServiceTest) TestUpdateUnavailableServiceError() {
}

func (t *PetServiceTest) TestDeleteSuccess() {
protoReq := &petproto.DeletePetRequest{
petProtoReq := &petproto.DeletePetRequest{
Id: t.Pet.Id,
}
protoResp := &petproto.DeletePetResponse{
petProtoResp := &petproto.DeletePetResponse{
Success: true,
}
imageProtoReq := &imgproto.DeleteByPetIdRequest{
PetId: t.Pet.Id,
}
imageProtoResp := &imgproto.DeleteByPetIdResponse{
Success: true,
}

expected := &dto.DeleteResponse{Success: true}

client := &petmock.PetClientMock{}
client.On("Delete", protoReq).Return(protoResp, nil)
client.On("Delete", petProtoReq).Return(petProtoResp, nil)

imageClient := imagemock.ImageClientMock{}
imageClient.On("DeleteByPetId", imageProtoReq).Return(imageProtoResp, nil)

imageSvc := imageSvc.NewService(&imageClient)
svc := NewService(client, imageSvc)
Expand Down
15 changes: 15 additions & 0 deletions src/mocks/service/image/image.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/pkg/service/image/image.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ type Service interface {
FindByPetId(string) ([]*dto.ImageResponse, *dto.ResponseErr)
Upload(*dto.UploadImageRequest) (*dto.ImageResponse, *dto.ResponseErr)
Delete(string) (*dto.DeleteImageResponse, *dto.ResponseErr)
DeleteByPetId(string) (*dto.DeleteImageResponse, *dto.ResponseErr)
AssignPet(*dto.AssignPetRequest) (*dto.AssignPetResponse, *dto.ResponseErr)
}

0 comments on commit d32347c

Please sign in to comment.