graph2: Added Chapter type and queries, moved loader out of old graphql pkg.
the build failed

This commit is contained in:
2018-09-16 10:33:55 +02:00
parent 0768cfd0bd
commit 875dac9ecb
10 changed files with 178 additions and 0 deletions
+15
View File
@@ -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"`
}
+36
View File
@@ -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")
})
}
+11
View File
@@ -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})
}
+11
View File
@@ -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})
}