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

Gear/joh 56 johnjud backend filter find all pet #18

Merged
merged 24 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.21.5
require (
github.com/bxcodec/faker/v3 v3.8.1
github.com/google/uuid v1.5.0
github.com/isd-sgcu/johnjud-go-proto v0.3.0
github.com/isd-sgcu/johnjud-go-proto v0.4.2
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.31.0
github.com/spf13/viper v1.18.2
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ 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.3.0 h1:UsAf2l06VypAp8nC5Ksru1QeoYwRjjxP/WKJwNOm1iY=
github.com/isd-sgcu/johnjud-go-proto v0.3.0/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4=
github.com/isd-sgcu/johnjud-go-proto v0.3.2 h1:lOz8wwoHx71lW6YnEshNDIpnhj2IUP39Zx4lqruYzA8=
github.com/isd-sgcu/johnjud-go-proto v0.3.2/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4=
github.com/isd-sgcu/johnjud-go-proto v0.4.0 h1:YMmi37Siexo84zlUEhkSnFsZtf/U+bFLxtaUW8+A8Ow=
github.com/isd-sgcu/johnjud-go-proto v0.4.0/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4=
github.com/isd-sgcu/johnjud-go-proto v0.4.1 h1:CrJbGs8oy3UdKpPv2XhCIZo2cKrSeGtsR/FKHeaSPM4=
github.com/isd-sgcu/johnjud-go-proto v0.4.1/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4=
github.com/isd-sgcu/johnjud-go-proto v0.4.2 h1:KepvAHrFmi9Ki5kdQTKWZ95knjxeVv78b6ru3WRjNpg=
github.com/isd-sgcu/johnjud-go-proto v0.4.2/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
Expand Down
16 changes: 16 additions & 0 deletions src/app/repository/pet/pet.repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ func NewRepository(db *gorm.DB) *Repository {

func (r *Repository) FindAll(result *[]*pet.Pet) error {
return r.db.Model(&pet.Pet{}).Find(result).Error
/*
err := r.db.Model(&pet.Pet{}).Find(result).Error
if err != nil {
return err
}

err = petUtils.FilterPet(result, query)
if err != nil {
return err
}
err = petUtils.PaginatePets(result, query.Page, query.PageSize)
if err != nil {
return err
}
return nil
*/
macgeargear marked this conversation as resolved.
Show resolved Hide resolved
}

func (r *Repository) FindOne(id string, result *pet.Pet) error {
Expand Down
139 changes: 19 additions & 120 deletions src/app/service/pet/pet.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package pet
import (
"context"
"errors"
"fmt"

"time"

"github.com/google/uuid"
"github.com/isd-sgcu/johnjud-backend/src/app/model"
"github.com/isd-sgcu/johnjud-backend/src/app/model/pet"
petConst "github.com/isd-sgcu/johnjud-backend/src/constant/pet"
petUtils "github.com/isd-sgcu/johnjud-backend/src/app/utils/pet"
proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1"
image_proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1"
"github.com/rs/zerolog/log"
Expand All @@ -25,11 +22,11 @@ type Service struct {
}

type IRepository interface {
FindAll(result *[]*pet.Pet) error
FindOne(id string, result *pet.Pet) error
Create(in *pet.Pet) error
Update(id string, result *pet.Pet) error
Delete(id string) error
FindAll(*[]*pet.Pet) error
FindOne(string, *pet.Pet) error
Create(*pet.Pet) error
Update(string, *pet.Pet) error
Delete(string) error
}

type ImageService interface {
Expand All @@ -52,7 +49,7 @@ func (s *Service) Delete(ctx context.Context, req *proto.DeletePetRequest) (*pro
}

func (s *Service) Update(_ context.Context, req *proto.UpdatePetRequest) (res *proto.UpdatePetResponse, err error) {
raw, err := DtoToRaw(req.Pet)
raw, err := petUtils.DtoToRaw(req.Pet)
if err != nil {
return nil, status.Error(codes.Internal, "error converting dto to raw")
}
Expand All @@ -67,15 +64,15 @@ func (s *Service) Update(_ context.Context, req *proto.UpdatePetRequest) (res *p
return nil, status.Error(codes.Internal, "error querying image service")
}

return &proto.UpdatePetResponse{Pet: RawToDto(raw, images)}, nil
return &proto.UpdatePetResponse{Pet: petUtils.RawToDto(raw, images)}, nil
}

func (s *Service) ChangeView(_ context.Context, req *proto.ChangeViewPetRequest) (res *proto.ChangeViewPetResponse, err error) {
petData, err := s.FindOne(context.Background(), &proto.FindOnePetRequest{Id: req.Id})
if err != nil {
return nil, status.Error(codes.NotFound, "pet not found")
}
pet, err := DtoToRaw(petData.Pet)
pet, err := petUtils.DtoToRaw(petData.Pet)
if err != nil {
return nil, status.Error(codes.Internal, "error converting dto to raw")
}
Expand All @@ -99,19 +96,20 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res
return nil, status.Error(codes.Unavailable, "Internal error")
}

petUtils.FilterPet(&pets, req)
petUtils.PaginatePets(&pets, req.Page, req.PageSize)

for _, pet := range pets {
images, err := s.imageService.FindByPetId(pet.ID.String())
if err != nil {
return nil, status.Error(codes.Internal, "error querying image service")
}
imagesList = append(imagesList, images)
}

petWithImages, err := RawToDtoList(&pets, imagesList)
petWithImages, err := petUtils.RawToDtoList(&pets, imagesList, req)
if err != nil {
return nil, status.Error(codes.Internal, "error converting raw to dto list")
return nil, status.Error(codes.Internal, fmt.Sprintf("error converting raw to dto list: %v", err))
}

return &proto.FindAllPetResponse{Pets: petWithImages}, nil
}

Expand All @@ -130,11 +128,11 @@ func (s Service) FindOne(_ context.Context, req *proto.FindOnePetRequest) (res *
return nil, status.Error(codes.Internal, "error querying image service")
}

return &proto.FindOnePetResponse{Pet: RawToDto(&pet, images)}, err
return &proto.FindOnePetResponse{Pet: petUtils.RawToDto(&pet, images)}, err
}

func (s *Service) Create(_ context.Context, req *proto.CreatePetRequest) (res *proto.CreatePetResponse, err error) {
raw, err := DtoToRaw(req.Pet)
raw, err := petUtils.DtoToRaw(req.Pet)
if err != nil {
return nil, status.Error(codes.Internal, "error converting dto to raw: "+err.Error())
}
Expand All @@ -146,15 +144,15 @@ func (s *Service) Create(_ context.Context, req *proto.CreatePetRequest) (res *p
return nil, status.Error(codes.Internal, "failed to create pet")
}

return &proto.CreatePetResponse{Pet: RawToDto(raw, images)}, nil
return &proto.CreatePetResponse{Pet: petUtils.RawToDto(raw, images)}, 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)
pet, err := petUtils.DtoToRaw(dtoPet.Pet)
if err != nil {
return nil, status.Error(codes.Internal, "error converting dto to raw")
}
Expand All @@ -167,102 +165,3 @@ func (s *Service) AdoptPet(ctx context.Context, req *proto.AdoptPetRequest) (res

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

func RawToDtoList(in *[]*pet.Pet, images [][]*image_proto.Image) ([]*proto.Pet, error) {
var result []*proto.Pet
if len(*in) != len(images) {
return nil, errors.New("length of in and imageUrls have to be the same")
}

for i, e := range *in {
result = append(result, RawToDto(e, images[i]))
}
return result, nil
}

func RawToDto(in *pet.Pet, images []*image_proto.Image) *proto.Pet {
return &proto.Pet{
Id: in.ID.String(),
Type: in.Type,
Species: in.Species,
Name: in.Name,
Birthdate: in.Birthdate,
Gender: string(in.Gender),
Color: in.Color,
Pattern: in.Pattern,
Habit: in.Habit,
Caption: in.Caption,
Status: string(in.Status),
Images: images,
IsSterile: in.IsSterile,
IsVaccinated: in.IsVaccinated,
IsVisible: in.IsVisible,
IsClubPet: in.IsClubPet,
Origin: in.Origin,
Address: in.Address,
Contact: in.Contact,
AdoptBy: in.AdoptBy,
}
}

func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) {
var id uuid.UUID
var gender petConst.Gender
var status petConst.Status

if in.Id != "" {
id, err = uuid.Parse(in.Id)
if err != nil {
return nil, err
}
}

switch in.Gender {
case string(petConst.MALE):
gender = petConst.MALE
case string(petConst.FEMALE):
gender = petConst.FEMALE
}

switch in.Status {
case string(petConst.ADOPTED):
status = petConst.ADOPTED
case string(petConst.FINDHOME):
status = petConst.FINDHOME
}

return &pet.Pet{
Base: model.Base{
ID: id,
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
DeletedAt: gorm.DeletedAt{},
},
Type: in.Type,
Species: in.Species,
Name: in.Name,
Birthdate: in.Birthdate,
Gender: gender,
Color: in.Color,
Pattern: in.Pattern,
Habit: in.Habit,
Caption: in.Caption,
Status: status,
IsSterile: in.IsSterile,
IsVaccinated: in.IsVaccinated,
IsVisible: in.IsVisible,
IsClubPet: in.IsClubPet,
Origin: in.Origin,
Address: in.Address,
Contact: in.Contact,
AdoptBy: in.AdoptBy,
}, nil
}

func ExtractImageUrls(in []*image_proto.Image) []string {
var result []string
for _, e := range in {
result = append(result, e.ImageUrl)
}
return result
}
1 change: 0 additions & 1 deletion src/app/service/pet/pet.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ func (t *PetServiceTest) TestFindAllSuccess() {
srv := NewService(repo, imgSrv)

actual, err := srv.FindAll(context.Background(), &proto.FindAllPetRequest{})

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