Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a5410dff2c |
@@ -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)
|
||||
|
||||
+28
-28
@@ -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
|
||||
}
|
||||
|
||||
+29
-29
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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})
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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})
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user