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

[JOH-48] Update pet type #17

Merged
merged 7 commits into from
Jan 10, 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
13 changes: 13 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Change made

- [ ]  New features
- [ ]  Bug fixes
- [ ]  Breaking changes
## Describe what you have done
-
### New Features
-
### Fix
-
### Others
-
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.0.9
github.com/isd-sgcu/johnjud-go-proto v0.3.0
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.31.0
github.com/spf13/viper v1.18.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
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.0.9 h1:cFfZ2JSpW0jg94Iv5zHQJGnoekj0eCQe42SJaTpnp3c=
github.com/isd-sgcu/johnjud-go-proto v0.0.9/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4=
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/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
4 changes: 3 additions & 1 deletion src/app/model/pet/pet.model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ type Pet struct {
Name string `json:"name" gorm:"tinytext"`
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"`
IsSterile bool `json:"is_sterile"`
IsVaccinated bool `json:"is_vaccine"`
IsVisible bool `json:"is_visible"`
IsClubPet bool `json:"is_club_pet"`
Background string `json:"background" gorm:"tinytext"`
Origin string `json:"origin" gorm:"tinytext"`
Address string `json:"address" gorm:"tinytext"`
Contact string `json:"contact" gorm:"tinytext"`
AdoptBy string `json:"adopt_by" gorm:"tinytext"`
Expand Down
2 changes: 1 addition & 1 deletion src/app/repository/pet/pet.repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (r *Repository) FindAll(result *[]*pet.Pet) error {
}

func (r *Repository) FindOne(id string, result *pet.Pet) error {
return r.db.Model(&pet.Pet{}).Find(result, "id = ?", id).Error
return r.db.Model(&pet.Pet{}).First(result, "id = ?", id).Error
}

func (r *Repository) Create(in *pet.Pet) error {
Expand Down
49 changes: 25 additions & 24 deletions src/app/service/pet/pet.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ func (s *Service) Update(_ context.Context, req *proto.UpdatePetRequest) (res *p
if err != nil {
return nil, status.Error(codes.Internal, "error querying image service")
}
imageUrls := ExtractImageUrls(images)

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

func (s *Service) ChangeView(_ context.Context, req *proto.ChangeViewPetRequest) (res *proto.ChangeViewPetResponse, err error) {
Expand All @@ -92,7 +91,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 imageUrlsList [][]string
var imagesList [][]*image_proto.Image

err = s.repository.FindAll(&pets)
if err != nil {
Expand All @@ -105,16 +104,15 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res
if err != nil {
return nil, status.Error(codes.Internal, "error querying image service")
}
imageUrls := ExtractImageUrls(images)
imageUrlsList = append(imageUrlsList, imageUrls)
imagesList = append(imagesList, images)
}

petWithImageUrls, err := RawToDtoList(&pets, imageUrlsList)
petWithImages, err := RawToDtoList(&pets, imagesList)
if err != nil {
return nil, status.Error(codes.Internal, "error converting raw to dto list")
}

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

func (s Service) FindOne(_ context.Context, req *proto.FindOnePetRequest) (res *proto.FindOnePetResponse, err error) {
Expand All @@ -131,9 +129,8 @@ func (s Service) FindOne(_ context.Context, req *proto.FindOnePetRequest) (res *
if err != nil {
return nil, status.Error(codes.Internal, "error querying image service")
}
imageUrls := ExtractImageUrls(images)

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

func (s *Service) Create(_ context.Context, req *proto.CreatePetRequest) (res *proto.CreatePetResponse, err error) {
Expand All @@ -142,14 +139,14 @@ func (s *Service) Create(_ context.Context, req *proto.CreatePetRequest) (res *p
return nil, status.Error(codes.Internal, "error converting dto to raw: "+err.Error())
}

imgUrls := []string{}
images := []*image_proto.Image{}

err = s.repository.Create(raw)
if err != nil {
return nil, status.Error(codes.Internal, "failed to create pet")
}

return &proto.CreatePetResponse{Pet: RawToDto(raw, imgUrls)}, nil
return &proto.CreatePetResponse{Pet: RawToDto(raw, images)}, nil
}

func (s *Service) AdoptPet(ctx context.Context, req *proto.AdoptPetRequest) (res *proto.AdoptPetResponse, err error) {
Expand All @@ -171,35 +168,37 @@ func (s *Service) AdoptPet(ctx context.Context, req *proto.AdoptPetRequest) (res
return &proto.AdoptPetResponse{Success: true}, nil
}

func RawToDtoList(in *[]*pet.Pet, imageUrls [][]string) ([]*proto.Pet, error) {
func RawToDtoList(in *[]*pet.Pet, images [][]*image_proto.Image) ([]*proto.Pet, error) {
var result []*proto.Pet
if len(*in) != len(imageUrls) {
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, imageUrls[i]))
result = append(result, RawToDto(e, images[i]))
}
return result, nil
}

func RawToDto(in *pet.Pet, imgUrl []string) *proto.Pet {
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: proto.Gender(in.Gender),
Gender: string(in.Gender),
Color: in.Color,
Pattern: in.Pattern,
Habit: in.Habit,
Caption: in.Caption,
Status: proto.PetStatus(in.Status),
ImageUrls: imgUrl,
Status: string(in.Status),
Images: images,
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 @@ -219,16 +218,16 @@ func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) {
}

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

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

Expand All @@ -244,14 +243,16 @@ func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) {
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,
Background: in.Background,
Origin: in.Origin,
Address: in.Address,
Contact: in.Contact,
AdoptBy: in.AdoptBy,
Expand Down
Loading
Loading