2022-08-11 01:18:51 +02:00
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Command struct {
|
2022-08-15 00:53:41 +02:00
|
|
|
ID int `json:"id"`
|
|
|
|
Name string `json:"name"`
|
2022-08-17 20:57:25 +02:00
|
|
|
Text string `json:"text,omitempty"`
|
|
|
|
Category string `json:"category,omitempty"`
|
|
|
|
Level int `json:"level,omitempty"`
|
2023-03-04 01:04:28 +01:00
|
|
|
Help string `json:"help,omitempty"`
|
2022-08-11 01:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type CommandModel struct {
|
|
|
|
DB *sql.DB
|
|
|
|
}
|
|
|
|
|
2022-08-13 02:06:54 +02:00
|
|
|
// Get tries to find a command in the database with the provided name.
|
2022-08-11 01:18:51 +02:00
|
|
|
func (c CommandModel) Get(name string) (*Command, error) {
|
|
|
|
query := `
|
2023-03-04 01:04:28 +01:00
|
|
|
SELECT id, name, text, category, level, help
|
2022-08-11 01:18:51 +02:00
|
|
|
FROM commands
|
|
|
|
WHERE name = $1`
|
|
|
|
|
|
|
|
var command Command
|
|
|
|
|
|
|
|
err := c.DB.QueryRow(query, name).Scan(
|
|
|
|
&command.ID,
|
|
|
|
&command.Name,
|
|
|
|
&command.Text,
|
2022-08-15 00:53:41 +02:00
|
|
|
&command.Category,
|
2022-08-11 20:35:22 +02:00
|
|
|
&command.Level,
|
2023-03-04 01:04:28 +01:00
|
|
|
&command.Help,
|
2022-08-11 01:18:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, sql.ErrNoRows):
|
|
|
|
return nil, ErrRecordNotFound
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &command, nil
|
|
|
|
}
|
|
|
|
|
2023-03-04 02:29:29 +01:00
|
|
|
// Insert adds a command into the database.
|
|
|
|
func (c CommandModel) Insert(command *Command) error {
|
|
|
|
query := `
|
|
|
|
INSERT into commands(name, text, category, level, help)
|
|
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
|
|
ON CONFLICT (name)
|
|
|
|
DO NOTHING
|
|
|
|
RETURNING id;
|
|
|
|
`
|
|
|
|
|
|
|
|
args := []interface{}{command.Name, command.Text, command.Category, command.Level, command.Help}
|
|
|
|
|
|
|
|
result, err := c.DB.Exec(query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rowsAffected, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rowsAffected == 0 {
|
|
|
|
return ErrCommandRecordAlreadyExists
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-17 20:57:25 +02:00
|
|
|
func (c CommandModel) Update(command *Command) error {
|
|
|
|
query := `
|
|
|
|
UPDATE commands
|
2023-03-04 01:04:28 +01:00
|
|
|
SET text = $2
|
2022-08-17 20:57:25 +02:00
|
|
|
WHERE name = $1
|
|
|
|
RETURNING id`
|
|
|
|
|
|
|
|
args := []interface{}{
|
|
|
|
command.Name,
|
|
|
|
command.Text,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.DB.QueryRow(query, args...).Scan(&command.ID)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, sql.ErrNoRows):
|
|
|
|
return ErrEditConflict
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-15 00:53:41 +02:00
|
|
|
// SetCategory queries the database for an entry with the provided name,
|
|
|
|
// if there is one it updates the categories level with the provided level.
|
|
|
|
func (c CommandModel) SetCategory(name string, category string) error {
|
|
|
|
query := `
|
|
|
|
UPDATE commands
|
|
|
|
SET category = $2
|
|
|
|
WHERE name = $1`
|
|
|
|
|
|
|
|
result, err := c.DB.Exec(query, name, category)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rowsAffected, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rowsAffected == 0 {
|
|
|
|
return ErrRecordNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-13 02:06:54 +02:00
|
|
|
// SetLevel queries the database for an entry with the provided name,
|
|
|
|
// if there is one it updates the entrys level with the provided level.
|
2022-08-11 20:35:22 +02:00
|
|
|
func (c CommandModel) SetLevel(name string, level int) error {
|
|
|
|
query := `
|
|
|
|
UPDATE commands
|
|
|
|
SET level = $2
|
|
|
|
WHERE name = $1`
|
|
|
|
|
|
|
|
result, err := c.DB.Exec(query, name, level)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rowsAffected, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rowsAffected == 0 {
|
|
|
|
return ErrRecordNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-04 02:29:29 +01:00
|
|
|
// SetHelp sets the help text for a given name of a command in the database.
|
2023-03-04 01:04:28 +01:00
|
|
|
func (c CommandModel) SetHelp(name string, helptext string) error {
|
|
|
|
query := `
|
|
|
|
UPDATE commands
|
|
|
|
SET help = $2
|
|
|
|
WHERE name = $1`
|
|
|
|
|
|
|
|
result, err := c.DB.Exec(query, name, helptext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rowsAffected, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rowsAffected == 0 {
|
|
|
|
return ErrRecordNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-13 02:06:54 +02:00
|
|
|
// Delete takes in a command name and queries the database for an entry with
|
|
|
|
// the same name and tries to delete that entry.
|
2022-08-11 01:18:51 +02:00
|
|
|
func (c CommandModel) Delete(name string) error {
|
|
|
|
// Prepare the statement.
|
|
|
|
query := `
|
|
|
|
DELETE FROM commands
|
|
|
|
WHERE name = $1`
|
|
|
|
|
|
|
|
// Execute the query returning the number of affected rows.
|
|
|
|
result, err := c.DB.Exec(query, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check how many rows were affected.
|
|
|
|
rowsAffected, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want atleast 1, if it is 0 the entry did not exist.
|
|
|
|
if rowsAffected == 0 {
|
|
|
|
return ErrRecordNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|