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.

44 lines
946 B

  1. package posts
  2. import (
  3. "time"
  4. "git.aiterp.net/rpdata/api/models"
  5. "github.com/globalsign/mgo/bson"
  6. )
  7. // Edit edits a post and returns the result if the edit succeeded.
  8. func Edit(post models.Post, time *time.Time, kind *string, nick *string, text *string) (models.Post, error) {
  9. mutex.RLock()
  10. defer mutex.RUnlock()
  11. changes := bson.M{}
  12. if time != nil && !time.IsZero() && !time.Equal(post.Time) {
  13. changes["time"] = *time
  14. post.Time = *time
  15. }
  16. if kind != nil && *kind != "" && *kind != post.Kind {
  17. changes["kind"] = *kind
  18. post.Kind = *kind
  19. }
  20. if nick != nil && *nick != "" && *nick != post.Nick {
  21. changes["nick"] = *nick
  22. post.Nick = *nick
  23. }
  24. if text != nil && *text != "" && *text != post.Text {
  25. changes["text"] = *text
  26. post.Text = *text
  27. }
  28. if len(changes) == 0 {
  29. return post, nil
  30. }
  31. err := collection.UpdateId(post.ID, bson.M{"$set": changes})
  32. if err != nil {
  33. return models.Post{}, err
  34. }
  35. return post, nil
  36. }