diff --git a/src/app/service/image/image.service.go b/src/app/service/image/image.service.go index 4a08c46..1ead4af 100644 --- a/src/app/service/image/image.service.go +++ b/src/app/service/image/image.service.go @@ -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() diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index 41a6aad..3ab604a 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -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 diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index 84f92e6..653376b 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -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) diff --git a/src/mocks/service/image/image.mock.go b/src/mocks/service/image/image.mock.go index 37bcdae..bb59c31 100644 --- a/src/mocks/service/image/image.mock.go +++ b/src/mocks/service/image/image.mock.go @@ -64,6 +64,21 @@ func (mr *MockServiceMockRecorder) Delete(arg0 interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockService)(nil).Delete), arg0) } +// DeleteByPetId mocks base method. +func (m *MockService) DeleteByPetId(arg0 string) (*dto.DeleteImageResponse, *dto.ResponseErr) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteByPetId", arg0) + ret0, _ := ret[0].(*dto.DeleteImageResponse) + ret1, _ := ret[1].(*dto.ResponseErr) + return ret0, ret1 +} + +// DeleteByPetId indicates an expected call of DeleteByPetId. +func (mr *MockServiceMockRecorder) DeleteByPetId(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteByPetId", reflect.TypeOf((*MockService)(nil).DeleteByPetId), arg0) +} + // FindAll mocks base method. func (m *MockService) FindAll() ([]*dto.ImageResponse, *dto.ResponseErr) { m.ctrl.T.Helper() diff --git a/src/pkg/service/image/image.service.go b/src/pkg/service/image/image.service.go index bb9b976..d4f92d8 100644 --- a/src/pkg/service/image/image.service.go +++ b/src/pkg/service/image/image.service.go @@ -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) }