Added chapter query and Chapter type resolvers, added functions to get chapters
This commit is contained in:
@@ -61,6 +61,25 @@ func (chapter *Chapter) Remove() error {
|
|||||||
return chapterCollection.RemoveId(chapter.ID)
|
return chapterCollection.RemoveId(chapter.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindChapterID finds a chapter by its own ID
|
||||||
|
func FindChapterID(id string) (Chapter, error) {
|
||||||
|
chapter := Chapter{}
|
||||||
|
err := chapterCollection.FindId(id).One(&chapter)
|
||||||
|
|
||||||
|
return chapter, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListChapterStoryID lists all chapters for the story ID
|
||||||
|
func ListChapterStoryID(storyID string) ([]Chapter, error) {
|
||||||
|
chapters := make([]Chapter, 0, 8)
|
||||||
|
err := chapterCollection.Find(bson.M{"storyId": storyID}).Sort("createdDate").One(&chapters)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return chapters, nil
|
||||||
|
}
|
||||||
|
|
||||||
// makeChapterID makes a random chapter ID that's 24 characters long
|
// makeChapterID makes a random chapter ID that's 24 characters long
|
||||||
func makeChapterID() string {
|
func makeChapterID() string {
|
||||||
result := "SC"
|
result := "SC"
|
||||||
|
|||||||
@@ -124,6 +124,11 @@ func (story *Story) Remove() error {
|
|||||||
return storyCollection.RemoveId(story.ID)
|
return storyCollection.RemoveId(story.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Chapters calls ListChapterStoryID with the story's ID:
|
||||||
|
func (story *Story) Chapters() ([]Chapter, error) {
|
||||||
|
return ListChapterStoryID(story.ID)
|
||||||
|
}
|
||||||
|
|
||||||
// AddChapter adds a chapter to the story. This does not enforce the `Open` setting, but it will log a warning if it
|
// AddChapter adds a chapter to the story. This does not enforce the `Open` setting, but it will log a warning if it
|
||||||
// occurs
|
// occurs
|
||||||
func (story *Story) AddChapter(title, author, source string, createdDate, finctionalDate time.Time) (Chapter, error) {
|
func (story *Story) AddChapter(title, author, source string, createdDate, finctionalDate time.Time) (Chapter, error) {
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package resolver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.aiterp.net/rpdata/api/model/story"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ChapterResolver for the Chapter graphql type
|
||||||
|
type ChapterResolver struct{ C story.Chapter }
|
||||||
|
|
||||||
|
// ChapterArgs is args for channel query
|
||||||
|
type ChapterArgs struct {
|
||||||
|
ID string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Chapter implements the channel query
|
||||||
|
func (r *QueryResolver) Chapter(ctx context.Context, args *ChapterArgs) (*ChapterResolver, error) {
|
||||||
|
chapter, err := story.FindChapterID(args.ID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ChapterResolver{C: chapter}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ID resolves Chapter.id
|
||||||
|
func (r *ChapterResolver) ID() string {
|
||||||
|
return r.C.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
// StoryID resolves Chapter.storyId
|
||||||
|
func (r *ChapterResolver) StoryID() string {
|
||||||
|
return r.C.StoryID
|
||||||
|
}
|
||||||
|
|
||||||
|
// Title resolves Chapter.title
|
||||||
|
func (r *ChapterResolver) Title() string {
|
||||||
|
return r.C.Title
|
||||||
|
}
|
||||||
|
|
||||||
|
// Author resolves Chapter.author
|
||||||
|
func (r *ChapterResolver) Author() string {
|
||||||
|
return r.C.Author
|
||||||
|
}
|
||||||
|
|
||||||
|
// Source resolves Chapter.source
|
||||||
|
func (r *ChapterResolver) Source() string {
|
||||||
|
return r.C.Source
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreatedDate resolves Chapter.createdDate
|
||||||
|
func (r *ChapterResolver) CreatedDate() string {
|
||||||
|
return r.C.CreatedDate.Format(time.RFC3339Nano)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FictionalDate resolves Chapter.createdDate
|
||||||
|
func (r *ChapterResolver) FictionalDate() string {
|
||||||
|
return r.C.FictionalDate.Format(time.RFC3339Nano)
|
||||||
|
}
|
||||||
|
|
||||||
|
// EditedDate resolves Chapter.createdDate
|
||||||
|
func (r *ChapterResolver) EditedDate() string {
|
||||||
|
return r.C.EditedDate.Format(time.RFC3339Nano)
|
||||||
|
}
|
||||||
@@ -36,6 +36,10 @@ type Query {
|
|||||||
files(includePublic: Boolean, mimeTypes: [String!]): [File!]!
|
files(includePublic: Boolean, mimeTypes: [String!]): [File!]!
|
||||||
|
|
||||||
|
|
||||||
|
# Find story chapter by ID
|
||||||
|
chapter(id: String!): Chapter
|
||||||
|
|
||||||
|
|
||||||
# Find current session
|
# Find current session
|
||||||
session: Session!
|
session: Session!
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user