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 70 johnjud backend fix pet service #23

Merged
merged 4 commits into from
Jan 19, 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
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.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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 0 additions & 1 deletion src/app/model/pet/pet.model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
92 changes: 44 additions & 48 deletions src/app/service/pet/pet.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
Expand Down Expand Up @@ -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),
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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,
Expand Down Expand Up @@ -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)],
Expand All @@ -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),
Expand All @@ -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,
Expand All @@ -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,
Expand Down
23 changes: 6 additions & 17 deletions src/app/utils/pet/pet.utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Loading