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.

48 lines
975 B

  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
  9. func Submit(model, op, author string, 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. }
  23. for _, object := range objects {
  24. switch object := object.(type) {
  25. case models.Log:
  26. change.Logs = append(change.Logs, object)
  27. case models.Character:
  28. change.Characters = append(change.Characters, object)
  29. case models.Post:
  30. change.Posts = append(change.Posts, object)
  31. }
  32. }
  33. // TODO: Push to subscribers
  34. err = collection.Insert(&change)
  35. if err != nil {
  36. return models.Change{}, err
  37. }
  38. return change, nil
  39. }