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.

34 lines
764 B

5 years ago
5 years ago
5 years ago
5 years ago
  1. package stories
  2. import (
  3. "errors"
  4. "git.aiterp.net/rpdata/api/models"
  5. "github.com/globalsign/mgo/bson"
  6. )
  7. // ErrTagNotExists is an error returned by Story.RemoveTag
  8. var ErrTagNotExists = errors.New("Tag does not exist on story")
  9. // RemoveTag removes a tag to the story. It returns ErrTagNotExists if the tag does not exist.
  10. func RemoveTag(story models.Story, tag models.Tag) (*models.Story, error) {
  11. index := -1
  12. for i := range story.Tags {
  13. if story.Tags[i].Equal(tag) {
  14. index = i
  15. break
  16. }
  17. }
  18. if index == -1 {
  19. return nil, ErrTagNotExists
  20. }
  21. err := collection.UpdateId(story.ID, bson.M{"$pull": bson.M{"tags": tag}})
  22. if err != nil {
  23. return nil, err
  24. }
  25. story.Tags = append(story.Tags[:index], story.Tags[index+1:]...)
  26. return &story, nil
  27. }