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 9 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
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
78 changes: 39 additions & 39 deletions src/app/service/pet/pet.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,20 @@ 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
// images, err := s.imageService.FindByPetId(req.Pet.Id)
// if err != nil {
// return nil, status.Error(codes.Internal, "error querying image service")
// }
// imageUrls := ExtractImageUrls(images)
macgeargear marked this conversation as resolved.
Show resolved Hide resolved
return &proto.UpdatePetResponse{Pet: RawToDto(raw, []string{})}, nil
}

func (s *Service) ChangeView(_ context.Context, req *proto.ChangeViewPetRequest) (res *proto.ChangeViewPetResponse, err error) {
Expand All @@ -78,9 +77,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,26 +91,26 @@ 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)
}
imageUrlsList := make([][]string, len(pets))
// for _, pet := range pets {
// images, err := s.imageService.FindByPetId(pet.ID.String())
macgeargear marked this conversation as resolved.
Show resolved Hide resolved
// if err != nil {
// return nil, status.Error(codes.Internal, "error querying image service")
// }
// imageUrls := ExtractImageUrls(images)
// imageUrlsList = append(imageUrlsList, imageUrls)
macgeargear marked this conversation as resolved.
Show resolved Hide resolved
// }

petWithImageUrls, err := RawToDtoList(&pets, imageUrlsList)
macgeargear marked this conversation as resolved.
Show resolved Hide resolved
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
Expand All @@ -127,19 +126,19 @@ 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)
// images, err := s.imageService.FindByPetId(req.Id)
// if err != nil {
// return nil, status.Error(codes.Internal, "error querying image service")
// }
// imageUrls := ExtractImageUrls(images)
macgeargear marked this conversation as resolved.
Show resolved Hide resolved

return &proto.FindOnePetResponse{Pet: RawToDto(&pet, imageUrls)}, err
return &proto.FindOnePetResponse{Pet: RawToDto(&pet, []string{})}, 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{}
Expand All @@ -159,7 +158,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 @@ -174,11 +173,12 @@ func (s *Service) AdoptPet(ctx context.Context, req *proto.AdoptPetRequest) (res
func RawToDtoList(in *[]*pet.Pet, imageUrls [][]string) ([]*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")
return nil, status.Error(codes.InvalidArgument, "length of in and imageUrls have to be the same")
}

for i, e := range *in {
result = append(result, RawToDto(e, imageUrls[i]))
for _, e := range *in {
// result = append(result, RawToDto(e, imageUrls[i]))
result = append(result, RawToDto(e, []string{}))
}
return result, nil
}
Expand All @@ -195,10 +195,10 @@ func RawToDto(in *pet.Pet, imgUrl []string) *proto.Pet {
Caption: in.Caption,
Status: proto.PetStatus(in.Status),
ImageUrls: imgUrl,
IsSterile: in.IsSterile,
IsVaccinated: in.IsVaccinated,
IsVisible: in.IsVisible,
IsClubPet: in.IsClubPet,
IsSterile: *in.IsSterile,
IsVaccinated: *in.IsVaccinated,
IsVisible: *in.IsVisible,
IsClubPet: *in.IsClubPet,
Background: in.Background,
Address: in.Address,
Contact: in.Contact,
Expand Down Expand Up @@ -247,10 +247,10 @@ func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) {
Habit: in.Habit,
Caption: in.Caption,
Status: status,
IsSterile: in.IsSterile,
IsVaccinated: in.IsVaccinated,
IsVisible: in.IsVisible,
IsClubPet: in.IsClubPet,
IsSterile: &in.IsSterile,
IsVaccinated: &in.IsVaccinated,
IsVisible: &in.IsVisible,
IsClubPet: &in.IsClubPet,
Background: in.Background,
Address: in.Address,
Contact: in.Contact,
Expand Down
71 changes: 36 additions & 35 deletions src/app/service/pet/pet.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/isd-sgcu/johnjud-backend/src/app/model"
"github.com/isd-sgcu/johnjud-backend/src/app/model/pet"
"github.com/isd-sgcu/johnjud-backend/src/app/utils"
proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1"
img_proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1"

Expand Down Expand Up @@ -66,26 +67,26 @@ func (t *PetServiceTest) SetupTest() {
Habit: faker.Paragraph(),
Caption: faker.Paragraph(),
Status: petConst.Status(rand.Intn(1) + 1),
IsSterile: true,
IsVaccinated: true,
IsVisible: true,
IsClubPet: true,
IsSterile: utils.BoolAddr(true),
IsVaccinated: utils.BoolAddr(true),
IsVisible: utils.BoolAddr(true),
IsClubPet: utils.BoolAddr(true),
Background: faker.Paragraph(),
Address: faker.Paragraph(),
Contact: faker.Paragraph(),
AdoptBy: "",
}
var images []*img_proto.Image
var imageUrls []string
for i := 0; i < 3; i++ {
url := faker.URL()
images = append(images, &img_proto.Image{
Id: faker.UUIDDigit(),
PetId: pet.ID.String(),
ImageUrl: url,
})
imageUrls = append(imageUrls, url)
}
imageUrls := []string{}
// for i := 0; i < 3; i++ {
// url := faker.URL()
// images = append(images, &img_proto.Image{
// Id: faker.UUIDDigit(),
// PetId: pet.ID.String(),
// ImageUrl: url,
// })
// imageUrls = append(imageUrls, url)
// }
t.ImagesList = append(t.ImagesList, images)
t.ImageUrlsList = append(t.ImageUrlsList, imageUrls)
pets = append(pets, pet)
Expand Down Expand Up @@ -113,10 +114,10 @@ func (t *PetServiceTest) SetupTest() {
Habit: t.Pet.Habit,
Caption: t.Pet.Caption,
Status: proto.PetStatus(t.Pet.Status),
IsSterile: t.Pet.IsSterile,
IsVaccinated: t.Pet.IsVaccinated,
IsVisible: t.Pet.IsVisible,
IsClubPet: t.Pet.IsClubPet,
IsSterile: *t.Pet.IsSterile,
IsVaccinated: *t.Pet.IsVaccinated,
IsVisible: *t.Pet.IsVisible,
IsClubPet: *t.Pet.IsClubPet,
Background: t.Pet.Background,
Address: t.Pet.Address,
Contact: t.Pet.Contact,
Expand Down Expand Up @@ -164,7 +165,7 @@ func (t *PetServiceTest) SetupTest() {
Status: t.Pet.Status,
IsSterile: t.Pet.IsSterile,
IsVaccinated: t.Pet.IsVaccinated,
IsVisible: false,
IsVisible: utils.BoolAddr(false),
IsClubPet: t.Pet.IsClubPet,
Background: t.Pet.Background,
Address: t.Pet.Address,
Expand All @@ -182,10 +183,10 @@ func (t *PetServiceTest) SetupTest() {
Caption: t.Pet.Caption,
Status: proto.PetStatus(t.Pet.Status),
ImageUrls: t.ImageUrls,
IsSterile: t.Pet.IsSterile,
IsVaccinated: t.Pet.IsVaccinated,
IsVisible: t.Pet.IsVaccinated,
IsClubPet: t.Pet.IsClubPet,
IsSterile: *t.Pet.IsSterile,
IsVaccinated: *t.Pet.IsVaccinated,
IsVisible: *t.Pet.IsVaccinated,
IsClubPet: *t.Pet.IsClubPet,
Background: t.Pet.Background,
Address: t.Pet.Address,
Contact: t.Pet.Contact,
Expand All @@ -204,10 +205,10 @@ func (t *PetServiceTest) SetupTest() {
Caption: t.Pet.Caption,
Status: proto.PetStatus(t.Pet.Status),
ImageUrls: t.ImageUrls,
IsSterile: t.Pet.IsSterile,
IsVaccinated: t.Pet.IsVaccinated,
IsVisible: t.Pet.IsVisible,
IsClubPet: t.Pet.IsClubPet,
IsSterile: *t.Pet.IsSterile,
IsVaccinated: *t.Pet.IsVaccinated,
IsVisible: *t.Pet.IsVisible,
IsClubPet: *t.Pet.IsClubPet,
Background: t.Pet.Background,
Address: t.Pet.Address,
Contact: t.Pet.Contact,
Expand Down Expand Up @@ -377,10 +378,10 @@ func createPets() []*pet.Pet {
Habit: faker.Paragraph(),
Caption: faker.Paragraph(),
Status: petConst.Status(rand.Intn(1) + 1),
IsSterile: true,
IsVaccinated: true,
IsVisible: true,
IsClubPet: true,
IsSterile: utils.BoolAddr(true),
IsVaccinated: utils.BoolAddr(true),
IsVisible: utils.BoolAddr(true),
IsClubPet: utils.BoolAddr(true),
Background: faker.Paragraph(),
Address: faker.Paragraph(),
Contact: faker.Paragraph(),
Expand All @@ -406,10 +407,10 @@ func (t *PetServiceTest) createPetsDto(in []*pet.Pet, imageUrlsList [][]string)
Caption: p.Caption,
Status: proto.PetStatus(p.Status),
ImageUrls: imageUrlsList[i],
IsSterile: p.IsSterile,
IsVaccinated: p.IsVaccinated,
IsVisible: p.IsVisible,
IsClubPet: p.IsClubPet,
IsSterile: *p.IsSterile,
IsVaccinated: *p.IsVaccinated,
IsVisible: *p.IsVisible,
IsClubPet: *p.IsClubPet,
Background: p.Background,
Address: p.Address,
Contact: p.Contact,
Expand Down
5 changes: 5 additions & 0 deletions src/app/utils/common.utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package utils

func BoolAddr(b bool) *bool {
return &b
}
10 changes: 10 additions & 0 deletions src/mocks/pet/pet.mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ func (r *RepositoryMock) Delete(id string) error {
args := r.Called(id)
return args.Error(0)
}

func (r *RepositoryMock) ChangeView(id string, visible bool, result *bool) error {
args := r.Called(id, result)

if args.Get(0) != nil {
*result = args.Get(0).(bool)
}

return args.Error(1)
}
Loading