models: Painstakingly converted half the model package to a models & repostiry stucture. All binaries are probably dead at this point.
the build failed
the build failed
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package characters
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"git.aiterp.net/rpdata/api/internal/store"
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo"
|
||||
)
|
||||
|
||||
var collection *mgo.Collection
|
||||
|
||||
func find(query interface{}) (models.Character, error) {
|
||||
character := models.Character{}
|
||||
err := collection.Find(query).One(&character)
|
||||
if err != nil {
|
||||
return models.Character{}, err
|
||||
}
|
||||
|
||||
return character, nil
|
||||
}
|
||||
|
||||
func list(query interface{}) ([]models.Character, error) {
|
||||
characters := make([]models.Character, 0, 64)
|
||||
err := collection.Find(query).All(&characters)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return characters, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
store.HandleInit(func(db *mgo.Database) {
|
||||
collection = db.C("common.characters")
|
||||
|
||||
collection.EnsureIndexKey("name")
|
||||
collection.EnsureIndexKey("shortName")
|
||||
collection.EnsureIndexKey("author")
|
||||
err := collection.EnsureIndex(mgo.Index{
|
||||
Key: []string{"nicks"},
|
||||
Unique: true,
|
||||
DropDups: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalln("init common.characters:", err)
|
||||
}
|
||||
err = collection.EnsureIndex(mgo.Index{
|
||||
Key: []string{"$text:description"},
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalln("init common.characters:", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package characters
|
||||
|
||||
import (
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
)
|
||||
|
||||
// FindID finds a character by id.
|
||||
func FindID(id string) (models.Character, error) {
|
||||
return find(bson.M{"_id": id})
|
||||
}
|
||||
|
||||
// FindNick finds a character by nick
|
||||
func FindNick(nick string) (models.Character, error) {
|
||||
return find(bson.M{"nicks": nick})
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package characters
|
||||
|
||||
import (
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
)
|
||||
|
||||
// Filter is used to filter the list of characters
|
||||
type Filter struct {
|
||||
IDs []string `json:"ids"`
|
||||
Nicks []string `json:"nicks"`
|
||||
Names []string `json:"names"`
|
||||
Author *string `json:"author"`
|
||||
Search *string `json:"search"`
|
||||
Logged *bool `json:"logged"`
|
||||
}
|
||||
|
||||
// List lists all characters
|
||||
func List(filter *Filter) ([]models.Character, error) {
|
||||
query := bson.M{}
|
||||
|
||||
if filter != nil {
|
||||
if len(filter.IDs) > 1 {
|
||||
query["_id"] = bson.M{"$in": filter.IDs}
|
||||
} else if len(filter.IDs) == 1 {
|
||||
query["_id"] = filter.IDs[0]
|
||||
}
|
||||
|
||||
if len(filter.Nicks) > 1 {
|
||||
query["nicks"] = bson.M{"$in": filter.Nicks}
|
||||
} else if len(filter.Nicks) == 1 {
|
||||
query["nicks"] = filter.Nicks[0]
|
||||
}
|
||||
|
||||
if len(filter.Names) > 1 {
|
||||
query["$or"] = bson.M{
|
||||
"name": bson.M{"$in": filter.Names},
|
||||
"shortName": bson.M{"$in": filter.Names},
|
||||
}
|
||||
} else if len(filter.Names) == 1 {
|
||||
query["$or"] = bson.M{
|
||||
"name": filter.Names[0],
|
||||
"shortName": filter.Names[0],
|
||||
}
|
||||
}
|
||||
|
||||
if filter.Logged != nil {
|
||||
query["logged"] = *filter.Logged
|
||||
}
|
||||
|
||||
if filter.Author != nil {
|
||||
query["author"] = *filter.Author
|
||||
}
|
||||
|
||||
if filter.Search != nil {
|
||||
query["$text"] = bson.M{"$search": *filter.Search}
|
||||
}
|
||||
}
|
||||
|
||||
return list(query)
|
||||
}
|
||||
Reference in New Issue
Block a user