Added graphql for story, started on resolvers, added finders for story

This commit is contained in:
2018-06-22 06:17:41 +02:00
parent 7f49f8c4b5
commit 5a2b8ef1c5
5 changed files with 143 additions and 0 deletions
+41
View File
@@ -179,6 +179,47 @@ func New(name, author, category string, listed, open bool, tags []Tag, createdDa
return story, nil
}
// FindID finds a story by ID
func FindID(id string) (Story, error) {
story := Story{}
err := storyCollection.FindId(id).One(&story)
return story, err
}
// List lists stories by any non-zero criteria passed with it.
func List(author string, tags []Tag, earliest, latest time.Time, limit int) ([]Story, error) {
query := bson.M{}
if author != "" {
query["author"] = author
}
if len(tags) > 0 {
query["tags"] = bson.M{"$in": tags}
}
if !earliest.IsZero() && !latest.IsZero() {
query["fictionalDate"] = bson.M{
"$gte": earliest,
"$lt": latest,
}
} else if !latest.IsZero() {
query["fictionalDate"] = bson.M{
"$lt": latest,
}
} else if !earliest.IsZero() {
query["fictionalDate"] = bson.M{
"$gte": earliest,
}
}
stories := make([]Story, 0, 128)
err := storyCollection.Find(query).Limit(limit).One(&stories)
return stories, err
}
// makeStoryID makes a random story ID that's 16 characters long
func makeStoryID() string {
result := "S"