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.

56 lines
1.3 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.Post:
  36. change.Posts = append(change.Posts, object)
  37. case []models.Post:
  38. change.Posts = append(change.Posts, object...)
  39. }
  40. }
  41. // TODO: Push to subscribers
  42. err = collection.Insert(&change)
  43. if err != nil {
  44. return models.Change{}, err
  45. }
  46. return change, nil
  47. }