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

feature/pet update and changeview #9

Merged
merged 6 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
60 changes: 50 additions & 10 deletions src/app/service/pet/pet.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package pet
import (
"context"
"errors"
"fmt"

"time"

Expand All @@ -12,14 +11,16 @@ import (
"github.com/isd-sgcu/johnjud-backend/src/app/model/pet"
petConst "github.com/isd-sgcu/johnjud-backend/src/constant/pet"
proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1"
image_proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1"
"github.com/rs/zerolog/log"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gorm.io/gorm"
)

type Service struct {
repository IRepository
repository IRepository
imageService ImageService
}

type IRepository interface {
Expand All @@ -30,8 +31,12 @@ type IRepository interface {
Delete(id string) error
}

func NewService(repository IRepository) *Service {
return &Service{repository: repository}
type ImageService interface {
FindByPetId(petId string) ([]*image_proto.Image, error)
}

func NewService(repository IRepository, imageService ImageService) *Service {
return &Service{repository, imageService}
}

func (s *Service) Delete(ctx context.Context, req *proto.DeletePetRequest) (*proto.DeletePetResponse, error) {
Expand All @@ -45,8 +50,37 @@ func (s *Service) Delete(ctx context.Context, req *proto.DeletePetRequest) (*pro
return &proto.DeletePetResponse{Success: true}, nil
}

func (*Service) Update(context.Context, *proto.UpdatePetRequest) (*proto.UpdatePetResponse, error) {
panic("unimplemented")
func (s *Service) Update(_ context.Context, req *proto.UpdatePetRequest) (res *proto.UpdatePetResponse, err error) {
raw, _ := DtoToRaw(req.Pet)

bookpanda marked this conversation as resolved.
Show resolved Hide resolved
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
}

func (s *Service) ChangeView(_ context.Context, req *proto.ChangeViewPetRequest) (res *proto.ChangeViewPetResponse, err error) {
petData, err := s.FindOne(context.Background(), &proto.FindOnePetRequest{Id: req.Id})
if err != nil {
return nil, status.Error(codes.NotFound, "pet not found")
}
pet, _ := DtoToRaw(petData.Pet)
pet.IsVisible = req.Visible

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

return &proto.ChangeViewPetResponse{Success: true}, nil
}

func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res *proto.FindAllPetResponse, err error) {
Expand Down Expand Up @@ -74,16 +108,14 @@ func (s Service) FindOne(_ context.Context, req *proto.FindOnePetRequest) (res *

func (s *Service) Create(_ context.Context, req *proto.CreatePetRequest) (res *proto.CreatePetResponse, err error) {
raw, _ := DtoToRaw(req.Pet)
imgUrl := []string{}
imgUrls := []string{}

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

fmt.Println(RawToDto(raw, []string{}))

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

func RawToDtoList(in *[]*pet.Pet) []*proto.Pet {
Expand Down Expand Up @@ -166,3 +198,11 @@ func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) {
Contact: in.Contact,
}, nil
}

func ExtractImageUrls(in []*image_proto.Image) []string {
var result []string
for _, e := range in {
result = append(result, e.ImageUrl)
}
return result
}
Loading
Loading