|
|
@ -2,7 +2,6 @@ package model |
|
|
|
|
|
|
|
import ( |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"git.aiterp.net/AiteRP/aitestory/server" |
|
|
|
"git.aiterp.net/gisle/wrouter/generate" |
|
|
@ -29,6 +28,67 @@ func (tag *Tag) Insert() error { |
|
|
|
db := server.Main.DB |
|
|
|
|
|
|
|
// Validate tag type
|
|
|
|
if err := tag.Validate(); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
// 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 |
|
|
|
} |
|
|
|
|
|
|
|
// Update saves the entry for the tag in the database
|
|
|
|
func (tag *Tag) Update() error { |
|
|
|
db := server.Main.DB |
|
|
|
|
|
|
|
// Validate tag type
|
|
|
|
if err := tag.Validate(); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
// Do the thing
|
|
|
|
_, err := db.Exec("UPDATE `tag` SET type=?,name=? WHERE id=?", tag.Type, tag.Name, tag.ID) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// Delete removes a tag from the database
|
|
|
|
func (tag *Tag) Delete() error { |
|
|
|
db := server.Main.DB |
|
|
|
|
|
|
|
// Do the thing
|
|
|
|
results, err := db.Exec("DELETE FROM `tag` WHERE id=? LIMIT 1", tag.ID) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
// Count the stuffs that were done things to
|
|
|
|
affected, err := results.RowsAffected() |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
if affected == 0 { |
|
|
|
return errors.New("tag not found") |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// Validate checks the name and type, and returns an error if they're not valid. It's
|
|
|
|
// ran by Update and Insert before doing anything
|
|
|
|
func (tag *Tag) Validate() error { |
|
|
|
validType := false |
|
|
|
for _, tagType := range TagTypes { |
|
|
|
if tagType == tag.Type { |
|
|
@ -37,26 +97,39 @@ func (tag *Tag) Insert() error { |
|
|
|
} |
|
|
|
} |
|
|
|
if !validType { |
|
|
|
return fmt.Errorf("\"%s\" is not a valid tag type", tag.Type) |
|
|
|
return errors.New("invalid tag type") |
|
|
|
} |
|
|
|
|
|
|
|
// Validate tag name
|
|
|
|
if len(tag.Name) == 0 { |
|
|
|
return errors.New("Tag name is empty") |
|
|
|
if len(tag.Name) == 0 || len(tag.Name) > 64 { |
|
|
|
return errors.New("invalid length") |
|
|
|
} |
|
|
|
|
|
|
|
// Generate an ID if none exists
|
|
|
|
if tag.ID == "" { |
|
|
|
tag.ID = generate.ID() |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// FindTag finds a tag by ID
|
|
|
|
func FindTag(key string, id string) (*Tag, error) { |
|
|
|
db := server.Main.DB |
|
|
|
|
|
|
|
// Make damn sure that – should this take user data as key – it
|
|
|
|
// does not open up for a SQL injection attack
|
|
|
|
if key != "name" && key != "id" { |
|
|
|
return nil, errors.New("invalid key") |
|
|
|
} |
|
|
|
|
|
|
|
// Do the thing
|
|
|
|
_, err := db.Exec("INSERT INTO `tag` (id,type,name,disabled) VALUES (?,?,?,false)", tag.ID, tag.Type, tag.Name) |
|
|
|
rows, err := db.Query("SELECT id,type,name FROM `tag` WHERE "+key+"=? AND disabled=false", id) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
defer rows.Close() |
|
|
|
if !rows.Next() { |
|
|
|
return nil, errors.New("not found") |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
tag := new(Tag) |
|
|
|
rows.Scan(&tag.ID, &tag.Type, &tag.Name) |
|
|
|
return tag, nil |
|
|
|
} |
|
|
|
|
|
|
|
// ListTags finds all the tags, without filter. If it hits
|
|
|
@ -64,12 +137,12 @@ func (tag *Tag) Insert() error { |
|
|
|
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{} |
|
|
|