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 28 pet and like endpoints #14

Open
wants to merge 18 commits into
base: dev
Choose a base branch
from
Open
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.0.9
github.com/isd-sgcu/johnjud-go-proto v0.2.4
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.31.0
github.com/spf13/viper v1.18.2
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.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.2.4 h1:amYofKCZGMKc+VQARmsZSPgmpxEJwQjv6VfbCxI9wLw=
github.com/isd-sgcu/johnjud-go-proto v0.2.4/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
8 changes: 4 additions & 4 deletions src/app/model/pet/pet.model.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type Pet struct {
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"`
IsSterile *bool `json:"is_sterile"`
IsVaccinated *bool `json:"is_vaccine"`
IsVisible *bool `json:"is_visible"`
IsClubPet *bool `json:"is_club_pet"`
Comment on lines +18 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should discuss about the best method we can avoid "update non-zero fields."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from gorm documentation I see 3 solutions they suggested.

  1. update only one column
  2. use map[string]interface{} instead of struct
  3. do select before update

ref

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my experience, I preferred the third option because it's very easy to implement it along fieldmask

// Update Pet

message UpdatePetRequest{
  Pet Pet = 1;
  google.protobuf.FieldMask field_mask = 2;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel free to state your opinion here, you won't be judged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for sharing your insights and great options, i'll take a look

Background string `json:"background" gorm:"tinytext"`
Address string `json:"address" gorm:"tinytext"`
Contact string `json:"contact" gorm:"tinytext"`
Expand Down
11 changes: 10 additions & 1 deletion src/app/repository/pet/pet.repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pet

import (
"github.com/google/uuid"
"github.com/isd-sgcu/johnjud-backend/src/app/model/pet"
"gorm.io/gorm"
)
Expand All @@ -18,7 +19,15 @@ 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
if err := r.db.Model(&pet.Pet{}).Find(result, "id = ?", id).Error; err != nil {
return err
}

if result.ID == uuid.Nil {
return gorm.ErrRecordNotFound
}

return nil
}

func (r *Repository) Create(in *pet.Pet) error {
Expand Down
164 changes: 87 additions & 77 deletions src/app/service/pet/pet.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,17 @@ 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)
if err != nil {
return nil, status.Error(codes.Internal, "error converting dto to raw")
return nil, status.Error(codes.InvalidArgument, "error converting dto to raw")
}

err = s.repository.Update(req.Pet.Id, raw)
if err != nil {
return nil, status.Error(codes.NotFound, "pet not found")
}

images, err := s.imageService.FindByPetId(req.Pet.Id)
if err != nil {
return nil, status.Error(codes.Internal, "error querying image service")
}
imageUrls := ExtractImageUrls(images)

return &proto.UpdatePetResponse{Pet: RawToDto(raw, imageUrls)}, nil
var images []*image_proto.Image
// images, err := s.imageService.FindByPetId(req.Pet.Id)
return &proto.UpdatePetResponse{Pet: RawToDto(raw, images)}, nil
}

func (s *Service) ChangeView(_ context.Context, req *proto.ChangeViewPetRequest) (res *proto.ChangeViewPetResponse, err error) {
Expand All @@ -78,9 +74,9 @@ func (s *Service) ChangeView(_ context.Context, req *proto.ChangeViewPetRequest)
}
pet, err := DtoToRaw(petData.Pet)
if err != nil {
return nil, status.Error(codes.Internal, "error converting dto to raw")
return nil, status.Error(codes.InvalidArgument, "error converting dto to raw")
}
pet.IsVisible = req.Visible
pet.IsVisible = &req.Visible

err = s.repository.Update(req.Id, pet)
if err != nil {
Expand All @@ -92,29 +88,20 @@ 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

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")
}

for _, pet := range pets {
images, err := s.imageService.FindByPetId(pet.ID.String())
if err != nil {
return nil, status.Error(codes.Internal, "error querying image service")
}
imageUrls := ExtractImageUrls(images)
imageUrlsList = append(imageUrlsList, imageUrls)
}

petWithImageUrls, err := RawToDtoList(&pets, imageUrlsList)
imagesList := make([][]*image_proto.Image, len(pets))
petWithImages, err := RawToDtoList(&pets, imagesList)
if err != nil {
return nil, status.Error(codes.Internal, "error converting raw to dto list")
return nil, status.Error(codes.InvalidArgument, "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 @@ -127,29 +114,24 @@ func (s Service) FindOne(_ context.Context, req *proto.FindOnePetRequest) (res *
return nil, status.Error(codes.NotFound, err.Error())
}

images, err := s.imageService.FindByPetId(req.Id)
if err != nil {
return nil, status.Error(codes.Internal, "error querying image service")
}
imageUrls := ExtractImageUrls(images)

return &proto.FindOnePetResponse{Pet: RawToDto(&pet, imageUrls)}, err
var images []*image_proto.Image
return &proto.FindOnePetResponse{Pet: RawToDto(&pet, images)}, err
}

func (s *Service) Create(_ context.Context, req *proto.CreatePetRequest) (res *proto.CreatePetResponse, err error) {
raw, err := DtoToRaw(req.Pet)
if err != nil {
return nil, status.Error(codes.Internal, "error converting dto to raw: "+err.Error())
return nil, status.Error(codes.InvalidArgument, "error converting dto to raw: "+err.Error())
}

imgUrls := []string{}

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
var images []*image_proto.Image

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 @@ -159,7 +141,7 @@ func (s *Service) AdoptPet(ctx context.Context, req *proto.AdoptPetRequest) (res
}
pet, err := DtoToRaw(dtoPet.Pet)
if err != nil {
return nil, status.Error(codes.Internal, "error converting dto to raw")
return nil, status.Error(codes.InvalidArgument, "error converting dto to raw")
}
pet.AdoptBy = req.UserId

Expand All @@ -171,39 +153,53 @@ 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, imagesList [][]*image_proto.Image) ([]*proto.Pet, error) {
var result []*proto.Pet
if len(*in) != len(imageUrls) {
return nil, errors.New("length of in and imageUrls have to be the same")
if len(*in) != len(imagesList) {
return nil, status.Error(codes.InvalidArgument, "length of in and imagesList have to be the same")
}

for i, e := range *in {
result = append(result, RawToDto(e, imageUrls[i]))
result = append(result, RawToDto(e, imagesList[i]))
}
return result, nil
}

func RawToDto(in *pet.Pet, imgUrl []string) *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),
Habit: in.Habit,
Caption: in.Caption,
Status: proto.PetStatus(in.Status),
ImageUrls: imgUrl,
IsSterile: in.IsSterile,
IsVaccinated: in.IsVaccinated,
IsVisible: in.IsVisible,
IsClubPet: in.IsClubPet,
Background: in.Background,
Address: in.Address,
Contact: in.Contact,
AdoptBy: in.AdoptBy,
func RawToDto(in *pet.Pet, images []*image_proto.Image) *proto.Pet {
pet := &proto.Pet{
Id: in.ID.String(),
Type: in.Type,
Species: in.Species,
Name: in.Name,
Birthdate: in.Birthdate,
Gender: proto.Gender(in.Gender),
Habit: in.Habit,
Caption: in.Caption,
Status: proto.PetStatus(in.Status),
Images: images,
Background: in.Background,
Address: in.Address,
Contact: in.Contact,
AdoptBy: in.AdoptBy,
}

if in.IsClubPet != nil {
pet.IsClubPet = *in.IsClubPet
}

if in.IsSterile != nil {
pet.IsSterile = *in.IsSterile
}

if in.IsVaccinated != nil {
pet.IsVaccinated = *in.IsVaccinated
}

if in.IsVisible != nil {
pet.IsVisible = *in.IsVisible
}

return pet
}

func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) {
Expand Down Expand Up @@ -232,30 +228,44 @@ func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) {
status = petConst.FINDHOME
}

return &pet.Pet{
pet := &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,
Habit: in.Habit,
Caption: in.Caption,
Status: status,
IsSterile: in.IsSterile,
IsVaccinated: in.IsVaccinated,
IsVisible: in.IsVisible,
IsClubPet: in.IsClubPet,
Background: in.Background,
Address: in.Address,
Contact: in.Contact,
AdoptBy: in.AdoptBy,
}, nil
Type: in.Type,
Species: in.Species,
Name: in.Name,
Birthdate: in.Birthdate,
Gender: gender,
Habit: in.Habit,
Caption: in.Caption,
Status: status,
Background: in.Background,
Address: in.Address,
Contact: in.Contact,
AdoptBy: in.AdoptBy,
}

if &in.IsSterile != nil {
pet.IsSterile = &in.IsSterile
}

if &in.IsVaccinated != nil {
pet.IsVaccinated = &in.IsVaccinated
}

if &in.IsVisible != nil {
pet.IsVisible = &in.IsVisible
}

if &in.IsClubPet != nil {
pet.IsClubPet = &in.IsClubPet
}

return pet, nil
}

func ExtractImageUrls(in []*image_proto.Image) []string {
Expand Down
Loading
Loading