From 7ce1d9f7866c8814c32c28bff11eeb5eafaf7e80 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sat, 6 Jan 2024 16:50:41 +0700 Subject: [PATCH 01/40] fix: build package add platform --- .github/workflows/build.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e8796d6..a0e9ba1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -35,7 +35,7 @@ jobs: - name: Set branch id: branch run: | - echo "::set-output name=BRANCH::${GITHUB_REF#refs/heads/}" + echo "BRANCH=${GITHUB_REF#refs/heads/}" >> $GITHUB_OUTPUT - name: Log in to the Container Registry uses: docker/login-action@v1 @@ -48,6 +48,7 @@ jobs: uses: docker/build-push-action@v2 with: context: . + platforms: linux/amd64,linux/arm64 push: true tags: ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }},${{ env.IMAGE_NAME }}:latest cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache From bbefa5c70e205033129f289439db5b5e3f8e350d Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 9 Jan 2024 19:36:02 +0700 Subject: [PATCH 02/40] feat: update pet model --- src/app/model/pet/pet.model.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/app/model/pet/pet.model.go b/src/app/model/pet/pet.model.go index 3c88a37..3b780cb 100644 --- a/src/app/model/pet/pet.model.go +++ b/src/app/model/pet/pet.model.go @@ -12,6 +12,8 @@ type Pet struct { Name string `json:"name" gorm:"tinytext"` Birthdate string `json:"birthdate" gorm:"tinytext"` Gender pet.Gender `json:"gender" gorm:"tinytext" example:"male"` + Color string `json:"color" gorm:"tinytext"` + Pattern string `json:"pattern" gorm:"tinytext"` Habit string `json:"habit" gorm:"mediumtext"` Caption string `json:"caption" gorm:"mediumtext"` Status pet.Status `json:"status" gorm:"mediumtext" example:"findhome"` @@ -19,7 +21,7 @@ type Pet struct { IsVaccinated bool `json:"is_vaccine"` IsVisible bool `json:"is_visible"` IsClubPet bool `json:"is_club_pet"` - Background string `json:"background" gorm:"tinytext"` + Origin string `json:"origin" gorm:"tinytext"` Address string `json:"address" gorm:"tinytext"` Contact string `json:"contact" gorm:"tinytext"` AdoptBy string `json:"adopt_by" gorm:"tinytext"` From d665f2faf165af73f74870159827a381aa495fee Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 9 Jan 2024 21:32:03 +0700 Subject: [PATCH 03/40] fix; image mock --- go.mod | 2 +- go.sum | 4 ++-- src/mocks/image/image.mock.go | 10 ++++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index ade5ec4..80b2093 100644 --- a/go.mod +++ b/go.mod @@ -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.3.0 github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.31.0 github.com/spf13/viper v1.18.2 diff --git a/go.sum b/go.sum index fc7670b..7c1b4df 100644 --- a/go.sum +++ b/go.sum @@ -20,8 +20,8 @@ github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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.3.0 h1:UsAf2l06VypAp8nC5Ksru1QeoYwRjjxP/WKJwNOm1iY= +github.com/isd-sgcu/johnjud-go-proto v0.3.0/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= diff --git a/src/mocks/image/image.mock.go b/src/mocks/image/image.mock.go index 19aba0d..d631497 100644 --- a/src/mocks/image/image.mock.go +++ b/src/mocks/image/image.mock.go @@ -32,6 +32,16 @@ func (c *ClientMock) FindByPetId(_ context.Context, in *proto.FindImageByPetIdRe return res, args.Error(1) } +func (c *ClientMock) AssignPet(_ context.Context, in *proto.AssignPetRequest, _ ...grpc.CallOption) (res *proto.AssignPetResponse, err error) { + args := c.Called(in) + + if args.Get(0) != nil { + res = args.Get(0).(*proto.AssignPetResponse) + } + + return res, args.Error(1) +} + func (c *ClientMock) Delete(_ context.Context, in *proto.DeleteImageRequest, _ ...grpc.CallOption) (res *proto.DeleteImageResponse, err error) { args := c.Called(in) From 2e1f6e64ffe7f37cdc4cffb956b486b3c39d597d Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 9 Jan 2024 21:32:17 +0700 Subject: [PATCH 04/40] fix: pet repo findone use First() --- src/app/repository/pet/pet.repository.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/repository/pet/pet.repository.go b/src/app/repository/pet/pet.repository.go index 2ee80f1..0f72ae1 100644 --- a/src/app/repository/pet/pet.repository.go +++ b/src/app/repository/pet/pet.repository.go @@ -18,7 +18,7 @@ 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 + return r.db.Model(&pet.Pet{}).First(result, "id = ?", id).Error } func (r *Repository) Create(in *pet.Pet) error { From 5583479df8829f1ddcc986e9f4b8abe4c3f8a8db Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 9 Jan 2024 21:32:31 +0700 Subject: [PATCH 05/40] fix: pet svc use images --- src/app/service/pet/pet.service.go | 45 ++++++++++++++---------------- src/constant/pet/pet.constant.go | 12 ++++---- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index 552f81b..a1db78d 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -66,9 +66,8 @@ func (s *Service) Update(_ context.Context, req *proto.UpdatePetRequest) (res *p if err != nil { return nil, status.Error(codes.Internal, "error querying image service") } - imageUrls := ExtractImageUrls(images) - return &proto.UpdatePetResponse{Pet: RawToDto(raw, imageUrls)}, nil + return &proto.UpdatePetResponse{Pet: RawToDto(raw, images)}, nil } func (s *Service) ChangeView(_ context.Context, req *proto.ChangeViewPetRequest) (res *proto.ChangeViewPetResponse, err error) { @@ -92,7 +91,7 @@ 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 + var imagesList [][]*image_proto.Image err = s.repository.FindAll(&pets) if err != nil { @@ -105,16 +104,15 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res if err != nil { return nil, status.Error(codes.Internal, "error querying image service") } - imageUrls := ExtractImageUrls(images) - imageUrlsList = append(imageUrlsList, imageUrls) + imagesList = append(imagesList, images) } - petWithImageUrls, err := RawToDtoList(&pets, imageUrlsList) + petWithImages, err := RawToDtoList(&pets, imagesList) if err != nil { return nil, status.Error(codes.Internal, "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) { @@ -131,9 +129,8 @@ func (s Service) FindOne(_ context.Context, req *proto.FindOnePetRequest) (res * if err != nil { return nil, status.Error(codes.Internal, "error querying image service") } - imageUrls := ExtractImageUrls(images) - return &proto.FindOnePetResponse{Pet: RawToDto(&pet, imageUrls)}, err + return &proto.FindOnePetResponse{Pet: RawToDto(&pet, images)}, err } func (s *Service) Create(_ context.Context, req *proto.CreatePetRequest) (res *proto.CreatePetResponse, err error) { @@ -142,14 +139,14 @@ func (s *Service) Create(_ context.Context, req *proto.CreatePetRequest) (res *p return nil, status.Error(codes.Internal, "error converting dto to raw: "+err.Error()) } - imgUrls := []string{} + images := []*image_proto.Image{} 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 + return &proto.CreatePetResponse{Pet: RawToDto(raw, images)}, nil } func (s *Service) AdoptPet(ctx context.Context, req *proto.AdoptPetRequest) (res *proto.AdoptPetResponse, err error) { @@ -171,35 +168,35 @@ 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, images [][]*image_proto.Image) ([]*proto.Pet, error) { var result []*proto.Pet - if len(*in) != len(imageUrls) { + if len(*in) != len(images) { return nil, errors.New("length of in and imageUrls have to be the same") } for i, e := range *in { - result = append(result, RawToDto(e, imageUrls[i])) + result = append(result, RawToDto(e, images[i])) } return result, nil } -func RawToDto(in *pet.Pet, imgUrl []string) *proto.Pet { +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), + Gender: string(in.Gender), Habit: in.Habit, Caption: in.Caption, - Status: proto.PetStatus(in.Status), - ImageUrls: imgUrl, + Status: string(in.Status), + Images: images, IsSterile: in.IsSterile, IsVaccinated: in.IsVaccinated, IsVisible: in.IsVisible, IsClubPet: in.IsClubPet, - Background: in.Background, + Origin: in.Origin, Address: in.Address, Contact: in.Contact, AdoptBy: in.AdoptBy, @@ -219,16 +216,16 @@ func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) { } switch in.Gender { - case 0: + case string(petConst.MALE): gender = petConst.MALE - case 1: + case string(petConst.FEMALE): gender = petConst.FEMALE } switch in.Status { - case 0: + case string(petConst.ADOPTED): status = petConst.ADOPTED - case 1: + case string(petConst.FINDHOME): status = petConst.FINDHOME } @@ -251,7 +248,7 @@ func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) { IsVaccinated: in.IsVaccinated, IsVisible: in.IsVisible, IsClubPet: in.IsClubPet, - Background: in.Background, + Origin: in.Origin, Address: in.Address, Contact: in.Contact, AdoptBy: in.AdoptBy, diff --git a/src/constant/pet/pet.constant.go b/src/constant/pet/pet.constant.go index 577bed6..77e50c7 100644 --- a/src/constant/pet/pet.constant.go +++ b/src/constant/pet/pet.constant.go @@ -1,15 +1,15 @@ package pet -type Gender int +type Gender string const ( - MALE = 0 - FEMALE = 1 + MALE Gender = "male" + FEMALE Gender = "female" ) -type Status int +type Status string const ( - ADOPTED = 0 - FINDHOME = 1 + ADOPTED Status = "adopted" + FINDHOME Status = "findhome" ) From 992100148e28f0c78a26fd09b07289c1abbf1037 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 9 Jan 2024 21:32:39 +0700 Subject: [PATCH 06/40] fix: pet svc test use images --- src/app/service/pet/pet.service_test.go | 76 ++++++++++++------------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index 04d36b9..d1d34de 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -37,9 +37,7 @@ type PetServiceTest struct { UpdatePetReqMock *proto.UpdatePetRequest ChangeViewPetReqMock *proto.ChangeViewPetRequest Images []*img_proto.Image - ImageUrls []string ImagesList [][]*img_proto.Image - ImageUrlsList [][]string ChangeAdoptBy *pet.Pet AdoptByReq *proto.AdoptPetRequest } @@ -50,6 +48,9 @@ func TestPetService(t *testing.T) { func (t *PetServiceTest) SetupTest() { var pets []*pet.Pet + genders := []petConst.Gender{petConst.MALE, petConst.FEMALE} + statuses := []petConst.Status{petConst.ADOPTED, petConst.FINDHOME} + for i := 0; i <= 3; i++ { pet := &pet.Pet{ Base: model.Base{ @@ -62,15 +63,15 @@ func (t *PetServiceTest) SetupTest() { Species: faker.Word(), Name: faker.Name(), Birthdate: faker.Word(), - Gender: petConst.Gender(rand.Intn(1) + 1), + Gender: genders[rand.Intn(2)], Habit: faker.Paragraph(), Caption: faker.Paragraph(), - Status: petConst.Status(rand.Intn(1) + 1), + Status: statuses[rand.Intn(2)], IsSterile: true, IsVaccinated: true, IsVisible: true, IsClubPet: true, - Background: faker.Paragraph(), + Origin: faker.Paragraph(), Address: faker.Paragraph(), Contact: faker.Paragraph(), AdoptBy: "", @@ -87,21 +88,12 @@ func (t *PetServiceTest) SetupTest() { imageUrls = append(imageUrls, url) } t.ImagesList = append(t.ImagesList, images) - t.ImageUrlsList = append(t.ImageUrlsList, imageUrls) pets = append(pets, pet) } t.Pets = pets t.Pet = pets[0] - - for _, images := range t.ImagesList { - for _, image := range images { - t.ImageUrls = append(t.ImageUrls, image.ImageUrl) - } - } - t.Images = t.ImagesList[0] - t.ImageUrls = t.ImageUrlsList[0] t.PetDto = &proto.Pet{ Id: t.Pet.ID.String(), @@ -109,18 +101,18 @@ func (t *PetServiceTest) SetupTest() { Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, - Gender: proto.Gender(t.Pet.Gender), + Gender: string(t.Pet.Gender), Habit: t.Pet.Habit, Caption: t.Pet.Caption, - Status: proto.PetStatus(t.Pet.Status), + Status: string(t.Pet.Status), IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, IsClubPet: t.Pet.IsClubPet, - Background: t.Pet.Background, + Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, - ImageUrls: t.ImageUrls, + Images: t.Images, } t.UpdatePet = &pet.Pet{ @@ -142,7 +134,7 @@ func (t *PetServiceTest) SetupTest() { IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, IsClubPet: t.Pet.IsClubPet, - Background: t.Pet.Background, + Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, } @@ -166,7 +158,7 @@ func (t *PetServiceTest) SetupTest() { IsVaccinated: t.Pet.IsVaccinated, IsVisible: false, IsClubPet: t.Pet.IsClubPet, - Background: t.Pet.Background, + Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, } @@ -177,16 +169,16 @@ func (t *PetServiceTest) SetupTest() { Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, - Gender: proto.Gender(t.Pet.Gender), + Gender: string(t.Pet.Gender), Habit: t.Pet.Habit, Caption: t.Pet.Caption, - Status: proto.PetStatus(t.Pet.Status), - ImageUrls: t.ImageUrls, + Status: string(t.Pet.Status), + Images: t.Images, IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVaccinated, IsClubPet: t.Pet.IsClubPet, - Background: t.Pet.Background, + Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, }, @@ -199,16 +191,16 @@ func (t *PetServiceTest) SetupTest() { Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, - Gender: proto.Gender(t.Pet.Gender), + Gender: string(t.Pet.Gender), Habit: t.Pet.Habit, Caption: t.Pet.Caption, - Status: proto.PetStatus(t.Pet.Status), - ImageUrls: t.ImageUrls, + Status: string(t.Pet.Status), + Images: t.Images, IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, IsClubPet: t.Pet.IsClubPet, - Background: t.Pet.Background, + Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, }, @@ -238,7 +230,7 @@ func (t *PetServiceTest) SetupTest() { IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, IsClubPet: t.Pet.IsClubPet, - Background: t.Pet.Background, + Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, AdoptBy: faker.UUIDDigit(), @@ -322,7 +314,7 @@ func (t *PetServiceTest) TestFindOneSuccess() { func (t *PetServiceTest) TestFindAllSuccess() { - want := &proto.FindAllPetResponse{Pets: t.createPetsDto(t.Pets, t.ImageUrlsList)} + want := &proto.FindAllPetResponse{Pets: t.createPetsDto(t.Pets, t.ImagesList)} var petsIn []*pet.Pet @@ -360,6 +352,8 @@ func (t *PetServiceTest) TestFindOneNotFound() { func createPets() []*pet.Pet { var result []*pet.Pet + genders := []petConst.Gender{petConst.MALE, petConst.FEMALE} + statuses := []petConst.Status{petConst.ADOPTED, petConst.FINDHOME} for i := 0; i < rand.Intn(4)+1; i++ { r := &pet.Pet{ @@ -373,15 +367,15 @@ func createPets() []*pet.Pet { Species: faker.Word(), Name: faker.Name(), Birthdate: faker.Word(), - Gender: petConst.Gender(rand.Intn(1) + 1), + Gender: genders[rand.Intn(2)], Habit: faker.Paragraph(), Caption: faker.Paragraph(), - Status: petConst.Status(rand.Intn(1) + 1), + Status: statuses[rand.Intn(2)], IsSterile: true, IsVaccinated: true, IsVisible: true, IsClubPet: true, - Background: faker.Paragraph(), + Origin: faker.Paragraph(), Address: faker.Paragraph(), Contact: faker.Paragraph(), } @@ -391,7 +385,7 @@ func createPets() []*pet.Pet { return result } -func (t *PetServiceTest) createPetsDto(in []*pet.Pet, imageUrlsList [][]string) []*proto.Pet { +func (t *PetServiceTest) createPetsDto(in []*pet.Pet, imagesList [][]*img_proto.Image) []*proto.Pet { var result []*proto.Pet for i, p := range in { @@ -401,16 +395,16 @@ func (t *PetServiceTest) createPetsDto(in []*pet.Pet, imageUrlsList [][]string) Species: p.Species, Name: p.Name, Birthdate: p.Birthdate, - Gender: proto.Gender(p.Gender), + Gender: string(p.Gender), Habit: p.Habit, Caption: p.Caption, - Status: proto.PetStatus(p.Status), - ImageUrls: imageUrlsList[i], + Status: string(p.Status), + Images: imagesList[i], IsSterile: p.IsSterile, IsVaccinated: p.IsVaccinated, IsVisible: p.IsVisible, IsClubPet: p.IsClubPet, - Background: p.Background, + Origin: p.Origin, Address: p.Address, Contact: p.Contact, } @@ -423,7 +417,7 @@ func (t *PetServiceTest) createPetsDto(in []*pet.Pet, imageUrlsList [][]string) func (t *PetServiceTest) TestCreateSuccess() { want := &proto.CreatePetResponse{Pet: t.PetDto} - want.Pet.ImageUrls = []string{} // when pet is first created, it has no images + want.Pet.Images = []*img_proto.Image{} // when pet is first created, it has no images repo := &mock.RepositoryMock{} @@ -440,7 +434,7 @@ func (t *PetServiceTest) TestCreateSuccess() { IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, IsClubPet: t.Pet.IsClubPet, - Background: t.Pet.Background, + Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, } @@ -472,7 +466,7 @@ func (t *PetServiceTest) TestCreateInternalErr() { IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, IsClubPet: t.Pet.IsClubPet, - Background: t.Pet.Background, + Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, } From 54c7e042fa2722088b19d8572183ac2c611e5315 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 9 Jan 2024 21:34:41 +0700 Subject: [PATCH 07/40] feat: pr template --- .../PULL_REQUEST_TEMPLATE/pull_request_template.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .github/PULL_REQUEST_TEMPLATE/pull_request_template.md diff --git a/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md b/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md new file mode 100644 index 0000000..7748e23 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md @@ -0,0 +1,13 @@ +## Change made + +- [ ]  New features +- [ ]  Bug fixes +- [ ]  Breaking changes +## Describe what you have done +- +### New Features +- +### Fix +- +### Others +- \ No newline at end of file From f10ca468f8733bdb468a85f0b6b7e0e3c4aad6f7 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 9 Jan 2024 22:07:53 +0700 Subject: [PATCH 08/40] fix: add color, pattern in svc, test --- src/app/service/pet/pet.service.go | 4 ++++ src/app/service/pet/pet.service_test.go | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index a1db78d..7317841 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -188,6 +188,8 @@ func RawToDto(in *pet.Pet, images []*image_proto.Image) *proto.Pet { Name: in.Name, Birthdate: in.Birthdate, Gender: string(in.Gender), + Color: in.Color, + Pattern: in.Pattern, Habit: in.Habit, Caption: in.Caption, Status: string(in.Status), @@ -241,6 +243,8 @@ func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) { Name: in.Name, Birthdate: in.Birthdate, Gender: gender, + Color: in.Color, + Pattern: in.Pattern, Habit: in.Habit, Caption: in.Caption, Status: status, diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index d1d34de..a2167b9 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -64,6 +64,8 @@ func (t *PetServiceTest) SetupTest() { Name: faker.Name(), Birthdate: faker.Word(), Gender: genders[rand.Intn(2)], + Color: faker.Word(), + Pattern: faker.Word(), Habit: faker.Paragraph(), Caption: faker.Paragraph(), Status: statuses[rand.Intn(2)], @@ -102,6 +104,8 @@ func (t *PetServiceTest) SetupTest() { Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: string(t.Pet.Gender), + Color: t.Pet.Color, + Pattern: t.Pet.Pattern, Habit: t.Pet.Habit, Caption: t.Pet.Caption, Status: string(t.Pet.Status), @@ -127,6 +131,8 @@ func (t *PetServiceTest) SetupTest() { Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, + Color: t.Pet.Color, + Pattern: t.Pet.Pattern, Habit: t.Pet.Habit, Caption: t.Pet.Caption, Status: t.Pet.Status, @@ -151,6 +157,8 @@ func (t *PetServiceTest) SetupTest() { Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, + Color: t.Pet.Color, + Pattern: t.Pet.Pattern, Habit: t.Pet.Habit, Caption: t.Pet.Caption, Status: t.Pet.Status, @@ -170,6 +178,8 @@ func (t *PetServiceTest) SetupTest() { Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: string(t.Pet.Gender), + Color: t.Pet.Color, + Pattern: t.Pet.Pattern, Habit: t.Pet.Habit, Caption: t.Pet.Caption, Status: string(t.Pet.Status), @@ -192,6 +202,8 @@ func (t *PetServiceTest) SetupTest() { Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: string(t.Pet.Gender), + Color: t.Pet.Color, + Pattern: t.Pet.Pattern, Habit: t.Pet.Habit, Caption: t.Pet.Caption, Status: string(t.Pet.Status), @@ -223,6 +235,8 @@ func (t *PetServiceTest) SetupTest() { Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, + Color: t.Pet.Color, + Pattern: t.Pet.Pattern, Habit: t.Pet.Habit, Caption: t.Pet.Caption, Status: t.Pet.Status, @@ -368,6 +382,8 @@ func createPets() []*pet.Pet { Name: faker.Name(), Birthdate: faker.Word(), Gender: genders[rand.Intn(2)], + Color: faker.Word(), + Pattern: faker.Word(), Habit: faker.Paragraph(), Caption: faker.Paragraph(), Status: statuses[rand.Intn(2)], @@ -396,6 +412,8 @@ func (t *PetServiceTest) createPetsDto(in []*pet.Pet, imagesList [][]*img_proto. Name: p.Name, Birthdate: p.Birthdate, Gender: string(p.Gender), + Color: p.Color, + Pattern: p.Pattern, Habit: p.Habit, Caption: p.Caption, Status: string(p.Status), @@ -427,6 +445,8 @@ func (t *PetServiceTest) TestCreateSuccess() { Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, + Color: t.Pet.Color, + Pattern: t.Pet.Pattern, Habit: t.Pet.Habit, Caption: t.Pet.Caption, Status: t.Pet.Status, @@ -459,6 +479,8 @@ func (t *PetServiceTest) TestCreateInternalErr() { Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, + Color: t.Pet.Color, + Pattern: t.Pet.Pattern, Habit: t.Pet.Habit, Caption: t.Pet.Caption, Status: t.Pet.Status, From e58f3286d4507cdfa7a5f6962f2738608d257732 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Fri, 12 Jan 2024 23:18:54 +0700 Subject: [PATCH 09/40] chage: update proto --- go.mod | 2 +- go.sum | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 80b2093..b29bdc9 100644 --- a/go.mod +++ b/go.mod @@ -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.3.0 + github.com/isd-sgcu/johnjud-go-proto v0.4.0 github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.31.0 github.com/spf13/viper v1.18.2 diff --git a/go.sum b/go.sum index 7c1b4df..d8c95df 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,10 @@ 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.3.0 h1:UsAf2l06VypAp8nC5Ksru1QeoYwRjjxP/WKJwNOm1iY= github.com/isd-sgcu/johnjud-go-proto v0.3.0/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= +github.com/isd-sgcu/johnjud-go-proto v0.3.2 h1:lOz8wwoHx71lW6YnEshNDIpnhj2IUP39Zx4lqruYzA8= +github.com/isd-sgcu/johnjud-go-proto v0.3.2/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= +github.com/isd-sgcu/johnjud-go-proto v0.4.0 h1:YMmi37Siexo84zlUEhkSnFsZtf/U+bFLxtaUW8+A8Ow= +github.com/isd-sgcu/johnjud-go-proto v0.4.0/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= From b493327a0d88e9b0c805ac491986c65203500751 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 00:43:05 +0700 Subject: [PATCH 10/40] chore: update proto --- go.mod | 2 +- go.sum | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index b29bdc9..acfaa24 100644 --- a/go.mod +++ b/go.mod @@ -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.4.0 + github.com/isd-sgcu/johnjud-go-proto v0.4.2 github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.31.0 github.com/spf13/viper v1.18.2 diff --git a/go.sum b/go.sum index d8c95df..efa2449 100644 --- a/go.sum +++ b/go.sum @@ -26,6 +26,10 @@ github.com/isd-sgcu/johnjud-go-proto v0.3.2 h1:lOz8wwoHx71lW6YnEshNDIpnhj2IUP39Z github.com/isd-sgcu/johnjud-go-proto v0.3.2/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= github.com/isd-sgcu/johnjud-go-proto v0.4.0 h1:YMmi37Siexo84zlUEhkSnFsZtf/U+bFLxtaUW8+A8Ow= github.com/isd-sgcu/johnjud-go-proto v0.4.0/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= +github.com/isd-sgcu/johnjud-go-proto v0.4.1 h1:CrJbGs8oy3UdKpPv2XhCIZo2cKrSeGtsR/FKHeaSPM4= +github.com/isd-sgcu/johnjud-go-proto v0.4.1/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= +github.com/isd-sgcu/johnjud-go-proto v0.4.2 h1:KepvAHrFmi9Ki5kdQTKWZ95knjxeVv78b6ru3WRjNpg= +github.com/isd-sgcu/johnjud-go-proto v0.4.2/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= From 98f6ef71aa80244e97f9ad7b1321093f6f7567bb Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 00:43:15 +0700 Subject: [PATCH 11/40] chore --- src/constant/pet/pet.constant.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/constant/pet/pet.constant.go b/src/constant/pet/pet.constant.go index 77e50c7..2ad000d 100644 --- a/src/constant/pet/pet.constant.go +++ b/src/constant/pet/pet.constant.go @@ -13,3 +13,6 @@ const ( ADOPTED Status = "adopted" FINDHOME Status = "findhome" ) + +const HOUR = 24 +const YEAR = 365 From 35400342611f21885a964c62b8d76accb6aa1580 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 00:43:53 +0700 Subject: [PATCH 12/40] feat: remove all helper to utils and add filter pets --- src/app/utils/pet/pet.utils.go | 171 +++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 src/app/utils/pet/pet.utils.go diff --git a/src/app/utils/pet/pet.utils.go b/src/app/utils/pet/pet.utils.go new file mode 100644 index 0000000..fce37a6 --- /dev/null +++ b/src/app/utils/pet/pet.utils.go @@ -0,0 +1,171 @@ +package pet + +import ( + "errors" + "fmt" + "strings" + "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" + imageProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1" + "gorm.io/gorm" +) + +func RawToDtoList(in *[]*pet.Pet, images [][]*imageProto.Image, query *proto.FindAllPetRequest) ([]*proto.Pet, error) { + var result []*proto.Pet + if len(*in) != len(images) { + return nil, errors.New("length of in and imageUrls have to be the same") + } + + for i, p := range *in { + res, err := filterAge(p, query.Age) + if err != nil { + return nil, err + } + if !res { + continue + } + if query.Search != "" && !strings.Contains(p.Name, query.Search) && !strings.Contains(p.Species, query.Search) { + continue + } + if query.Type != "" && p.Type != query.Type { + continue + } + if query.Gender != "" && p.Gender != petConst.Gender(query.Gender) { + continue + } + if query.Color != "" && p.Color != query.Color { + continue + } + if query.Pattern != "" && p.Pattern != query.Pattern { + continue + } + if query.Origin != "" && p.Origin != query.Origin { + continue + } + result = append(result, RawToDto(p, images[i])) + } + return result, nil +} + +func RawToDto(in *pet.Pet, images []*imageProto.Image) *proto.Pet { + return &proto.Pet{ + Id: in.ID.String(), + Type: in.Type, + Species: in.Species, + Name: in.Name, + Birthdate: in.Birthdate, + Gender: string(in.Gender), + Color: in.Color, + Pattern: in.Pattern, + Habit: in.Habit, + Caption: in.Caption, + Status: string(in.Status), + Images: images, + IsSterile: in.IsSterile, + IsVaccinated: in.IsVaccinated, + IsVisible: in.IsVisible, + IsClubPet: in.IsClubPet, + Origin: in.Origin, + Address: in.Address, + Contact: in.Contact, + AdoptBy: in.AdoptBy, + } +} + +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 string(petConst.MALE): + gender = petConst.MALE + case string(petConst.FEMALE): + gender = petConst.FEMALE + } + + switch in.Status { + case string(petConst.ADOPTED): + status = petConst.ADOPTED + case string(petConst.FINDHOME): + 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, + Color: in.Color, + Pattern: in.Pattern, + Habit: in.Habit, + Caption: in.Caption, + Status: status, + IsSterile: in.IsSterile, + IsVaccinated: in.IsVaccinated, + IsVisible: in.IsVisible, + IsClubPet: in.IsClubPet, + Origin: in.Origin, + Address: in.Address, + Contact: in.Contact, + AdoptBy: in.AdoptBy, + }, nil +} + +func ExtractImageUrls(in []*imageProto.Image) []string { + var result []string + for _, e := range in { + result = append(result, e.ImageUrl) + } + return result +} + +func parseDate(dateStr string) (time.Time, error) { + parsedTime, err := time.Parse(time.RFC3339, dateStr) + if err != nil { + return time.Time{}, err + } + return parsedTime, nil +} + +func filterAge(pet *pet.Pet, age string) (bool, error) { + birthdate, err := parseDate(pet.Birthdate) + if err != nil { + return false, err + } + + currYear := time.Now() + birthYear := birthdate + diff := currYear.Sub(birthYear).Hours() / petConst.HOUR / petConst.YEAR + + fmt.Println(age, diff, currYear.Year()-birthYear.Year()) + switch age { + case "kitten": + return diff < 1, nil + case "adult": + return diff >= 1 && diff < 7, nil + case "senior": + return diff >= 7, nil + default: + return false, errors.New("filter age error. your age must be 'kitten' | 'adult' | 'senior' ") + } +} From dca9b07849508ccc419e5d2bb4f44fbedb4ca714 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 00:44:02 +0700 Subject: [PATCH 13/40] chore --- src/app/service/pet/pet.service.go | 127 +++-------------------------- 1 file changed, 12 insertions(+), 115 deletions(-) diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index 7317841..8a7ded1 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -3,13 +3,10 @@ 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" + petUtils "github.com/isd-sgcu/johnjud-backend/src/app/utils/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" @@ -52,7 +49,7 @@ 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) + raw, err := petUtils.DtoToRaw(req.Pet) if err != nil { return nil, status.Error(codes.Internal, "error converting dto to raw") } @@ -67,7 +64,7 @@ func (s *Service) Update(_ context.Context, req *proto.UpdatePetRequest) (res *p return nil, status.Error(codes.Internal, "error querying image service") } - return &proto.UpdatePetResponse{Pet: RawToDto(raw, images)}, nil + return &proto.UpdatePetResponse{Pet: petUtils.RawToDto(raw, images)}, nil } func (s *Service) ChangeView(_ context.Context, req *proto.ChangeViewPetRequest) (res *proto.ChangeViewPetResponse, err error) { @@ -75,7 +72,7 @@ func (s *Service) ChangeView(_ context.Context, req *proto.ChangeViewPetRequest) if err != nil { return nil, status.Error(codes.NotFound, "pet not found") } - pet, err := DtoToRaw(petData.Pet) + pet, err := petUtils.DtoToRaw(petData.Pet) if err != nil { return nil, status.Error(codes.Internal, "error converting dto to raw") } @@ -106,12 +103,11 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res } imagesList = append(imagesList, images) } - - petWithImages, err := RawToDtoList(&pets, imagesList) + fmt.Println(req) + petWithImages, err := petUtils.RawToDtoList(&pets, imagesList, req) if err != nil { - return nil, status.Error(codes.Internal, "error converting raw to dto list") + return nil, status.Error(codes.Internal, fmt.Sprintf("error converting raw to dto list: %v", err)) } - return &proto.FindAllPetResponse{Pets: petWithImages}, nil } @@ -130,11 +126,11 @@ func (s Service) FindOne(_ context.Context, req *proto.FindOnePetRequest) (res * return nil, status.Error(codes.Internal, "error querying image service") } - return &proto.FindOnePetResponse{Pet: RawToDto(&pet, images)}, err + return &proto.FindOnePetResponse{Pet: petUtils.RawToDto(&pet, images)}, err } func (s *Service) Create(_ context.Context, req *proto.CreatePetRequest) (res *proto.CreatePetResponse, err error) { - raw, err := DtoToRaw(req.Pet) + raw, err := petUtils.DtoToRaw(req.Pet) if err != nil { return nil, status.Error(codes.Internal, "error converting dto to raw: "+err.Error()) } @@ -146,7 +142,7 @@ func (s *Service) Create(_ context.Context, req *proto.CreatePetRequest) (res *p return nil, status.Error(codes.Internal, "failed to create pet") } - return &proto.CreatePetResponse{Pet: RawToDto(raw, images)}, nil + return &proto.CreatePetResponse{Pet: petUtils.RawToDto(raw, images)}, nil } func (s *Service) AdoptPet(ctx context.Context, req *proto.AdoptPetRequest) (res *proto.AdoptPetResponse, err error) { @@ -154,7 +150,7 @@ func (s *Service) AdoptPet(ctx context.Context, req *proto.AdoptPetRequest) (res if err != nil { return nil, status.Error(codes.NotFound, "pet not found") } - pet, err := DtoToRaw(dtoPet.Pet) + pet, err := petUtils.DtoToRaw(dtoPet.Pet) if err != nil { return nil, status.Error(codes.Internal, "error converting dto to raw") } @@ -167,102 +163,3 @@ func (s *Service) AdoptPet(ctx context.Context, req *proto.AdoptPetRequest) (res return &proto.AdoptPetResponse{Success: true}, nil } - -func RawToDtoList(in *[]*pet.Pet, images [][]*image_proto.Image) ([]*proto.Pet, error) { - var result []*proto.Pet - if len(*in) != len(images) { - return nil, errors.New("length of in and imageUrls have to be the same") - } - - for i, e := range *in { - result = append(result, RawToDto(e, images[i])) - } - return result, nil -} - -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: string(in.Gender), - Color: in.Color, - Pattern: in.Pattern, - Habit: in.Habit, - Caption: in.Caption, - Status: string(in.Status), - Images: images, - IsSterile: in.IsSterile, - IsVaccinated: in.IsVaccinated, - IsVisible: in.IsVisible, - IsClubPet: in.IsClubPet, - Origin: in.Origin, - Address: in.Address, - Contact: in.Contact, - AdoptBy: in.AdoptBy, - } -} - -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 string(petConst.MALE): - gender = petConst.MALE - case string(petConst.FEMALE): - gender = petConst.FEMALE - } - - switch in.Status { - case string(petConst.ADOPTED): - status = petConst.ADOPTED - case string(petConst.FINDHOME): - 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, - Color: in.Color, - Pattern: in.Pattern, - Habit: in.Habit, - Caption: in.Caption, - Status: status, - IsSterile: in.IsSterile, - IsVaccinated: in.IsVaccinated, - IsVisible: in.IsVisible, - IsClubPet: in.IsClubPet, - Origin: in.Origin, - Address: in.Address, - Contact: in.Contact, - AdoptBy: in.AdoptBy, - }, nil -} - -func ExtractImageUrls(in []*image_proto.Image) []string { - var result []string - for _, e := range in { - result = append(result, e.ImageUrl) - } - return result -} From 2a31dba7f62c8b23ef7906b0e5ebf03940146032 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:50:19 +0700 Subject: [PATCH 14/40] feat: filter and paginate --- src/app/repository/pet/pet.repository.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/app/repository/pet/pet.repository.go b/src/app/repository/pet/pet.repository.go index 0f72ae1..062e2da 100644 --- a/src/app/repository/pet/pet.repository.go +++ b/src/app/repository/pet/pet.repository.go @@ -2,6 +2,8 @@ package pet import ( "github.com/isd-sgcu/johnjud-backend/src/app/model/pet" + petUtils "github.com/isd-sgcu/johnjud-backend/src/app/utils/pet" + proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" "gorm.io/gorm" ) @@ -13,8 +15,21 @@ func NewRepository(db *gorm.DB) *Repository { return &Repository{db: db} } -func (r *Repository) FindAll(result *[]*pet.Pet) error { - return r.db.Model(&pet.Pet{}).Find(result).Error +func (r *Repository) FindAll(result *[]*pet.Pet, query *proto.FindAllPetRequest) error { + err := r.db.Model(&pet.Pet{}).Find(result).Error + if err != nil { + return err + } + + err = petUtils.FilterPet(result, query) + if err != nil { + return err + } + err = petUtils.PaginatePets(result, query.Page, query.PageSize) + if err != nil { + return err + } + return nil } func (r *Repository) FindOne(id string, result *pet.Pet) error { From c9a5490fd52d4ce0a5e107ebb101b538e709781e Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:50:43 +0700 Subject: [PATCH 15/40] fix: findAll --- src/app/service/pet/pet.service.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index 8a7ded1..b34dc03 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -22,11 +22,11 @@ type Service struct { } type IRepository interface { - FindAll(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 + FindAll(*[]*pet.Pet, *proto.FindAllPetRequest) error + FindOne(string, *pet.Pet) error + Create(*pet.Pet) error + Update(string, *pet.Pet) error + Delete(string) error } type ImageService interface { @@ -90,7 +90,7 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res var pets []*pet.Pet var imagesList [][]*image_proto.Image - err = s.repository.FindAll(&pets) + err = s.repository.FindAll(&pets, req) 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") @@ -103,7 +103,6 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res } imagesList = append(imagesList, images) } - fmt.Println(req) petWithImages, err := petUtils.RawToDtoList(&pets, imagesList, req) if err != nil { return nil, status.Error(codes.Internal, fmt.Sprintf("error converting raw to dto list: %v", err)) From 4fb672db9b96ee767928eea5751d3acc24e95ebe Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:51:15 +0700 Subject: [PATCH 16/40] feat: filter and paginate --- src/app/service/pet/pet.service_test.go | 23 +++++++++-- src/app/utils/pet/pet.utils.go | 54 +++++++++++++++++++------ 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index a2167b9..961b651 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -3,6 +3,7 @@ package pet import ( "context" "errors" + "fmt" "math/rand" "testing" "time" @@ -33,6 +34,7 @@ type PetServiceTest struct { ChangeViewPet *pet.Pet Pets []*pet.Pet PetDto *proto.Pet + FindAllPetReqMock *proto.FindAllPetRequest CreatePetReqMock *proto.CreatePetRequest UpdatePetReqMock *proto.UpdatePetRequest ChangeViewPetReqMock *proto.ChangeViewPetRequest @@ -119,6 +121,18 @@ func (t *PetServiceTest) SetupTest() { Images: t.Images, } + t.FindAllPetReqMock = &proto.FindAllPetRequest{ + Search: "", + Type: "", + Gender: "", + Color: "", + Pattern: "", + Age: "", + Origin: "", + PageSize: 0, + Page: 0, + } + t.UpdatePet = &pet.Pet{ Base: model.Base{ ID: t.Pet.Base.ID, @@ -333,7 +347,7 @@ func (t *PetServiceTest) TestFindAllSuccess() { var petsIn []*pet.Pet repo := &mock.RepositoryMock{} - repo.On("FindAll", petsIn).Return(&t.Pets, nil) + repo.On("FindAll", petsIn, t.FindAllPetReqMock).Return(&t.Pets, nil) imgSrv := new(img_mock.ServiceMock) for i, pet := range t.Pets { @@ -342,8 +356,11 @@ func (t *PetServiceTest) TestFindAllSuccess() { srv := NewService(repo, imgSrv) - actual, err := srv.FindAll(context.Background(), &proto.FindAllPetRequest{}) - + actual, err := srv.FindAll(context.Background(), t.FindAllPetReqMock) + fmt.Println("err: ", err) + _ = actual + _ = err + fmt.Println(actual) assert.Nil(t.T(), err) assert.Equal(t.T(), want, actual) } diff --git a/src/app/utils/pet/pet.utils.go b/src/app/utils/pet/pet.utils.go index fce37a6..8e087a2 100644 --- a/src/app/utils/pet/pet.utils.go +++ b/src/app/utils/pet/pet.utils.go @@ -2,29 +2,25 @@ package pet import ( "errors" - "fmt" "strings" "time" "github.com/google/uuid" "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/constant" petConst "github.com/isd-sgcu/johnjud-backend/src/constant/pet" proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" imageProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/file/image/v1" "gorm.io/gorm" ) -func RawToDtoList(in *[]*pet.Pet, images [][]*imageProto.Image, query *proto.FindAllPetRequest) ([]*proto.Pet, error) { - var result []*proto.Pet - if len(*in) != len(images) { - return nil, errors.New("length of in and imageUrls have to be the same") - } - - for i, p := range *in { +func FilterPet(in *[]*pet.Pet, query *proto.FindAllPetRequest) error { + var results []*pet.Pet + for _, p := range *in { res, err := filterAge(p, query.Age) if err != nil { - return nil, err + return err } if !res { continue @@ -47,6 +43,41 @@ func RawToDtoList(in *[]*pet.Pet, images [][]*imageProto.Image, query *proto.Fin if query.Origin != "" && p.Origin != query.Origin { continue } + results = append(results, p) + } + *in = results + return nil +} + +func PaginatePets(pets *[]*pet.Pet, page int32, pageSize int32) error { + if page <= 0 { + page = 1 + } + if pageSize <= 0 { + pageSize = 10 + } + start := (page - 1) * pageSize + end := start + pageSize + + if start > int32(len(*pets)) { + *pets = []*pet.Pet{} + return nil + } + if end > int32(len(*pets)) { + end = int32(len(*pets)) + } + *pets = (*pets)[start:end] + return nil +} + +func RawToDtoList(in *[]*pet.Pet, images [][]*imageProto.Image, query *proto.FindAllPetRequest) ([]*proto.Pet, error) { + var result []*proto.Pet + if len(*in) != len(images) { + return nil, errors.New("length of in and imageUrls have to be the same") + } + + for i, p := range *in { + // TODO: create new filter image function this wont work result = append(result, RawToDto(p, images[i])) } return result, nil @@ -155,9 +186,8 @@ func filterAge(pet *pet.Pet, age string) (bool, error) { currYear := time.Now() birthYear := birthdate - diff := currYear.Sub(birthYear).Hours() / petConst.HOUR / petConst.YEAR + diff := currYear.Sub(birthYear).Hours() / constant.DAY / constant.YEAR - fmt.Println(age, diff, currYear.Year()-birthYear.Year()) switch age { case "kitten": return diff < 1, nil @@ -166,6 +196,6 @@ func filterAge(pet *pet.Pet, age string) (bool, error) { case "senior": return diff >= 7, nil default: - return false, errors.New("filter age error. your age must be 'kitten' | 'adult' | 'senior' ") + return true, nil } } From a703eb95d3f501302f138bcbab6f73061ad151c5 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:51:19 +0700 Subject: [PATCH 17/40] chore --- src/constant/pet/pet.constant.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/constant/pet/pet.constant.go b/src/constant/pet/pet.constant.go index 2ad000d..77e50c7 100644 --- a/src/constant/pet/pet.constant.go +++ b/src/constant/pet/pet.constant.go @@ -13,6 +13,3 @@ const ( ADOPTED Status = "adopted" FINDHOME Status = "findhome" ) - -const HOUR = 24 -const YEAR = 365 From 5a3ab2d4c7b05b5570b5e169be2c6e7c9bbeea25 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:51:21 +0700 Subject: [PATCH 18/40] chore --- src/constant/common.constant.go | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/constant/common.constant.go diff --git a/src/constant/common.constant.go b/src/constant/common.constant.go new file mode 100644 index 0000000..1ac27a3 --- /dev/null +++ b/src/constant/common.constant.go @@ -0,0 +1,4 @@ +package constant + +const DAY = 24 +const YEAR = 365 From 5a038377ab13871db883fd359dc92ad8a5f945fe Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:51:25 +0700 Subject: [PATCH 19/40] chore --- src/mocks/pet/pet.mock.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/mocks/pet/pet.mock.go b/src/mocks/pet/pet.mock.go index d1f5d87..f7ebe9c 100644 --- a/src/mocks/pet/pet.mock.go +++ b/src/mocks/pet/pet.mock.go @@ -2,6 +2,7 @@ package pet import ( "github.com/isd-sgcu/johnjud-backend/src/app/model/pet" + proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" "github.com/stretchr/testify/mock" ) @@ -29,8 +30,8 @@ func (r *RepositoryMock) Create(in *pet.Pet) error { return args.Error(1) } -func (r *RepositoryMock) FindAll(result *[]*pet.Pet) error { - args := r.Called(*result) +func (r *RepositoryMock) FindAll(result *[]*pet.Pet, query *proto.FindAllPetRequest) error { + args := r.Called(*result, query) if args.Get(0) != nil { *result = *args.Get(0).(*[]*pet.Pet) From e80b7e32203b14305bcc496d7369267c58caf80c Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:56:59 +0700 Subject: [PATCH 20/40] chore --- src/app/service/pet/pet.service_test.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index 961b651..f4f6098 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -3,7 +3,6 @@ package pet import ( "context" "errors" - "fmt" "math/rand" "testing" "time" @@ -357,10 +356,6 @@ func (t *PetServiceTest) TestFindAllSuccess() { srv := NewService(repo, imgSrv) actual, err := srv.FindAll(context.Background(), t.FindAllPetReqMock) - fmt.Println("err: ", err) - _ = actual - _ = err - fmt.Println(actual) assert.Nil(t.T(), err) assert.Equal(t.T(), want, actual) } From 79e5ad3fe73bd493fce0ebcbffb94b561731ac23 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 11:06:35 +0700 Subject: [PATCH 21/40] fix: remove logic in repository to service --- src/app/repository/pet/pet.repository.go | 35 ++++++++++++------------ src/app/service/pet/pet.service.go | 7 +++-- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/app/repository/pet/pet.repository.go b/src/app/repository/pet/pet.repository.go index 062e2da..c0e433d 100644 --- a/src/app/repository/pet/pet.repository.go +++ b/src/app/repository/pet/pet.repository.go @@ -2,8 +2,6 @@ package pet import ( "github.com/isd-sgcu/johnjud-backend/src/app/model/pet" - petUtils "github.com/isd-sgcu/johnjud-backend/src/app/utils/pet" - proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" "gorm.io/gorm" ) @@ -15,21 +13,24 @@ func NewRepository(db *gorm.DB) *Repository { return &Repository{db: db} } -func (r *Repository) FindAll(result *[]*pet.Pet, query *proto.FindAllPetRequest) error { - err := r.db.Model(&pet.Pet{}).Find(result).Error - if err != nil { - return err - } - - err = petUtils.FilterPet(result, query) - if err != nil { - return err - } - err = petUtils.PaginatePets(result, query.Page, query.PageSize) - if err != nil { - return err - } - return nil +func (r *Repository) FindAll(result *[]*pet.Pet) error { + return r.db.Model(&pet.Pet{}).Find(result).Error + /* + err := r.db.Model(&pet.Pet{}).Find(result).Error + if err != nil { + return err + } + + err = petUtils.FilterPet(result, query) + if err != nil { + return err + } + err = petUtils.PaginatePets(result, query.Page, query.PageSize) + if err != nil { + return err + } + return nil + */ } func (r *Repository) FindOne(id string, result *pet.Pet) error { diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index b34dc03..004d524 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -22,7 +22,7 @@ type Service struct { } type IRepository interface { - FindAll(*[]*pet.Pet, *proto.FindAllPetRequest) error + FindAll(*[]*pet.Pet) error FindOne(string, *pet.Pet) error Create(*pet.Pet) error Update(string, *pet.Pet) error @@ -90,12 +90,15 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res var pets []*pet.Pet var imagesList [][]*image_proto.Image - err = s.repository.FindAll(&pets, req) + 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") } + petUtils.FilterPet(&pets, req) + petUtils.PaginatePets(&pets, req.Page, req.PageSize) + for _, pet := range pets { images, err := s.imageService.FindByPetId(pet.ID.String()) if err != nil { From 0ecdc89716f1801197a374939a13132c64e53a16 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 11:06:37 +0700 Subject: [PATCH 22/40] chore --- src/mocks/pet/pet.mock.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/mocks/pet/pet.mock.go b/src/mocks/pet/pet.mock.go index f7ebe9c..d1f5d87 100644 --- a/src/mocks/pet/pet.mock.go +++ b/src/mocks/pet/pet.mock.go @@ -2,7 +2,6 @@ package pet import ( "github.com/isd-sgcu/johnjud-backend/src/app/model/pet" - proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/backend/pet/v1" "github.com/stretchr/testify/mock" ) @@ -30,8 +29,8 @@ func (r *RepositoryMock) Create(in *pet.Pet) error { return args.Error(1) } -func (r *RepositoryMock) FindAll(result *[]*pet.Pet, query *proto.FindAllPetRequest) error { - args := r.Called(*result, query) +func (r *RepositoryMock) FindAll(result *[]*pet.Pet) error { + args := r.Called(*result) if args.Get(0) != nil { *result = *args.Get(0).(*[]*pet.Pet) From d66c9a87e512673689c9e2426b75db4623f208c8 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 11:19:48 +0700 Subject: [PATCH 23/40] chore --- src/app/service/pet/pet.service_test.go | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index f4f6098..53fc957 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -33,7 +33,6 @@ type PetServiceTest struct { ChangeViewPet *pet.Pet Pets []*pet.Pet PetDto *proto.Pet - FindAllPetReqMock *proto.FindAllPetRequest CreatePetReqMock *proto.CreatePetRequest UpdatePetReqMock *proto.UpdatePetRequest ChangeViewPetReqMock *proto.ChangeViewPetRequest @@ -120,18 +119,6 @@ func (t *PetServiceTest) SetupTest() { Images: t.Images, } - t.FindAllPetReqMock = &proto.FindAllPetRequest{ - Search: "", - Type: "", - Gender: "", - Color: "", - Pattern: "", - Age: "", - Origin: "", - PageSize: 0, - Page: 0, - } - t.UpdatePet = &pet.Pet{ Base: model.Base{ ID: t.Pet.Base.ID, @@ -346,7 +333,7 @@ func (t *PetServiceTest) TestFindAllSuccess() { var petsIn []*pet.Pet repo := &mock.RepositoryMock{} - repo.On("FindAll", petsIn, t.FindAllPetReqMock).Return(&t.Pets, nil) + repo.On("FindAll", petsIn).Return(&t.Pets, nil) imgSrv := new(img_mock.ServiceMock) for i, pet := range t.Pets { @@ -355,7 +342,7 @@ func (t *PetServiceTest) TestFindAllSuccess() { srv := NewService(repo, imgSrv) - actual, err := srv.FindAll(context.Background(), t.FindAllPetReqMock) + actual, err := srv.FindAll(context.Background(), &proto.FindAllPetRequest{}) assert.Nil(t.T(), err) assert.Equal(t.T(), want, actual) } From 206a6731750b8d72eeedac5369cd78800e9e8f54 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sat, 13 Jan 2024 14:17:15 +0700 Subject: [PATCH 24/40] chore: update proto --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 80b2093..6b1437b 100644 --- a/go.mod +++ b/go.mod @@ -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.3.0 + github.com/isd-sgcu/johnjud-go-proto v0.5.0 github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.31.0 github.com/spf13/viper v1.18.2 diff --git a/go.sum b/go.sum index 7c1b4df..cf9a865 100644 --- a/go.sum +++ b/go.sum @@ -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.3.0 h1:UsAf2l06VypAp8nC5Ksru1QeoYwRjjxP/WKJwNOm1iY= github.com/isd-sgcu/johnjud-go-proto v0.3.0/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= +github.com/isd-sgcu/johnjud-go-proto v0.5.0 h1:GgqRzWjya5p1yhfU/kpX8i4WL42+qT2TkyXZmssH6B4= +github.com/isd-sgcu/johnjud-go-proto v0.5.0/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= From f047c7fa020371375f8da06b939bedd87fe1b811 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sat, 13 Jan 2024 14:17:27 +0700 Subject: [PATCH 25/40] fix: pet remove species, isclubpet --- src/app/model/pet/pet.model.go | 2 -- src/app/service/pet/pet.service.go | 4 ---- src/app/service/pet/pet.service_test.go | 22 ---------------------- 3 files changed, 28 deletions(-) diff --git a/src/app/model/pet/pet.model.go b/src/app/model/pet/pet.model.go index 3b780cb..ceff18a 100644 --- a/src/app/model/pet/pet.model.go +++ b/src/app/model/pet/pet.model.go @@ -8,7 +8,6 @@ import ( type Pet struct { model.Base Type string `json:"type" gorm:"tinytext"` - Species string `json:"species" gorm:"tinytext"` Name string `json:"name" gorm:"tinytext"` Birthdate string `json:"birthdate" gorm:"tinytext"` Gender pet.Gender `json:"gender" gorm:"tinytext" example:"male"` @@ -20,7 +19,6 @@ type Pet struct { IsSterile bool `json:"is_sterile"` IsVaccinated bool `json:"is_vaccine"` IsVisible bool `json:"is_visible"` - IsClubPet bool `json:"is_club_pet"` Origin string `json:"origin" gorm:"tinytext"` Address string `json:"address" gorm:"tinytext"` Contact string `json:"contact" gorm:"tinytext"` diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index 7317841..2d76c38 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -184,7 +184,6 @@ 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: string(in.Gender), @@ -197,7 +196,6 @@ func RawToDto(in *pet.Pet, images []*image_proto.Image) *proto.Pet { IsSterile: in.IsSterile, IsVaccinated: in.IsVaccinated, IsVisible: in.IsVisible, - IsClubPet: in.IsClubPet, Origin: in.Origin, Address: in.Address, Contact: in.Contact, @@ -239,7 +237,6 @@ func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) { DeletedAt: gorm.DeletedAt{}, }, Type: in.Type, - Species: in.Species, Name: in.Name, Birthdate: in.Birthdate, Gender: gender, @@ -251,7 +248,6 @@ func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) { IsSterile: in.IsSterile, IsVaccinated: in.IsVaccinated, IsVisible: in.IsVisible, - IsClubPet: in.IsClubPet, Origin: in.Origin, Address: in.Address, Contact: in.Contact, diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index a2167b9..f13fabc 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -60,7 +60,6 @@ func (t *PetServiceTest) SetupTest() { DeletedAt: gorm.DeletedAt{}, }, Type: faker.Word(), - Species: faker.Word(), Name: faker.Name(), Birthdate: faker.Word(), Gender: genders[rand.Intn(2)], @@ -72,7 +71,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: true, IsVaccinated: true, IsVisible: true, - IsClubPet: true, Origin: faker.Paragraph(), Address: faker.Paragraph(), Contact: faker.Paragraph(), @@ -100,7 +98,6 @@ func (t *PetServiceTest) SetupTest() { t.PetDto = &proto.Pet{ Id: t.Pet.ID.String(), Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: string(t.Pet.Gender), @@ -112,7 +109,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -127,7 +123,6 @@ func (t *PetServiceTest) SetupTest() { DeletedAt: t.Pet.Base.DeletedAt, }, Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, @@ -139,7 +134,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -153,7 +147,6 @@ func (t *PetServiceTest) SetupTest() { DeletedAt: t.Pet.Base.DeletedAt, }, Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, @@ -165,7 +158,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: false, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -174,7 +166,6 @@ func (t *PetServiceTest) SetupTest() { t.CreatePetReqMock = &proto.CreatePetRequest{ Pet: &proto.Pet{ Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: string(t.Pet.Gender), @@ -187,7 +178,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVaccinated, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -198,7 +188,6 @@ func (t *PetServiceTest) SetupTest() { Pet: &proto.Pet{ Id: t.Pet.ID.String(), Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: string(t.Pet.Gender), @@ -211,7 +200,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -231,7 +219,6 @@ func (t *PetServiceTest) SetupTest() { DeletedAt: t.Pet.Base.DeletedAt, }, Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, @@ -243,7 +230,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -378,7 +364,6 @@ func createPets() []*pet.Pet { DeletedAt: gorm.DeletedAt{}, }, Type: faker.Word(), - Species: faker.Word(), Name: faker.Name(), Birthdate: faker.Word(), Gender: genders[rand.Intn(2)], @@ -390,7 +375,6 @@ func createPets() []*pet.Pet { IsSterile: true, IsVaccinated: true, IsVisible: true, - IsClubPet: true, Origin: faker.Paragraph(), Address: faker.Paragraph(), Contact: faker.Paragraph(), @@ -408,7 +392,6 @@ func (t *PetServiceTest) createPetsDto(in []*pet.Pet, imagesList [][]*img_proto. r := &proto.Pet{ Id: p.ID.String(), Type: p.Type, - Species: p.Species, Name: p.Name, Birthdate: p.Birthdate, Gender: string(p.Gender), @@ -421,7 +404,6 @@ func (t *PetServiceTest) createPetsDto(in []*pet.Pet, imagesList [][]*img_proto. IsSterile: p.IsSterile, IsVaccinated: p.IsVaccinated, IsVisible: p.IsVisible, - IsClubPet: p.IsClubPet, Origin: p.Origin, Address: p.Address, Contact: p.Contact, @@ -441,7 +423,6 @@ func (t *PetServiceTest) TestCreateSuccess() { in := &pet.Pet{ Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, @@ -453,7 +434,6 @@ func (t *PetServiceTest) TestCreateSuccess() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -475,7 +455,6 @@ func (t *PetServiceTest) TestCreateInternalErr() { in := &pet.Pet{ Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, @@ -487,7 +466,6 @@ func (t *PetServiceTest) TestCreateInternalErr() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, From ae4a2882d91ebf6cdb66faa04110167056188ca1 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 14:48:25 +0700 Subject: [PATCH 26/40] chore --- src/app/repository/pet/pet.repository.go | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/app/repository/pet/pet.repository.go b/src/app/repository/pet/pet.repository.go index c0e433d..0f72ae1 100644 --- a/src/app/repository/pet/pet.repository.go +++ b/src/app/repository/pet/pet.repository.go @@ -15,22 +15,6 @@ func NewRepository(db *gorm.DB) *Repository { func (r *Repository) FindAll(result *[]*pet.Pet) error { return r.db.Model(&pet.Pet{}).Find(result).Error - /* - err := r.db.Model(&pet.Pet{}).Find(result).Error - if err != nil { - return err - } - - err = petUtils.FilterPet(result, query) - if err != nil { - return err - } - err = petUtils.PaginatePets(result, query.Page, query.PageSize) - if err != nil { - return err - } - return nil - */ } func (r *Repository) FindOne(id string, result *pet.Pet) error { From e2dbcc6eef57faa8e78f086dc5b73d4bae76b11b Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sat, 13 Jan 2024 14:17:15 +0700 Subject: [PATCH 27/40] chore: update proto --- go.mod | 2 +- go.sum | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index acfaa24..6b1437b 100644 --- a/go.mod +++ b/go.mod @@ -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.4.2 + github.com/isd-sgcu/johnjud-go-proto v0.5.0 github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.31.0 github.com/spf13/viper v1.18.2 diff --git a/go.sum b/go.sum index efa2449..cf9a865 100644 --- a/go.sum +++ b/go.sum @@ -22,14 +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.3.0 h1:UsAf2l06VypAp8nC5Ksru1QeoYwRjjxP/WKJwNOm1iY= github.com/isd-sgcu/johnjud-go-proto v0.3.0/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= -github.com/isd-sgcu/johnjud-go-proto v0.3.2 h1:lOz8wwoHx71lW6YnEshNDIpnhj2IUP39Zx4lqruYzA8= -github.com/isd-sgcu/johnjud-go-proto v0.3.2/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= -github.com/isd-sgcu/johnjud-go-proto v0.4.0 h1:YMmi37Siexo84zlUEhkSnFsZtf/U+bFLxtaUW8+A8Ow= -github.com/isd-sgcu/johnjud-go-proto v0.4.0/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= -github.com/isd-sgcu/johnjud-go-proto v0.4.1 h1:CrJbGs8oy3UdKpPv2XhCIZo2cKrSeGtsR/FKHeaSPM4= -github.com/isd-sgcu/johnjud-go-proto v0.4.1/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= -github.com/isd-sgcu/johnjud-go-proto v0.4.2 h1:KepvAHrFmi9Ki5kdQTKWZ95knjxeVv78b6ru3WRjNpg= -github.com/isd-sgcu/johnjud-go-proto v0.4.2/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= +github.com/isd-sgcu/johnjud-go-proto v0.5.0 h1:GgqRzWjya5p1yhfU/kpX8i4WL42+qT2TkyXZmssH6B4= +github.com/isd-sgcu/johnjud-go-proto v0.5.0/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= From 766969d6c22aee4acfc48943527a0fe4f41111fc Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Sat, 13 Jan 2024 14:17:27 +0700 Subject: [PATCH 28/40] fix: pet remove species, isclubpet --- src/app/model/pet/pet.model.go | 2 -- src/app/service/pet/pet.service_test.go | 22 ---------------------- 2 files changed, 24 deletions(-) diff --git a/src/app/model/pet/pet.model.go b/src/app/model/pet/pet.model.go index 3b780cb..ceff18a 100644 --- a/src/app/model/pet/pet.model.go +++ b/src/app/model/pet/pet.model.go @@ -8,7 +8,6 @@ import ( type Pet struct { model.Base Type string `json:"type" gorm:"tinytext"` - Species string `json:"species" gorm:"tinytext"` Name string `json:"name" gorm:"tinytext"` Birthdate string `json:"birthdate" gorm:"tinytext"` Gender pet.Gender `json:"gender" gorm:"tinytext" example:"male"` @@ -20,7 +19,6 @@ type Pet struct { IsSterile bool `json:"is_sterile"` IsVaccinated bool `json:"is_vaccine"` IsVisible bool `json:"is_visible"` - IsClubPet bool `json:"is_club_pet"` Origin string `json:"origin" gorm:"tinytext"` Address string `json:"address" gorm:"tinytext"` Contact string `json:"contact" gorm:"tinytext"` diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index 53fc957..e0c0edd 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -60,7 +60,6 @@ func (t *PetServiceTest) SetupTest() { DeletedAt: gorm.DeletedAt{}, }, Type: faker.Word(), - Species: faker.Word(), Name: faker.Name(), Birthdate: faker.Word(), Gender: genders[rand.Intn(2)], @@ -72,7 +71,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: true, IsVaccinated: true, IsVisible: true, - IsClubPet: true, Origin: faker.Paragraph(), Address: faker.Paragraph(), Contact: faker.Paragraph(), @@ -100,7 +98,6 @@ func (t *PetServiceTest) SetupTest() { t.PetDto = &proto.Pet{ Id: t.Pet.ID.String(), Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: string(t.Pet.Gender), @@ -112,7 +109,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -127,7 +123,6 @@ func (t *PetServiceTest) SetupTest() { DeletedAt: t.Pet.Base.DeletedAt, }, Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, @@ -139,7 +134,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -153,7 +147,6 @@ func (t *PetServiceTest) SetupTest() { DeletedAt: t.Pet.Base.DeletedAt, }, Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, @@ -165,7 +158,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: false, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -174,7 +166,6 @@ func (t *PetServiceTest) SetupTest() { t.CreatePetReqMock = &proto.CreatePetRequest{ Pet: &proto.Pet{ Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: string(t.Pet.Gender), @@ -187,7 +178,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVaccinated, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -198,7 +188,6 @@ func (t *PetServiceTest) SetupTest() { Pet: &proto.Pet{ Id: t.Pet.ID.String(), Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: string(t.Pet.Gender), @@ -211,7 +200,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -231,7 +219,6 @@ func (t *PetServiceTest) SetupTest() { DeletedAt: t.Pet.Base.DeletedAt, }, Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, @@ -243,7 +230,6 @@ func (t *PetServiceTest) SetupTest() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -377,7 +363,6 @@ func createPets() []*pet.Pet { DeletedAt: gorm.DeletedAt{}, }, Type: faker.Word(), - Species: faker.Word(), Name: faker.Name(), Birthdate: faker.Word(), Gender: genders[rand.Intn(2)], @@ -389,7 +374,6 @@ func createPets() []*pet.Pet { IsSterile: true, IsVaccinated: true, IsVisible: true, - IsClubPet: true, Origin: faker.Paragraph(), Address: faker.Paragraph(), Contact: faker.Paragraph(), @@ -407,7 +391,6 @@ func (t *PetServiceTest) createPetsDto(in []*pet.Pet, imagesList [][]*img_proto. r := &proto.Pet{ Id: p.ID.String(), Type: p.Type, - Species: p.Species, Name: p.Name, Birthdate: p.Birthdate, Gender: string(p.Gender), @@ -420,7 +403,6 @@ func (t *PetServiceTest) createPetsDto(in []*pet.Pet, imagesList [][]*img_proto. IsSterile: p.IsSterile, IsVaccinated: p.IsVaccinated, IsVisible: p.IsVisible, - IsClubPet: p.IsClubPet, Origin: p.Origin, Address: p.Address, Contact: p.Contact, @@ -440,7 +422,6 @@ func (t *PetServiceTest) TestCreateSuccess() { in := &pet.Pet{ Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, @@ -452,7 +433,6 @@ func (t *PetServiceTest) TestCreateSuccess() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, @@ -474,7 +454,6 @@ func (t *PetServiceTest) TestCreateInternalErr() { in := &pet.Pet{ Type: t.Pet.Type, - Species: t.Pet.Species, Name: t.Pet.Name, Birthdate: t.Pet.Birthdate, Gender: t.Pet.Gender, @@ -486,7 +465,6 @@ func (t *PetServiceTest) TestCreateInternalErr() { IsSterile: t.Pet.IsSterile, IsVaccinated: t.Pet.IsVaccinated, IsVisible: t.Pet.IsVisible, - IsClubPet: t.Pet.IsClubPet, Origin: t.Pet.Origin, Address: t.Pet.Address, Contact: t.Pet.Contact, From f171febff792ac503afd95ce2a67057a7663581f Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 15:03:02 +0700 Subject: [PATCH 29/40] chore --- src/app/utils/pet/pet.utils.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/app/utils/pet/pet.utils.go b/src/app/utils/pet/pet.utils.go index 8e087a2..0349078 100644 --- a/src/app/utils/pet/pet.utils.go +++ b/src/app/utils/pet/pet.utils.go @@ -2,6 +2,7 @@ package pet import ( "errors" + "math" "strings" "time" @@ -25,7 +26,7 @@ func FilterPet(in *[]*pet.Pet, query *proto.FindAllPetRequest) error { if !res { continue } - if query.Search != "" && !strings.Contains(p.Name, query.Search) && !strings.Contains(p.Species, query.Search) { + if query.Search != "" && !strings.Contains(p.Name, query.Search) { continue } if query.Type != "" && p.Type != query.Type { @@ -49,24 +50,30 @@ func FilterPet(in *[]*pet.Pet, query *proto.FindAllPetRequest) error { return nil } -func PaginatePets(pets *[]*pet.Pet, page int32, pageSize int32) error { +func PaginatePets(pets *[]*pet.Pet, page int32, pageSize int32, metadata *proto.FindAllPetMetaData) error { + totalsPets := int32(len(*pets)) if page <= 0 { page = 1 } if pageSize <= 0 { - pageSize = 10 + pageSize = totalsPets } start := (page - 1) * pageSize end := start + pageSize - if start > int32(len(*pets)) { + if start > totalsPets { *pets = []*pet.Pet{} return nil } - if end > int32(len(*pets)) { - end = int32(len(*pets)) + if end > totalsPets { + end = totalsPets } *pets = (*pets)[start:end] + + metadata.Page = page + metadata.PageSize = pageSize + metadata.Total = totalsPets + metadata.TotalPages = int32(math.Ceil(float64(totalsPets / pageSize))) return nil } @@ -87,7 +94,6 @@ func RawToDto(in *pet.Pet, images []*imageProto.Image) *proto.Pet { return &proto.Pet{ Id: in.ID.String(), Type: in.Type, - Species: in.Species, Name: in.Name, Birthdate: in.Birthdate, Gender: string(in.Gender), @@ -100,7 +106,6 @@ func RawToDto(in *pet.Pet, images []*imageProto.Image) *proto.Pet { IsSterile: in.IsSterile, IsVaccinated: in.IsVaccinated, IsVisible: in.IsVisible, - IsClubPet: in.IsClubPet, Origin: in.Origin, Address: in.Address, Contact: in.Contact, @@ -142,7 +147,6 @@ func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) { DeletedAt: gorm.DeletedAt{}, }, Type: in.Type, - Species: in.Species, Name: in.Name, Birthdate: in.Birthdate, Gender: gender, @@ -154,7 +158,6 @@ func DtoToRaw(in *proto.Pet) (res *pet.Pet, err error) { IsSterile: in.IsSterile, IsVaccinated: in.IsVaccinated, IsVisible: in.IsVisible, - IsClubPet: in.IsClubPet, Origin: in.Origin, Address: in.Address, Contact: in.Contact, From a6e953bf642147126fc877cba3411626a1e1f9b7 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 15:03:08 +0700 Subject: [PATCH 30/40] feat: meta data --- src/app/service/pet/pet.service.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index 004d524..1d8ec5e 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -89,6 +89,7 @@ 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 imagesList [][]*image_proto.Image + var metaData *proto.FindAllPetMetaData err = s.repository.FindAll(&pets) if err != nil { @@ -97,7 +98,7 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res } petUtils.FilterPet(&pets, req) - petUtils.PaginatePets(&pets, req.Page, req.PageSize) + petUtils.PaginatePets(&pets, req.Page, req.PageSize, metaData) for _, pet := range pets { images, err := s.imageService.FindByPetId(pet.ID.String()) @@ -110,7 +111,7 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res if err != nil { return nil, status.Error(codes.Internal, fmt.Sprintf("error converting raw to dto list: %v", err)) } - return &proto.FindAllPetResponse{Pets: petWithImages}, nil + return &proto.FindAllPetResponse{Pets: petWithImages, Metadata: metaData}, nil } func (s Service) FindOne(_ context.Context, req *proto.FindOnePetRequest) (res *proto.FindOnePetResponse, err error) { From 4911446710c27b36d538938ca848879b37b79dd5 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 15:04:58 +0700 Subject: [PATCH 31/40] chore --- src/app/service/pet/pet.service.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/app/service/pet/pet.service.go b/src/app/service/pet/pet.service.go index 1d8ec5e..92f4fcd 100644 --- a/src/app/service/pet/pet.service.go +++ b/src/app/service/pet/pet.service.go @@ -89,7 +89,7 @@ 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 imagesList [][]*image_proto.Image - var metaData *proto.FindAllPetMetaData + metaData := proto.FindAllPetMetaData{} err = s.repository.FindAll(&pets) if err != nil { @@ -98,7 +98,7 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res } petUtils.FilterPet(&pets, req) - petUtils.PaginatePets(&pets, req.Page, req.PageSize, metaData) + petUtils.PaginatePets(&pets, req.Page, req.PageSize, &metaData) for _, pet := range pets { images, err := s.imageService.FindByPetId(pet.ID.String()) @@ -111,7 +111,7 @@ func (s *Service) FindAll(_ context.Context, req *proto.FindAllPetRequest) (res if err != nil { return nil, status.Error(codes.Internal, fmt.Sprintf("error converting raw to dto list: %v", err)) } - return &proto.FindAllPetResponse{Pets: petWithImages, Metadata: metaData}, nil + return &proto.FindAllPetResponse{Pets: petWithImages, Metadata: &metaData}, nil } func (s Service) FindOne(_ context.Context, req *proto.FindOnePetRequest) (res *proto.FindOnePetResponse, err error) { From 79fec9f3e5fdf4a3b71d6f989bce4e470dee8b66 Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 15:12:30 +0700 Subject: [PATCH 32/40] fix: test --- src/app/service/pet/pet.service_test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/app/service/pet/pet.service_test.go b/src/app/service/pet/pet.service_test.go index e0c0edd..fa75c0c 100644 --- a/src/app/service/pet/pet.service_test.go +++ b/src/app/service/pet/pet.service_test.go @@ -314,7 +314,15 @@ func (t *PetServiceTest) TestFindOneSuccess() { func (t *PetServiceTest) TestFindAllSuccess() { - want := &proto.FindAllPetResponse{Pets: t.createPetsDto(t.Pets, t.ImagesList)} + want := &proto.FindAllPetResponse{ + Pets: t.createPetsDto(t.Pets, t.ImagesList), + Metadata: &proto.FindAllPetMetaData{ + Page: 1, + TotalPages: 1, + PageSize: int32(len(t.Pets)), + Total: int32(len(t.Pets)), + }, + } var petsIn []*pet.Pet From ff668201536760529524005bf6a61b0f350ff59a Mon Sep 17 00:00:00 2001 From: Gear <84141000+macgeargear@users.noreply.github.com> Date: Sat, 13 Jan 2024 15:47:19 +0700 Subject: [PATCH 33/40] fix: change type to float64 before divide --- src/app/utils/pet/pet.utils.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/app/utils/pet/pet.utils.go b/src/app/utils/pet/pet.utils.go index 0349078..8708acb 100644 --- a/src/app/utils/pet/pet.utils.go +++ b/src/app/utils/pet/pet.utils.go @@ -70,10 +70,12 @@ func PaginatePets(pets *[]*pet.Pet, page int32, pageSize int32, metadata *proto. } *pets = (*pets)[start:end] + totalPages := int32(math.Ceil(float64(totalsPets) / float64(pageSize))) + metadata.Page = page metadata.PageSize = pageSize metadata.Total = totalsPets - metadata.TotalPages = int32(math.Ceil(float64(totalsPets / pageSize))) + metadata.TotalPages = totalPages return nil } From b41547bda1a215cd1714f3a2649831e4496fa8a3 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Mon, 15 Jan 2024 17:10:25 +0700 Subject: [PATCH 34/40] feat: new env --- .env.file.template | 10 ++++++++++ .env.template | 6 ++++++ .gitignore | 1 + 3 files changed, 17 insertions(+) create mode 100644 .env.file.template create mode 100644 .env.template diff --git a/.env.file.template b/.env.file.template new file mode 100644 index 0000000..50b0486 --- /dev/null +++ b/.env.file.template @@ -0,0 +1,10 @@ +APP_PORT=3004 +APP_ENV=development + +DB_URL=postgres://root:root@localhost:5432/johnjud_db + +BUCKET_ENDPOINT=BUCKET_ENDPOINT +BUCKET_ACCESS_KEY=BUCKET_ACCESS_KEY +BUCKET_SECRET_KEY=BUCKET_SECRET_KEY +BUCKET_NAME=johnjud-pet-images +BUCKET_USE_SSL=false \ No newline at end of file diff --git a/.env.template b/.env.template new file mode 100644 index 0000000..e115817 --- /dev/null +++ b/.env.template @@ -0,0 +1,6 @@ +APP_PORT=3003 +APP_ENV=development + +DB_URL=postgres://root:root@localhost:5432/johnjud_db + +SERVICE_FILE=localhost:3004 diff --git a/.gitignore b/.gitignore index e921962..572dda1 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ config.yaml .env .env.prod +.env.file # idea .idea From 94ea2c3ab750ba738f78d9d7629a5ffe41a505dd Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Mon, 15 Jan 2024 17:10:42 +0700 Subject: [PATCH 35/40] fix: config --- go.mod | 1 - go.sum | 3 - src/config/config.go | 113 ++++++++++++++++++++------ src/database/postgresql.connection.go | 7 +- src/main.go | 2 +- 5 files changed, 90 insertions(+), 36 deletions(-) diff --git a/go.mod b/go.mod index 6b1437b..44f35c6 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ require ( github.com/bxcodec/faker/v3 v3.8.1 github.com/google/uuid v1.5.0 github.com/isd-sgcu/johnjud-go-proto v0.5.0 - 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 diff --git a/go.sum b/go.sum index cf9a865..a5d4841 100644 --- a/go.sum +++ b/go.sum @@ -20,8 +20,6 @@ github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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.3.0 h1:UsAf2l06VypAp8nC5Ksru1QeoYwRjjxP/WKJwNOm1iY= -github.com/isd-sgcu/johnjud-go-proto v0.3.0/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= github.com/isd-sgcu/johnjud-go-proto v0.5.0 h1:GgqRzWjya5p1yhfU/kpX8i4WL42+qT2TkyXZmssH6B4= github.com/isd-sgcu/johnjud-go-proto v0.5.0/go.mod h1:1OK6aiCgtXQiLhxp0r6iLEejYIRpckWQZDrCZ9Trbo4= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= @@ -49,7 +47,6 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= diff --git a/src/config/config.go b/src/config/config.go index fc6876a..8be6537 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -1,50 +1,113 @@ +// package config + +// import ( +// "github.com/pkg/errors" +// "github.com/spf13/viper" +// ) + +// type Database struct { +// Host string `mapstructure:"host"` +// Port int `mapstructure:"port"` +// Name string `mapstructure:"name"` +// Username string `mapstructure:"username"` +// Password string `mapstructure:"password"` +// SSL string `mapstructure:"ssl"` +// } + +// type App struct { +// Port int `mapstructure:"port"` +// Debug bool `mapstructure:"debug"` +// } + +// type Service struct { +// File string `mapstructure:"file"` +// } + +// type Config struct { +// App App `mapstructure:"app"` +// Database Database `mapstructure:"database"` +// Service Service `mapstructure:"service"` +// } + +// func LoadConfig() (config *Config, err error) { +// viper.AddConfigPath("./config") +// viper.SetConfigName("config") +// viper.SetConfigType("yaml") + +// viper.AutomaticEnv() + +// err = viper.ReadInConfig() +// if err != nil { +// return nil, errors.Wrap(err, "error occurs while reading the config") +// } + +// err = viper.Unmarshal(&config) +// if err != nil { +// return nil, errors.Wrap(err, "error occurs while unmarshal the config") +// } + +// return +// } + package config import ( - "github.com/pkg/errors" + "github.com/rs/zerolog/log" "github.com/spf13/viper" ) type Database struct { - Host string `mapstructure:"host"` - Port int `mapstructure:"port"` - Name string `mapstructure:"name"` - Username string `mapstructure:"username"` - Password string `mapstructure:"password"` - SSL string `mapstructure:"ssl"` + Url string `mapstructure:"db_url"` } type App struct { - Port int `mapstructure:"port"` - Debug bool `mapstructure:"debug"` + Port int `mapstructure:"app_port"` + Env string `mapstructure:"app_env"` } type Service struct { - File string `mapstructure:"file"` + File string `mapstructure:"service_file"` } type Config struct { - App App `mapstructure:"app"` - Database Database `mapstructure:"database"` - Service Service `mapstructure:"service"` + App App + Database Database + Service Service } -func LoadConfig() (config *Config, err error) { - viper.AddConfigPath("./config") - viper.SetConfigName("config") - viper.SetConfigType("yaml") +func LoadConfig() (*Config, error) { + viper.SetConfigFile(".env") + err := viper.ReadInConfig() + if err != nil { + log.Fatal().Err(err). + Str("service", "file"). + Msg("Failed to load .env file") + } - viper.AutomaticEnv() + var dbConfig Database + if err := viper.Unmarshal(&dbConfig); err != nil { + return nil, err + } - err = viper.ReadInConfig() - if err != nil { - return nil, errors.Wrap(err, "error occurs while reading the config") + var appConfig App + if err := viper.Unmarshal(&appConfig); err != nil { + return nil, err } - err = viper.Unmarshal(&config) - if err != nil { - return nil, errors.Wrap(err, "error occurs while unmarshal the config") + var serviceConfig Service + if err := viper.Unmarshal(&serviceConfig); err != nil { + return nil, err + } + + config := &Config{ + Database: dbConfig, + App: appConfig, + Service: serviceConfig, } - return + return config, nil +} + +func (ac *App) IsDevelopment() bool { + return ac.Env == "development" } diff --git a/src/database/postgresql.connection.go b/src/database/postgresql.connection.go index 8369137..2acc6de 100644 --- a/src/database/postgresql.connection.go +++ b/src/database/postgresql.connection.go @@ -1,9 +1,6 @@ package database import ( - "fmt" - "strconv" - "github.com/isd-sgcu/johnjud-backend/src/app/model/like" "github.com/isd-sgcu/johnjud-backend/src/app/model/pet" "github.com/isd-sgcu/johnjud-backend/src/app/model/user" @@ -14,15 +11,13 @@ import ( ) func InitPostgresDatabase(conf *config.Database, isDebug bool) (db *gorm.DB, err error) { - dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s", conf.Host, strconv.Itoa(conf.Port), conf.Username, conf.Password, conf.Name, conf.SSL) - gormConf := &gorm.Config{} if !isDebug { gormConf.Logger = gormLogger.Default.LogMode(gormLogger.Silent) } - db, err = gorm.Open(postgres.Open(dsn), gormConf) + db, err = gorm.Open(postgres.Open(conf.Url), gormConf) if err != nil { return nil, err } diff --git a/src/main.go b/src/main.go index e23b1b4..7ee7b44 100644 --- a/src/main.go +++ b/src/main.go @@ -93,7 +93,7 @@ func main() { Msg("Failed to load config") } - db, err := database.InitPostgresDatabase(&conf.Database, conf.App.Debug) + db, err := database.InitPostgresDatabase(&conf.Database, conf.App.IsDevelopment()) if err != nil { log.Fatal(). Err(err). From 185a3bbadd76227c58d4c0fbf6c74e6726a166d8 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 16 Jan 2024 23:04:56 +0700 Subject: [PATCH 36/40] feat: load env viper --- Makefile | 2 +- go.mod | 2 +- go.sum | 4 +- src/config/config.go | 92 +++++++++++--------------------------------- tools/export-env.sh | 16 ++++++++ 5 files changed, 42 insertions(+), 74 deletions(-) create mode 100644 tools/export-env.sh diff --git a/Makefile b/Makefile index bc834cc..0dce9f5 100644 --- a/Makefile +++ b/Makefile @@ -13,4 +13,4 @@ test: go tool cover -html=coverage.out -o coverage.html server: - go run ./src/. + . ./tools/export-env.sh ; go run ./src/. diff --git a/go.mod b/go.mod index 44f35c6..ba7e136 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/google/uuid v1.5.0 github.com/isd-sgcu/johnjud-go-proto v0.5.0 github.com/rs/zerolog v1.31.0 - github.com/spf13/viper v1.18.2 + github.com/spf13/viper v1.18.1 github.com/stretchr/testify v1.8.4 google.golang.org/grpc v1.60.1 gorm.io/driver/postgres v1.5.4 diff --git a/go.sum b/go.sum index a5d4841..458a30d 100644 --- a/go.sum +++ b/go.sum @@ -68,8 +68,8 @@ github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= -github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= +github.com/spf13/viper v1.18.1 h1:rmuU42rScKWlhhJDyXZRKJQHXFX02chSVW1IvkPGiVM= +github.com/spf13/viper v1.18.1/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= diff --git a/src/config/config.go b/src/config/config.go index 8be6537..9992687 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -1,72 +1,20 @@ -// package config - -// import ( -// "github.com/pkg/errors" -// "github.com/spf13/viper" -// ) - -// type Database struct { -// Host string `mapstructure:"host"` -// Port int `mapstructure:"port"` -// Name string `mapstructure:"name"` -// Username string `mapstructure:"username"` -// Password string `mapstructure:"password"` -// SSL string `mapstructure:"ssl"` -// } - -// type App struct { -// Port int `mapstructure:"port"` -// Debug bool `mapstructure:"debug"` -// } - -// type Service struct { -// File string `mapstructure:"file"` -// } - -// type Config struct { -// App App `mapstructure:"app"` -// Database Database `mapstructure:"database"` -// Service Service `mapstructure:"service"` -// } - -// func LoadConfig() (config *Config, err error) { -// viper.AddConfigPath("./config") -// viper.SetConfigName("config") -// viper.SetConfigType("yaml") - -// viper.AutomaticEnv() - -// err = viper.ReadInConfig() -// if err != nil { -// return nil, errors.Wrap(err, "error occurs while reading the config") -// } - -// err = viper.Unmarshal(&config) -// if err != nil { -// return nil, errors.Wrap(err, "error occurs while unmarshal the config") -// } - -// return -// } - package config import ( - "github.com/rs/zerolog/log" "github.com/spf13/viper" ) type Database struct { - Url string `mapstructure:"db_url"` + Url string `mapstructure:"URL"` } type App struct { - Port int `mapstructure:"app_port"` - Env string `mapstructure:"app_env"` + Port int `mapstructure:"PORT"` + Env string `mapstructure:"ENV"` } type Service struct { - File string `mapstructure:"service_file"` + File string `mapstructure:"FILE"` } type Config struct { @@ -76,26 +24,30 @@ type Config struct { } func LoadConfig() (*Config, error) { - viper.SetConfigFile(".env") - err := viper.ReadInConfig() - if err != nil { - log.Fatal().Err(err). - Str("service", "file"). - Msg("Failed to load .env file") - } - - var dbConfig Database - if err := viper.Unmarshal(&dbConfig); err != nil { + dbCfgLdr := viper.New() + dbCfgLdr.SetEnvPrefix("DB") + dbCfgLdr.AutomaticEnv() + dbCfgLdr.AllowEmptyEnv(false) + dbConfig := Database{} + if err := dbCfgLdr.Unmarshal(&dbConfig); err != nil { return nil, err } - var appConfig App - if err := viper.Unmarshal(&appConfig); err != nil { + appCfgLdr := viper.New() + appCfgLdr.SetEnvPrefix("APP") + appCfgLdr.AutomaticEnv() + dbCfgLdr.AllowEmptyEnv(false) + appConfig := App{} + if err := appCfgLdr.Unmarshal(&appConfig); err != nil { return nil, err } - var serviceConfig Service - if err := viper.Unmarshal(&serviceConfig); err != nil { + serviceCfgLdr := viper.New() + serviceCfgLdr.SetEnvPrefix("SERVICE") + serviceCfgLdr.AutomaticEnv() + dbCfgLdr.AllowEmptyEnv(false) + serviceConfig := Service{} + if err := serviceCfgLdr.Unmarshal(&serviceConfig); err != nil { return nil, err } diff --git a/tools/export-env.sh b/tools/export-env.sh new file mode 100644 index 0000000..39688ee --- /dev/null +++ b/tools/export-env.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +## Usage: +## . ./export-env.sh ; $COMMAND +## . ./export-env.sh ; echo ${MINIENTREGA_FECHALIMITE} + +unamestr=$(uname) +if [ "$unamestr" = 'Linux' ]; then + + export $(grep -v '^#' .env | xargs -d '\n') + +elif [ "$unamestr" = 'FreeBSD' ] || [ "$unamestr" = 'Darwin' ]; then + + export $(grep -v '^#' .env | xargs -0) + +fi \ No newline at end of file From 14b235b13b7f199b9e038417fdbeabd57fc7fc12 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 16 Jan 2024 23:05:10 +0700 Subject: [PATCH 37/40] fix: compose --- docker-compose.yaml | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 707a72e..98edf91 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -7,10 +7,19 @@ services: depends_on: - local-db restart: unless-stopped - volumes: - - ./config/file:/app/config + environment: + - APP_PORT=3004 + - APP_ENV=development + - DB_URL=postgres://root:root@johnjud-local-db:5432/johnjud_db + - BUCKET_ENDPOINT=BUCKET_ENDPOINT + - BUCKET_ACCESS_KEY=BUCKET_ACCESS_KEY + - BUCKET_SECRET_KEY=BUCKET_SECRET_KEY + - BUCKET_NAME=johnjud-pet-images + - BUCKET_USE_SSL=false ports: - "3004:3004" + networks: + - johnjud local-db: image: postgres:15.1-alpine3.17 @@ -24,6 +33,10 @@ services: - postgres:/var/lib/postgresql/data ports: - "5432:5432" + networks: + - johnjud volumes: - postgres: \ No newline at end of file + postgres: +networks: + johnjud: \ No newline at end of file From 5b2ad0a031cc33ffb688b1b0ee623e291e28485f Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 16 Jan 2024 23:16:36 +0700 Subject: [PATCH 38/40] feat: prod compose --- docker-compose.prod.yaml | 55 ++++++++++++++++++++++++++++++++++++++++ docker-compose.yaml | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 docker-compose.prod.yaml diff --git a/docker-compose.prod.yaml b/docker-compose.prod.yaml new file mode 100644 index 0000000..8d80e27 --- /dev/null +++ b/docker-compose.prod.yaml @@ -0,0 +1,55 @@ +version: "3.9" + +services: + backend: + container_name: johnjud-backend + restart: unless-stopped + build: . + ports: + - 3003:3003 + environment: + - APP_PORT=3003 + - APP_ENV=production + - DB_URL=postgres://root:root@johnjud-local-db:5432/johnjud_db + - SERVICE_FILE=localhost:3004 + networks: + - johnjud + local-file: + image: ghcr.io/isd-sgcu/johnjud-file + container_name: johnjud-file + depends_on: + - local-db + restart: unless-stopped + environment: + - APP_PORT=3004 + - APP_ENV=production + - DB_URL=postgres://root:root@johnjud-local-db:5432/johnjud_db + - BUCKET_ENDPOINT=BUCKET_ENDPOINT + - BUCKET_ACCESS_KEY=BUCKET_ACCESS_KEY + - BUCKET_SECRET_KEY=BUCKET_SECRET_KEY + - BUCKET_NAME=johnjud-pet-images + - BUCKET_USE_SSL=false + ports: + - "3004:3004" + networks: + - johnjud + + local-db: + image: postgres:15.1-alpine3.17 + container_name: johnjud-local-db + restart: unless-stopped + environment: + POSTGRES_USER: root + POSTGRES_PASSWORD: root + POSTGRES_DB: johnjud_db + volumes: + - postgres:/var/lib/postgresql/data + ports: + - "5432:5432" + networks: + - johnjud + +volumes: + postgres: +networks: + johnjud: \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 98edf91..c40bdfe 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -3,7 +3,7 @@ version: "3.9" services: local-file: image: ghcr.io/isd-sgcu/johnjud-file - container_name: file + container_name: johnjud-file depends_on: - local-db restart: unless-stopped From 42237bcc89f406269c6c3254227367f4b3096a88 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 16 Jan 2024 23:16:43 +0700 Subject: [PATCH 39/40] fix: remove config --- .env.file.template | 10 ---------- config/config.example.yaml | 13 ------------- config/file/config.example.yaml | 14 -------------- 3 files changed, 37 deletions(-) delete mode 100644 .env.file.template delete mode 100644 config/config.example.yaml delete mode 100644 config/file/config.example.yaml diff --git a/.env.file.template b/.env.file.template deleted file mode 100644 index 50b0486..0000000 --- a/.env.file.template +++ /dev/null @@ -1,10 +0,0 @@ -APP_PORT=3004 -APP_ENV=development - -DB_URL=postgres://root:root@localhost:5432/johnjud_db - -BUCKET_ENDPOINT=BUCKET_ENDPOINT -BUCKET_ACCESS_KEY=BUCKET_ACCESS_KEY -BUCKET_SECRET_KEY=BUCKET_SECRET_KEY -BUCKET_NAME=johnjud-pet-images -BUCKET_USE_SSL=false \ No newline at end of file diff --git a/config/config.example.yaml b/config/config.example.yaml deleted file mode 100644 index 34bc114..0000000 --- a/config/config.example.yaml +++ /dev/null @@ -1,13 +0,0 @@ -app: - port: 3003 - debug: true - -database: - host: localhost - port: 5432 - name: johnjud_db - username: root - password: root - -service: - file: localhost:3004 \ No newline at end of file diff --git a/config/file/config.example.yaml b/config/file/config.example.yaml deleted file mode 100644 index f1ecd10..0000000 --- a/config/file/config.example.yaml +++ /dev/null @@ -1,14 +0,0 @@ -app: - port: 3004 - debug: true - -database: - host: local-db - port: 5432 - name: johnjud_db - username: root - password: root - -s3: - bucket_name: - region: \ No newline at end of file From dfd729982d4f8b79c8aa470aff8c632bad574aa3 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Tue, 16 Jan 2024 23:49:57 +0700 Subject: [PATCH 40/40] fix:rename compose --- docker-compose.prod.yaml => docker-compose-prod.yaml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename docker-compose.prod.yaml => docker-compose-prod.yaml (100%) diff --git a/docker-compose.prod.yaml b/docker-compose-prod.yaml similarity index 100% rename from docker-compose.prod.yaml rename to docker-compose-prod.yaml