diff --git a/internal/data/models.go b/internal/data/models.go index cec960c..ba74699 100644 --- a/internal/data/models.go +++ b/internal/data/models.go @@ -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}, } } diff --git a/internal/data/timers.go b/internal/data/timers.go index 7c8a20e..4e837b9 100644 --- a/internal/data/timers.go +++ b/internal/data/timers.go @@ -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 +}