mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
start implementing timers
This commit is contained in:
parent
61f15ef9e3
commit
feea944f64
|
@ -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
1
cmd/bot/timer.go
Normal file
|
@ -0,0 +1 @@
|
|||
package main
|
|
@ -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
46
internal/data/timers.go
Normal 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
|
||||
}
|
Loading…
Reference in a new issue