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.

72 lines
1.9 KiB

  1. package changes
  2. import (
  3. "strconv"
  4. "time"
  5. "git.aiterp.net/rpdata/api/internal/counter"
  6. "git.aiterp.net/rpdata/api/models"
  7. )
  8. // Submit a change to the database. The objects may be any supported model, or arrays.
  9. func Submit(model, op, author string, listed bool, keys []models.ChangeKey, objects ...interface{}) (models.Change, error) {
  10. submitMutex.Lock()
  11. defer submitMutex.Unlock()
  12. id, err := counter.Next("auto_increment", "Change")
  13. if err != nil {
  14. return models.Change{}, err
  15. }
  16. change := models.Change{
  17. ID: "Change_" + strconv.Itoa(id),
  18. Model: model,
  19. Date: time.Now(),
  20. Op: op,
  21. Author: author,
  22. Keys: keys,
  23. Listed: listed,
  24. }
  25. for _, object := range objects {
  26. switch object := object.(type) {
  27. case models.Log:
  28. change.Logs = append(change.Logs, object)
  29. case []models.Log:
  30. change.Logs = append(change.Logs, object...)
  31. case models.Character:
  32. change.Characters = append(change.Characters, object)
  33. case []models.Character:
  34. change.Characters = append(change.Characters, object...)
  35. case models.Channel:
  36. change.Channels = append(change.Channels, object)
  37. case []models.Channel:
  38. change.Channels = append(change.Channels, object...)
  39. case models.Post:
  40. change.Posts = append(change.Posts, object)
  41. case []models.Post:
  42. change.Posts = append(change.Posts, object...)
  43. case models.Story:
  44. change.Stories = append(change.Stories, object)
  45. case []models.Story:
  46. change.Stories = append(change.Stories, object...)
  47. case models.Tag:
  48. change.Tags = append(change.Tags, object)
  49. case []models.Tag:
  50. change.Tags = append(change.Tags, object...)
  51. case models.Chapter:
  52. change.Chapters = append(change.Chapters, object)
  53. case []models.Chapter:
  54. change.Chapters = append(change.Chapters, object...)
  55. }
  56. }
  57. // TODO: Push to subscribers
  58. err = collection.Insert(&change)
  59. if err != nil {
  60. return models.Change{}, err
  61. }
  62. return change, nil
  63. }