Skip to content

Commit

Permalink
refactor: SOS post enum
Browse files Browse the repository at this point in the history
  • Loading branch information
barabobBOB committed Jun 15, 2024
1 parent e40f658 commit 66c37cc
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 112 deletions.
36 changes: 0 additions & 36 deletions internal/domain/commonvo/vo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,6 @@ const (
PetTypeCat PetType = "cat"
)

// SOSPost
type (
CareType string
CarerGender string
RewardType string
)

const (
CareTypeFoster CareType = "foster"
CareTypeVisiting CareType = "visiting"
)

const (
CarerGenderMale CarerGender = "male"
CarerGenderFemale CarerGender = "female"
CarerGenderAll CarerGender = "all"
)

const (
RewardTypeFee RewardType = "fee"
RewardTypeGifticon RewardType = "gifticon"
RewardTypeNegotiable RewardType = "negotiable"
)

func (p *PetType) String() string {
return string(*p)
}

func (c *CareType) String() string {
return string(*c)
}

func (c *CarerGender) String() string {
return string(*c)
}

func (r *RewardType) String() string {
return string(*r)
}
95 changes: 64 additions & 31 deletions internal/domain/sospost/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ 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"

"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 All @@ -19,19 +17,59 @@ import (
"github.com/pet-sitter/pets-next-door-api/internal/infra/database"
)

type (
CareType string
CarerGender string
RewardType string
)

const (
CareTypeFoster CareType = "foster"
CareTypeVisiting CareType = "visiting"
)

const (
CarerGenderMale CarerGender = "male"
CarerGenderFemale CarerGender = "female"
CarerGenderAll CarerGender = "all"
)

const (
RewardTypeFee RewardType = "fee"
RewardTypeGifticon RewardType = "gifticon"
RewardTypeNegotiable RewardType = "negotiable"
)

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

func (c *CareType) String() string {
return string(*c)
}

func (c *CarerGender) String() string {
return string(*c)
}

func (r *RewardType) String() string {
return string(*r)
}

type SOSPost struct {
ID int `field:"id"`
AuthorID int `field:"author_id"`
Title string `field:"title"`
Content string `field:"content"`
Reward string `field:"reward"`
CareType commonvo.CareType `field:"care_type"`
CarerGender commonvo.CarerGender `field:"carer_gender"`
RewardType commonvo.RewardType `field:"reward_type"`
ThumbnailID *int64 `field:"thumbnail_id"`
CreatedAt time.Time `field:"created_at"`
UpdatedAt time.Time `field:"updated_at"`
DeletedAt time.Time `field:"deleted_at"`
ID int `field:"id"`
AuthorID int `field:"author_id"`
Title string `field:"title"`
Content string `field:"content"`
Reward string `field:"reward"`
CareType CareType `field:"care_type"`
CarerGender CarerGender `field:"carer_gender"`
RewardType RewardType `field:"reward_type"`
ThumbnailID *int64 `field:"thumbnail_id"`
CreatedAt time.Time `field:"created_at"`
UpdatedAt time.Time `field:"updated_at"`
DeletedAt time.Time `field:"deleted_at"`
}

type SOSPostList struct {
Expand All @@ -48,9 +86,9 @@ type SOSPostInfo struct {
Pets pet.ViewListForSOSPost `field:"pets" json:"pets"`
Reward string `field:"reward" json:"reward"`
Dates SOSDatesList `field:"dates" json:"dates"`
CareType commonvo.CareType `field:"careType" json:"careType"`
CarerGender commonvo.CarerGender `field:"carerGender" json:"carerGender"`
RewardType commonvo.RewardType `field:"rewardType" json:"rewardType"`
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"`
Expand All @@ -72,9 +110,9 @@ func ToInfoFromFindRow(row databasegen.FindSOSPostsRow) *SOSPostInfo {
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),
CareType: CareType(row.CareType.String),
CarerGender: CarerGender(row.CarerGender.String),
RewardType: RewardType(row.RewardType.String),
ThumbnailID: &row.ThumbnailID.Int64,
CreatedAt: row.CreatedAt,
UpdatedAt: row.UpdatedAt,
Expand All @@ -101,9 +139,9 @@ func ToInfoFromFindAuthorIDRow(row databasegen.FindSOSPostsByAuthorIDRow) *SOSPo
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),
CareType: CareType(row.CareType.String),
CarerGender: CarerGender(row.CarerGender.String),
RewardType: RewardType(row.RewardType.String),
ThumbnailID: &row.ThumbnailID.Int64,
CreatedAt: row.CreatedAt,
UpdatedAt: row.UpdatedAt,
Expand Down Expand Up @@ -131,9 +169,9 @@ func ToInfoFromFindByIDRow(row databasegen.FindSOSPostByIDRow) *SOSPostInfo {
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),
CareType: CareType(row.CareType.String),
CarerGender: CarerGender(row.CarerGender.String),
RewardType: RewardType(row.RewardType.String),
ThumbnailID: &row.ThumbnailID.Int64,
CreatedAt: row.CreatedAt,
UpdatedAt: row.UpdatedAt,
Expand All @@ -152,11 +190,6 @@ 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 {
Expand Down
44 changes: 21 additions & 23 deletions internal/domain/sospost/request.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
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"`
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 CareType `json:"careType" validate:"required,oneof=foster visiting"`
CarerGender CarerGender `json:"carerGender" validate:"required,oneof=male female all"`
RewardType 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"`
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 CareType `json:"careType" validate:"required,oneof=foster visiting"`
CarerGender CarerGender `json:"carerGender" validate:"required,oneof=male female all"`
RewardType RewardType `json:"rewardType" validate:"required,oneof=fee gifticon negotiable"`
ConditionIDs []int `json:"conditionIds" validate:"required"`
PetIDs []int64 `json:"petIds" validate:"required,gte=1"`
}
31 changes: 15 additions & 16 deletions internal/domain/sospost/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package sospost
import (
pnd "github.com/pet-sitter/pets-next-door-api/api"
utils "github.com/pet-sitter/pets-next-door-api/internal/common"
"github.com/pet-sitter/pets-next-door-api/internal/domain/commonvo"
"github.com/pet-sitter/pets-next-door-api/internal/domain/media"
"github.com/pet-sitter/pets-next-door-api/internal/domain/pet"
"github.com/pet-sitter/pets-next-door-api/internal/domain/soscondition"
Expand All @@ -21,9 +20,9 @@ type ViewParams struct {
Pets []pet.DetailView
Reward string
SOSDates []SOSDateView
CareType commonvo.CareType
CarerGender commonvo.CarerGender
RewardType commonvo.RewardType
CareType CareType
CarerGender CarerGender
RewardType RewardType
ThumbnailID *int64
CreatedAt string
UpdatedAt string
Expand Down Expand Up @@ -57,9 +56,9 @@ type DetailView struct {
Pets []pet.DetailView `json:"pets"`
Reward string `json:"reward"`
Dates []SOSDateView `json:"dates"`
CareType commonvo.CareType `json:"careType"`
CarerGender commonvo.CarerGender `json:"carerGender"`
RewardType commonvo.RewardType `json:"rewardType"`
CareType CareType `json:"careType"`
CarerGender CarerGender `json:"carerGender"`
RewardType RewardType `json:"rewardType"`
ThumbnailID *int64 `json:"thumbnailId"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
Expand Down Expand Up @@ -96,9 +95,9 @@ func CreateViewParams(input ViewParamsInput) ViewParams {
Pets: input.Pets,
Reward: input.Reward,
SOSDates: input.SOSDates,
CareType: commonvo.CareType(input.CareType),
CarerGender: commonvo.CarerGender(input.CarerGender),
RewardType: commonvo.RewardType(input.RewardType),
CareType: CareType(input.CareType),
CarerGender: CarerGender(input.CarerGender),
RewardType: RewardType(input.RewardType),
ThumbnailID: input.ThumbnailID,
CreatedAt: input.CreatedAt,
UpdatedAt: input.UpdatedAt,
Expand Down Expand Up @@ -143,9 +142,9 @@ type FindSOSPostView struct {
Pets []pet.DetailView `json:"pets"`
Reward string `json:"reward"`
Dates []SOSDateView `json:"dates"`
CareType commonvo.CareType `json:"careType"`
CarerGender commonvo.CarerGender `json:"carerGender"`
RewardType commonvo.RewardType `json:"rewardType"`
CareType CareType `json:"careType"`
CarerGender CarerGender `json:"carerGender"`
RewardType RewardType `json:"rewardType"`
ThumbnailID *int64 `json:"thumbnailId"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
Expand Down Expand Up @@ -240,9 +239,9 @@ func UpdateDetailView(
Pets: pets,
Reward: utils.NullStrToStr(sosPost.Reward),
SOSDates: sosDates,
CareType: commonvo.CareType(sosPost.CareType.String),
CarerGender: commonvo.CarerGender(sosPost.CarerGender.String),
RewardType: commonvo.RewardType(sosPost.RewardType.String),
CareType: CareType(sosPost.CareType.String),
CarerGender: CarerGender(sosPost.CarerGender.String),
RewardType: RewardType(sosPost.RewardType.String),
ThumbnailID: &sosPost.ThumbnailID.Int64,
CreatedAt: utils.FormatDateTimeFromTime(sosPost.CreatedAt),
UpdatedAt: utils.FormatDateTimeFromTime(sosPost.UpdatedAt),
Expand Down
6 changes: 3 additions & 3 deletions internal/service/tests/sos_post_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,9 @@ func TestUpdateSOSPost(t *testing.T) {
{"2024-04-10", "2024-04-20"},
{"2024-05-10", "2024-05-20"},
},
CareType: commonvo.CareTypeFoster,
CarerGender: commonvo.CarerGenderMale,
RewardType: commonvo.RewardTypeFee,
CareType: sospost.CareTypeFoster,
CarerGender: sospost.CarerGenderMale,
RewardType: sospost.RewardTypeFee,
ConditionIDs: []int{1, 2},
PetIDs: []int64{addPets.Pets[0].ID},
}
Expand Down
6 changes: 3 additions & 3 deletions internal/tests/factories.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func NewDummyWriteSOSPostRequest(imageID, petIDs []int64, sosPostCnt int) *sospo
{DateStartAt: fmt.Sprintf("2024-04-1%d", sosPostCnt), DateEndAt: fmt.Sprintf("2024-04-2%d", sosPostCnt)},
{DateStartAt: fmt.Sprintf("2024-05-1%d", sosPostCnt), DateEndAt: fmt.Sprintf("2024-05-2%d", sosPostCnt)},
},
CareType: commonvo.CareTypeFoster,
CarerGender: commonvo.CarerGenderMale,
RewardType: commonvo.RewardTypeFee,
CareType: sospost.CareTypeFoster,
CarerGender: sospost.CarerGenderMale,
RewardType: sospost.RewardTypeFee,
ConditionIDs: []int{1, 2},
PetIDs: petIDs,
}
Expand Down

0 comments on commit 66c37cc

Please sign in to comment.