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.

36 lines
974 B

  1. package characters
  2. import (
  3. "git.aiterp.net/rpdata/api/models"
  4. "github.com/globalsign/mgo/bson"
  5. )
  6. // Edit sets the fields of metadata. Only non-empty and different fields will be set in the
  7. // database, preventing out of order edits to two fields from conflicting
  8. func Edit(character models.Character, name, shortName, description *string) (models.Character, error) {
  9. changes := bson.M{}
  10. if name != nil && *name != character.Name {
  11. character.Name = *name
  12. changes["name"] = *name
  13. }
  14. if shortName != nil && *shortName != character.ShortName {
  15. character.ShortName = *shortName
  16. changes["shortName"] = *shortName
  17. }
  18. if description != nil && *description != character.Description {
  19. character.Description = *description
  20. changes["description"] = *description
  21. }
  22. if len(changes) == 0 {
  23. return character, nil
  24. }
  25. err := collection.UpdateId(character.ID, bson.M{"$set": changes})
  26. if err != nil {
  27. return models.Character{}, err
  28. }
  29. return character, nil
  30. }