From cbdd427c79840774edfa971b49b8aa6a3332b6ba Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Fri, 19 Jan 2024 17:58:51 +0700 Subject: [PATCH 1/4] fix(model): remove pattern --- src/app/model/pet/pet.model.go | 1 - 1 file changed, 1 deletion(-) diff --git a/src/app/model/pet/pet.model.go b/src/app/model/pet/pet.model.go index ceff18a..825a79f 100644 --- a/src/app/model/pet/pet.model.go +++ b/src/app/model/pet/pet.model.go @@ -12,7 +12,6 @@ type Pet struct { Birthdate string `json:"birthdate" gorm:"tinytext"` Gender pet.Gender `json:"gender" gorm:"tinytext" example:"male"` Color string `json:"color" gorm:"tinytext"` - Pattern string `json:"pattern" gorm:"tinytext"` Habit string `json:"habit" gorm:"mediumtext"` Caption string `json:"caption" gorm:"mediumtext"` Status pet.Status `json:"status" gorm:"mediumtext" example:"findhome"` From d41453900424e92ede0dc026502d7201def2cdc1 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Fri, 19 Jan 2024 17:58:57 +0700 Subject: [PATCH 2/4] fix(test) --- src/app/service/pet/pet.service_test.go | 92 ++++++++++++------------- 1 file changed, 44 insertions(+), 48 deletions(-) diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index fa75c0c..f569a8d 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -64,7 +64,6 @@ func (t *PetServiceTest) SetupTest() { Birthdate: faker.Word(), Gender: genders[rand.Intn(2)], Color: faker.Word(), - Pattern: faker.Word(), Habit: faker.Paragraph(), Caption: faker.Paragraph(), Status: statuses[rand.Intn(2)], @@ -102,7 +101,6 @@ func (t *PetServiceTest) SetupTest() { Birthdate: t.Pet.Birthdate, Gender: string(t.Pet.Gender), Color: t.Pet.Color, - Pattern: t.Pet.Pattern, Habit: t.Pet.Habit, Caption: t.Pet.Caption, Status: string(t.Pet.Status), @@ -127,7 +125,6 @@ func (t *PetServiceTest) SetupTest() { Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, Color: t.Pet.Color, - Pattern: t.Pet.Pattern, Habit: t.Pet.Habit, Caption: t.Pet.Caption, Status: t.Pet.Status, @@ -146,12 +143,12 @@ func (t *PetServiceTest) SetupTest() { UpdatedAt: t.Pet.Base.UpdatedAt, DeletedAt: t.Pet.Base.DeletedAt, }, - Type: t.Pet.Type, - Name: t.Pet.Name, - Birthdate: t.Pet.Birthdate, - Gender: t.Pet.Gender, - Color: t.Pet.Color, - Pattern: t.Pet.Pattern, + Type: t.Pet.Type, + Name: t.Pet.Name, + Birthdate: t.Pet.Birthdate, + Gender: t.Pet.Gender, + Color: t.Pet.Color, + Habit: t.Pet.Habit, Caption: t.Pet.Caption, Status: t.Pet.Status, @@ -165,12 +162,12 @@ func (t *PetServiceTest) SetupTest() { t.CreatePetReqMock = &proto.CreatePetRequest{ Pet: &proto.Pet{ - Type: t.Pet.Type, - Name: t.Pet.Name, - Birthdate: t.Pet.Birthdate, - Gender: string(t.Pet.Gender), - Color: t.Pet.Color, - Pattern: t.Pet.Pattern, + Type: t.Pet.Type, + Name: t.Pet.Name, + Birthdate: t.Pet.Birthdate, + Gender: string(t.Pet.Gender), + Color: t.Pet.Color, + Habit: t.Pet.Habit, Caption: t.Pet.Caption, Status: string(t.Pet.Status), @@ -186,13 +183,13 @@ func (t *PetServiceTest) SetupTest() { t.UpdatePetReqMock = &proto.UpdatePetRequest{ Pet: &proto.Pet{ - Id: t.Pet.ID.String(), - Type: t.Pet.Type, - Name: t.Pet.Name, - Birthdate: t.Pet.Birthdate, - Gender: string(t.Pet.Gender), - Color: t.Pet.Color, - Pattern: t.Pet.Pattern, + Id: t.Pet.ID.String(), + Type: t.Pet.Type, + Name: t.Pet.Name, + Birthdate: t.Pet.Birthdate, + Gender: string(t.Pet.Gender), + Color: t.Pet.Color, + Habit: t.Pet.Habit, Caption: t.Pet.Caption, Status: string(t.Pet.Status), @@ -218,12 +215,12 @@ func (t *PetServiceTest) SetupTest() { UpdatedAt: t.Pet.Base.UpdatedAt, DeletedAt: t.Pet.Base.DeletedAt, }, - Type: t.Pet.Type, - Name: t.Pet.Name, - Birthdate: t.Pet.Birthdate, - Gender: t.Pet.Gender, - Color: t.Pet.Color, - Pattern: t.Pet.Pattern, + Type: t.Pet.Type, + Name: t.Pet.Name, + Birthdate: t.Pet.Birthdate, + Gender: t.Pet.Gender, + Color: t.Pet.Color, + Habit: t.Pet.Habit, Caption: t.Pet.Caption, Status: t.Pet.Status, @@ -375,7 +372,6 @@ func createPets() []*pet.Pet { Birthdate: faker.Word(), Gender: genders[rand.Intn(2)], Color: faker.Word(), - Pattern: faker.Word(), Habit: faker.Paragraph(), Caption: faker.Paragraph(), Status: statuses[rand.Intn(2)], @@ -397,13 +393,13 @@ func (t *PetServiceTest) createPetsDto(in []*pet.Pet, imagesList [][]*img_proto. for i, p := range in { r := &proto.Pet{ - Id: p.ID.String(), - Type: p.Type, - Name: p.Name, - Birthdate: p.Birthdate, - Gender: string(p.Gender), - Color: p.Color, - Pattern: p.Pattern, + Id: p.ID.String(), + Type: p.Type, + Name: p.Name, + Birthdate: p.Birthdate, + Gender: string(p.Gender), + Color: p.Color, + Habit: p.Habit, Caption: p.Caption, Status: string(p.Status), @@ -429,12 +425,12 @@ func (t *PetServiceTest) TestCreateSuccess() { repo := &mock.RepositoryMock{} in := &pet.Pet{ - Type: t.Pet.Type, - Name: t.Pet.Name, - Birthdate: t.Pet.Birthdate, - Gender: t.Pet.Gender, - Color: t.Pet.Color, - Pattern: t.Pet.Pattern, + Type: t.Pet.Type, + Name: t.Pet.Name, + Birthdate: t.Pet.Birthdate, + Gender: t.Pet.Gender, + Color: t.Pet.Color, + Habit: t.Pet.Habit, Caption: t.Pet.Caption, Status: t.Pet.Status, @@ -461,12 +457,12 @@ func (t *PetServiceTest) TestCreateInternalErr() { repo := &mock.RepositoryMock{} in := &pet.Pet{ - Type: t.Pet.Type, - Name: t.Pet.Name, - Birthdate: t.Pet.Birthdate, - Gender: t.Pet.Gender, - Color: t.Pet.Color, - Pattern: t.Pet.Pattern, + Type: t.Pet.Type, + Name: t.Pet.Name, + Birthdate: t.Pet.Birthdate, + Gender: t.Pet.Gender, + Color: t.Pet.Color, + Habit: t.Pet.Habit, Caption: t.Pet.Caption, Status: t.Pet.Status, From 9eff15edfeaefccdcba840ac9ca8f812db3786e4 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Fri, 19 Jan 2024 17:59:24 +0700 Subject: [PATCH 3/4] fix(utils): change filter `age` to `maxAge` and `minAge` --- src/app/utils/pet/pet.utils.go | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/src/app/utils/pet/pet.utils.go b/src/app/utils/pet/pet.utils.go index 8708acb..630607b 100644 --- a/src/app/utils/pet/pet.utils.go +++ b/src/app/utils/pet/pet.utils.go @@ -17,9 +17,12 @@ import ( ) func FilterPet(in *[]*pet.Pet, query *proto.FindAllPetRequest) error { + if query.MaxAge == 0 { + query.MaxAge = math.MaxInt32 + } var results []*pet.Pet for _, p := range *in { - res, err := filterAge(p, query.Age) + res, err := filterAge(p, query.MinAge, query.MaxAge) if err != nil { return err } @@ -38,9 +41,6 @@ func FilterPet(in *[]*pet.Pet, query *proto.FindAllPetRequest) error { if query.Color != "" && p.Color != query.Color { continue } - if query.Pattern != "" && p.Pattern != query.Pattern { - continue - } if query.Origin != "" && p.Origin != query.Origin { continue } @@ -100,7 +100,6 @@ func RawToDto(in *pet.Pet, images []*imageProto.Image) *proto.Pet { Birthdate: in.Birthdate, Gender: string(in.Gender), Color: in.Color, - Pattern: in.Pattern, Habit: in.Habit, Caption: in.Caption, Status: string(in.Status), @@ -153,7 +152,6 @@ func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) { Birthdate: in.Birthdate, Gender: gender, Color: in.Color, - Pattern: in.Pattern, Habit: in.Habit, Caption: in.Caption, Status: status, @@ -183,7 +181,7 @@ func parseDate(dateStr string) (time.Time, error) { return parsedTime, nil } -func filterAge(pet *pet.Pet, age string) (bool, error) { +func filterAge(pet *pet.Pet, minAge, maxAge int32) (bool, error) { birthdate, err := parseDate(pet.Birthdate) if err != nil { return false, err @@ -193,14 +191,5 @@ func filterAge(pet *pet.Pet, age string) (bool, error) { birthYear := birthdate diff := currYear.Sub(birthYear).Hours() / constant.DAY / constant.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 true, nil - } + return diff >= float64(minAge) && diff <= float64(maxAge), nil } From d4c1d51366efc4806304df0a03b2fceb14ecb9a5 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Fri, 19 Jan 2024 17:59:31 +0700 Subject: [PATCH 4/4] chore: update proto --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index ba7e136..6596041 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.5.0 + github.com/isd-sgcu/johnjud-go-proto v0.5.2 github.com/rs/zerolog v1.31.0 github.com/spf13/viper v1.18.1 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index 458a30d..34dd02e 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +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.5.0 h1:GgqRzWjya5p1yhfU/kpX8i4WL42+qT2TkyXZmssH6B4= github.com/isd-sgcu/johnjud-go-proto v0.5.0/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= +github.com/isd-sgcu/johnjud-go-proto v0.5.2 h1:LWhi7zaEeEOJ60nyCxxMeAr7Bvl9stWvQbaw0RWz/Cs= +github.com/isd-sgcu/johnjud-go-proto v0.5.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=