Skip to content

Commit

Permalink
refactor: replace Write, Update in SOSpost with sqlc
Browse files Browse the repository at this point in the history
  • Loading branch information
barabobBOB committed Jun 12, 2024
1 parent 675ddf3 commit b814449
Show file tree
Hide file tree
Showing 8 changed files with 499 additions and 560 deletions.
27 changes: 26 additions & 1 deletion internal/common/null.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package utils

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

func DerefOrEmpty[T any](val *T) T {
if val == nil {
Expand Down Expand Up @@ -94,3 +98,24 @@ func Int64PtrToNullInt32(val *int64) sql.NullInt32 {
Valid: IsNotNil(val),
}
}

func StrToNullTime(val string) (sql.NullTime, *pnd.AppError) {
const timeLayout = "2006-01-02"
parsedTime, err := time.Parse(timeLayout, val)
if err != nil {
return sql.NullTime{
Valid: false,
}, pnd.FromPostgresError(err)
}
return sql.NullTime{
Time: parsedTime,
Valid: true,
}, nil
}

func NullTimeToStr(val sql.NullTime) string {
if val.Valid {
return val.Time.Format("2006-01-02")
}
return ""
}
39 changes: 26 additions & 13 deletions internal/domain/sospost/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ type UpdateSOSPostRequest struct {
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 []int `json:"petIds" validate:"required,gte=1"`
PetIDs []int64 `json:"petIds" validate:"required,gte=1"`
}

type UpdateSOSPostView struct {
Expand All @@ -190,28 +190,29 @@ type UpdateSOSPostView struct {
UpdatedAt string `json:"updatedAt"`
}

func (p *SOSPost) ToUpdateSOSPostView(
func ToUpdateSOSPostView(
updateSOSPost databasegen.UpdateSOSPostRow,
mediaList media.ListView,
conditions soscondition.ListView,
pets []pet.DetailView,
sosDates []SOSDateView,
) *UpdateSOSPostView {
return &UpdateSOSPostView{
ID: p.ID,
AuthorID: p.AuthorID,
Title: p.Title,
Content: p.Content,
ID: int(updateSOSPost.ID),
AuthorID: int(updateSOSPost.AuthorID.Int64),
Title: utils.NullStrToStr(updateSOSPost.Title),
Content: utils.NullStrToStr(updateSOSPost.Content),
Media: mediaList,
Conditions: conditions,
Pets: pets,
Reward: p.Reward,
Reward: utils.NullStrToStr(updateSOSPost.Reward),
Dates: sosDates,
CareType: p.CareType,
CarerGender: p.CarerGender,
RewardType: p.RewardType,
ThumbnailID: p.ThumbnailID,
CreatedAt: utils.FormatDateTime(p.CreatedAt),
UpdatedAt: utils.FormatDateTime(p.UpdatedAt),
CareType: commonvo.CareType(updateSOSPost.CareType.String),
CarerGender: commonvo.CarerGender(updateSOSPost.CarerGender.String),
RewardType: commonvo.RewardType(updateSOSPost.RewardType.String),
ThumbnailID: &updateSOSPost.ThumbnailID.Int64,
CreatedAt: utils.FormatDateTime(updateSOSPost.CreatedAt),
UpdatedAt: utils.FormatDateTime(updateSOSPost.UpdatedAt),
}
}

Expand All @@ -227,6 +228,18 @@ func (d *SOSDates) ToSOSDateView() SOSDateView {
}
}

func ToListViewFromSOSDateRows(rows []databasegen.FindDatesBySOSPostIDRow) []SOSDateView {
sosDateViews := make([]SOSDateView, len(rows))
for i, row := range rows {
date := SOSDates{
DateStartAt: utils.NullTimeToStr(row.DateStartAt),
DateEndAt: utils.NullTimeToStr(row.DateEndAt),
}
sosDateViews[i] = date.ToSOSDateView()
}
return sosDateViews
}

func (dl *SOSDatesList) ToSOSDateViewList() []SOSDateView {
sosDateViews := make([]SOSDateView, len(*dl))
for i, d := range *dl {
Expand Down
14 changes: 14 additions & 0 deletions internal/infra/database/gen/resource_media.sql.go

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

96 changes: 96 additions & 0 deletions internal/infra/database/gen/sos_posts.sql.go

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

Loading

0 comments on commit b814449

Please sign in to comment.