Browse Source

graph2: Added add/remove nick mutations to character.

1.0
Gisle Aune 6 years ago
parent
commit
86125c7ce9
  1. 27
      models/characters/add-nick.go
  2. 32
      models/characters/remove-nick.go

27
models/characters/add-nick.go

@ -0,0 +1,27 @@
package characters
import (
"errors"
"strings"
"git.aiterp.net/rpdata/api/models"
"github.com/globalsign/mgo/bson"
)
// AddNick adds a nick to a characters
func AddNick(character models.Character, nick string) (models.Character, error) {
for i := range character.Nicks {
if strings.EqualFold(character.Nicks[i], nick) {
return models.Character{}, errors.New("Nick already exists")
}
}
err := collection.UpdateId(character.ID, bson.M{"$push": bson.M{"nicks": nick}})
if err != nil {
return models.Character{}, err
}
character.Nicks = append(character.Nicks, nick)
return character, nil
}

32
models/characters/remove-nick.go

@ -0,0 +1,32 @@
package characters
import (
"errors"
"strings"
"git.aiterp.net/rpdata/api/models"
"github.com/globalsign/mgo/bson"
)
// RemoveNick removes a nick to a characters
func RemoveNick(character models.Character, nick string) (models.Character, error) {
index := -1
for i := range character.Nicks {
if strings.EqualFold(character.Nicks[i], nick) {
index = i
break
}
}
if index == -1 {
return models.Character{}, errors.New("Nick does not exist")
}
err := collection.UpdateId(character.ID, bson.M{"$pull": bson.M{"nicks": nick}})
if err != nil {
return models.Character{}, err
}
character.Nicks = append(character.Nicks[:index], character.Nicks[index+1:]...)
return character, nil
}
Loading…
Cancel
Save