2022-08-11 01:18:51 +02:00
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Command struct {
|
2022-08-11 20:35:22 +02:00
|
|
|
ID int `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
Level int `json:"level"`
|
2022-08-11 01:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type CommandModel struct {
|
|
|
|
DB *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CommandModel) Get(name string) (*Command, error) {
|
|
|
|
query := `
|
2022-08-11 20:35:22 +02:00
|
|
|
SELECT id, name, text, level
|
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-11 20:35:22 +02:00
|
|
|
&command.Level,
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-08-11 01:18:51 +02:00
|
|
|
func (c CommandModel) Insert(name, text string) error {
|
|
|
|
perms := 0
|
|
|
|
query := `
|
2022-08-11 20:35:22 +02:00
|
|
|
INSERT into commands(name, text, level)
|
2022-08-11 01:18:51 +02:00
|
|
|
VALUES ($1, $2, $3)
|
|
|
|
ON CONFLICT (name)
|
|
|
|
DO NOTHING
|
|
|
|
RETURNING id;
|
|
|
|
`
|
|
|
|
|
|
|
|
args := []interface{}{name, text, perms}
|
|
|
|
|
|
|
|
result, err := c.DB.Exec(query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rowsAffected, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rowsAffected == 0 {
|
2022-08-11 01:50:30 +02:00
|
|
|
return ErrCommandRecordAlreadyExists
|
2022-08-11 01:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|