Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions migrations/20230725215653-add_pin_order_to_stories.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- +migrate Up

ALTER TABLE stories
ADD COLUMN pin_order INT;

-- +migrate Down

ALTER TABLE stories
DROP COLUMN pin_order;
3 changes: 3 additions & 0 deletions model/stories.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Story struct {
Author User
Title string
Content string
PinOrder *int // nil if not pinned
}

var (
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions params/stories/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,5 +19,6 @@ func (params *Create) ToModel() *model.Story {
AuthorID: params.AuthorID,
Title: params.Title,
Content: params.Content,
PinOrder: params.PinOrder,
}
}
2 changes: 2 additions & 0 deletions view/stories/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions view/stories/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}