From 8f3c9c43e518937be6fcd66c49944c4d557093e3 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Wed, 21 Feb 2024 23:46:17 +0700 Subject: [PATCH] feat: isAdmin pet findall --- go.mod | 4 ++-- go.sum | 8 +++---- src/app/handler/pet/pet.handler.go | 31 +++++++++++++++++++++++-- src/app/handler/pet/pet.handler_test.go | 4 ++-- src/app/service/pet/pet.service.go | 6 ++--- src/app/service/pet/pet.service_test.go | 8 +++---- src/app/utils/pet/pet.utils.go | 8 +++++-- src/constant/auth/auth.constant.go | 1 + src/main.go | 1 + src/mocks/service/pet/pet.mock.go | 8 +++---- src/pkg/service/pet/pet.service.go | 2 +- 11 files changed, 57 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index b9fcea2..3a00ffe 100644 --- a/go.mod +++ b/go.mod @@ -12,12 +12,12 @@ require ( github.com/gofiber/fiber/v2 v2.52.0 github.com/golang/mock v1.6.0 github.com/google/uuid v1.5.0 - github.com/isd-sgcu/johnjud-go-proto v0.6.0 + github.com/isd-sgcu/johnjud-go-proto v0.6.1 github.com/rs/zerolog v1.31.0 github.com/spf13/viper v1.18.1 github.com/stretchr/testify v1.8.4 github.com/swaggo/swag v1.16.2 - google.golang.org/grpc v1.61.0 + google.golang.org/grpc v1.61.1 ) require ( diff --git a/go.sum b/go.sum index df32434..9663fd7 100644 --- a/go.sum +++ b/go.sum @@ -66,8 +66,8 @@ github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= 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.6.0 h1:sOWGjsqXwzpSaweSlNqPlttExZryp8mV76ob95LppjM= -github.com/isd-sgcu/johnjud-go-proto v0.6.0/go.mod h1:Yfajs+jSTcuVAKK9xLcVbZkUvCBO3exZV8bOGdelvW0= +github.com/isd-sgcu/johnjud-go-proto v0.6.1 h1:9gGBsMOONhuIsSEavMb9FiD7d+naX26niSQPwQGHOM0= +github.com/isd-sgcu/johnjud-go-proto v0.6.1/go.mod h1:0728XfYpZw/4K/+NmXsyVt1z7Cxwbc4nxLg7LpowIE4= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= @@ -244,8 +244,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f h1:ultW7fxlIvee4HYrtnaRPon9HpEgFk5zYpmfMgtKB5I= google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f/go.mod h1:L9KNLi232K1/xB6f7AlSX692koaRnKaWSR0stBki0Yc= -google.golang.org/grpc v1.61.0 h1:TOvOcuXn30kRao+gfcvsebNEa5iZIiLkisYEkf7R7o0= -google.golang.org/grpc v1.61.0/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs= +google.golang.org/grpc v1.61.1 h1:kLAiWrZs7YeDM6MumDe7m3y4aM6wacLzM1Y/wiLP9XY= +google.golang.org/grpc v1.61.1/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs= 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.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= diff --git a/src/app/handler/pet/pet.handler.go b/src/app/handler/pet/pet.handler.go index dfce616..c35048b 100644 --- a/src/app/handler/pet/pet.handler.go +++ b/src/app/handler/pet/pet.handler.go @@ -23,7 +23,7 @@ func NewHandler(service petSvc.Service, imageService imageSvc.Service, validate return &Handler{service, imageService, validate} } -// FindAll is a function that returns all pets in database +// FindAll is a function that returns all VISIBLE pets in database // @Summary finds all pets // @Description Returns the data of pets if successful // @Tags pet @@ -40,7 +40,34 @@ func (h *Handler) FindAll(c router.IContext) { c.JSON(http.StatusBadRequest, err) } - response, respErr := h.service.FindAll(request) + response, respErr := h.service.FindAll(request, false) + if respErr != nil { + c.JSON(respErr.StatusCode, respErr) + return + } + + c.JSON(http.StatusOK, response) + return +} + +// FindAllAdmin is a function that returns ALL pets in database +// @Summary finds all pets +// @Description Returns the data of pets if successful +// @Tags pet +// @Accept json +// @Produce json +// @Success 200 {object} []dto.PetResponse +// @Failure 500 {object} dto.ResponseInternalErr "Internal service error" +// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down" +// @Router /v1/pets/admin [get] +func (h *Handler) FindAllAdmin(c router.IContext) { + queries := c.Queries() + request, err := petUtils.QueriesToFindAllDto(queries) + if err != nil { + c.JSON(http.StatusBadRequest, err) + } + + response, respErr := h.service.FindAll(request, true) if respErr != nil { c.JSON(respErr.StatusCode, respErr) return diff --git a/src/app/handler/pet/pet.handler_test.go b/src/app/handler/pet/pet.handler_test.go index 96cc980..aa337d2 100644 --- a/src/app/handler/pet/pet.handler_test.go +++ b/src/app/handler/pet/pet.handler_test.go @@ -169,7 +169,7 @@ func (t *PetHandlerTest) SetupTest() { } func (t *PetHandlerTest) TestFindAllSuccess() { - findAllResponse := utils.ProtoToDtoList(t.Pets, t.ImagesList) + findAllResponse := utils.ProtoToDtoList(t.Pets, t.ImagesList, false) metadataResponse := t.Metadata expectedResponse := &dto.FindAllPetResponse{ Pets: findAllResponse, @@ -184,7 +184,7 @@ func (t *PetHandlerTest) TestFindAllSuccess() { context := routerMock.NewMockIContext(controller) context.EXPECT().Queries().Return(t.QueriesMock) - petSvc.EXPECT().FindAll(t.FindAllPetRequest).Return(expectedResponse, nil) + petSvc.EXPECT().FindAll(t.FindAllPetRequest, false).Return(expectedResponse, nil) context.EXPECT().JSON(http.StatusOK, expectedResponse) handler := NewHandler(petSvc, imageSvc, validator) diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index f15d67a..be98fa4 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -28,11 +28,11 @@ func NewService(petClient petproto.PetServiceClient, imageService imageSvc.Servi } } -func (s *Service) FindAll(in *dto.FindAllPetRequest) (result *dto.FindAllPetResponse, err *dto.ResponseErr) { +func (s *Service) FindAll(in *dto.FindAllPetRequest, isAdmin bool) (result *dto.FindAllPetResponse, err *dto.ResponseErr) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - res, errRes := s.petClient.FindAll(ctx, utils.FindAllDtoToProto(in)) + res, errRes := s.petClient.FindAll(ctx, utils.FindAllDtoToProto(in, isAdmin)) if errRes != nil { st, _ := status.FromError(errRes) log.Error(). @@ -61,7 +61,7 @@ func (s *Service) FindAll(in *dto.FindAllPetRequest) (result *dto.FindAllPetResp } imagesList := utils.ImageList(images) - findAllDto := utils.ProtoToDtoList(res.Pets, imagesList) + findAllDto := utils.ProtoToDtoList(res.Pets, imagesList, isAdmin) metaData := utils.MetadataProtoToDto(res.Metadata) return &dto.FindAllPetResponse{ diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index 72f1bfd..3d4bdfe 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -199,7 +199,7 @@ func (t *PetServiceTest) SetupTest() { AdoptBy: t.Pet.AdoptBy, } - t.FindAllPetReq = utils.FindAllDtoToProto(t.FindAllPetDto) + t.FindAllPetReq = utils.FindAllDtoToProto(t.FindAllPetDto, true) t.CreatePetReq = utils.CreateDtoToProto(t.CreatePetDto) t.UpdatePetReq = utils.UpdateDtoToProto(t.Pet.Id, t.UpdatePetDto) @@ -268,7 +268,7 @@ func (t *PetServiceTest) TestFindAllSuccess() { Metadata: t.MetadataProto, } - findAllPPetsDto := utils.ProtoToDtoList(t.Pets, t.ImagesList) + findAllPPetsDto := utils.ProtoToDtoList(t.Pets, t.ImagesList, false) metadataDto := t.MetadataDto expected := &dto.FindAllPetResponse{ @@ -287,7 +287,7 @@ func (t *PetServiceTest) TestFindAllSuccess() { imageSvc := imageSvc.NewService(&imageClient) svc := NewService(&client, imageSvc) - actual, err := svc.FindAll(t.FindAllPetDto) + actual, err := svc.FindAll(t.FindAllPetDto, true) assert.Nil(t.T(), err) assert.Equal(t.T(), expected, actual) @@ -305,7 +305,7 @@ func (t *PetServiceTest) TestFindAllUnavailableServiceError() { imageSvc := imageSvc.NewService(&imageClient) svc := NewService(&client, imageSvc) - actual, err := svc.FindAll(t.FindAllPetDto) + actual, err := svc.FindAll(t.FindAllPetDto, true) assert.Nil(t.T(), actual) assert.Equal(t.T(), expected, err) diff --git a/src/app/utils/pet/pet.utils.go b/src/app/utils/pet/pet.utils.go index 310b91f..54b5205 100644 --- a/src/app/utils/pet/pet.utils.go +++ b/src/app/utils/pet/pet.utils.go @@ -143,9 +143,12 @@ func UpdateDtoToProto(id string, in *dto.UpdatePetRequest) *petproto.UpdatePetRe return req } -func ProtoToDtoList(in []*petproto.Pet, imagesList map[string][]*imgproto.Image) []*dto.PetResponse { +func ProtoToDtoList(in []*petproto.Pet, imagesList map[string][]*imgproto.Image, isAdmin bool) []*dto.PetResponse { var resp []*dto.PetResponse for _, p := range in { + if !isAdmin && !p.IsVisible { + continue + } pet := &dto.PetResponse{ Id: p.Id, Type: p.Type, @@ -182,7 +185,7 @@ func extractImages(images []*imgproto.Image) []dto.ImageResponse { return result } -func FindAllDtoToProto(in *dto.FindAllPetRequest) *petproto.FindAllPetRequest { +func FindAllDtoToProto(in *dto.FindAllPetRequest, isAdmin bool) *petproto.FindAllPetRequest { return &petproto.FindAllPetRequest{ Search: in.Search, Type: in.Type, @@ -194,6 +197,7 @@ func FindAllDtoToProto(in *dto.FindAllPetRequest) *petproto.FindAllPetRequest { Page: int32(in.Page), MaxAge: int32(in.MaxAge), MinAge: int32(in.MinAge), + IsAdmin: isAdmin, } } diff --git a/src/constant/auth/auth.constant.go b/src/constant/auth/auth.constant.go index e41e96b..615b0c8 100644 --- a/src/constant/auth/auth.constant.go +++ b/src/constant/auth/auth.constant.go @@ -14,6 +14,7 @@ var ExcludePath = map[string]struct{}{ var AdminPath = map[string]struct{}{ // "DELETE /user/:id": {}, + // "GET /pets/admin": {}, // "POST /pets": {}, // "PUT /pets/:id": {}, // "PUT /pets/:id/visible": {}, diff --git a/src/main.go b/src/main.go index 0538fa1..04e7a66 100644 --- a/src/main.go +++ b/src/main.go @@ -144,6 +144,7 @@ func main() { r.GetHealthCheck("", hc.HealthCheck) r.GetPet("", petHandler.FindAll) + r.GetPet("/admin", petHandler.FindAllAdmin) r.GetPet("/:id", petHandler.FindOne) r.PostPet("", petHandler.Create) r.PutPet("/:id", petHandler.Update) diff --git a/src/mocks/service/pet/pet.mock.go b/src/mocks/service/pet/pet.mock.go index eba3652..2e5ff16 100644 --- a/src/mocks/service/pet/pet.mock.go +++ b/src/mocks/service/pet/pet.mock.go @@ -95,18 +95,18 @@ func (mr *MockServiceMockRecorder) Delete(arg0 interface{}) *gomock.Call { } // FindAll mocks base method. -func (m *MockService) FindAll(arg0 *dto.FindAllPetRequest) (*dto.FindAllPetResponse, *dto.ResponseErr) { +func (m *MockService) FindAll(arg0 *dto.FindAllPetRequest, arg1 bool) (*dto.FindAllPetResponse, *dto.ResponseErr) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "FindAll", arg0) + ret := m.ctrl.Call(m, "FindAll", arg0, arg1) ret0, _ := ret[0].(*dto.FindAllPetResponse) ret1, _ := ret[1].(*dto.ResponseErr) return ret0, ret1 } // FindAll indicates an expected call of FindAll. -func (mr *MockServiceMockRecorder) FindAll(arg0 interface{}) *gomock.Call { +func (mr *MockServiceMockRecorder) FindAll(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindAll", reflect.TypeOf((*MockService)(nil).FindAll), arg0) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindAll", reflect.TypeOf((*MockService)(nil).FindAll), arg0, arg1) } // FindOne mocks base method. diff --git a/src/pkg/service/pet/pet.service.go b/src/pkg/service/pet/pet.service.go index fbbe5bc..302e947 100644 --- a/src/pkg/service/pet/pet.service.go +++ b/src/pkg/service/pet/pet.service.go @@ -5,7 +5,7 @@ import ( ) type Service interface { - FindAll(*dto.FindAllPetRequest) (*dto.FindAllPetResponse, *dto.ResponseErr) + FindAll(*dto.FindAllPetRequest, bool) (*dto.FindAllPetResponse, *dto.ResponseErr) FindOne(string) (*dto.PetResponse, *dto.ResponseErr) Create(*dto.CreatePetRequest) (*dto.PetResponse, *dto.ResponseErr) Update(string, *dto.UpdatePetRequest) (*dto.PetResponse, *dto.ResponseErr)