Skip to content

Commit

Permalink
refactor: streamline and use sqlc for SOS condition and SOS post cond…
Browse files Browse the repository at this point in the history
…ition
  • Loading branch information
litsynp committed Apr 27, 2024
1 parent 5ba3190 commit 3a1d348
Show file tree
Hide file tree
Showing 17 changed files with 433 additions and 257 deletions.
27 changes: 11 additions & 16 deletions cmd/import_conditions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"context"
"log"

pnd "github.com/pet-sitter/pets-next-door-api/api"
"github.com/pet-sitter/pets-next-door-api/internal/service"

"github.com/pet-sitter/pets-next-door-api/internal/configs"
"github.com/pet-sitter/pets-next-door-api/internal/domain/sospost"
"github.com/pet-sitter/pets-next-door-api/internal/infra/database"
"github.com/pet-sitter/pets-next-door-api/internal/postgres"
)

func main() {
Expand All @@ -19,22 +18,18 @@ func main() {
log.Fatalf("error opening database: %v\n", err)
}

var result string
var err2 *pnd.AppError

ctx := context.Background()
err2 = database.WithTransaction(ctx, db, func(tx *database.Tx) *pnd.AppError {
result, err2 = postgres.InitConditions(ctx, tx, sospost.ConditionName)
if err2 != nil {
return err2
}

return nil
})

conditionService := service.NewSOSConditionService(db)
conditionList, err2 := conditionService.InitConditions(ctx)
if err2 != nil {
log.Fatalf("error initializing condition: %v\n", err2)
log.Fatalf("error initializing conditions: %v\n", err2)
}

log.Println("Total conditions imported: ", len(conditionList))
for _, condition := range conditionList {
log.Println("Condition ID: ", condition.ID, "Condition Name: ", condition.Name)
}

log.Println(result)
log.Println("Finished importing condition")
}
6 changes: 3 additions & 3 deletions cmd/server/handler/condition_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
)

type ConditionHandler struct {
conditionService service.ConditionService
conditionService service.SOSConditionService
}

func NewConditionHandler(conditionService service.ConditionService) *ConditionHandler {
func NewConditionHandler(conditionService service.SOSConditionService) *ConditionHandler {
return &ConditionHandler{conditionService: conditionService}
}

Expand All @@ -22,7 +22,7 @@ func NewConditionHandler(conditionService service.ConditionService) *ConditionHa
// @Tags posts
// @Accept json
// @Produce json
// @Success 200 {object} []sospost.ConditionView
// @Success 200 {object} soscondition.ListView
// @Router /posts/sos/conditions [get]
func (h *ConditionHandler) FindConditions(c echo.Context) error {
res, err := h.conditionService.FindConditions(c.Request().Context())
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewRouter(app *firebaseinfra.FirebaseApp) (*echo.Echo, error) {
authService := service.NewFirebaseBearerAuthService(authClient, userService)
breedService := service.NewBreedService(db)
sosPostService := service.NewSOSPostService(db)
conditionService := service.NewConditionService(db)
conditionService := service.NewSOSConditionService(db)

// Initialize handlers
authHandler := handler.NewAuthHandler(authService, kakaoinfra.NewKakaoDefaultClient())
Expand Down
4 changes: 4 additions & 0 deletions internal/common/null.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func NullStrToStrPtr(val sql.NullString) *string {
return nil
}

func NullStrToStr(val sql.NullString) string {
return val.String
}

func StrPtrToNullStr(val *string) sql.NullString {
return sql.NullString{
String: DerefOrEmpty(val),
Expand Down
25 changes: 25 additions & 0 deletions internal/domain/soscondition/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package soscondition

type ViewForSOSPost struct {
ID int `field:"id"`
Name string `field:"name"`
CreatedAt string `field:"created_at"`
UpdatedAt string `field:"update_at"`
DeletedAt string `field:"deleted_at"`
}

type ViewListForSOSPost []*ViewForSOSPost

type AvailableName string

const (
CCTVPermission AvailableName = "CCTV, 펫캠 촬영 동의"
IDVerification AvailableName = "신분증 인증"
PhonePermission AvailableName = "사전 통화 가능 여부"
)

func (c AvailableName) String() string {
return string(c)
}

var AvailableNames = []AvailableName{CCTVPermission, IDVerification, PhonePermission}
73 changes: 73 additions & 0 deletions internal/domain/soscondition/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package soscondition

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 DetailView struct {
ID int64 `json:"id"`
Name string `json:"name"`
}

func ToDetailView(row databasegen.SosCondition) *DetailView {
return &DetailView{
ID: int64(row.ID),
Name: utils.NullStrToStr(row.Name),
}
}

func ToDetailViewFromRows(row databasegen.SosCondition) *DetailView {
return &DetailView{
ID: int64(row.ID),
Name: utils.NullStrToStr(row.Name),
}
}

func ToDetailViewFromSOSPostCondition(row databasegen.FindSOSPostConditionsRow) *DetailView {
return &DetailView{
ID: int64(row.ID),
Name: utils.NullStrToStr(row.Name),
}
}

func ToDetailViewFromViewForSOSPost(view ViewForSOSPost) *DetailView {
return &DetailView{
ID: int64(view.ID),
Name: view.Name,
}
}

type ListView []*DetailView

func ToListView(row []databasegen.SosCondition) ListView {
conditionViews := make(ListView, len(row))
for i, condition := range row {
conditionViews[i] = ToDetailView(condition)
}
return conditionViews
}

func ToListViewFromRows(rows []databasegen.SosCondition) ListView {
conditionViews := make(ListView, len(rows))
for i, row := range rows {
conditionViews[i] = ToDetailViewFromRows(row)
}
return conditionViews
}

func ToListViewFromSOSPostConditions(rows []databasegen.FindSOSPostConditionsRow) ListView {
conditionViews := make(ListView, len(rows))
for i, row := range rows {
conditionViews[i] = ToDetailViewFromSOSPostCondition(row)
}
return conditionViews
}

func ToListViewFromViewForSOSPost(views ViewListForSOSPost) ListView {
conditionViews := make(ListView, len(views))
for i, view := range views {
conditionViews[i] = ToDetailViewFromViewForSOSPost(*view)
}
return conditionViews
}
34 changes: 0 additions & 34 deletions internal/domain/sospost/condition.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"time"

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

pnd "github.com/pet-sitter/pets-next-door-api/api"
"github.com/pet-sitter/pets-next-door-api/internal/domain/media"
"github.com/pet-sitter/pets-next-door-api/internal/domain/pet"
Expand Down Expand Up @@ -53,22 +55,22 @@ type SOSPostList struct {
}

type SOSPostInfo struct {
ID int `field:"id" json:"id"`
AuthorID int `field:"author" json:"author"`
Title string `field:"title" json:"title"`
Content string `field:"content" json:"content"`
Media media.ViewListForSOSPost `field:"media" json:"media"`
Conditions ConditionList `field:"conditions" json:"conditions"`
Pets pet.ViewListForSOSPost `field:"pets" json:"pets"`
Reward string `field:"reward" json:"reward"`
Dates SOSDatesList `field:"dates" json:"dates"`
CareType CareType `field:"careType" json:"careType"`
CarerGender CarerGender `field:"carerGender" json:"carerGender"`
RewardType RewardType `field:"rewardType" json:"rewardType"`
ThumbnailID int64 `field:"thumbnailId" json:"thumbnailId"`
CreatedAt time.Time `field:"createdAt" json:"createdAt"`
UpdatedAt time.Time `field:"updatedAt" json:"updatedAt"`
DeletedAt time.Time `field:"deletedAt" json:"deletedAt"`
ID int `field:"id" json:"id"`
AuthorID int `field:"author" json:"author"`
Title string `field:"title" json:"title"`
Content string `field:"content" json:"content"`
Media media.ViewListForSOSPost `field:"media" json:"media"`
Conditions soscondition.ViewListForSOSPost `field:"conditions" json:"conditions"`
Pets pet.ViewListForSOSPost `field:"pets" json:"pets"`
Reward string `field:"reward" json:"reward"`
Dates SOSDatesList `field:"dates" json:"dates"`
CareType CareType `field:"careType" json:"careType"`
CarerGender CarerGender `field:"carerGender" json:"carerGender"`
RewardType RewardType `field:"rewardType" json:"rewardType"`
ThumbnailID int64 `field:"thumbnailId" json:"thumbnailId"`
CreatedAt time.Time `field:"createdAt" json:"createdAt"`
UpdatedAt time.Time `field:"updatedAt" json:"updatedAt"`
DeletedAt time.Time `field:"deletedAt" json:"deletedAt"`
}

type SOSPostInfoList struct {
Expand Down Expand Up @@ -124,7 +126,7 @@ type SOSPostStore interface {
) (*SOSPostInfoList, *pnd.AppError)
FindSOSPostByID(ctx context.Context, tx *database.Tx, id int) (*SOSPost, *pnd.AppError)
UpdateSOSPost(ctx context.Context, tx *database.Tx, request *UpdateSOSPostRequest) (*SOSPost, *pnd.AppError)
FindConditionByID(ctx context.Context, tx *database.Tx, id int) (*ConditionList, *pnd.AppError)
FindConditionByID(ctx context.Context, tx *database.Tx, id int) (*soscondition.ViewListForSOSPost, *pnd.AppError)
FindPetsByID(ctx context.Context, tx *database.Tx, id int) (*pet.ViewListForSOSPost, *pnd.AppError)
WriteDates(ctx context.Context, tx *database.Tx, dates []string, sosPostID int) (*SOSDatesList, *pnd.AppError)
FindDatesBySOSPostID(ctx context.Context, tx *database.Tx, sosPostID int) (*SOSDatesList, *pnd.AppError)
Expand Down
Loading

0 comments on commit 3a1d348

Please sign in to comment.