Browse Source

add sortByFictionalDate field to Story, and sorting by fictional date if set.

master 2.1.1
Gisle Aune 4 years ago
parent
commit
63bc262091
  1. 4
      database/mongodb/stories.go
  2. 6
      graph2/resolvers/story.go
  3. 9
      graph2/schema/types/Story.gql
  4. 2
      models/story.go
  5. 32
      services/stories.go

4
database/mongodb/stories.go

@ -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 {

6
graph2/resolvers/story.go

@ -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) {

9
graph2/schema/types/Story.gql

@ -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

2
models/story.go

@ -15,6 +15,7 @@ type Story struct {
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
@ -42,4 +43,5 @@ type StoryUpdate struct {
Listed *bool Listed *bool
FictionalDate *time.Time FictionalDate *time.Time
UpdatedDate *time.Time UpdatedDate *time.Time
SortByFictionalDate *bool
} }

32
services/stories.go

@ -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 {
@ -64,6 +88,7 @@ func (s *StoryService) CreateStory(ctx context.Context, name string, author *str
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")
} }
@ -195,6 +220,7 @@ func (s *StoryService) EditStory(ctx context.Context, story *models.Story, name
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

Loading…
Cancel
Save