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

Feat/pet service #8

Merged
merged 6 commits into from
Dec 23, 2023
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
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ 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.8
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.31.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
google.golang.org/grpc v1.60.1
gorm.io/driver/postgres v1.5.4
gorm.io/gorm v1.25.5
Expand All @@ -29,6 +31,7 @@ require (

require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/isd-sgcu/johnjud-go-proto v0.0.8
github.com/jackc/pgpassfile v1.0.0 // indirect
Expand All @@ -37,22 +40,29 @@ require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
129 changes: 121 additions & 8 deletions src/app/service/pet/pet.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ package pet
import (
"context"
"errors"
"fmt"

"time"

"github.com/google/uuid"
"github.com/isd-sgcu/johnjud-backend/src/app/model"
"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"
"github.com/rs/zerolog/log"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gorm.io/gorm"
Expand All @@ -17,7 +24,7 @@ type Service struct {

type IRepository interface {
FindAll(result *[]*pet.Pet) error
FindOne(id string, result *[]*pet.Pet) error
FindOne(id string, result *pet.Pet) error
Create(in *pet.Pet) error
Update(id string, result *pet.Pet) error
Delete(id string) error
Expand All @@ -38,18 +45,124 @@ func (s *Service) Delete(ctx context.Context, req *proto.DeletePetRequest) (*pro
return &proto.DeletePetResponse{Success: true}, nil
}

func (*Service) Create(context.Context, *proto.CreatePetRequest) (*proto.CreatePetResponse, error) {
func (*Service) Update(context.Context, *proto.UpdatePetRequest) (*proto.UpdatePetResponse, error) {
panic("unimplemented")
}

func (*Service) FindAll(context.Context, *proto.FindAllPetRequest) (*proto.FindAllPetResponse, error) {
panic("unimplemented")
func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res *proto.FindAllPetResponse, err error) {
var pets []*pet.Pet

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")
}
return &proto.FindAllPetResponse{Pets: RawToDtoList(&pets)}, nil
}

func (*Service) FindOne(context.Context, *proto.FindOnePetRequest) (*proto.FindOnePetResponse, error) {
panic("unimplemented")
func (s Service) FindOne(_ context.Context, req *proto.FindOnePetRequest) (res *proto.FindOnePetResponse, err error) {
var pet pet.Pet

err = s.repository.FindOne(req.Id, &pet)
if err != nil {
log.Error().Err(err).
Str("service", "pet").Str("module", "find one").Str("id", req.Id).Msg("Not found")
return nil, status.Error(codes.NotFound, err.Error())
}
return &proto.FindOnePetResponse{Pet: RawToDto(&pet, []string{})}, err
}

func (*Service) Update(context.Context, *proto.UpdatePetRequest) (*proto.UpdatePetResponse, error) {
panic("unimplemented")
func (s *Service) Create(_ context.Context, req *proto.CreatePetRequest) (res *proto.CreatePetResponse, err error) {
raw, _ := DtoToRaw(req.Pet)
imgUrl := []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
}

func RawToDtoList(in *[]*pet.Pet) []*proto.Pet {
var result []*proto.Pet
for _, e := range *in {
result = append(result, RawToDto(e, []string{}))
}
return result
}

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,
}
}

func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) {
var id uuid.UUID
var gender petConst.Gender
var status petConst.Status

if in.Id != "" {
id, err = uuid.Parse(in.Id)
if err != nil {
return nil, err
}
}

switch in.Gender {
case 1:
gender = petConst.MALE
case 2:
gender = petConst.FEMALE
}

switch in.Status {
case 1:
status = petConst.ADOPTED
case 2:
status = petConst.FINDHOME
}

return &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,
}, nil
}
Loading
Loading