Skip to content

Commit

Permalink
refactor: streamline and use sqlc for breed
Browse files Browse the repository at this point in the history
  • Loading branch information
litsynp committed Apr 27, 2024
1 parent 9346fc3 commit 7254939
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 255 deletions.
69 changes: 36 additions & 33 deletions cmd/import_breeds/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"context"
"database/sql"
"errors"
"flag"
"log"

utils "github.com/pet-sitter/pets-next-door-api/internal/common"
databasegen "github.com/pet-sitter/pets-next-door-api/internal/infra/database/gen"

"github.com/pet-sitter/pets-next-door-api/internal/domain/breed"
"github.com/pet-sitter/pets-next-door-api/internal/domain/commonvo"

Expand All @@ -15,7 +16,6 @@ import (
pnd "github.com/pet-sitter/pets-next-door-api/api"
"github.com/pet-sitter/pets-next-door-api/internal/configs"
"github.com/pet-sitter/pets-next-door-api/internal/infra/database"
"github.com/pet-sitter/pets-next-door-api/internal/postgres"
)

func main() {
Expand Down Expand Up @@ -93,45 +93,48 @@ func parseFlags() Flags {

func importBreed(
ctx context.Context, conn *database.DB, petType commonvo.PetType, row breedsimporterservice.Row,
) (*breed.Breed, *pnd.AppError) {
) (*breed.DetailView, *pnd.AppError) {
log.Printf("Importing breed with pet_type: %s, name: %s to database", petType, row.Breed)

var breedData *breed.Breed
err := database.WithTransaction(ctx, conn, func(tx *database.Tx) *pnd.AppError {
existing, err := postgres.FindBreedByPetTypeAndName(ctx, tx, petType, row.Breed)
if err != nil && !errors.Is(err.Err, sql.ErrNoRows) {
return err
}

if existing != nil {
log.Printf(
"Breed with id: %d, pet_type: %s, name: %s already exists in database",
existing.ID,
existing.PetType,
existing.Name,
)
breedData = existing
return nil
}

breedData, err = postgres.CreateBreed(ctx, tx, &breed.Breed{PetType: petType, Name: row.Breed})
if err != nil {
return err
}
existingList, err := databasegen.New(conn).FindBreeds(ctx, databasegen.FindBreedsParams{
PetType: utils.StrToNullStr(petType.String()),
Name: utils.StrToNullStr(row.Breed),
})
if err != nil {
return nil, pnd.FromPostgresError(err)
}

if len(existingList) > 1 {
existing := existingList[0]
log.Printf(
"Succeeded to import breed with id: %d, pet_type: %s, name: %s to database",
breedData.ID,
breedData.PetType,
breedData.Name,
"Breed with id: %d, pet_type: %s, name: %s already exists in database",
existing.ID,
existing.PetType,
existing.Name,
)
return nil
return breed.ToDetailViewFromRows(existing), nil
}

breedData, err := databasegen.New(conn).CreateBreed(ctx, databasegen.CreateBreedParams{
Name: row.Breed,
PetType: petType.String(),
})
if err != nil {
return nil, err
return nil, pnd.FromPostgresError(err)
}

return breedData, nil
log.Printf(
"Succeeded to import breed with id: %d, pet_type: %s, name: %s to database",
breedData.ID,
breedData.PetType,
breedData.Name,
)

return &breed.DetailView{
ID: int64(breedData.ID),
PetType: commonvo.PetType(breedData.PetType),
Name: breedData.Name,
}, nil
}

func importBreeds(ctx context.Context, conn *database.DB, petType commonvo.PetType, rows *[]breedsimporterservice.Row) {
Expand Down
10 changes: 8 additions & 2 deletions cmd/server/handler/breed_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package handler
import (
"net/http"

"github.com/pet-sitter/pets-next-door-api/internal/domain/breed"

"github.com/labstack/echo/v4"
pnd "github.com/pet-sitter/pets-next-door-api/api"
"github.com/pet-sitter/pets-next-door-api/internal/service"
Expand All @@ -25,7 +27,7 @@ func NewBreedHandler(breedService service.BreedService) *BreedHandler {
// @Param page query int false "페이지 번호" default(1)
// @Param size query int false "페이지 사이즈" default(20)
// @Param pet_type query string false "펫 종류" Enums(dog, cat)
// @Success 200 {object} breed.BreedListView
// @Success 200 {object} breed.ListView
// @Router /breeds [get]
func (h *BreedHandler) FindBreeds(c echo.Context) error {
petType := pnd.ParseOptionalStringQuery(c, "pet_type")
Expand All @@ -34,7 +36,11 @@ func (h *BreedHandler) FindBreeds(c echo.Context) error {
return c.JSON(err.StatusCode, err)
}

res, err := h.breedService.FindBreeds(c.Request().Context(), page, size, petType)
res, err := h.breedService.FindBreeds(c.Request().Context(), &breed.FindBreedsParams{
Page: page,
Size: size,
PetType: petType,
})
if err != nil {
return c.JSON(err.StatusCode, err)
}
Expand Down
37 changes: 0 additions & 37 deletions internal/domain/breed/model.go

This file was deleted.

23 changes: 23 additions & 0 deletions internal/domain/breed/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package breed

import (
utils "github.com/pet-sitter/pets-next-door-api/internal/common"
databasegen "github.com/pet-sitter/pets-next-door-api/internal/infra/database/gen"
)

type FindBreedsParams struct {
Page int
Size int
PetType *string
IncludeDeleted bool
}

func (p *FindBreedsParams) ToDBParams() databasegen.FindBreedsParams {
pagination := utils.OffsetAndLimit(p.Page, p.Size)
return databasegen.FindBreedsParams{
Limit: int32(pagination.Limit),
Offset: int32(pagination.Offset),
PetType: utils.StrPtrToNullStr(p.PetType),
IncludeDeleted: p.IncludeDeleted,
}
}
36 changes: 19 additions & 17 deletions internal/domain/breed/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,33 @@ package breed
import (
pnd "github.com/pet-sitter/pets-next-door-api/api"
"github.com/pet-sitter/pets-next-door-api/internal/domain/commonvo"
databasegen "github.com/pet-sitter/pets-next-door-api/internal/infra/database/gen"
)

type BreedView struct {
ID int `json:"id"`
type DetailView struct {
ID int64 `json:"id"`
PetType commonvo.PetType `json:"petType"`
Name string `json:"name"`
}

type BreedListView struct {
*pnd.PaginatedView[*BreedView]
func ToDetailViewFromRows(row databasegen.FindBreedsRow) *DetailView {
return &DetailView{
ID: int64(row.ID),
PetType: commonvo.PetType(row.PetType),
Name: row.Name,
}
}

func (breeds *BreedList) ToBreedListView() *BreedListView {
breedViews := make([]*BreedView, len(breeds.Items))
for i, breed := range breeds.Items {
breedViews[i] = &BreedView{
ID: breed.ID,
PetType: breed.PetType,
Name: breed.Name,
}
}
type ListView struct {
*pnd.PaginatedView[*DetailView]
}

return &BreedListView{
PaginatedView: pnd.NewPaginatedView(
breeds.Page, breeds.Size, breeds.IsLastPage, breedViews,
),
func ToListViewFromRows(page, size int, rows []databasegen.FindBreedsRow) *ListView {
bl := &ListView{PaginatedView: pnd.NewPaginatedView(page, size, false, make([]*DetailView, len(rows)))}
for i, row := range rows {
bl.Items[i] = ToDetailViewFromRows(row)
}

bl.CalcLastPage()
return bl
}
4 changes: 4 additions & 0 deletions internal/domain/commonvo/vo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ const (
PetTypeDog PetType = "dog"
PetTypeCat PetType = "cat"
)

func (p *PetType) String() string {
return string(*p)
}
112 changes: 112 additions & 0 deletions internal/infra/database/gen/breeds.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7254939

Please sign in to comment.