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.

32 lines
906 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. err := collection.UpdateId(character.ID, changes)
  23. if err != nil {
  24. return models.Character{}, err
  25. }
  26. return character, nil
  27. }