add sortByFictionalDate field to Story, and sorting by fictional date if set.
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -145,6 +145,10 @@ func (r *storyRepository) Update(ctx context.Context, story models.Story, update
|
|||||||
updateBson["listed"] = *update.Listed
|
updateBson["listed"] = *update.Listed
|
||||||
story.Listed = *update.Listed
|
story.Listed = *update.Listed
|
||||||
}
|
}
|
||||||
|
if update.SortByFictionalDate != nil {
|
||||||
|
updateBson["sortByFictionalDate"] = *update.SortByFictionalDate
|
||||||
|
story.SortByFictionalDate = *update.SortByFictionalDate
|
||||||
|
}
|
||||||
|
|
||||||
err := r.stories.UpdateId(story.ID, bson.M{"$set": updateBson})
|
err := r.stories.UpdateId(story.ID, bson.M{"$set": updateBson})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -36,7 +36,9 @@ func (r *mutationResolver) AddStory(ctx context.Context, input graphcore.StoryAd
|
|||||||
fictionalDate = *input.FictionalDate
|
fictionalDate = *input.FictionalDate
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.s.Stories.CreateStory(ctx, input.Name, input.Author, input.Category, listed, open, tags, time.Now(), fictionalDate)
|
sortByFictionalDate := input.SortByFictionalDate != nil && *input.SortByFictionalDate
|
||||||
|
|
||||||
|
return r.s.Stories.CreateStory(ctx, input.Name, input.Author, input.Category, listed, open, tags, time.Now(), fictionalDate, sortByFictionalDate)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *mutationResolver) AddStoryTag(ctx context.Context, input graphcore.StoryTagAddInput) (*models.Story, error) {
|
func (r *mutationResolver) AddStoryTag(ctx context.Context, input graphcore.StoryTagAddInput) (*models.Story, error) {
|
||||||
@@ -63,7 +65,7 @@ func (r *mutationResolver) EditStory(ctx context.Context, input graphcore.StoryE
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.s.Stories.EditStory(ctx, story, input.Name, input.Category, input.Listed, input.Open, input.FictionalDate)
|
return r.s.Stories.EditStory(ctx, story, input.Name, input.Category, input.Listed, input.Open, input.FictionalDate, input.SortByFictionalDate)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *mutationResolver) RemoveStory(ctx context.Context, input graphcore.StoryRemoveInput) (*models.Story, error) {
|
func (r *mutationResolver) RemoveStory(ctx context.Context, input graphcore.StoryRemoveInput) (*models.Story, error) {
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ type Story {
|
|||||||
|
|
||||||
# The date of the last major update to the story. This being bumped means a new chapter has been added.
|
# The date of the last major update to the story. This being bumped means a new chapter has been added.
|
||||||
updatedDate: Time!
|
updatedDate: Time!
|
||||||
|
|
||||||
|
# Whether to sort by fictional date.
|
||||||
|
sortByFictionalDate: Boolean!
|
||||||
}
|
}
|
||||||
|
|
||||||
# Filter for stories query.
|
# Filter for stories query.
|
||||||
@@ -86,6 +89,9 @@ input StoryAddInput {
|
|||||||
|
|
||||||
# Set the fictional date of the story.
|
# Set the fictional date of the story.
|
||||||
fictionalDate: Time
|
fictionalDate: Time
|
||||||
|
|
||||||
|
# Whether to sort by fictional date.
|
||||||
|
sortByFictionalDate: Boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
# Input for the editStory mutation
|
# Input for the editStory mutation
|
||||||
@@ -115,6 +121,9 @@ input StoryEditInput {
|
|||||||
|
|
||||||
# Clear the fictional date of the story.
|
# Clear the fictional date of the story.
|
||||||
clearFictionalDate: Boolean
|
clearFictionalDate: Boolean
|
||||||
|
|
||||||
|
# Whether to sort by fictional date.
|
||||||
|
sortByFictionalDate: Boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
# Input for the addStoryTag mutation
|
# Input for the addStoryTag mutation
|
||||||
|
|||||||
+19
-17
@@ -5,16 +5,17 @@ import "time"
|
|||||||
// A Story is user content that does not have a wiki-suitable format. Documents, new stories, short stories, and so on.
|
// A Story is user content that does not have a wiki-suitable format. Documents, new stories, short stories, and so on.
|
||||||
// The story model is a container for multiple chapters this time, in contrast to the previous version.
|
// The story model is a container for multiple chapters this time, in contrast to the previous version.
|
||||||
type Story struct {
|
type Story struct {
|
||||||
ID string `bson:"_id"`
|
ID string `bson:"_id"`
|
||||||
Author string `bson:"author"`
|
Author string `bson:"author"`
|
||||||
Name string `bson:"name"`
|
Name string `bson:"name"`
|
||||||
Category StoryCategory `bson:"category"`
|
Category StoryCategory `bson:"category"`
|
||||||
Open bool `bson:"open"`
|
Open bool `bson:"open"`
|
||||||
Listed bool `bson:"listed"`
|
Listed bool `bson:"listed"`
|
||||||
Tags []Tag `bson:"tags"`
|
Tags []Tag `bson:"tags"`
|
||||||
CreatedDate time.Time `bson:"createdDate"`
|
CreatedDate time.Time `bson:"createdDate"`
|
||||||
FictionalDate time.Time `bson:"fictionalDate,omitempty"`
|
FictionalDate time.Time `bson:"fictionalDate,omitempty"`
|
||||||
UpdatedDate time.Time `bson:"updatedDate"`
|
UpdatedDate time.Time `bson:"updatedDate"`
|
||||||
|
SortByFictionalDate bool `bson:"sortByFictionalDate"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsChangeObject is an interface implementation to identify it as a valid
|
// IsChangeObject is an interface implementation to identify it as a valid
|
||||||
@@ -35,11 +36,12 @@ type StoryFilter struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type StoryUpdate struct {
|
type StoryUpdate struct {
|
||||||
Name *string
|
Name *string
|
||||||
Category *StoryCategory
|
Category *StoryCategory
|
||||||
Author *string
|
Author *string
|
||||||
Open *bool
|
Open *bool
|
||||||
Listed *bool
|
Listed *bool
|
||||||
FictionalDate *time.Time
|
FictionalDate *time.Time
|
||||||
UpdatedDate *time.Time
|
UpdatedDate *time.Time
|
||||||
|
SortByFictionalDate *bool
|
||||||
}
|
}
|
||||||
|
|||||||
+43
-17
@@ -7,6 +7,7 @@ import (
|
|||||||
"git.aiterp.net/rpdata/api/models"
|
"git.aiterp.net/rpdata/api/models"
|
||||||
"git.aiterp.net/rpdata/api/models/changekeys"
|
"git.aiterp.net/rpdata/api/models/changekeys"
|
||||||
"git.aiterp.net/rpdata/api/repositories"
|
"git.aiterp.net/rpdata/api/repositories"
|
||||||
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -37,14 +38,37 @@ func (s *StoryService) ListStories(ctx context.Context, filter models.StoryFilte
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StoryService) ListChapters(ctx context.Context, story models.Story) ([]*models.Chapter, error) {
|
func (s *StoryService) ListChapters(ctx context.Context, story models.Story) ([]*models.Chapter, error) {
|
||||||
return s.chapters.List(ctx, models.ChapterFilter{StoryID: &story.ID, Limit: 0})
|
chapters, err := s.chapters.List(ctx, models.ChapterFilter{StoryID: &story.ID, Limit: 0})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if story.SortByFictionalDate {
|
||||||
|
sort.Slice(chapters, func(i, j int) bool {
|
||||||
|
if !chapters[i].FictionalDate.IsZero() && !chapters[j].FictionalDate.IsZero() {
|
||||||
|
if chapters[i].FictionalDate.Equal(chapters[j].FictionalDate) {
|
||||||
|
return chapters[i].CreatedDate.Before(chapters[j].CreatedDate)
|
||||||
|
}
|
||||||
|
|
||||||
|
return chapters[i].FictionalDate.Before(chapters[j].FictionalDate)
|
||||||
|
} else if chapters[i].FictionalDate.IsZero() && !chapters[j].FictionalDate.IsZero() {
|
||||||
|
return false
|
||||||
|
} else if !chapters[i].FictionalDate.IsZero() && chapters[j].FictionalDate.IsZero() {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return chapters[i].CreatedDate.Before(chapters[j].CreatedDate)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return chapters, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StoryService) ListComments(ctx context.Context, chapter models.Chapter, limit int) ([]*models.Comment, error) {
|
func (s *StoryService) ListComments(ctx context.Context, chapter models.Chapter, limit int) ([]*models.Comment, error) {
|
||||||
return s.comments.List(ctx, models.CommentFilter{ChapterID: &chapter.ID, Limit: limit})
|
return s.comments.List(ctx, models.CommentFilter{ChapterID: &chapter.ID, Limit: limit})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StoryService) CreateStory(ctx context.Context, name string, author *string, category models.StoryCategory, listed, open bool, tags []models.Tag, createdDate, fictionalDate time.Time) (*models.Story, error) {
|
func (s *StoryService) CreateStory(ctx context.Context, name string, author *string, category models.StoryCategory, listed, open bool, tags []models.Tag, createdDate, fictionalDate time.Time, sortByFictionalDate bool) (*models.Story, error) {
|
||||||
if author == nil {
|
if author == nil {
|
||||||
token := s.authService.TokenFromContext(ctx)
|
token := s.authService.TokenFromContext(ctx)
|
||||||
if token == nil {
|
if token == nil {
|
||||||
@@ -55,15 +79,16 @@ func (s *StoryService) CreateStory(ctx context.Context, name string, author *str
|
|||||||
}
|
}
|
||||||
|
|
||||||
story := &models.Story{
|
story := &models.Story{
|
||||||
Name: name,
|
Name: name,
|
||||||
Author: *author,
|
Author: *author,
|
||||||
Category: category,
|
Category: category,
|
||||||
Listed: listed,
|
Listed: listed,
|
||||||
Open: open,
|
Open: open,
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
CreatedDate: createdDate,
|
CreatedDate: createdDate,
|
||||||
FictionalDate: fictionalDate,
|
FictionalDate: fictionalDate,
|
||||||
UpdatedDate: createdDate,
|
UpdatedDate: createdDate,
|
||||||
|
SortByFictionalDate: sortByFictionalDate,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.authService.CheckPermission(ctx, "add", story); err != nil {
|
if err := s.authService.CheckPermission(ctx, "add", story); err != nil {
|
||||||
@@ -180,7 +205,7 @@ func (s *StoryService) CreateComment(ctx context.Context, chapter models.Chapter
|
|||||||
return comment, nil
|
return comment, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StoryService) EditStory(ctx context.Context, story *models.Story, name *string, category *models.StoryCategory, listed, open *bool, fictionalDate *time.Time) (*models.Story, error) {
|
func (s *StoryService) EditStory(ctx context.Context, story *models.Story, name *string, category *models.StoryCategory, listed, open *bool, fictionalDate *time.Time, sortByFictionalDate *bool) (*models.Story, error) {
|
||||||
if story == nil {
|
if story == nil {
|
||||||
panic("StoryService.Edit called with nil story")
|
panic("StoryService.Edit called with nil story")
|
||||||
}
|
}
|
||||||
@@ -190,11 +215,12 @@ func (s *StoryService) EditStory(ctx context.Context, story *models.Story, name
|
|||||||
}
|
}
|
||||||
|
|
||||||
story, err := s.stories.Update(ctx, *story, models.StoryUpdate{
|
story, err := s.stories.Update(ctx, *story, models.StoryUpdate{
|
||||||
Name: name,
|
Name: name,
|
||||||
Open: open,
|
Open: open,
|
||||||
Listed: listed,
|
Listed: listed,
|
||||||
Category: category,
|
Category: category,
|
||||||
FictionalDate: fictionalDate,
|
FictionalDate: fictionalDate,
|
||||||
|
SortByFictionalDate: sortByFictionalDate,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Reference in New Issue
Block a user