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 }