You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
784 B
34 lines
784 B
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
|
|
}
|