diff --git a/graph2/queries/channel.go b/graph2/queries/channel.go index 79b5049..24e960a 100644 --- a/graph2/queries/channel.go +++ b/graph2/queries/channel.go @@ -14,7 +14,7 @@ import ( // Queries -func (r *resolver) Channel(ctx context.Context, name string) (models.Channel, error) { +func (r *resolver) Channel(ctx context.Context, name string) (*models.Channel, error) { return channels.FindName(name) } @@ -24,10 +24,10 @@ func (r *resolver) Channels(ctx context.Context, filter *channels.Filter) ([]mod // Mutations -func (r *mutationResolver) AddChannel(ctx context.Context, input input.ChannelAddInput) (models.Channel, error) { +func (r *mutationResolver) AddChannel(ctx context.Context, input input.ChannelAddInput) (*models.Channel, error) { token := auth.TokenFromContext(ctx) if !token.Authenticated() || !token.Permitted("channel.add") { - return models.Channel{}, errors.New("You are not permitted to add channels") + return nil, errors.New("You are not permitted to add channels") } logged := false @@ -49,7 +49,7 @@ func (r *mutationResolver) AddChannel(ctx context.Context, input input.ChannelAd channel, err := channels.Add(input.Name, logged, hub, eventName, locationName) if err != nil { - return models.Channel{}, errors.New("Failed to add channel: " + err.Error()) + return nil, errors.New("Failed to add channel: " + err.Error()) } go changes.Submit("Channel", "add", token.UserID, true, changekeys.Listed(channel), channel) @@ -57,20 +57,20 @@ func (r *mutationResolver) AddChannel(ctx context.Context, input input.ChannelAd return channel, nil } -func (r *mutationResolver) EditChannel(ctx context.Context, input input.ChannelEditInput) (models.Channel, error) { +func (r *mutationResolver) EditChannel(ctx context.Context, input input.ChannelEditInput) (*models.Channel, error) { token := auth.TokenFromContext(ctx) if !token.Authenticated() || !token.Permitted("channel.edit") { - return models.Channel{}, errors.New("You are not permitted to edit channels") + return nil, errors.New("You are not permitted to edit channels") } channel, err := channels.FindName(input.Name) if err != nil { - return models.Channel{}, errors.New("Channel not found") + return nil, errors.New("Channel not found") } - channel, err = channels.Edit(channel, input.Logged, input.Hub, input.EventName, input.LocationName) + channel, err = channels.Edit(*channel, input.Logged, input.Hub, input.EventName, input.LocationName) if err != nil { - return models.Channel{}, errors.New("Failed to edit channel: " + err.Error()) + return nil, errors.New("Failed to edit channel: " + err.Error()) } go changes.Submit("Channel", "edit", token.UserID, true, changekeys.Listed(channel), channel) diff --git a/graph2/queries/chapter.go b/graph2/queries/chapter.go index 55582ca..5a2ddff 100644 --- a/graph2/queries/chapter.go +++ b/graph2/queries/chapter.go @@ -19,34 +19,34 @@ import ( // Queries -func (r *resolver) Chapter(ctx context.Context, id string) (models.Chapter, error) { +func (r *resolver) Chapter(ctx context.Context, id string) (*models.Chapter, error) { return chapters.FindID(id) } // Mutations -func (r *mutationResolver) AddChapter(ctx context.Context, input input.ChapterAddInput) (models.Chapter, error) { +func (r *mutationResolver) AddChapter(ctx context.Context, input input.ChapterAddInput) (*models.Chapter, error) { story, err := stories.FindID(input.StoryID) if err != nil { - return models.Chapter{}, errors.New("Story not found") + return nil, errors.New("Story not found") } token := auth.TokenFromContext(ctx) if !token.Permitted("member", "story.add") { - return models.Chapter{}, errors.New("Unauthorized") + return nil, errors.New("Unauthorized") } author := token.UserID if input.Author != nil && *input.Author != author { if !token.Permitted("story.add") { - return models.Chapter{}, errors.New("False pretender") + return nil, errors.New("False pretender") } author = *input.Author } if !story.Open && story.Author != author { - return models.Chapter{}, errors.New("Story is not open") + return nil, errors.New("Story is not open") } commentMode := models.ChapterCommentModeDisabled @@ -54,40 +54,40 @@ func (r *mutationResolver) AddChapter(ctx context.Context, input input.ChapterAd commentMode = *input.CommentMode } - chapter, err := chapters.Add(story, input.Title, author, input.Source, time.Now(), input.FictionalDate, commentMode) + chapter, err := chapters.Add(*story, input.Title, author, input.Source, time.Now(), input.FictionalDate, commentMode) if err != nil { - return models.Chapter{}, errors.New("Failed to create chapter: " + err.Error()) + return nil, errors.New("Failed to create chapter: " + err.Error()) } go changes.Submit("Chapter", "add", token.UserID, story.Listed, changekeys.Listed(story, chapter), story, chapter) - return chapter, nil + return &chapter, nil } -func (r *mutationResolver) MoveChapter(ctx context.Context, input input.ChapterMoveInput) (models.Chapter, error) { +func (r *mutationResolver) MoveChapter(ctx context.Context, input input.ChapterMoveInput) (*models.Chapter, error) { chapter, err := chapters.FindID(input.ID) if err != nil { - return models.Chapter{}, errors.New("Chapter not found") + return nil, errors.New("Chapter not found") } token := auth.TokenFromContext(ctx) if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.move") { - return models.Chapter{}, errors.New("You are not allowed to move this chapter") + return nil, errors.New("You are not allowed to move this chapter") } target, err := stories.FindID(input.StoryID) if err != nil { - return models.Chapter{}, errors.New("Target story not found") + return nil, errors.New("Target story not found") } if !target.Open && !token.PermittedUser(target.Author, "member", "chapter.move") { - return models.Chapter{}, errors.New("You are not permitted to move chapters to this story") + return nil, errors.New("You are not permitted to move chapters to this story") } oldStoryID := chapter.StoryID - chapter, err = chapters.Move(chapter, target) + chapter, err = chapters.Move(chapter, *target) if err != nil { - return models.Chapter{}, errors.New("Failed to move chapter: " + err.Error()) + return nil, errors.New("Failed to move chapter: " + err.Error()) } go func() { @@ -105,18 +105,18 @@ func (r *mutationResolver) MoveChapter(ctx context.Context, input input.ChapterM changes.Submit("Chapter", "move-in", token.UserID, story.Listed, changekeys.Listed(story, chapter), story, chapter) }() - return chapter, nil + return &chapter, nil } -func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterEditInput) (models.Chapter, error) { +func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterEditInput) (*models.Chapter, error) { chapter, err := chapters.FindID(input.ID) if err != nil { - return models.Chapter{}, errors.New("Chapter not found") + return nil, errors.New("Chapter not found") } token := auth.TokenFromContext(ctx) if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.edit") { - return models.Chapter{}, errors.New("Unauthorized") + return nil, errors.New("Unauthorized") } if input.ClearFictionalDate != nil && *input.ClearFictionalDate == true { @@ -125,7 +125,7 @@ func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterE chapter, err = chapters.Edit(chapter, input.Title, input.Source, input.FictionalDate, input.CommentMode, input.CommentsLocked) if err != nil { - return models.Chapter{}, errors.New("Failed to edit chapter: " + err.Error()) + return nil, errors.New("Failed to edit chapter: " + err.Error()) } go func() { @@ -137,28 +137,28 @@ func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterE changes.Submit("Chapter", "edit", token.UserID, story.Listed, changekeys.Many(story, chapter), chapter) }() - return chapter, nil + return &chapter, nil } -func (r *mutationResolver) RemoveChapter(ctx context.Context, input input.ChapterRemoveInput) (models.Chapter, error) { +func (r *mutationResolver) RemoveChapter(ctx context.Context, input input.ChapterRemoveInput) (*models.Chapter, error) { chapter, err := chapters.FindID(input.ID) if err != nil { - return models.Chapter{}, errors.New("Chapter not found") + return nil, errors.New("Chapter not found") } token := auth.TokenFromContext(ctx) if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.remove") { - return models.Chapter{}, errors.New("Unauthorized") + return nil, errors.New("Unauthorized") } chapter, err = chapters.Remove(chapter) if err != nil { - return models.Chapter{}, errors.New("Failed to remove chapter: " + err.Error()) + return nil, errors.New("Failed to remove chapter: " + err.Error()) } err = comments.RemoveChapter(chapter) if err != nil { - return models.Chapter{}, errors.New("Chapter was removed, but comment removal failed: " + err.Error()) + return nil, errors.New("Chapter was removed, but comment removal failed: " + err.Error()) } go func() { @@ -170,5 +170,5 @@ func (r *mutationResolver) RemoveChapter(ctx context.Context, input input.Chapte changes.Submit("Chapter", "remove", token.UserID, story.Listed, changekeys.Many(story, chapter), chapter) }() - return chapter, nil + return &chapter, nil } diff --git a/graph2/queries/story.go b/graph2/queries/story.go index a857664..c9f4479 100644 --- a/graph2/queries/story.go +++ b/graph2/queries/story.go @@ -15,7 +15,7 @@ import ( "git.aiterp.net/rpdata/api/models/stories" ) -func (r *resolver) Story(ctx context.Context, id string) (models.Story, error) { +func (r *resolver) Story(ctx context.Context, id string) (*models.Story, error) { return stories.FindID(id) } @@ -38,16 +38,16 @@ func (r *resolver) Stories(ctx context.Context, filter *stories.Filter) ([]model // Mutations -func (r *mutationResolver) AddStory(ctx context.Context, input input.StoryAddInput) (models.Story, error) { +func (r *mutationResolver) AddStory(ctx context.Context, input input.StoryAddInput) (*models.Story, error) { token := auth.TokenFromContext(ctx) if token == nil || !token.Permitted("member", "story.add") { - return models.Story{}, errors.New("Permission denied") + return nil, errors.New("Permission denied") } author := token.UserID if input.Author != nil && *input.Author != author { if !token.Permitted("story.add") { - return models.Story{}, errors.New("You are not permitted to add a story in another author's name") + return nil, errors.New("You are not permitted to add a story in another author's name") } author = *input.Author @@ -63,7 +63,7 @@ func (r *mutationResolver) AddStory(ctx context.Context, input input.StoryAddInp story, err := stories.Add(input.Name, author, input.Category, listed, open, input.Tags, time.Now(), fictionalDate) if err != nil { - return models.Story{}, errors.New("Failed to add story: " + err.Error()) + return nil, errors.New("Failed to add story: " + err.Error()) } go changes.Submit("Story", "add", token.UserID, story.Listed, changekeys.Listed(story), story) @@ -71,27 +71,27 @@ func (r *mutationResolver) AddStory(ctx context.Context, input input.StoryAddInp return story, nil } -func (r *mutationResolver) AddStoryTag(ctx context.Context, input input.StoryTagAddInput) (models.Story, error) { +func (r *mutationResolver) AddStoryTag(ctx context.Context, input input.StoryTagAddInput) (*models.Story, error) { token := auth.TokenFromContext(ctx) story, err := stories.FindID(input.ID) if err != nil { - return models.Story{}, errors.New("Story not found") + return nil, errors.New("Story not found") } if story.Open { if !token.Permitted("member") { - return models.Story{}, errors.New("You are not permitted to edit this story") + return nil, errors.New("You are not permitted to edit this story") } } else { if !token.PermittedUser(story.Author, "member", "story.edit") { - return models.Story{}, errors.New("You are not permitted to edit this story") + return nil, errors.New("You are not permitted to edit this story") } } - story, err = stories.AddTag(story, input.Tag) + story, err = stories.AddTag(*story, input.Tag) if err != nil { - return models.Story{}, errors.New("Failed to add story: " + err.Error()) + return nil, errors.New("Failed to add story: " + err.Error()) } go changes.Submit("Story", "tag", token.UserID, story.Listed, changekeys.Listed(story), story, input.Tag) @@ -99,27 +99,27 @@ func (r *mutationResolver) AddStoryTag(ctx context.Context, input input.StoryTag return story, nil } -func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input input.StoryTagRemoveInput) (models.Story, error) { +func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input input.StoryTagRemoveInput) (*models.Story, error) { token := auth.TokenFromContext(ctx) story, err := stories.FindID(input.ID) if err != nil { - return models.Story{}, errors.New("Story not found") + return nil, errors.New("Story not found") } if story.Open { if !token.Permitted("member") { - return models.Story{}, errors.New("You are not permitted to edit this story") + return nil, errors.New("You are not permitted to edit this story") } } else { if !token.PermittedUser(story.Author, "member", "story.edit") { - return models.Story{}, errors.New("You are not permitted to edit this story") + return nil, errors.New("You are not permitted to edit this story") } } - story, err = stories.RemoveTag(story, input.Tag) + story, err = stories.RemoveTag(*story, input.Tag) if err != nil { - return models.Story{}, errors.New("Failed to add story: " + err.Error()) + return nil, errors.New("Failed to add story: " + err.Error()) } go changes.Submit("Story", "untag", token.UserID, story.Listed, changekeys.Listed(story), story, input.Tag) @@ -127,25 +127,25 @@ func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input input.Story return story, nil } -func (r *mutationResolver) EditStory(ctx context.Context, input input.StoryEditInput) (models.Story, error) { +func (r *mutationResolver) EditStory(ctx context.Context, input input.StoryEditInput) (*models.Story, error) { token := auth.TokenFromContext(ctx) story, err := stories.FindID(input.ID) if err != nil { - return models.Story{}, errors.New("Story not found") + return nil, errors.New("Story not found") } if !token.PermittedUser(story.Author, "member", "story.edit") { - return models.Story{}, errors.New("You are not permitted to remove this story") + return nil, errors.New("You are not permitted to remove this story") } if input.ClearFictionalDate != nil && *input.ClearFictionalDate { input.FictionalDate = &time.Time{} } - story, err = stories.Edit(story, input.Name, input.Category, input.Listed, input.Open, input.FictionalDate) + story, err = stories.Edit(*story, input.Name, input.Category, input.Listed, input.Open, input.FictionalDate) if err != nil { - return models.Story{}, errors.New("Failed to add story: " + err.Error()) + return nil, errors.New("Failed to add story: " + err.Error()) } go changes.Submit("Story", "edit", token.UserID, story.Listed, changekeys.Listed(story), story) @@ -153,26 +153,26 @@ func (r *mutationResolver) EditStory(ctx context.Context, input input.StoryEditI return story, nil } -func (r *mutationResolver) RemoveStory(ctx context.Context, input input.StoryRemoveInput) (models.Story, error) { +func (r *mutationResolver) RemoveStory(ctx context.Context, input input.StoryRemoveInput) (*models.Story, error) { token := auth.TokenFromContext(ctx) story, err := stories.FindID(input.ID) if err != nil { - return models.Story{}, errors.New("Story not found") + return nil, errors.New("Story not found") } if !token.PermittedUser(story.Author, "member", "story.remove") { - return models.Story{}, errors.New("You are not permitted to remove this story") + return nil, errors.New("You are not permitted to remove this story") } - story, err = stories.Remove(story) + story, err = stories.Remove(*story) if err != nil { - return models.Story{}, err + return nil, err } - err = chapters.RemoveStory(story) + err = chapters.RemoveStory(*story) if err != nil { - return models.Story{}, errors.New("Failed to remove chapters, but story is removed: " + err.Error()) + return nil, errors.New("Failed to remove chapters, but story is removed: " + err.Error()) } go changes.Submit("Story", "remove", token.UserID, story.Listed, changekeys.Listed(story), story) diff --git a/graph2/types/change.go b/graph2/types/change.go index b1f9a48..3006e35 100644 --- a/graph2/types/change.go +++ b/graph2/types/change.go @@ -2,6 +2,7 @@ package types import ( "context" + "log" "git.aiterp.net/rpdata/api/graph2/input" "git.aiterp.net/rpdata/api/models" @@ -12,9 +13,13 @@ type changeResolver struct{} func (r *changeResolver) Objects(ctx context.Context, obj *models.Change) ([]input.ChangeObject, error) { objects := obj.Objects() - results := make([]input.ChangeObject, len(objects)) - for i := range objects { - results[i] = objects[i] + results := make([]input.ChangeObject, 0, len(objects)) + for _, object := range objects { + if cObj, ok := object.(input.ChangeObject); ok { + results = append(results, cObj) + } else { + log.Printf("Warning: %T is not a change object", object) + } } return results, nil diff --git a/models/channel.go b/models/channel.go index d65e23f..cb6e8c9 100644 --- a/models/channel.go +++ b/models/channel.go @@ -8,3 +8,8 @@ type Channel struct { EventName string `bson:"eventName,omitempty"` LocationName string `bson:"locationName,omitempty"` } + +// IsChangeObject is a dummy interface implementation +func (*Channel) IsChangeObject() { + // Dummy interface implementation +} diff --git a/models/channels/add.go b/models/channels/add.go index 1103ce0..8a91005 100644 --- a/models/channels/add.go +++ b/models/channels/add.go @@ -11,9 +11,9 @@ import ( var ErrInvalidName = errors.New("Invalid channel name") // Add creates a new channel. -func Add(name string, logged, hub bool, event, location string) (models.Channel, error) { +func Add(name string, logged, hub bool, event, location string) (*models.Channel, error) { if len(name) < 3 && !strings.HasPrefix(name, "#") { - return models.Channel{}, ErrInvalidName + return nil, ErrInvalidName } channel := models.Channel{ @@ -26,8 +26,8 @@ func Add(name string, logged, hub bool, event, location string) (models.Channel, err := collection.Insert(channel) if err != nil { - return models.Channel{}, err + return nil, err } - return channel, nil + return &channel, nil } diff --git a/models/channels/edit.go b/models/channels/edit.go index 3cdf5cc..39eadb7 100644 --- a/models/channels/edit.go +++ b/models/channels/edit.go @@ -6,7 +6,7 @@ import ( ) // Edit edits a channels -func Edit(channel models.Channel, logged, hub *bool, eventName, locationName *string) (models.Channel, error) { +func Edit(channel models.Channel, logged, hub *bool, eventName, locationName *string) (*models.Channel, error) { mutation := bson.M{} if logged != nil && *logged != channel.Logged { @@ -27,13 +27,13 @@ func Edit(channel models.Channel, logged, hub *bool, eventName, locationName *st } if len(mutation) == 0 { - return channel, nil + return &channel, nil } err := collection.UpdateId(channel.Name, bson.M{"$set": mutation}) if err != nil { - return models.Channel{}, err + return nil, err } - return channel, nil + return &channel, nil } diff --git a/models/channels/ensure.go b/models/channels/ensure.go index 5eddef7..f0d2d32 100644 --- a/models/channels/ensure.go +++ b/models/channels/ensure.go @@ -10,22 +10,22 @@ import ( // Ensure adds a channel if it doesn't exist. If logged is set and the found channel isn't logged, // that is changed. -func Ensure(name string, logged bool) (models.Channel, error) { +func Ensure(name string, logged bool) (*models.Channel, error) { if len(name) < 3 && !strings.HasPrefix(name, "#") { - return models.Channel{}, ErrInvalidName + return nil, ErrInvalidName } channel, err := FindName(name) if err == mgo.ErrNotFound { return Add(name, logged, false, "", "") } else if err != nil { - return models.Channel{}, err + return nil, err } if logged && !channel.Logged { - channel, err = Edit(channel, &logged, nil, nil, nil) + channel, err = Edit(*channel, &logged, nil, nil, nil) if err != nil { - return models.Channel{}, err + return nil, err } } diff --git a/models/channels/find.go b/models/channels/find.go index 50ef371..461dd28 100644 --- a/models/channels/find.go +++ b/models/channels/find.go @@ -3,9 +3,12 @@ package channels import "git.aiterp.net/rpdata/api/models" // FindName finds a channel by its id (its name). -func FindName(name string) (models.Channel, error) { +func FindName(name string) (*models.Channel, error) { channel := models.Channel{} err := collection.FindId(name).One(&channel) + if err != nil { + return nil, err + } - return channel, err + return &channel, nil } diff --git a/models/chapter.go b/models/chapter.go index e795af7..ca51738 100644 --- a/models/chapter.go +++ b/models/chapter.go @@ -20,3 +20,8 @@ type Chapter struct { func (chapter *Chapter) CanComment() bool { return !chapter.CommentsLocked && chapter.CommentMode.IsEnabled() } + +// IsChangeObject is a dummy interface implementation +func (*Chapter) IsChangeObject() { + // Dummy interface implementation +} diff --git a/models/chapters/add.go b/models/chapters/add.go index 37c7dfd..8ad13a1 100644 --- a/models/chapters/add.go +++ b/models/chapters/add.go @@ -8,7 +8,7 @@ import ( ) // Add adds a new chapter. -func Add(story models.Story, title, author, source string, createdDate time.Time, finctionalDate *time.Time, commentMode models.ChapterCommentMode) (models.Chapter, error) { +func Add(story models.Story, title, author, source string, createdDate time.Time, finctionalDate *time.Time, commentMode models.ChapterCommentMode) (*models.Chapter, error) { chapter := models.Chapter{ ID: makeChapterID(), StoryID: story.ID, @@ -27,7 +27,7 @@ func Add(story models.Story, title, author, source string, createdDate time.Time err := collection.Insert(chapter) if err != nil { - return models.Chapter{}, err + return nil, err } if createdDate.After(story.UpdatedDate) { @@ -36,5 +36,5 @@ func Add(story models.Story, title, author, source string, createdDate time.Time } } - return chapter, nil + return &chapter, nil } diff --git a/models/chapters/db.go b/models/chapters/db.go index 71d791a..15961e6 100644 --- a/models/chapters/db.go +++ b/models/chapters/db.go @@ -13,11 +13,14 @@ import ( var collection *mgo.Collection var storyCollection *mgo.Collection -func find(query interface{}) (models.Chapter, error) { +func find(query interface{}) (*models.Chapter, error) { chapter := models.Chapter{} err := collection.Find(query).One(&chapter) + if err != nil { + return nil, err + } - return chapter, err + return &chapter, err } func list(query interface{}) ([]models.Chapter, error) { diff --git a/models/chapters/edit.go b/models/chapters/edit.go index fddefb7..449a800 100644 --- a/models/chapters/edit.go +++ b/models/chapters/edit.go @@ -9,7 +9,7 @@ import ( // Edit edits a chapter, and updates EditedDate. While many Edit functions cheat if there's nothing to // change, this functill will due to EditedDate. -func Edit(chapter models.Chapter, title, source *string, fictionalDate *time.Time, commentMode *models.ChapterCommentMode, commentsLocked *bool) (models.Chapter, error) { +func Edit(chapter models.Chapter, title, source *string, fictionalDate *time.Time, commentMode *models.ChapterCommentMode, commentsLocked *bool) (*models.Chapter, error) { now := time.Now() changes := bson.M{"editedDate": now} @@ -39,8 +39,8 @@ func Edit(chapter models.Chapter, title, source *string, fictionalDate *time.Tim err := collection.UpdateId(chapter.ID, bson.M{"$set": changes}) if err != nil { - return chapter, err + return nil, err } - return edited, nil + return &edited, nil } diff --git a/models/chapters/find.go b/models/chapters/find.go index f326fb0..dff4d47 100644 --- a/models/chapters/find.go +++ b/models/chapters/find.go @@ -6,6 +6,6 @@ import ( ) // FindID finds a chapter by ID -func FindID(id string) (models.Chapter, error) { +func FindID(id string) (*models.Chapter, error) { return find(bson.M{"_id": id}) } diff --git a/models/chapters/move.go b/models/chapters/move.go index c60328f..bc7d3e3 100644 --- a/models/chapters/move.go +++ b/models/chapters/move.go @@ -8,12 +8,12 @@ import ( ) // Move updates the chapter, moving it to the given story. -func Move(chapter models.Chapter, story models.Story) (models.Chapter, error) { +func Move(chapter models.Chapter, story models.Story) (*models.Chapter, error) { now := time.Now() err := collection.UpdateId(chapter.ID, bson.M{"$set": bson.M{"editedDate": now, "storyId": story.ID}}) if err != nil { - return models.Chapter{}, err + return nil, err } chapter.EditedDate = now @@ -24,5 +24,5 @@ func Move(chapter models.Chapter, story models.Story) (models.Chapter, error) { } } - return chapter, nil + return &chapter, nil } diff --git a/models/chapters/remove.go b/models/chapters/remove.go index dd300b6..3e517fb 100644 --- a/models/chapters/remove.go +++ b/models/chapters/remove.go @@ -6,9 +6,9 @@ import ( ) // Remove removes a chapter. -func Remove(chapter models.Chapter) (models.Chapter, error) { +func Remove(chapter models.Chapter) (*models.Chapter, error) { if err := collection.RemoveId(chapter.ID); err != nil { - return models.Chapter{}, err + return nil, err } return chapter, nil diff --git a/models/character.go b/models/character.go index 37e819a..a13b27c 100644 --- a/models/character.go +++ b/models/character.go @@ -29,3 +29,8 @@ func (character *Character) HasNick(nick string) bool { return false } + +// IsChangeObject is a dummy interface implementation +func (*Character) IsChangeObject() { + // Dummy interface implementation +} diff --git a/models/comment.go b/models/comment.go index f1ccd31..15063f4 100644 --- a/models/comment.go +++ b/models/comment.go @@ -15,3 +15,8 @@ type Comment struct { EditedDate time.Time `bson:"editeddDate"` Source string `bson:"source"` } + +// IsChangeObject is a dummy interface implementation +func (*Comment) IsChangeObject() { + // Dummy interface implementation +} diff --git a/models/file.go b/models/file.go index 97a9013..470632b 100644 --- a/models/file.go +++ b/models/file.go @@ -14,3 +14,8 @@ type File struct { Author string `bson:"author" json:"author"` URL string `bson:"url,omitempty" json:"url,omitempty"` } + +// IsChangeObject is a dummy interface implementation +func (*File) IsChangeObject() { + // Dummy interface implementation +} diff --git a/models/log.go b/models/log.go index 5171ea8..77089e9 100644 --- a/models/log.go +++ b/models/log.go @@ -14,3 +14,8 @@ type Log struct { Open bool `bson:"open"` CharacterIDs []string `bson:"characterIds"` } + +// IsChangeObject is a dummy interface implementation +func (*Log) IsChangeObject() { + // Dummy interface implementation +} diff --git a/models/post.go b/models/post.go index 4769ca4..4f142c4 100644 --- a/models/post.go +++ b/models/post.go @@ -12,3 +12,8 @@ type Post struct { Text string `bson:"text"` Position int `bson:"position"` } + +// IsChangeObject is a dummy interface implementation +func (*Post) IsChangeObject() { + // Dummy interface implementation +} diff --git a/models/stories/add-tag.go b/models/stories/add-tag.go index 55a189e..ed4ed4f 100644 --- a/models/stories/add-tag.go +++ b/models/stories/add-tag.go @@ -11,19 +11,19 @@ import ( 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) { +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 + return nil, ErrTagAlreadyExists } } err := collection.UpdateId(story.ID, bson.M{"$push": bson.M{"tags": tag}}) if err != nil { - return models.Story{}, err + return nil, err } story.Tags = append(story.Tags, tag) - return story, nil + return &story, nil } diff --git a/models/stories/add.go b/models/stories/add.go index 9d0b4bd..4148dc7 100644 --- a/models/stories/add.go +++ b/models/stories/add.go @@ -7,7 +7,7 @@ import ( ) // 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) { +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, @@ -23,8 +23,8 @@ func Add(name, author string, category models.StoryCategory, listed, open bool, err := collection.Insert(story) if err != nil { - return models.Story{}, err + return nil, err } - return story, nil + return &story, nil } diff --git a/models/stories/db.go b/models/stories/db.go index 1a48556..f383939 100644 --- a/models/stories/db.go +++ b/models/stories/db.go @@ -12,11 +12,14 @@ import ( var collection *mgo.Collection -func find(query interface{}) (models.Story, error) { +func find(query interface{}) (*models.Story, error) { story := models.Story{} err := collection.Find(query).One(&story) + if err != nil { + return nil, err + } - return story, err + return &story, err } func list(query interface{}, limit int) ([]models.Story, error) { diff --git a/models/stories/edit.go b/models/stories/edit.go index fae96c8..d441ade 100644 --- a/models/stories/edit.go +++ b/models/stories/edit.go @@ -8,7 +8,7 @@ import ( ) // 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) { +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 { @@ -33,13 +33,13 @@ func Edit(story models.Story, name *string, category *models.StoryCategory, list } if len(changes) == 0 { - return story, nil + return &story, nil } err := collection.UpdateId(story.ID, bson.M{"$set": changes}) if err != nil { - return models.Story{}, err + return nil, err } - return story, nil + return &story, nil } diff --git a/models/stories/find.go b/models/stories/find.go index a48e472..3ae5367 100644 --- a/models/stories/find.go +++ b/models/stories/find.go @@ -6,6 +6,6 @@ import ( ) // FindID finds a story by ID -func FindID(id string) (models.Story, error) { +func FindID(id string) (*models.Story, error) { return find(bson.M{"_id": id}) } diff --git a/models/stories/remove-tag.go b/models/stories/remove-tag.go index ec7300b..a27b0c2 100644 --- a/models/stories/remove-tag.go +++ b/models/stories/remove-tag.go @@ -11,7 +11,7 @@ import ( 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) { +func RemoveTag(story models.Story, tag models.Tag) (*models.Story, error) { index := -1 for i := range story.Tags { if story.Tags[i].Equal(tag) { @@ -20,15 +20,15 @@ func RemoveTag(story models.Story, tag models.Tag) (models.Story, error) { } } if index == -1 { - return models.Story{}, ErrTagNotExists + return nil, ErrTagNotExists } err := collection.UpdateId(story.ID, bson.M{"$pull": bson.M{"tags": tag}}) if err != nil { - return models.Story{}, err + return nil, err } story.Tags = append(story.Tags[:index], story.Tags[index+1:]...) - return story, nil + return &story, nil } diff --git a/models/stories/remove.go b/models/stories/remove.go index 7f2a157..4b67161 100644 --- a/models/stories/remove.go +++ b/models/stories/remove.go @@ -5,6 +5,6 @@ import ( ) // Remove the story from the database -func Remove(story models.Story) (models.Story, error) { - return story, collection.RemoveId(story.ID) +func Remove(story models.Story) (*models.Story, error) { + return &story, collection.RemoveId(story.ID) } diff --git a/models/story.go b/models/story.go index 8910b39..790fe4e 100644 --- a/models/story.go +++ b/models/story.go @@ -16,3 +16,8 @@ type Story struct { FictionalDate time.Time `bson:"fictionalDate,omitempty"` UpdatedDate time.Time `bson:"updatedDate"` } + +// IsChangeObject is a dummy interface implementation +func (*Story) IsChangeObject() { + // Dummy interface implementation +} diff --git a/models/tag.go b/models/tag.go index a9f37c3..4e4d4b5 100644 --- a/models/tag.go +++ b/models/tag.go @@ -10,3 +10,8 @@ type Tag struct { func (tag *Tag) Equal(other Tag) bool { return tag.Kind == other.Kind && tag.Name == other.Name } + +// IsChangeObject is a dummy interface implementation +func (*Tag) IsChangeObject() { + // Dummy interface implementation +}