From e58f3286d4507cdfa7a5f6962f2738608d257732 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Fri, 12 Jan 2024 23:18:54 +0700 Subject: [PATCH 01/23] chage: update proto --- go.mod | 2 +- go.sum | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 80b2093..b29bdc9 100644 --- a/go.mod +++ b/go.mod @@ -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.0 github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.31.0 github.com/spf13/viper v1.18.2 diff --git a/go.sum b/go.sum index 7c1b4df..d8c95df 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,10 @@ 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/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= From b493327a0d88e9b0c805ac491986c65203500751 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 00:43:05 +0700 Subject: [PATCH 02/23] chore: update proto --- go.mod | 2 +- go.sum | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index b29bdc9..acfaa24 100644 --- a/go.mod +++ b/go.mod @@ -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.4.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 diff --git a/go.sum b/go.sum index d8c95df..efa2449 100644 --- a/go.sum +++ b/go.sum @@ -26,6 +26,10 @@ github.com/isd-sgcu/johnjud-go-proto v0.3.2 h1:lOz8wwoHx71lW6YnEshNDIpnhj2IUP39Z 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= From 98f6ef71aa80244e97f9ad7b1321093f6f7567bb Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 00:43:15 +0700 Subject: [PATCH 03/23] chore --- src/constant/pet/pet.constant.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/constant/pet/pet.constant.go b/src/constant/pet/pet.constant.go index 77e50c7..2ad000d 100644 --- a/src/constant/pet/pet.constant.go +++ b/src/constant/pet/pet.constant.go @@ -13,3 +13,6 @@ const ( ADOPTED Status = "adopted" FINDHOME Status = "findhome" ) + +const HOUR = 24 +const YEAR = 365 From 35400342611f21885a964c62b8d76accb6aa1580 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 00:43:53 +0700 Subject: [PATCH 04/23] feat: remove all helper to utils and add filter pets --- src/app/utils/pet/pet.utils.go | 171 +++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 src/app/utils/pet/pet.utils.go diff --git a/src/app/utils/pet/pet.utils.go b/src/app/utils/pet/pet.utils.go new file mode 100644 index 0000000..fce37a6 --- /dev/null +++ b/src/app/utils/pet/pet.utils.go @@ -0,0 +1,171 @@ +package pet + +import ( + "errors" + "fmt" + "strings" + "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" + proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" + imageProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1" + "gorm.io/gorm" +) + +func RawToDtoList(in *[]*pet.Pet, images [][]*imageProto.Image, query *proto.FindAllPetRequest) ([]*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, p := range *in { + res, err := filterAge(p, query.Age) + if err != nil { + return nil, err + } + if !res { + continue + } + if query.Search != "" && !strings.Contains(p.Name, query.Search) && !strings.Contains(p.Species, query.Search) { + continue + } + if query.Type != "" && p.Type != query.Type { + continue + } + if query.Gender != "" && p.Gender != petConst.Gender(query.Gender) { + continue + } + if query.Color != "" && p.Color != query.Color { + continue + } + if query.Pattern != "" && p.Pattern != query.Pattern { + continue + } + if query.Origin != "" && p.Origin != query.Origin { + continue + } + result = append(result, RawToDto(p, images[i])) + } + return result, nil +} + +func RawToDto(in *pet.Pet, images []*imageProto.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 []*imageProto.Image) []string { + var result []string + for _, e := range in { + result = append(result, e.ImageUrl) + } + return result +} + +func parseDate(dateStr string) (time.Time, error) { + parsedTime, err := time.Parse(time.RFC3339, dateStr) + if err != nil { + return time.Time{}, err + } + return parsedTime, nil +} + +func filterAge(pet *pet.Pet, age string) (bool, error) { + birthdate, err := parseDate(pet.Birthdate) + if err != nil { + return false, err + } + + currYear := time.Now() + birthYear := birthdate + diff := currYear.Sub(birthYear).Hours() / petConst.HOUR / petConst.YEAR + + fmt.Println(age, diff, currYear.Year()-birthYear.Year()) + switch age { + case "kitten": + return diff < 1, nil + case "adult": + return diff >= 1 && diff < 7, nil + case "senior": + return diff >= 7, nil + default: + return false, errors.New("filter age error. your age must be 'kitten' | 'adult' | 'senior' ") + } +} From dca9b07849508ccc419e5d2bb4f44fbedb4ca714 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 00:44:02 +0700 Subject: [PATCH 05/23] chore --- src/app/service/pet/pet.service.go | 127 +++-------------------------- 1 file changed, 12 insertions(+), 115 deletions(-) diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index 7317841..8a7ded1 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -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" @@ -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") } @@ -67,7 +64,7 @@ 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) { @@ -75,7 +72,7 @@ func (s *Service) ChangeView(_ context.Context, req *proto.ChangeViewPetRequest) 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") } @@ -106,12 +103,11 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res } imagesList = append(imagesList, images) } - - petWithImages, err := RawToDtoList(&pets, imagesList) + fmt.Println(req) + 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 } @@ -130,11 +126,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()) } @@ -146,7 +142,7 @@ 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) { @@ -154,7 +150,7 @@ func (s *Service) AdoptPet(ctx context.Context, req *proto.AdoptPetRequest) (res 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") } @@ -167,102 +163,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 -} From 2a31dba7f62c8b23ef7906b0e5ebf03940146032 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:50:19 +0700 Subject: [PATCH 06/23] feat: filter and paginate --- src/app/repository/pet/pet.repository.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/app/repository/pet/pet.repository.go b/src/app/repository/pet/pet.repository.go index 0f72ae1..062e2da 100644 --- a/src/app/repository/pet/pet.repository.go +++ b/src/app/repository/pet/pet.repository.go @@ -2,6 +2,8 @@ package pet import ( "github.com/isd-sgcu/johnjud-backend/src/app/model/pet" + petUtils "github.com/isd-sgcu/johnjud-backend/src/app/utils/pet" + proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" "gorm.io/gorm" ) @@ -13,8 +15,21 @@ func NewRepository(db *gorm.DB) *Repository { return &Repository{db: db} } -func (r *Repository) FindAll(result *[]*pet.Pet) error { - return r.db.Model(&pet.Pet{}).Find(result).Error +func (r *Repository) FindAll(result *[]*pet.Pet, query *proto.FindAllPetRequest) 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 } func (r *Repository) FindOne(id string, result *pet.Pet) error { From c9a5490fd52d4ce0a5e107ebb101b538e709781e Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:50:43 +0700 Subject: [PATCH 07/23] fix: findAll --- src/app/service/pet/pet.service.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index 8a7ded1..b34dc03 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -22,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, *proto.FindAllPetRequest) error + FindOne(string, *pet.Pet) error + Create(*pet.Pet) error + Update(string, *pet.Pet) error + Delete(string) error } type ImageService interface { @@ -90,7 +90,7 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res var pets []*pet.Pet var imagesList [][]*image_proto.Image - err = s.repository.FindAll(&pets) + err = s.repository.FindAll(&pets, req) if err != nil { log.Error().Err(err).Str("service", "event").Str("module", "find all").Msg("Error while querying all events") return nil, status.Error(codes.Unavailable, "Internal error") @@ -103,7 +103,6 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res } imagesList = append(imagesList, images) } - fmt.Println(req) petWithImages, err := petUtils.RawToDtoList(&pets, imagesList, req) if err != nil { return nil, status.Error(codes.Internal, fmt.Sprintf("error converting raw to dto list: %v", err)) From 4fb672db9b96ee767928eea5751d3acc24e95ebe Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:51:15 +0700 Subject: [PATCH 08/23] feat: filter and paginate --- src/app/service/pet/pet.service_test.go | 23 +++++++++-- src/app/utils/pet/pet.utils.go | 54 +++++++++++++++++++------ 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index a2167b9..961b651 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -3,6 +3,7 @@ package pet import ( "context" "errors" + "fmt" "math/rand" "testing" "time" @@ -33,6 +34,7 @@ type PetServiceTest struct { ChangeViewPet *pet.Pet Pets []*pet.Pet PetDto *proto.Pet + FindAllPetReqMock *proto.FindAllPetRequest CreatePetReqMock *proto.CreatePetRequest UpdatePetReqMock *proto.UpdatePetRequest ChangeViewPetReqMock *proto.ChangeViewPetRequest @@ -119,6 +121,18 @@ func (t *PetServiceTest) SetupTest() { Images: t.Images, } + t.FindAllPetReqMock = &proto.FindAllPetRequest{ + Search: "", + Type: "", + Gender: "", + Color: "", + Pattern: "", + Age: "", + Origin: "", + PageSize: 0, + Page: 0, + } + t.UpdatePet = &pet.Pet{ Base: model.Base{ ID: t.Pet.Base.ID, @@ -333,7 +347,7 @@ func (t *PetServiceTest) TestFindAllSuccess() { var petsIn []*pet.Pet repo := &mock.RepositoryMock{} - repo.On("FindAll", petsIn).Return(&t.Pets, nil) + repo.On("FindAll", petsIn, t.FindAllPetReqMock).Return(&t.Pets, nil) imgSrv := new(img_mock.ServiceMock) for i, pet := range t.Pets { @@ -342,8 +356,11 @@ func (t *PetServiceTest) TestFindAllSuccess() { srv := NewService(repo, imgSrv) - actual, err := srv.FindAll(context.Background(), &proto.FindAllPetRequest{}) - + actual, err := srv.FindAll(context.Background(), t.FindAllPetReqMock) + fmt.Println("err: ", err) + _ = actual + _ = err + fmt.Println(actual) assert.Nil(t.T(), err) assert.Equal(t.T(), want, actual) } diff --git a/src/app/utils/pet/pet.utils.go b/src/app/utils/pet/pet.utils.go index fce37a6..8e087a2 100644 --- a/src/app/utils/pet/pet.utils.go +++ b/src/app/utils/pet/pet.utils.go @@ -2,29 +2,25 @@ package pet import ( "errors" - "fmt" "strings" "time" "github.com/google/uuid" "github.com/isd-sgcu/johnjud-backend/src/app/model" "github.com/isd-sgcu/johnjud-backend/src/app/model/pet" + "github.com/isd-sgcu/johnjud-backend/src/constant" petConst "github.com/isd-sgcu/johnjud-backend/src/constant/pet" proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" imageProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1" "gorm.io/gorm" ) -func RawToDtoList(in *[]*pet.Pet, images [][]*imageProto.Image, query *proto.FindAllPetRequest) ([]*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, p := range *in { +func FilterPet(in *[]*pet.Pet, query *proto.FindAllPetRequest) error { + var results []*pet.Pet + for _, p := range *in { res, err := filterAge(p, query.Age) if err != nil { - return nil, err + return err } if !res { continue @@ -47,6 +43,41 @@ func RawToDtoList(in *[]*pet.Pet, images [][]*imageProto.Image, query *proto.Fin if query.Origin != "" && p.Origin != query.Origin { continue } + results = append(results, p) + } + *in = results + return nil +} + +func PaginatePets(pets *[]*pet.Pet, page int32, pageSize int32) error { + if page <= 0 { + page = 1 + } + if pageSize <= 0 { + pageSize = 10 + } + start := (page - 1) * pageSize + end := start + pageSize + + if start > int32(len(*pets)) { + *pets = []*pet.Pet{} + return nil + } + if end > int32(len(*pets)) { + end = int32(len(*pets)) + } + *pets = (*pets)[start:end] + return nil +} + +func RawToDtoList(in *[]*pet.Pet, images [][]*imageProto.Image, query *proto.FindAllPetRequest) ([]*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, p := range *in { + // TODO: create new filter image function this wont work result = append(result, RawToDto(p, images[i])) } return result, nil @@ -155,9 +186,8 @@ func filterAge(pet *pet.Pet, age string) (bool, error) { currYear := time.Now() birthYear := birthdate - diff := currYear.Sub(birthYear).Hours() / petConst.HOUR / petConst.YEAR + diff := currYear.Sub(birthYear).Hours() / constant.DAY / constant.YEAR - fmt.Println(age, diff, currYear.Year()-birthYear.Year()) switch age { case "kitten": return diff < 1, nil @@ -166,6 +196,6 @@ func filterAge(pet *pet.Pet, age string) (bool, error) { case "senior": return diff >= 7, nil default: - return false, errors.New("filter age error. your age must be 'kitten' | 'adult' | 'senior' ") + return true, nil } } From a703eb95d3f501302f138bcbab6f73061ad151c5 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:51:19 +0700 Subject: [PATCH 09/23] chore --- src/constant/pet/pet.constant.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/constant/pet/pet.constant.go b/src/constant/pet/pet.constant.go index 2ad000d..77e50c7 100644 --- a/src/constant/pet/pet.constant.go +++ b/src/constant/pet/pet.constant.go @@ -13,6 +13,3 @@ const ( ADOPTED Status = "adopted" FINDHOME Status = "findhome" ) - -const HOUR = 24 -const YEAR = 365 From 5a3ab2d4c7b05b5570b5e169be2c6e7c9bbeea25 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:51:21 +0700 Subject: [PATCH 10/23] chore --- src/constant/common.constant.go | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/constant/common.constant.go diff --git a/src/constant/common.constant.go b/src/constant/common.constant.go new file mode 100644 index 0000000..1ac27a3 --- /dev/null +++ b/src/constant/common.constant.go @@ -0,0 +1,4 @@ +package constant + +const DAY = 24 +const YEAR = 365 From 5a038377ab13871db883fd359dc92ad8a5f945fe Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:51:25 +0700 Subject: [PATCH 11/23] chore --- src/mocks/pet/pet.mock.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/mocks/pet/pet.mock.go b/src/mocks/pet/pet.mock.go index d1f5d87..f7ebe9c 100644 --- a/src/mocks/pet/pet.mock.go +++ b/src/mocks/pet/pet.mock.go @@ -2,6 +2,7 @@ package pet import ( "github.com/isd-sgcu/johnjud-backend/src/app/model/pet" + proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" "github.com/stretchr/testify/mock" ) @@ -29,8 +30,8 @@ func (r *RepositoryMock) Create(in *pet.Pet) error { return args.Error(1) } -func (r *RepositoryMock) FindAll(result *[]*pet.Pet) error { - args := r.Called(*result) +func (r *RepositoryMock) FindAll(result *[]*pet.Pet, query *proto.FindAllPetRequest) error { + args := r.Called(*result, query) if args.Get(0) != nil { *result = *args.Get(0).(*[]*pet.Pet) From e80b7e32203b14305bcc496d7369267c58caf80c Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:56:59 +0700 Subject: [PATCH 12/23] chore --- src/app/service/pet/pet.service_test.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index 961b651..f4f6098 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -3,7 +3,6 @@ package pet import ( "context" "errors" - "fmt" "math/rand" "testing" "time" @@ -357,10 +356,6 @@ func (t *PetServiceTest) TestFindAllSuccess() { srv := NewService(repo, imgSrv) actual, err := srv.FindAll(context.Background(), t.FindAllPetReqMock) - fmt.Println("err: ", err) - _ = actual - _ = err - fmt.Println(actual) assert.Nil(t.T(), err) assert.Equal(t.T(), want, actual) } From 79e5ad3fe73bd493fce0ebcbffb94b561731ac23 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 11:06:35 +0700 Subject: [PATCH 13/23] fix: remove logic in repository to service --- src/app/repository/pet/pet.repository.go | 35 ++++++++++++------------ src/app/service/pet/pet.service.go | 7 +++-- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/app/repository/pet/pet.repository.go b/src/app/repository/pet/pet.repository.go index 062e2da..c0e433d 100644 --- a/src/app/repository/pet/pet.repository.go +++ b/src/app/repository/pet/pet.repository.go @@ -2,8 +2,6 @@ package pet import ( "github.com/isd-sgcu/johnjud-backend/src/app/model/pet" - petUtils "github.com/isd-sgcu/johnjud-backend/src/app/utils/pet" - proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" "gorm.io/gorm" ) @@ -15,21 +13,24 @@ func NewRepository(db *gorm.DB) *Repository { return &Repository{db: db} } -func (r *Repository) FindAll(result *[]*pet.Pet, query *proto.FindAllPetRequest) 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 +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 + */ } func (r *Repository) FindOne(id string, result *pet.Pet) error { diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index b34dc03..004d524 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -22,7 +22,7 @@ type Service struct { } type IRepository interface { - FindAll(*[]*pet.Pet, *proto.FindAllPetRequest) error + FindAll(*[]*pet.Pet) error FindOne(string, *pet.Pet) error Create(*pet.Pet) error Update(string, *pet.Pet) error @@ -90,12 +90,15 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res var pets []*pet.Pet var imagesList [][]*image_proto.Image - err = s.repository.FindAll(&pets, req) + err = s.repository.FindAll(&pets) if err != nil { log.Error().Err(err).Str("service", "event").Str("module", "find all").Msg("Error while querying all events") 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 { From 0ecdc89716f1801197a374939a13132c64e53a16 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 11:06:37 +0700 Subject: [PATCH 14/23] chore --- src/mocks/pet/pet.mock.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/mocks/pet/pet.mock.go b/src/mocks/pet/pet.mock.go index f7ebe9c..d1f5d87 100644 --- a/src/mocks/pet/pet.mock.go +++ b/src/mocks/pet/pet.mock.go @@ -2,7 +2,6 @@ package pet import ( "github.com/isd-sgcu/johnjud-backend/src/app/model/pet" - proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" "github.com/stretchr/testify/mock" ) @@ -30,8 +29,8 @@ func (r *RepositoryMock) Create(in *pet.Pet) error { return args.Error(1) } -func (r *RepositoryMock) FindAll(result *[]*pet.Pet, query *proto.FindAllPetRequest) error { - args := r.Called(*result, query) +func (r *RepositoryMock) FindAll(result *[]*pet.Pet) error { + args := r.Called(*result) if args.Get(0) != nil { *result = *args.Get(0).(*[]*pet.Pet) From d66c9a87e512673689c9e2426b75db4623f208c8 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 11:19:48 +0700 Subject: [PATCH 15/23] chore --- src/app/service/pet/pet.service_test.go | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index f4f6098..53fc957 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -33,7 +33,6 @@ type PetServiceTest struct { ChangeViewPet *pet.Pet Pets []*pet.Pet PetDto *proto.Pet - FindAllPetReqMock *proto.FindAllPetRequest CreatePetReqMock *proto.CreatePetRequest UpdatePetReqMock *proto.UpdatePetRequest ChangeViewPetReqMock *proto.ChangeViewPetRequest @@ -120,18 +119,6 @@ func (t *PetServiceTest) SetupTest() { Images: t.Images, } - t.FindAllPetReqMock = &proto.FindAllPetRequest{ - Search: "", - Type: "", - Gender: "", - Color: "", - Pattern: "", - Age: "", - Origin: "", - PageSize: 0, - Page: 0, - } - t.UpdatePet = &pet.Pet{ Base: model.Base{ ID: t.Pet.Base.ID, @@ -346,7 +333,7 @@ func (t *PetServiceTest) TestFindAllSuccess() { var petsIn []*pet.Pet repo := &mock.RepositoryMock{} - repo.On("FindAll", petsIn, t.FindAllPetReqMock).Return(&t.Pets, nil) + repo.On("FindAll", petsIn).Return(&t.Pets, nil) imgSrv := new(img_mock.ServiceMock) for i, pet := range t.Pets { @@ -355,7 +342,7 @@ func (t *PetServiceTest) TestFindAllSuccess() { srv := NewService(repo, imgSrv) - actual, err := srv.FindAll(context.Background(), t.FindAllPetReqMock) + actual, err := srv.FindAll(context.Background(), &proto.FindAllPetRequest{}) assert.Nil(t.T(), err) assert.Equal(t.T(), want, actual) } From ae4a2882d91ebf6cdb66faa04110167056188ca1 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 14:48:25 +0700 Subject: [PATCH 16/23] chore --- src/app/repository/pet/pet.repository.go | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/app/repository/pet/pet.repository.go b/src/app/repository/pet/pet.repository.go index c0e433d..0f72ae1 100644 --- a/src/app/repository/pet/pet.repository.go +++ b/src/app/repository/pet/pet.repository.go @@ -15,22 +15,6 @@ 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 - */ } func (r *Repository) FindOne(id string, result *pet.Pet) error { From e2dbcc6eef57faa8e78f086dc5b73d4bae76b11b Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sat, 13 Jan 2024 14:17:15 +0700 Subject: [PATCH 17/23] chore: update proto --- go.mod | 2 +- go.sum | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index acfaa24..6b1437b 100644 --- a/go.mod +++ b/go.mod @@ -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.4.2 + github.com/isd-sgcu/johnjud-go-proto v0.5.0 github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.31.0 github.com/spf13/viper v1.18.2 diff --git a/go.sum b/go.sum index efa2449..cf9a865 100644 --- a/go.sum +++ b/go.sum @@ -22,14 +22,8 @@ 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/isd-sgcu/johnjud-go-proto v0.5.0 h1:GgqRzWjya5p1yhfU/kpX8i4WL42+qT2TkyXZmssH6B4= +github.com/isd-sgcu/johnjud-go-proto v0.5.0/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= From 766969d6c22aee4acfc48943527a0fe4f41111fc Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sat, 13 Jan 2024 14:17:27 +0700 Subject: [PATCH 18/23] fix: pet remove species, isclubpet --- src/app/model/pet/pet.model.go | 2 -- src/app/service/pet/pet.service_test.go | 22 ---------------------- 2 files changed, 24 deletions(-) diff --git a/src/app/model/pet/pet.model.go b/src/app/model/pet/pet.model.go index 3b780cb..ceff18a 100644 --- a/src/app/model/pet/pet.model.go +++ b/src/app/model/pet/pet.model.go @@ -8,7 +8,6 @@ import ( type Pet struct { model.Base Type string `json:"type" gorm:"tinytext"` - Species string `json:"species" gorm:"tinytext"` Name string `json:"name" gorm:"tinytext"` Birthdate string `json:"birthdate" gorm:"tinytext"` Gender pet.Gender `json:"gender" gorm:"tinytext" example:"male"` @@ -20,7 +19,6 @@ type Pet struct { IsSterile bool `json:"is_sterile"` IsVaccinated bool `json:"is_vaccine"` IsVisible bool `json:"is_visible"` - IsClubPet bool `json:"is_club_pet"` Origin string `json:"origin" gorm:"tinytext"` Address string `json:"address" gorm:"tinytext"` Contact string `json:"contact" gorm:"tinytext"` diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index 53fc957..e0c0edd 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -60,7 +60,6 @@ func (t *PetServiceTest) SetupTest() { DeletedAt: gorm.DeletedAt{}, }, Type: faker.Word(), - Species: faker.Word(), Name: faker.Name(), Birthdate: faker.Word(), Gender: genders[rand.Intn(2)], @@ -72,7 +71,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: true, IsVaccinated: true, IsVisible: true, - IsClubPet: true, Origin: faker.Paragraph(), Address: faker.Paragraph(), Contact: faker.Paragraph(), @@ -100,7 +98,6 @@ func (t *PetServiceTest) SetupTest() { t.PetDto = &proto.Pet{ Id: t.Pet.ID.String(), Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: string(t.Pet.Gender), @@ -112,7 +109,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -127,7 +123,6 @@ func (t *PetServiceTest) SetupTest() { DeletedAt: t.Pet.Base.DeletedAt, }, Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, @@ -139,7 +134,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -153,7 +147,6 @@ func (t *PetServiceTest) SetupTest() { DeletedAt: t.Pet.Base.DeletedAt, }, Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, @@ -165,7 +158,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: false, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -174,7 +166,6 @@ func (t *PetServiceTest) SetupTest() { t.CreatePetReqMock = &proto.CreatePetRequest{ Pet: &proto.Pet{ Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: string(t.Pet.Gender), @@ -187,7 +178,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVaccinated, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -198,7 +188,6 @@ func (t *PetServiceTest) SetupTest() { Pet: &proto.Pet{ Id: t.Pet.ID.String(), Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: string(t.Pet.Gender), @@ -211,7 +200,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -231,7 +219,6 @@ func (t *PetServiceTest) SetupTest() { DeletedAt: t.Pet.Base.DeletedAt, }, Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, @@ -243,7 +230,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -377,7 +363,6 @@ func createPets() []*pet.Pet { DeletedAt: gorm.DeletedAt{}, }, Type: faker.Word(), - Species: faker.Word(), Name: faker.Name(), Birthdate: faker.Word(), Gender: genders[rand.Intn(2)], @@ -389,7 +374,6 @@ func createPets() []*pet.Pet { IsSterile: true, IsVaccinated: true, IsVisible: true, - IsClubPet: true, Origin: faker.Paragraph(), Address: faker.Paragraph(), Contact: faker.Paragraph(), @@ -407,7 +391,6 @@ func (t *PetServiceTest) createPetsDto(in []*pet.Pet, imagesList [][]*img_proto. r := &proto.Pet{ Id: p.ID.String(), Type: p.Type, - Species: p.Species, Name: p.Name, Birthdate: p.Birthdate, Gender: string(p.Gender), @@ -420,7 +403,6 @@ func (t *PetServiceTest) createPetsDto(in []*pet.Pet, imagesList [][]*img_proto. IsSterile: p.IsSterile, IsVaccinated: p.IsVaccinated, IsVisible: p.IsVisible, - IsClubPet: p.IsClubPet, Origin: p.Origin, Address: p.Address, Contact: p.Contact, @@ -440,7 +422,6 @@ func (t *PetServiceTest) TestCreateSuccess() { in := &pet.Pet{ Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, @@ -452,7 +433,6 @@ func (t *PetServiceTest) TestCreateSuccess() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -474,7 +454,6 @@ func (t *PetServiceTest) TestCreateInternalErr() { in := &pet.Pet{ Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, @@ -486,7 +465,6 @@ func (t *PetServiceTest) TestCreateInternalErr() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, From f171febff792ac503afd95ce2a67057a7663581f Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 15:03:02 +0700 Subject: [PATCH 19/23] chore --- src/app/utils/pet/pet.utils.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/app/utils/pet/pet.utils.go b/src/app/utils/pet/pet.utils.go index 8e087a2..0349078 100644 --- a/src/app/utils/pet/pet.utils.go +++ b/src/app/utils/pet/pet.utils.go @@ -2,6 +2,7 @@ package pet import ( "errors" + "math" "strings" "time" @@ -25,7 +26,7 @@ func FilterPet(in *[]*pet.Pet, query *proto.FindAllPetRequest) error { if !res { continue } - if query.Search != "" && !strings.Contains(p.Name, query.Search) && !strings.Contains(p.Species, query.Search) { + if query.Search != "" && !strings.Contains(p.Name, query.Search) { continue } if query.Type != "" && p.Type != query.Type { @@ -49,24 +50,30 @@ func FilterPet(in *[]*pet.Pet, query *proto.FindAllPetRequest) error { return nil } -func PaginatePets(pets *[]*pet.Pet, page int32, pageSize int32) error { +func PaginatePets(pets *[]*pet.Pet, page int32, pageSize int32, metadata *proto.FindAllPetMetaData) error { + totalsPets := int32(len(*pets)) if page <= 0 { page = 1 } if pageSize <= 0 { - pageSize = 10 + pageSize = totalsPets } start := (page - 1) * pageSize end := start + pageSize - if start > int32(len(*pets)) { + if start > totalsPets { *pets = []*pet.Pet{} return nil } - if end > int32(len(*pets)) { - end = int32(len(*pets)) + if end > totalsPets { + end = totalsPets } *pets = (*pets)[start:end] + + metadata.Page = page + metadata.PageSize = pageSize + metadata.Total = totalsPets + metadata.TotalPages = int32(math.Ceil(float64(totalsPets / pageSize))) return nil } @@ -87,7 +94,6 @@ func RawToDto(in *pet.Pet, images []*imageProto.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), @@ -100,7 +106,6 @@ func RawToDto(in *pet.Pet, images []*imageProto.Image) *proto.Pet { IsSterile: in.IsSterile, IsVaccinated: in.IsVaccinated, IsVisible: in.IsVisible, - IsClubPet: in.IsClubPet, Origin: in.Origin, Address: in.Address, Contact: in.Contact, @@ -142,7 +147,6 @@ func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) { DeletedAt: gorm.DeletedAt{}, }, Type: in.Type, - Species: in.Species, Name: in.Name, Birthdate: in.Birthdate, Gender: gender, @@ -154,7 +158,6 @@ func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) { IsSterile: in.IsSterile, IsVaccinated: in.IsVaccinated, IsVisible: in.IsVisible, - IsClubPet: in.IsClubPet, Origin: in.Origin, Address: in.Address, Contact: in.Contact, From a6e953bf642147126fc877cba3411626a1e1f9b7 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 15:03:08 +0700 Subject: [PATCH 20/23] feat: meta data --- src/app/service/pet/pet.service.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index 004d524..1d8ec5e 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -89,6 +89,7 @@ func (s *Service) ChangeView(_ context.Context, req *proto.ChangeViewPetRequest) func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res *proto.FindAllPetResponse, err error) { var pets []*pet.Pet var imagesList [][]*image_proto.Image + var metaData *proto.FindAllPetMetaData err = s.repository.FindAll(&pets) if err != nil { @@ -97,7 +98,7 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res } petUtils.FilterPet(&pets, req) - petUtils.PaginatePets(&pets, req.Page, req.PageSize) + petUtils.PaginatePets(&pets, req.Page, req.PageSize, metaData) for _, pet := range pets { images, err := s.imageService.FindByPetId(pet.ID.String()) @@ -110,7 +111,7 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res if err != nil { return nil, status.Error(codes.Internal, fmt.Sprintf("error converting raw to dto list: %v", err)) } - return &proto.FindAllPetResponse{Pets: petWithImages}, nil + return &proto.FindAllPetResponse{Pets: petWithImages, Metadata: metaData}, nil } func (s Service) FindOne(_ context.Context, req *proto.FindOnePetRequest) (res *proto.FindOnePetResponse, err error) { From 4911446710c27b36d538938ca848879b37b79dd5 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 15:04:58 +0700 Subject: [PATCH 21/23] chore --- src/app/service/pet/pet.service.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index 1d8ec5e..92f4fcd 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -89,7 +89,7 @@ func (s *Service) ChangeView(_ context.Context, req *proto.ChangeViewPetRequest) func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res *proto.FindAllPetResponse, err error) { var pets []*pet.Pet var imagesList [][]*image_proto.Image - var metaData *proto.FindAllPetMetaData + metaData := proto.FindAllPetMetaData{} err = s.repository.FindAll(&pets) if err != nil { @@ -98,7 +98,7 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res } petUtils.FilterPet(&pets, req) - petUtils.PaginatePets(&pets, req.Page, req.PageSize, metaData) + petUtils.PaginatePets(&pets, req.Page, req.PageSize, &metaData) for _, pet := range pets { images, err := s.imageService.FindByPetId(pet.ID.String()) @@ -111,7 +111,7 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res if err != nil { return nil, status.Error(codes.Internal, fmt.Sprintf("error converting raw to dto list: %v", err)) } - return &proto.FindAllPetResponse{Pets: petWithImages, Metadata: metaData}, nil + return &proto.FindAllPetResponse{Pets: petWithImages, Metadata: &metaData}, nil } func (s Service) FindOne(_ context.Context, req *proto.FindOnePetRequest) (res *proto.FindOnePetResponse, err error) { From 79fec9f3e5fdf4a3b71d6f989bce4e470dee8b66 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 15:12:30 +0700 Subject: [PATCH 22/23] fix: test --- src/app/service/pet/pet.service_test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index e0c0edd..fa75c0c 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -314,7 +314,15 @@ func (t *PetServiceTest) TestFindOneSuccess() { func (t *PetServiceTest) TestFindAllSuccess() { - want := &proto.FindAllPetResponse{Pets: t.createPetsDto(t.Pets, t.ImagesList)} + want := &proto.FindAllPetResponse{ + Pets: t.createPetsDto(t.Pets, t.ImagesList), + Metadata: &proto.FindAllPetMetaData{ + Page: 1, + TotalPages: 1, + PageSize: int32(len(t.Pets)), + Total: int32(len(t.Pets)), + }, + } var petsIn []*pet.Pet From ff668201536760529524005bf6a61b0f350ff59a Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 15:47:19 +0700 Subject: [PATCH 23/23] fix: change type to float64 before divide --- src/app/utils/pet/pet.utils.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/app/utils/pet/pet.utils.go b/src/app/utils/pet/pet.utils.go index 0349078..8708acb 100644 --- a/src/app/utils/pet/pet.utils.go +++ b/src/app/utils/pet/pet.utils.go @@ -70,10 +70,12 @@ func PaginatePets(pets *[]*pet.Pet, page int32, pageSize int32, metadata *proto. } *pets = (*pets)[start:end] + totalPages := int32(math.Ceil(float64(totalsPets) / float64(pageSize))) + metadata.Page = page metadata.PageSize = pageSize metadata.Total = totalsPets - metadata.TotalPages = int32(math.Ceil(float64(totalsPets / pageSize))) + metadata.TotalPages = totalPages return nil }