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