diff --git a/migrations/20230725215653-add_pin_order_to_stories.sql b/migrations/20230725215653-add_pin_order_to_stories.sql new file mode 100644 index 0000000..79d5234 --- /dev/null +++ b/migrations/20230725215653-add_pin_order_to_stories.sql @@ -0,0 +1,9 @@ +-- +migrate Up + +ALTER TABLE stories + ADD COLUMN pin_order INT; + +-- +migrate Down + +ALTER TABLE stories + DROP COLUMN pin_order; diff --git a/model/stories.go b/model/stories.go index 8f568e6..29673cf 100644 --- a/model/stories.go +++ b/model/stories.go @@ -11,6 +11,7 @@ type Story struct { Author User Title string Content string + PinOrder *int // nil if not pinned } var ( @@ -23,6 +24,8 @@ func GetAllStories(db *gorm.DB) ([]Story, error) { var stories []Story err := db. Scopes(preloadAssociations). + // TODO: Abstract out + Order("pin_order ASC NULLS LAST, title ASC, content ASC"). Find(&stories). Error if err != nil { diff --git a/params/stories/create.go b/params/stories/create.go index 24e1591..a6a5648 100644 --- a/params/stories/create.go +++ b/params/stories/create.go @@ -6,6 +6,7 @@ type Create struct { AuthorID uint `json:"authorId"` Title string `json:"title"` Content string `json:"content"` + PinOrder *int `json:"pinOrder"` } // TODO: Add some validation @@ -18,5 +19,6 @@ func (params *Create) ToModel() *model.Story { AuthorID: params.AuthorID, Title: params.Title, Content: params.Content, + PinOrder: params.PinOrder, } } diff --git a/view/stories/list.go b/view/stories/list.go index 59ed59b..67f7b68 100644 --- a/view/stories/list.go +++ b/view/stories/list.go @@ -10,6 +10,7 @@ type ListView struct { AuthorName string `json:"authorName"` Title string `json:"title"` Content string `json:"content"` + Pinned bool `json:"isPinned"` } func ListFrom(stories []model.Story) []ListView { @@ -21,6 +22,7 @@ func ListFrom(stories []model.Story) []ListView { AuthorName: author.Name, Title: story.Title, Content: story.Content, + Pinned: story.PinOrder != nil, } } return storiesListView diff --git a/view/stories/view.go b/view/stories/view.go index 16fbd99..2b58f1f 100644 --- a/view/stories/view.go +++ b/view/stories/view.go @@ -11,6 +11,7 @@ type View struct { AuthorName string `json:"authorName"` Title string `json:"title"` Content string `json:"content"` + Pinned bool `json:"isPinned"` } func SingleFrom(story model.Story) View { @@ -21,6 +22,7 @@ func SingleFrom(story model.Story) View { AuthorName: author.Name, Title: story.Title, Content: story.Content, + Pinned: story.PinOrder != nil, } return storyView }