@@ -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