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.
81 lines
1.5 KiB
81 lines
1.5 KiB
package model
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"git.aiterp.net/AiteRP/aitestory/server"
|
|
"git.aiterp.net/gisle/wrouter/generate"
|
|
)
|
|
|
|
// TagTypes are the allowed values for Tag.Type
|
|
var TagTypes = []string{
|
|
"Location",
|
|
"Character",
|
|
"Event",
|
|
"Organization",
|
|
"Source",
|
|
}
|
|
|
|
// Tag describes a tag
|
|
type Tag struct {
|
|
ID string
|
|
Type string
|
|
Name string
|
|
}
|
|
|
|
// Insert adds the tag to the database, giving it a new unique ID
|
|
func (tag *Tag) Insert() error {
|
|
db := server.Main.DB
|
|
|
|
// Validate tag type
|
|
validType := false
|
|
for _, tagType := range TagTypes {
|
|
if tagType == tag.Type {
|
|
validType = true
|
|
break
|
|
}
|
|
}
|
|
if !validType {
|
|
return fmt.Errorf("\"%s\" is not a valid tag type", tag.Type)
|
|
}
|
|
|
|
// Validate tag name
|
|
if len(tag.Name) == 0 {
|
|
return errors.New("Tag name is empty")
|
|
}
|
|
|
|
// Generate an ID if none exists
|
|
if tag.ID == "" {
|
|
tag.ID = generate.ID()
|
|
}
|
|
|
|
// Do the thing
|
|
_, err := db.Exec("INSERT INTO `tag` (id,type,name,disabled) VALUES (?,?,?,false)", tag.ID, tag.Type, tag.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ListTags finds all the tags, without filter. If it hits
|
|
// the tag cache, it will copy it making it safe to modify
|
|
func ListTags() ([]Tag, error) {
|
|
db := server.Main.DB
|
|
|
|
// Read from the database
|
|
rows, err := db.Query("SELECT id,type,name FROM `tag` WHERE disabled=false")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
results := make([]Tag, 0, 64)
|
|
for rows.Next() {
|
|
tag := Tag{}
|
|
rows.Scan(&tag.ID, &tag.Type, &tag.Name)
|
|
results = append(results, tag)
|
|
}
|
|
|
|
return results, nil
|
|
}
|