mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
add get and insert models for timers
This commit is contained in:
parent
53045e1b20
commit
0e51d7f45b
|
@ -39,6 +39,7 @@ type Models struct {
|
|||
}
|
||||
Timers interface {
|
||||
Get(name string) (*Timer, error)
|
||||
Insert(timer *Timer) error
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,6 +48,6 @@ func NewModels(db *sql.DB) Models {
|
|||
Channels: ChannelModel{DB: db},
|
||||
Users: UserModel{DB: db},
|
||||
Commands: CommandModel{DB: db},
|
||||
Timers: TimerModel{DB, db},
|
||||
Timers: TimerModel{DB: db},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,3 +44,30 @@ func (t TimerModel) Get(name string) (*Timer, error) {
|
|||
|
||||
return &timer, nil
|
||||
}
|
||||
|
||||
// Insert adds a command into the database.
|
||||
func (t TimerModel) Insert(timer *Timer) error {
|
||||
query := `
|
||||
INSERT into timers(name, text, channel, repeat)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id;
|
||||
`
|
||||
|
||||
args := []interface{}{timer.Name, timer.Text, timer.Channel, timer.Repeat}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue