From ad4ed097c63bf85904df404bba0d2b1fd683eee4 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Thu, 4 Jan 2024 18:24:27 +0700 Subject: [PATCH] fix: validate DtoToRaw & RawToDto --- src/app/service/pet/pet.service.go | 102 ++++++++++++++++++----------- 1 file changed, 65 insertions(+), 37 deletions(-) diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index b888007..f9c2674 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -166,26 +166,40 @@ func RawToDtoList(in *[]*pet.Pet, imagesList [][]*image_proto.Image) ([]*proto.P } 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), - Habit: in.Habit, - Caption: in.Caption, - Status: proto.PetStatus(in.Status), - Images: images, - IsSterile: *in.IsSterile, - IsVaccinated: *in.IsVaccinated, - IsVisible: *in.IsVisible, - IsClubPet: *in.IsClubPet, - Background: in.Background, - Address: in.Address, - Contact: in.Contact, - AdoptBy: in.AdoptBy, + 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) { @@ -214,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 {