Skip to content

Commit

Permalink
refactor: replace in SOSpost with sqlc
Browse files Browse the repository at this point in the history
  • Loading branch information
barabobBOB committed Jun 14, 2024
1 parent b814449 commit a24ad08
Show file tree
Hide file tree
Showing 12 changed files with 543 additions and 599 deletions.
2 changes: 1 addition & 1 deletion cmd/server/handler/sos_post_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewSOSPostHandler(sosPostService service.SOSPostService, authService servic
// @Produce json
// @Param request body sospost.WriteSOSPostRequest true "돌봄급구 게시글 업로드 요청"
// @Security FirebaseAuth
// @Success 201 {object} sospost.WriteSOSPostView
// @Success 201 {object} sospost.DetailView
// @Router /posts/sos [post]
func (h *SOSPostHandler) WriteSOSPost(c echo.Context) error {
foundUser, err := h.authService.VerifyAuthAndGetUser(c.Request().Context(), c.Request().Header.Get("Authorization"))
Expand Down
23 changes: 17 additions & 6 deletions internal/common/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@ package utils

import "time"

// FormatDate formats datetime string to date string.
// FormatDateString formats datetime string to date string.
// Example: 2021-01-01T00:00:00Z -> 2021-01-01
func FormatDate(datetimeStr string) string {
func FormatDateString(datetimeStr string) string {
return datetimeStr[:10]
}

// FormatTime formats datetime time.Time to date string.
// FormatTimeFromTime formats datetime time.Time to date string.
// Example: 2021-01-01T10:06:23Z -> 10:06
func FormatTime(datetime time.Time) string {
func FormatTimeFromTime(datetime time.Time) string {
return datetime.Format("15:04")
}

// FormatDateTime formats datetime time.Time to datetime string.
// FormatDateTimeFromTime formats datetime time.Time to datetime string.
// Example: 2021-01-01T10:06:23.9999999Z -> 2021-01-01T10:06:23
func FormatDateTime(datetime time.Time) string {
func FormatDateTimeFromTime(datetime time.Time) string {
return datetime.Format("2006-01-02T15:04:05")
}

// FormatDateTimeString formats datetime string to datetime string.
// Example: 2021-01-01T10:06:23.9999999Z -> 2021-01-01T10:06:23
func FormatDateTimeString(datetimeStr string) time.Time {
t, err := time.Parse(time.RFC3339Nano, datetimeStr)
if err != nil {
return time.Time{}
}

return t
}
3 changes: 2 additions & 1 deletion internal/common/null.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package utils

import (
"database/sql"
pnd "github.com/pet-sitter/pets-next-door-api/api"
"time"

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

func DerefOrEmpty[T any](val *T) T {
Expand Down
148 changes: 147 additions & 1 deletion internal/domain/sospost/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ package sospost

import (
"context"
"github.com/pet-sitter/pets-next-door-api/internal/domain/commonvo"
"encoding/json"
"log"
"time"

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/commonvo"

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

pnd "github.com/pet-sitter/pets-next-door-api/api"
Expand Down Expand Up @@ -55,6 +61,85 @@ type SOSPostInfoList struct {
*pnd.PaginatedView[SOSPostInfo]
}

func ToInfoFromFindRow(row databasegen.FindSOSPostsRow) *SOSPostInfo {
return &SOSPostInfo{
ID: int(row.ID),
AuthorID: int(*utils.NullInt64ToInt64Ptr(row.AuthorID)),
Title: utils.NullStrToStr(row.Title),
Content: utils.NullStrToStr(row.Content),
Media: ParseMediaList(row.MediaInfo.RawMessage),
Conditions: ParseConditionsList(row.ConditionsInfo.RawMessage),
Pets: ParsePetsList(row.PetsInfo.RawMessage),
Reward: utils.NullStrToStr(row.Reward),
Dates: ParseSOSDatesList(row.Dates),
CareType: commonvo.CareType(row.CareType.String),
CarerGender: commonvo.CarerGender(row.CarerGender.String),
RewardType: commonvo.RewardType(row.RewardType.String),
ThumbnailID: &row.ThumbnailID.Int64,
CreatedAt: row.CreatedAt,
UpdatedAt: row.UpdatedAt,
}
}

func ToInfoListFromFindRow(rows []databasegen.FindSOSPostsRow, page, size int) *SOSPostInfoList {
sl := NewSOSPostInfoList(page, size)
for _, row := range rows {
sl.Items = append(sl.Items, *ToInfoFromFindRow(row))
}
sl.CalcLastPage()
return sl
}

func ToInfoFromFindAuthorIDRow(row databasegen.FindSOSPostsByAuthorIDRow) *SOSPostInfo {
return &SOSPostInfo{
ID: int(row.ID),
AuthorID: int(*utils.NullInt64ToInt64Ptr(row.AuthorID)),
Title: utils.NullStrToStr(row.Title),
Content: utils.NullStrToStr(row.Content),
Media: ParseMediaList(row.MediaInfo.RawMessage),
Conditions: ParseConditionsList(row.ConditionsInfo.RawMessage),
Pets: ParsePetsList(row.PetsInfo.RawMessage),
Reward: utils.NullStrToStr(row.Reward),
Dates: ParseSOSDatesList(row.Dates),
CareType: commonvo.CareType(row.CareType.String),
CarerGender: commonvo.CarerGender(row.CarerGender.String),
RewardType: commonvo.RewardType(row.RewardType.String),
ThumbnailID: &row.ThumbnailID.Int64,
CreatedAt: row.CreatedAt,
UpdatedAt: row.UpdatedAt,
}
}

func ToInfoListFromFindAuthorIDRow(rows []databasegen.FindSOSPostsByAuthorIDRow, page, size int) *SOSPostInfoList {
sl := NewSOSPostInfoList(page, size)
for _, row := range rows {
sl.Items = append(sl.Items, *ToInfoFromFindAuthorIDRow(row))
}

sl.CalcLastPage()
return sl
}

func ToInfoFromFindByIDRow(row databasegen.FindSOSPostByIDRow) *SOSPostInfo {
return &SOSPostInfo{
ID: int(row.ID),
AuthorID: int(*utils.NullInt64ToInt64Ptr(row.AuthorID)),
Title: utils.NullStrToStr(row.Title),
Content: utils.NullStrToStr(row.Content),
Media: ParseMediaList(row.MediaInfo.RawMessage),
Conditions: ParseConditionsList(row.ConditionsInfo.RawMessage),
Pets: ParsePetsList(row.PetsInfo.RawMessage),
Reward: utils.NullStrToStr(row.Reward),
Dates: ParseSOSDatesList(row.Dates),
CareType: commonvo.CareType(row.CareType.String),
CarerGender: commonvo.CarerGender(row.CarerGender.String),
RewardType: commonvo.RewardType(row.RewardType.String),
ThumbnailID: &row.ThumbnailID.Int64,
CreatedAt: row.CreatedAt,
UpdatedAt: row.UpdatedAt,
}
}

func NewSOSPostList(page, size int) *SOSPostList {
return &SOSPostList{PaginatedView: pnd.NewPaginatedView(
page, size, false, make([]SOSPost, 0),
Expand All @@ -67,6 +152,67 @@ func NewSOSPostInfoList(page, size int) *SOSPostInfoList {
)}
}

const (
JSONNullString = "null"
JSONEmptyArray = "[]"
)

func ParseMediaList(rows json.RawMessage) media.ViewListForSOSPost {
var mediaList media.ViewListForSOSPost
if len(rows) == 0 || string(rows) == JSONNullString || string(rows) == JSONEmptyArray {
return mediaList
}

if err := json.Unmarshal(rows, &mediaList); err != nil {
log.Println("Error unmarshalling media:", err)
return mediaList
}

return mediaList
}

func ParseConditionsList(rows json.RawMessage) soscondition.ViewListForSOSPost {
var conditionsList soscondition.ViewListForSOSPost
if len(rows) == 0 || string(rows) == JSONNullString || string(rows) == JSONEmptyArray {
return conditionsList
}

if err := json.Unmarshal(rows, &conditionsList); err != nil {
log.Println("Error unmarshalling conditions:", err)
return conditionsList
}
return conditionsList
}

func ParsePetsList(rows json.RawMessage) pet.ViewListForSOSPost {
var petList pet.ViewListForSOSPost

if len(rows) == 0 || string(rows) == JSONNullString || string(rows) == JSONEmptyArray {
return petList
}

if err := json.Unmarshal(rows, &petList); err != nil {
log.Println("Error unmarshalling pets:", err)
return petList
}

return petList
}

func ParseSOSDatesList(rows json.RawMessage) SOSDatesList {
var sosDatesList SOSDatesList

if len(rows) == 0 || string(rows) == JSONNullString || string(rows) == JSONEmptyArray {
return sosDatesList
}
if err := json.Unmarshal(rows, &sosDatesList); err != nil {
log.Println("Error unmarshalling sosDates:", err)
return sosDatesList
}

return sosDatesList
}

type SOSDates struct {
ID int `field:"id" json:"id"`
DateStartAt string `field:"date_start_at" json:"date_start_at"`
Expand Down
30 changes: 30 additions & 0 deletions internal/domain/sospost/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package sospost

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

type WriteSOSPostRequest struct {
Title string `json:"title" validate:"required"`
Content string `json:"content" validate:"required"`
ImageIDs []int64 `json:"imageIds" validate:"required"`
Reward string `json:"reward" validate:"required"`
Dates []SOSDateView `json:"dates" validate:"required,gte=1"`
CareType commonvo.CareType `json:"careType" validate:"required,oneof=foster visiting"`
CarerGender commonvo.CarerGender `json:"carerGender" validate:"required,oneof=male female all"`
RewardType commonvo.RewardType `json:"rewardType" validate:"required,oneof=fee gifticon negotiable"`
ConditionIDs []int `json:"conditionIds" validate:"required"`
PetIDs []int64 `json:"petIds" validate:"required,gte=1"`
}

type UpdateSOSPostRequest struct {
ID int `json:"id" validate:"required"`
Title string `json:"title" validate:"required"`
Content string `json:"content" validate:"required"`
ImageIDs []int64 `json:"imageIds" validate:"required"`
Dates []SOSDateView `json:"dates" validate:"required,gte=1"`
Reward string `json:"reward" validate:"required"`
CareType commonvo.CareType `json:"careType" validate:"required,oneof=foster visiting"`
CarerGender commonvo.CarerGender `json:"carerGender" validate:"required,oneof=male female all"`
RewardType commonvo.RewardType `json:"rewardType" validate:"required,oneof=fee gifticon negotiable"`
ConditionIDs []int `json:"conditionIds" validate:"required"`
PetIDs []int64 `json:"petIds" validate:"required,gte=1"`
}
Loading

0 comments on commit a24ad08

Please sign in to comment.