Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: isAdmin pet findall #36

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
31 changes: 29 additions & 2 deletions src/app/handler/pet/pet.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/app/handler/pet/pet.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/app/service/pet/pet.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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{
Expand Down
8 changes: 4 additions & 4 deletions src/app/service/pet/pet.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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{
Expand All @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions src/app/utils/pet/pet.utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}
}

Expand Down
1 change: 1 addition & 0 deletions src/constant/auth/auth.constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
Expand Down
1 change: 1 addition & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/mocks/service/pet/pet.mock.go

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

2 changes: 1 addition & 1 deletion src/pkg/service/pet/pet.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading