package characters import ( "git.aiterp.net/rpdata/api/models" "github.com/globalsign/mgo/bson" ) // Edit sets the fields of metadata. Only non-empty and different fields will be set in the // database, preventing out of order edits to two fields from conflicting func Edit(character models.Character, name, shortName, description *string) (models.Character, error) { changes := bson.M{} if name != nil && *name != character.Name { character.Name = *name changes["name"] = *name } if shortName != nil && *shortName != character.ShortName { character.ShortName = *shortName changes["shortName"] = *shortName } if description != nil && *description != character.Description { character.Description = *description changes["description"] = *description } if len(changes) == 0 { return character, nil } err := collection.UpdateId(character.ID, bson.M{"$set": changes}) if err != nil { return models.Character{}, err } return character, nil }