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.
48 lines
1.2 KiB
48 lines
1.2 KiB
package characters
|
|
|
|
import (
|
|
"errors"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.aiterp.net/rpdata/api/model/counter"
|
|
"git.aiterp.net/rpdata/api/models"
|
|
)
|
|
|
|
// Add creates a Character and pushes it to the database. It does some validation
|
|
// on nick, name, shortName and author. It will generate a shortname from the first
|
|
// name if a blank one is provided.
|
|
func Add(nick, name, shortName, author, description string) (models.Character, error) {
|
|
if len(nick) < 1 || len(name) < 1 || len(author) < 1 {
|
|
return models.Character{}, errors.New("Nick, name, or author name too short or empty")
|
|
}
|
|
if shortName == "" {
|
|
shortName = strings.SplitN(name, " ", 2)[0]
|
|
}
|
|
|
|
char, err := FindNick(nick)
|
|
if err == nil && char.ID != "" {
|
|
return models.Character{}, errors.New("Nick is occupied")
|
|
}
|
|
|
|
nextID, err := counter.Next("auto_increment", "models.Character")
|
|
if err != nil {
|
|
return models.Character{}, err
|
|
}
|
|
|
|
character := models.Character{
|
|
ID: "C" + strconv.Itoa(nextID),
|
|
Nicks: []string{nick},
|
|
Name: name,
|
|
ShortName: shortName,
|
|
Author: author,
|
|
Description: description,
|
|
}
|
|
|
|
err = collection.Insert(character)
|
|
if err != nil {
|
|
return models.Character{}, err
|
|
}
|
|
|
|
return character, nil
|
|
}
|