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.

50 lines
1.1 KiB

  1. package logs
  2. import (
  3. "git.aiterp.net/rpdata/api/models"
  4. "github.com/globalsign/mgo/bson"
  5. )
  6. // Edit sets a log's meta-data.
  7. func Edit(log models.Log, title *string, event *string, description *string, open *bool) (models.Log, error) {
  8. changes := bson.M{}
  9. if title != nil && *title != log.Title {
  10. changes["title"] = *title
  11. log.Title = *title
  12. }
  13. if event != nil && *event != log.EventName {
  14. changes["event"] = *event
  15. log.EventName = *event
  16. }
  17. if description != nil && *description != log.Description {
  18. changes["description"] = *description
  19. log.Description = *description
  20. }
  21. if open != nil && *open != log.Open {
  22. changes["open"] = *open
  23. log.Open = *open
  24. }
  25. if len(changes) == 0 {
  26. return log, nil
  27. }
  28. err := collection.UpdateId(log.ID, bson.M{"$set": changes})
  29. if err != nil {
  30. return models.Log{}, err
  31. }
  32. // There can be only one open log. TODO: Transaction
  33. if changes["open"] != nil && *open {
  34. query := bson.M{
  35. "_id": bson.M{"$ne": log.ID},
  36. "open": true,
  37. "channel": log.ChannelName,
  38. }
  39. go collection.UpdateAll(query, bson.M{"$set": bson.M{"open": false}})
  40. }
  41. return log, nil
  42. }