Skip to content

Commit

Permalink
feat: new pet type
Browse files Browse the repository at this point in the history
  • Loading branch information
bookpanda committed Jan 9, 2024
1 parent f9a2f07 commit a337002
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 35 deletions.
14 changes: 9 additions & 5 deletions src/app/dto/pet.dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ type PetResponse struct {
Name string `json:"name"`
Birthdate string `json:"birthdate"`
Gender pet.Gender `json:"gender"`
Color string `json:"color"`
Pattern string `json:"pattern"`
Habit string `json:"habit"`
Caption string `json:"caption"`
Status pet.Status `json:"status"`
IsSterile *bool `json:"is_sterile"`
IsVaccinated *bool `json:"is_vaccinated"`
IsVisible *bool `json:"is_visible"`
IsClubPet *bool `json:"is_club_pet"`
Background string `json:"background"`
Origin string `json:"origin"`
Address string `json:"address"`
Contact string `json:"contact"`
AdoptBy string `json:"adopt_by"`
Expand All @@ -35,17 +37,17 @@ type CreatePetRequest struct {
Species string `json:"species" validate:"required"`
Name string `json:"name" validate:"required"`
Birthdate string `json:"birthdate" validate:"required"`
Gender pet.Gender `json:"gender" validate:"required" example:"1"`
Gender pet.Gender `json:"gender" validate:"required" example:"male"`
Color string `json:"color" validate:"required"`
Pattern string `json:"pattern" validate:"required"`
Habit string `json:"habit" validate:"required"`
Caption string `json:"caption"`
Status pet.Status `json:"status" validate:"required" example:"1"`
Status pet.Status `json:"status" validate:"required" example:"findhome"`
IsSterile *bool `json:"is_sterile" validate:"required"`
IsVaccinated *bool `json:"is_vaccinated" validate:"required"`
IsVisible *bool `json:"is_visible" validate:"required"`
IsClubPet *bool `json:"is_club_pet" validate:"required"`
Origin string `json:"origin"`
Origin string `json:"origin" validate:"required"`
Address string `json:"address"`
Contact string `json:"contact"`
AdoptBy string `json:"adopt_by"`
Expand Down Expand Up @@ -75,14 +77,16 @@ type UpdatePetRequest struct {
Name string `json:"name"`
Birthdate string `json:"birthdate"`
Gender pet.Gender `json:"gender"`
Color string `json:"color"`
Pattern string `json:"pattern"`
Habit string `json:"habit"`
Caption string `json:"caption"`
Status pet.Status `json:"status"`
IsSterile *bool `json:"is_sterile"`
IsVaccinated *bool `json:"is_vaccinated"`
IsVisible *bool `json:"is_visible"`
IsClubPet *bool `json:"is_club_pet"`
Background string `json:"background"`
Origin string `json:"origin"`
Address string `json:"address"`
Contact string `json:"contact"`
AdoptBy string `json:"adopt_by"`
Expand Down
46 changes: 27 additions & 19 deletions src/app/utils/pet/pet.utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ func ProtoToDto(in *petproto.Pet, images []*imgproto.Image) *dto.PetResponse {
Name: in.Name,
Birthdate: in.Birthdate,
Gender: pet.Gender(in.Gender),
Color: in.Color,
Pattern: in.Pattern,
Habit: in.Habit,
Caption: in.Caption,
Status: pet.Status(in.Status),
IsSterile: &in.IsSterile,
IsVaccinated: &in.IsVaccinated,
IsVisible: &in.IsVisible,
IsClubPet: &in.IsClubPet,
Background: in.Background,
Origin: in.Origin,
Address: in.Address,
Contact: in.Contact,
AdoptBy: in.AdoptBy,
Expand All @@ -58,16 +60,18 @@ func CreateDtoToProto(in *dto.CreatePetRequest) *petproto.CreatePetRequest {
Species: in.Species,
Name: in.Name,
Birthdate: in.Birthdate,
Gender: petproto.Gender(in.Gender),
Gender: string(in.Gender),
Color: in.Color,
Pattern: in.Pattern,
Habit: in.Habit,
Caption: in.Caption,
Images: []*imgproto.Image{},
Status: petproto.PetStatus(in.Status),
Status: string(in.Status),
IsSterile: *in.IsSterile,
IsVaccinated: *in.IsVaccinated,
IsVisible: *in.IsVisible,
IsClubPet: *in.IsClubPet,
Background: in.Background,
Origin: in.Origin,
Address: in.Address,
Contact: in.Contact,
AdoptBy: in.AdoptBy,
Expand All @@ -78,20 +82,22 @@ func CreateDtoToProto(in *dto.CreatePetRequest) *petproto.CreatePetRequest {
func UpdateDtoToProto(id string, in *dto.UpdatePetRequest) *petproto.UpdatePetRequest {
req := &petproto.UpdatePetRequest{
Pet: &petproto.Pet{
Id: id,
Type: in.Type,
Species: in.Species,
Name: in.Name,
Birthdate: in.Birthdate,
Gender: petproto.Gender(in.Gender),
Habit: in.Habit,
Caption: in.Caption,
Images: []*imgproto.Image{},
Status: petproto.PetStatus(in.Status),
Background: in.Background,
Address: in.Address,
Contact: in.Contact,
AdoptBy: in.AdoptBy,
Id: id,
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,
Images: []*imgproto.Image{},
Status: string(in.Status),
Origin: in.Origin,
Address: in.Address,
Contact: in.Contact,
AdoptBy: in.AdoptBy,
},
}

Expand Down Expand Up @@ -132,14 +138,16 @@ func ProtoToDtoList(in []*petproto.Pet, imagesList [][]*imgproto.Image) []*dto.P
Name: p.Name,
Birthdate: p.Birthdate,
Gender: pet.Gender(p.Gender),
Color: p.Color,
Pattern: p.Pattern,
Habit: p.Habit,
Caption: p.Caption,
Status: pet.Status(p.Status),
IsSterile: &p.IsSterile,
IsVaccinated: &p.IsVaccinated,
IsVisible: &p.IsVisible,
IsClubPet: &p.IsClubPet,
Background: p.Background,
Origin: p.Origin,
Address: p.Address,
Contact: p.Contact,
AdoptBy: p.AdoptBy,
Expand Down
22 changes: 11 additions & 11 deletions src/constant/pet/pet.constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import (
"strings"
)

type Gender int
type Gender string

const (
MALE = 1
FEMALE = 2
MALE Gender = "male"
FEMALE Gender = "female"
)

type Status int
type Status string

const (
ADOPTED = 1
FINDHOME = 2
ADOPTED Status = "adopted"
FINDHOME Status = "findhome"
)

func (g *Gender) UnmarshalJSON(data []byte) error {
Expand All @@ -28,10 +28,10 @@ func (g *Gender) UnmarshalJSON(data []byte) error {
s = strings.ToUpper(s)
switch s {
case "MALE":
*g = Gender(1)
*g = MALE
return nil
case "FEMALE":
*g = Gender(2)
*g = FEMALE
return nil

default:
Expand All @@ -47,14 +47,14 @@ func (st *Status) UnmarshalJSON(data []byte) error {
s = strings.ToUpper(s)
switch s {
case "ADOPTED":
*st = Status(1)
*st = ADOPTED
return nil
case "FINDHOME":
*st = Status(2)
*st = FINDHOME
return nil

default:
return errors.New("invalid gender")
return errors.New("invalid status")
}
}

Expand Down

0 comments on commit a337002

Please sign in to comment.