Gisle Aune
6 years ago
9 changed files with 271 additions and 0 deletions
-
4graph2/queries/chapter.go
-
94graph2/queries/story.go
-
16graph2/schema/root.gql
-
9graph2/schema/types/Story.gql
-
29models/stories/add-tag.go
-
30models/stories/add.go
-
45models/stories/edit.go
-
34models/stories/remove-tag.go
-
10models/stories/remove.go
@ -0,0 +1,29 @@ |
|||
package stories |
|||
|
|||
import ( |
|||
"errors" |
|||
|
|||
"git.aiterp.net/rpdata/api/models" |
|||
"github.com/globalsign/mgo/bson" |
|||
) |
|||
|
|||
// ErrTagAlreadyExists is an error returned by Story.AddTag
|
|||
var ErrTagAlreadyExists = errors.New("Tag already exists on story") |
|||
|
|||
// AddTag adds a tag to the story. It returns ErrTagAlreadyExists if the tag is already there
|
|||
func AddTag(story models.Story, tag models.Tag) (models.Story, error) { |
|||
for i := range story.Tags { |
|||
if story.Tags[i].Equal(tag) { |
|||
return models.Story{}, ErrTagAlreadyExists |
|||
} |
|||
} |
|||
|
|||
err := collection.UpdateId(story.ID, bson.M{"$push": bson.M{"tags": tag}}) |
|||
if err != nil { |
|||
return models.Story{}, err |
|||
} |
|||
|
|||
story.Tags = append(story.Tags, tag) |
|||
|
|||
return story, nil |
|||
} |
@ -0,0 +1,30 @@ |
|||
package stories |
|||
|
|||
import ( |
|||
"time" |
|||
|
|||
"git.aiterp.net/rpdata/api/models" |
|||
) |
|||
|
|||
// Add creates a new story.
|
|||
func Add(name, author string, category models.StoryCategory, listed, open bool, tags []models.Tag, createdDate, fictionalDate time.Time) (models.Story, error) { |
|||
story := models.Story{ |
|||
ID: makeStoryID(), |
|||
Name: name, |
|||
Author: author, |
|||
Category: category, |
|||
Listed: listed, |
|||
Open: open, |
|||
Tags: tags, |
|||
CreatedDate: createdDate, |
|||
FictionalDate: fictionalDate, |
|||
UpdatedDate: createdDate, |
|||
} |
|||
|
|||
err := collection.Insert(story) |
|||
if err != nil { |
|||
return models.Story{}, err |
|||
} |
|||
|
|||
return story, nil |
|||
} |
@ -0,0 +1,45 @@ |
|||
package stories |
|||
|
|||
import ( |
|||
"time" |
|||
|
|||
"git.aiterp.net/rpdata/api/models" |
|||
"github.com/globalsign/mgo/bson" |
|||
) |
|||
|
|||
// Edit edits the story and returns the edited story if it succeeds.
|
|||
func Edit(story models.Story, name *string, category *models.StoryCategory, listed, open *bool, fictionalDate *time.Time) (models.Story, error) { |
|||
changes := bson.M{} |
|||
|
|||
if name != nil && *name != story.Name { |
|||
changes["name"] = *name |
|||
story.Name = *name |
|||
} |
|||
if category != nil && *category != story.Category { |
|||
changes["category"] = *category |
|||
story.Category = *category |
|||
} |
|||
if listed != nil && *listed != story.Listed { |
|||
changes["listed"] = *listed |
|||
story.Listed = *listed |
|||
} |
|||
if open != nil && *open != story.Open { |
|||
changes["open"] = *open |
|||
story.Open = *open |
|||
} |
|||
if fictionalDate != nil && !fictionalDate.Equal(story.FictionalDate) { |
|||
changes["fictionalDate"] = *fictionalDate |
|||
story.FictionalDate = *fictionalDate |
|||
} |
|||
|
|||
if len(changes) == 0 { |
|||
return story, nil |
|||
} |
|||
|
|||
err := collection.UpdateId(story.ID, bson.M{"$set": changes}) |
|||
if err != nil { |
|||
return models.Story{}, err |
|||
} |
|||
|
|||
return story, nil |
|||
} |
@ -0,0 +1,34 @@ |
|||
package stories |
|||
|
|||
import ( |
|||
"errors" |
|||
|
|||
"git.aiterp.net/rpdata/api/models" |
|||
"github.com/globalsign/mgo/bson" |
|||
) |
|||
|
|||
// ErrTagNotExists is an error returned by Story.RemoveTag
|
|||
var ErrTagNotExists = errors.New("Tag does not exist on story") |
|||
|
|||
// RemoveTag removes a tag to the story. It returns ErrTagNotExists if the tag does not exist.
|
|||
func RemoveTag(story models.Story, tag models.Tag) (models.Story, error) { |
|||
index := -1 |
|||
for i := range story.Tags { |
|||
if story.Tags[i].Equal(tag) { |
|||
index = i |
|||
break |
|||
} |
|||
} |
|||
if index == -1 { |
|||
return models.Story{}, ErrTagNotExists |
|||
} |
|||
|
|||
err := collection.UpdateId(story.ID, bson.M{"$pull": bson.M{"tags": tag}}) |
|||
if err != nil { |
|||
return models.Story{}, err |
|||
} |
|||
|
|||
story.Tags = append(story.Tags[:index], story.Tags[index+1:]...) |
|||
|
|||
return story, nil |
|||
} |
@ -0,0 +1,10 @@ |
|||
package stories |
|||
|
|||
import ( |
|||
"git.aiterp.net/rpdata/api/models" |
|||
) |
|||
|
|||
// Remove the story from the database
|
|||
func Remove(story models.Story) (models.Story, error) { |
|||
return story, collection.RemoveId(story.ID) |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue