From 875dac9ecbc528b20bf2d438cb327b1c18eb9b0b Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sun, 16 Sep 2018 10:33:55 +0200 Subject: [PATCH] graph2: Added Chapter type and queries, moved loader out of old graphql pkg. --- graph2/gqlgen.yml | 5 +++ graph2/graph.go | 4 +++ graph2/queries/chapter.go | 12 +++++++ graph2/schema/root.gql | 4 +++ graph2/schema/types/Chapter.gql | 59 +++++++++++++++++++++++++++++++++ graph2/types/chapter.go | 21 ++++++++++++ models/chapter.go | 15 +++++++++ models/chapters/db.go | 36 ++++++++++++++++++++ models/chapters/find.go | 11 ++++++ models/chapters/list.go | 11 ++++++ 10 files changed, 178 insertions(+) create mode 100644 graph2/queries/chapter.go create mode 100644 graph2/schema/types/Chapter.gql create mode 100644 graph2/types/chapter.go create mode 100644 models/chapter.go create mode 100644 models/chapters/db.go create mode 100644 models/chapters/find.go create mode 100644 models/chapters/list.go diff --git a/graph2/gqlgen.yml b/graph2/gqlgen.yml index 8ec91a7..33e388c 100644 --- a/graph2/gqlgen.yml +++ b/graph2/gqlgen.yml @@ -29,5 +29,10 @@ models: model: git.aiterp.net/rpdata/api/models.Log LogsFilter: model: git.aiterp.net/rpdata/api/models/logs.Filter + Chapter: + model: git.aiterp.net/rpdata/api/models.Chapter + fields: + fictionalDate: + resolver: true Date: model: git.aiterp.net/rpdata/api/models/scalars.Date \ No newline at end of file diff --git a/graph2/graph.go b/graph2/graph.go index 6fd8b46..220e580 100644 --- a/graph2/graph.go +++ b/graph2/graph.go @@ -25,3 +25,7 @@ func (r *rootResolver) Query() QueryResolver { func (r *rootResolver) Log() LogResolver { return &types.LogResolver } + +func (r *rootResolver) Chapter() ChapterResolver { + return &types.ChapterResolver +} diff --git a/graph2/queries/chapter.go b/graph2/queries/chapter.go new file mode 100644 index 0000000..b9db775 --- /dev/null +++ b/graph2/queries/chapter.go @@ -0,0 +1,12 @@ +package queries + +import ( + "context" + + "git.aiterp.net/rpdata/api/models" + "git.aiterp.net/rpdata/api/models/chapters" +) + +func (r *resolver) Chapter(ctx context.Context, id string) (models.Chapter, error) { + return chapters.FindID(id) +} diff --git a/graph2/schema/root.gql b/graph2/schema/root.gql index 4238f6f..7d8dee7 100644 --- a/graph2/schema/root.gql +++ b/graph2/schema/root.gql @@ -31,6 +31,10 @@ type Query { logs(filter: LogsFilter): [Log!]! + # Find story chapter by ID + chapter(id: String!): Chapter! + + # Find all distinct tags used in stories tags: [Tag!]! } diff --git a/graph2/schema/types/Chapter.gql b/graph2/schema/types/Chapter.gql new file mode 100644 index 0000000..f932ac7 --- /dev/null +++ b/graph2/schema/types/Chapter.gql @@ -0,0 +1,59 @@ +# A Chapter is the main content body of a story. +type Chapter { + # The chapter's ID + id: String! + + # The chapter's title + title: String! + + # The chapter's author + author: String! + + # The chapter's source + source: String! + + # When the chapter was posted initialy + createdDate: Date! + + # The in-universe date and time of the chapter + fictionalDate: Date + + # The date of edit. + editedDate: Date! +} + +# Input for addChapter mutation +input ChapterAddInput { + # The story to add a chapter to + storyId: String! + + # The title to give the chapter. It can not be left empty + title: String! + + # The author to set for the chapter. It will use the logged in user ID, and only + # users with appropriate permissions can post as another author. Even then, it's only + # for importing/moving content. This author name will not be used when logging the + # submission + author: String + + # The markdown source for the content + source: String! + + # Optionally, assign a fictional date for the chapter + fictionalDate: Date +} + +# Input for editChapter mutation +input ChapterEditInput { + # The chapter to edit + id: String! + + # Change the chapter's title + title: String + + # Change the source + source: String + + # Set the fictional date for a chapter + fictionalDate: Date +} \ No newline at end of file diff --git a/graph2/types/chapter.go b/graph2/types/chapter.go new file mode 100644 index 0000000..df1c48c --- /dev/null +++ b/graph2/types/chapter.go @@ -0,0 +1,21 @@ +package types + +import ( + "context" + "time" + + "git.aiterp.net/rpdata/api/models" +) + +type chapterResolver struct{} + +func (r *chapterResolver) FictionalDate(ctx context.Context, chapter *models.Chapter) (*time.Time, error) { + if chapter.FictionalDate.IsZero() { + return nil, nil + } + + return &chapter.FictionalDate, nil +} + +// ChapterResolver is a resolver +var ChapterResolver chapterResolver diff --git a/models/chapter.go b/models/chapter.go new file mode 100644 index 0000000..abf5b7b --- /dev/null +++ b/models/chapter.go @@ -0,0 +1,15 @@ +package models + +import "time" + +// A Chapter is a part of a story. +type Chapter struct { + ID string `bson:"_id"` + StoryID string `bson:"storyId"` + Title string `bson:"title"` + Author string `bson:"author"` + Source string `bson:"source"` + CreatedDate time.Time `bson:"createdDate"` + FictionalDate time.Time `bson:"fictionalDate,omitempty"` + EditedDate time.Time `bson:"editedDate"` +} diff --git a/models/chapters/db.go b/models/chapters/db.go new file mode 100644 index 0000000..c111baf --- /dev/null +++ b/models/chapters/db.go @@ -0,0 +1,36 @@ +package chapters + +import ( + "git.aiterp.net/rpdata/api/internal/store" + "git.aiterp.net/rpdata/api/models" + "github.com/globalsign/mgo" +) + +var collection *mgo.Collection + +func find(query interface{}) (models.Chapter, error) { + chapter := models.Chapter{} + err := collection.Find(query).One(&chapter) + + return chapter, err +} + +func list(query interface{}) ([]models.Chapter, error) { + chapters := make([]models.Chapter, 0, 8) + err := collection.Find(query).Sort("createdDate").All(&chapters) + if err != nil { + return nil, err + } + + return chapters, nil +} + +func init() { + store.HandleInit(func(db *mgo.Database) { + collection = db.C("story.chapters") + + collection.EnsureIndexKey("storyId") + collection.EnsureIndexKey("author") + collection.EnsureIndexKey("createdDate") + }) +} diff --git a/models/chapters/find.go b/models/chapters/find.go new file mode 100644 index 0000000..f326fb0 --- /dev/null +++ b/models/chapters/find.go @@ -0,0 +1,11 @@ +package chapters + +import ( + "git.aiterp.net/rpdata/api/models" + "github.com/globalsign/mgo/bson" +) + +// FindID finds a chapter by ID +func FindID(id string) (models.Chapter, error) { + return find(bson.M{"_id": id}) +} diff --git a/models/chapters/list.go b/models/chapters/list.go new file mode 100644 index 0000000..a5fa8d4 --- /dev/null +++ b/models/chapters/list.go @@ -0,0 +1,11 @@ +package chapters + +import ( + "git.aiterp.net/rpdata/api/models" + "github.com/globalsign/mgo/bson" +) + +// ListStoryID lists all chapters for the story ID +func ListStoryID(storyID string) ([]models.Chapter, error) { + return list(bson.M{"storyId": storyID}) +}