start implementing timers

This commit is contained in:
lyx0 2022-08-21 21:04:27 +02:00
parent 61f15ef9e3
commit feea944f64
4 changed files with 53 additions and 1 deletions

View file

@ -132,10 +132,11 @@ func (app *application) updateCommandHandler(w http.ResponseWriter, r *http.Requ
switch {
case errors.Is(err, data.ErrEditConflict):
app.editConflictResponse(w, r)
return
default:
app.serverErrorResponse(w, r, err)
return
}
return
}
app.Logger.Infow("PATCH Command",

1
cmd/bot/timer.go Normal file
View file

@ -0,0 +1 @@
package main

View file

@ -37,6 +37,9 @@ type Models struct {
SetCategory(name, category string) error
Delete(name string) error
}
Timers interface {
Get(name string) (*Timer, error)
}
}
func NewModels(db *sql.DB) Models {
@ -44,5 +47,6 @@ func NewModels(db *sql.DB) Models {
Channels: ChannelModel{DB: db},
Users: UserModel{DB: db},
Commands: CommandModel{DB: db},
Timers: TimerModel{DB, db},
}
}

46
internal/data/timers.go Normal file
View file

@ -0,0 +1,46 @@
package data
import (
"database/sql"
"errors"
)
type Timer struct {
ID int `json:"id"`
Name string `json:"name"`
Text string `json:"text"`
Channel string `json:"channel"`
Repeat string `json:"repeat"`
}
type TimerModel struct {
DB *sql.DB
}
func (t TimerModel) Get(name string) (*Timer, error) {
query := `
SELECT id, name, text, channel, repeat
FROM timers
WHERE name = $1
`
var timer Timer
err := t.DB.QueryRow(query, name).Scan(
&timer.ID,
&timer.Name,
&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
}