2023-09-07 22:34:53 +02:00
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Timer struct {
|
2023-10-04 00:05:38 +02:00
|
|
|
ID int `json:"id" redis:"timer-id"`
|
|
|
|
Name string `json:"name" redis:"timer-name"`
|
|
|
|
Identifier string `json:"identifier"`
|
|
|
|
Text string `json:"text" redis:"timer-text"`
|
|
|
|
Channel string `json:"channel" redis:"timer-channel"`
|
|
|
|
Repeat string `json:"repeat" redis:"timer-repeat"`
|
2023-09-07 22:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type TimerModel struct {
|
|
|
|
DB *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t TimerModel) Get(name string) (*Timer, error) {
|
|
|
|
query := `
|
2023-10-04 00:05:38 +02:00
|
|
|
SELECT id, name, identifier, text, channel, repeat
|
2023-09-07 22:34:53 +02:00
|
|
|
FROM timers
|
|
|
|
WHERE name = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
var timer Timer
|
|
|
|
|
|
|
|
err := t.DB.QueryRow(query, name).Scan(
|
|
|
|
&timer.ID,
|
|
|
|
&timer.Name,
|
2023-10-04 00:05:38 +02:00
|
|
|
&timer.Identifier,
|
2023-09-07 22:34:53 +02:00
|
|
|
&timer.Text,
|
|
|
|
&timer.Channel,
|
|
|
|
&timer.Repeat,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, sql.ErrNoRows):
|
|
|
|
return nil, ErrRecordNotFound
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &timer, nil
|
|
|
|
}
|
|
|
|
|
2023-10-04 00:05:38 +02:00
|
|
|
func (t TimerModel) GetIdentifier(name string) (string, error) {
|
|
|
|
query := `
|
|
|
|
SELECT id, name, identifier, text, channel, repeat
|
|
|
|
FROM timers
|
|
|
|
WHERE name = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
var timer Timer
|
|
|
|
|
|
|
|
err := t.DB.QueryRow(query, name).Scan(
|
|
|
|
&timer.ID,
|
|
|
|
&timer.Name,
|
|
|
|
&timer.Identifier,
|
|
|
|
&timer.Text,
|
|
|
|
&timer.Channel,
|
|
|
|
&timer.Repeat,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, sql.ErrNoRows):
|
|
|
|
return "", ErrRecordNotFound
|
|
|
|
default:
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return timer.Identifier, nil
|
|
|
|
}
|
|
|
|
|
2023-09-07 22:34:53 +02:00
|
|
|
// Insert adds a command into the database.
|
|
|
|
func (t TimerModel) Insert(timer *Timer) error {
|
|
|
|
query := `
|
2023-10-04 00:05:38 +02:00
|
|
|
INSERT into timers(name, identifier, text, channel, repeat)
|
|
|
|
VALUES ($1, $2, $3, $4, $5)
|
2023-09-07 22:34:53 +02:00
|
|
|
RETURNING id;
|
|
|
|
`
|
|
|
|
|
2023-10-04 00:05:38 +02:00
|
|
|
args := []interface{}{timer.Name, timer.Identifier, timer.Text, timer.Channel, timer.Repeat}
|
2023-09-07 22:34:53 +02:00
|
|
|
|
|
|
|
result, err := t.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
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAll() returns a pointer to a slice of all channels (`[]*Channel`) in the database.
|
|
|
|
func (t TimerModel) GetAll() ([]*Timer, error) {
|
|
|
|
query := `
|
2023-10-04 00:05:38 +02:00
|
|
|
SELECT id, name, identifier, text, channel, repeat
|
2023-09-07 22:34:53 +02:00
|
|
|
FROM timers
|
|
|
|
ORDER BY id`
|
|
|
|
|
|
|
|
// Create a context with 3 seconds timeout.
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Use QueryContext() the context and query. This returns a
|
|
|
|
// sql.Rows resultset containing our channels.
|
|
|
|
rows, err := t.DB.QueryContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to defer a call to rows.Close() to ensure the resultset
|
|
|
|
// is closed before GetAll() returns.
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
// Initialize an empty slice to hold the data.
|
|
|
|
timers := []*Timer{}
|
|
|
|
|
|
|
|
// Iterate over the resultset.
|
|
|
|
for rows.Next() {
|
|
|
|
// Initialize an empty Channel struct where we put on
|
|
|
|
// a single channel value.
|
|
|
|
var timer Timer
|
|
|
|
|
|
|
|
// Scan the values onto the channel struct
|
|
|
|
err := rows.Scan(
|
|
|
|
&timer.ID,
|
|
|
|
&timer.Name,
|
2023-10-04 00:05:38 +02:00
|
|
|
&timer.Identifier,
|
2023-09-07 22:34:53 +02:00
|
|
|
&timer.Text,
|
|
|
|
&timer.Channel,
|
|
|
|
&timer.Repeat,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Add the single movie struct onto the slice.
|
|
|
|
timers = append(timers, &timer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// When rows.Next() finishes call rows.Err() to retrieve any errors.
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return timers, nil
|
|
|
|
}
|
|
|
|
|
2023-12-17 20:11:02 +01:00
|
|
|
// GetAll() returns a pointer to a slice of all channels (`[]*Channel`) in the database.
|
|
|
|
func (t TimerModel) GetChannelTimer(channel string) ([]*Timer, error) {
|
|
|
|
query := `
|
|
|
|
SELECT id, name, identifier, text, channel, repeat
|
|
|
|
FROM timers
|
|
|
|
WHERE channel = $1
|
|
|
|
ORDER BY name`
|
|
|
|
|
|
|
|
// Create a context with 3 seconds timeout.
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Use QueryContext() the context and query. This returns a
|
|
|
|
// sql.Rows resultset containing our channels.
|
|
|
|
rows, err := t.DB.QueryContext(ctx, query, channel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to defer a call to rows.Close() to ensure the resultset
|
|
|
|
// is closed before GetAll() returns.
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
// Initialize an empty slice to hold the data.
|
|
|
|
timers := []*Timer{}
|
|
|
|
|
|
|
|
// Iterate over the resultset.
|
|
|
|
for rows.Next() {
|
|
|
|
// Initialize an empty Channel struct where we put on
|
|
|
|
// a single channel value.
|
|
|
|
var timer Timer
|
|
|
|
|
|
|
|
err := rows.Scan(
|
|
|
|
&timer.ID,
|
|
|
|
&timer.Name,
|
|
|
|
&timer.Identifier,
|
|
|
|
&timer.Text,
|
|
|
|
&timer.Channel,
|
|
|
|
&timer.Repeat,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, sql.ErrNoRows):
|
|
|
|
return nil, ErrRecordNotFound
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add the single movie struct onto the slice.
|
|
|
|
timers = append(timers, &timer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// When rows.Next() finishes call rows.Err() to retrieve any errors.
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return timers, nil
|
|
|
|
}
|
|
|
|
|
2023-09-07 22:34:53 +02:00
|
|
|
func (t TimerModel) Update(timer *Timer) error {
|
|
|
|
query := `
|
|
|
|
UPDATE timers
|
|
|
|
SET text = $2, channel = $3, repeat = $4
|
|
|
|
WHERE name = $1
|
|
|
|
RETURNING id`
|
|
|
|
|
|
|
|
args := []interface{}{
|
|
|
|
timer.Name,
|
|
|
|
timer.Text,
|
|
|
|
timer.Channel,
|
|
|
|
timer.Repeat,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := t.DB.QueryRow(query, args...).Scan(&timer.ID)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, sql.ErrNoRows):
|
|
|
|
return ErrEditConflict
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete takes in a command name and queries the database for an entry with
|
|
|
|
// the same name and tries to delete that entry.
|
2023-10-04 00:05:38 +02:00
|
|
|
func (t TimerModel) Delete(identifier string) error {
|
2023-09-07 22:34:53 +02:00
|
|
|
// Prepare the statement.
|
|
|
|
query := `
|
|
|
|
DELETE FROM timers
|
2023-10-04 00:05:38 +02:00
|
|
|
WHERE identifier = $1`
|
2023-09-07 22:34:53 +02:00
|
|
|
|
|
|
|
// Execute the query returning the number of affected rows.
|
2023-10-04 00:05:38 +02:00
|
|
|
result, err := t.DB.Exec(query, identifier)
|
2023-09-07 22:34:53 +02:00
|
|
|
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
|
|
|
|
}
|