add get and insert models for timers

This commit is contained in:
lyx0 2022-08-22 02:12:54 +02:00
parent 53045e1b20
commit 0e51d7f45b
2 changed files with 29 additions and 1 deletions

View file

@ -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},
}
}

View file

@ -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
}