From 87cd5e8e3ad5fd212553d3a82ba8029c588ae658 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sat, 7 Jul 2018 12:55:41 +0200 Subject: [PATCH] Added chapter query and Chapter type resolvers, added functions to get chapters --- model/story/chapter.go | 19 ++++++++++++ model/story/story.go | 5 ++++ resolver/chapter.go | 66 ++++++++++++++++++++++++++++++++++++++++++ schema/root.graphql | 4 +++ 4 files changed, 94 insertions(+) create mode 100644 resolver/chapter.go diff --git a/model/story/chapter.go b/model/story/chapter.go index 543b3ff..cbc36ad 100644 --- a/model/story/chapter.go +++ b/model/story/chapter.go @@ -61,6 +61,25 @@ func (chapter *Chapter) Remove() error { 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 func makeChapterID() string { result := "SC" diff --git a/model/story/story.go b/model/story/story.go index 844572b..fb419f6 100644 --- a/model/story/story.go +++ b/model/story/story.go @@ -124,6 +124,11 @@ func (story *Story) Remove() error { 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 // occurs func (story *Story) AddChapter(title, author, source string, createdDate, finctionalDate time.Time) (Chapter, error) { diff --git a/resolver/chapter.go b/resolver/chapter.go new file mode 100644 index 0000000..199e9dc --- /dev/null +++ b/resolver/chapter.go @@ -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) +} diff --git a/schema/root.graphql b/schema/root.graphql index b5babe8..8ac238c 100644 --- a/schema/root.graphql +++ b/schema/root.graphql @@ -36,6 +36,10 @@ type Query { files(includePublic: Boolean, mimeTypes: [String!]): [File!]! + # Find story chapter by ID + chapter(id: String!): Chapter + + # Find current session session: Session! }