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:
+43
-17
@@ -7,6 +7,7 @@ import (
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"git.aiterp.net/rpdata/api/models/changekeys"
|
||||
"git.aiterp.net/rpdata/api/repositories"
|
||||
"sort"
|
||||
"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) {
|
||||
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) {
|
||||
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 {
|
||||
token := s.authService.TokenFromContext(ctx)
|
||||
if token == nil {
|
||||
@@ -55,15 +79,16 @@ func (s *StoryService) CreateStory(ctx context.Context, name string, author *str
|
||||
}
|
||||
|
||||
story := &models.Story{
|
||||
Name: name,
|
||||
Author: *author,
|
||||
Category: category,
|
||||
Listed: listed,
|
||||
Open: open,
|
||||
Tags: tags,
|
||||
CreatedDate: createdDate,
|
||||
FictionalDate: fictionalDate,
|
||||
UpdatedDate: createdDate,
|
||||
Name: name,
|
||||
Author: *author,
|
||||
Category: category,
|
||||
Listed: listed,
|
||||
Open: open,
|
||||
Tags: tags,
|
||||
CreatedDate: createdDate,
|
||||
FictionalDate: fictionalDate,
|
||||
UpdatedDate: createdDate,
|
||||
SortByFictionalDate: sortByFictionalDate,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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 {
|
||||
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{
|
||||
Name: name,
|
||||
Open: open,
|
||||
Listed: listed,
|
||||
Category: category,
|
||||
FictionalDate: fictionalDate,
|
||||
Name: name,
|
||||
Open: open,
|
||||
Listed: listed,
|
||||
Category: category,
|
||||
FictionalDate: fictionalDate,
|
||||
SortByFictionalDate: sortByFictionalDate,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user