GraphQL API and utilities for the rpdata project
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.

29 lines
706 B

  1. package stories
  2. import (
  3. "errors"
  4. "git.aiterp.net/rpdata/api/models"
  5. "github.com/globalsign/mgo/bson"
  6. )
  7. // ErrTagAlreadyExists is an error returned by Story.AddTag
  8. var ErrTagAlreadyExists = errors.New("Tag already exists on story")
  9. // AddTag adds a tag to the story. It returns ErrTagAlreadyExists if the tag is already there
  10. func AddTag(story models.Story, tag models.Tag) (models.Story, error) {
  11. for i := range story.Tags {
  12. if story.Tags[i].Equal(tag) {
  13. return models.Story{}, ErrTagAlreadyExists
  14. }
  15. }
  16. err := collection.UpdateId(story.ID, bson.M{"$push": bson.M{"tags": tag}})
  17. if err != nil {
  18. return models.Story{}, err
  19. }
  20. story.Tags = append(story.Tags, tag)
  21. return story, nil
  22. }