From 138526d5396c11776b7b60c0993df7c6fdee3a95 Mon Sep 17 00:00:00 2001 From: Richard Dominick <34370238+RichDom2185@users.noreply.github.com> Date: Tue, 25 Jul 2023 22:00:41 +0800 Subject: [PATCH 1/3] Create migration file --- migrations/20230725215653-add_pin_order_to_stories.sql | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 migrations/20230725215653-add_pin_order_to_stories.sql 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; From f3242973fbd3a682192473a299644defa9efbf7d Mon Sep 17 00:00:00 2001 From: Richard Dominick <34370238+RichDom2185@users.noreply.github.com> Date: Tue, 25 Jul 2023 22:12:30 +0800 Subject: [PATCH 2/3] Update Story model --- model/stories.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/model/stories.go b/model/stories.go index ad3faf4..b2c5d11 100644 --- a/model/stories.go +++ b/model/stories.go @@ -10,11 +10,15 @@ type Story struct { AuthorID uint Title string Content string + PinOrder *int // nil if not pinned } func GetAllStories(db *gorm.DB) ([]Story, error) { var stories []Story - err := db.Find(&stories).Error + err := db. + // TODO: Abstract out + Order("pin_order ASC NULLS LAST, title ASC, content ASC"). + Find(&stories).Error if err != nil { return stories, database.HandleDBError(err, "story") } From c032d025fb91862e7ffe5e7c135ef947227d12f7 Mon Sep 17 00:00:00 2001 From: Richard Dominick <34370238+RichDom2185@users.noreply.github.com> Date: Tue, 25 Jul 2023 22:12:56 +0800 Subject: [PATCH 3/3] Update story params and views --- params/stories/create.go | 2 ++ view/stories/list.go | 2 ++ view/stories/view.go | 2 ++ 3 files changed, 6 insertions(+) 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 545d6e6..acb41e5 100644 --- a/view/stories/list.go +++ b/view/stories/list.go @@ -6,6 +6,7 @@ type ListView struct { AuthorID uint `json:"authorId"` Title string `json:"title"` Content string `json:"content"` + Pinned bool `json:"isPinned"` } func ListFrom(stories []model.Story) []ListView { @@ -15,6 +16,7 @@ func ListFrom(stories []model.Story) []ListView { AuthorID: story.AuthorID, 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 8a4b64b..9f750cb 100644 --- a/view/stories/view.go +++ b/view/stories/view.go @@ -7,6 +7,7 @@ type View struct { AuthorID uint `json:"authorId"` Title string `json:"title"` Content string `json:"content"` + Pinned bool `json:"isPinned"` } func SingleFrom(story model.Story) View { @@ -15,6 +16,7 @@ func SingleFrom(story model.Story) View { AuthorID: story.AuthorID, Title: story.Title, Content: story.Content, + Pinned: story.PinOrder != nil, } return storyView }