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.

33 lines
826 B

  1. package posts
  2. import (
  3. "git.aiterp.net/rpdata/api/internal/counter"
  4. "git.aiterp.net/rpdata/api/models"
  5. "github.com/globalsign/mgo/bson"
  6. )
  7. // Remove removes a post, moving all subsequent post up one position
  8. func Remove(post models.Post) (models.Post, error) {
  9. mutex.Lock()
  10. defer mutex.Unlock()
  11. err := collection.RemoveId(post.ID)
  12. if err != nil {
  13. return models.Post{}, err
  14. }
  15. _, err = collection.UpdateAll(bson.M{"logId": post.LogID, "position": bson.M{"$gt": post.Position}}, bson.M{"$inc": bson.M{"position": -1}})
  16. if err != nil {
  17. return models.Post{}, err
  18. }
  19. counter.NextMany("next_post_id", post.LogID, -1)
  20. return post, nil
  21. }
  22. // RemoveAllInLog removes all posts for the given log.
  23. func RemoveAllInLog(log models.Log) error {
  24. _, err := collection.RemoveAll(bson.M{"logId": log.ShortID})
  25. return err
  26. }