package model import ( "errors" "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 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 { validType = true break } } if !validType { return errors.New("invalid tag type") } // Validate tag name if len(tag.Name) == 0 || len(tag.Name) > 64 { return errors.New("invalid length") } 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") } rows, err := db.Query("SELECT id,type,name FROM `tag` WHERE "+key+"=? AND disabled=false", id) if err != nil { return nil, err } defer rows.Close() if !rows.Next() { return nil, errors.New("not found") } tag := new(Tag) rows.Scan(&tag.ID, &tag.Type, &tag.Name) return tag, 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 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 }