delete a timer from the database

This commit is contained in:
lyx0 2022-08-24 21:00:09 +02:00
parent 9bf18d3cdf
commit b55b3ed0cb
5 changed files with 54 additions and 6 deletions

View file

@ -377,6 +377,17 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
app.DeleteUser(cmdParams[1], message)
return
}
case "deletetimer":
if userLevel < 1000 { // Limit to myself for now.
return
} else if msgLen < 2 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else {
// ()deletetimer dank
app.DeleteTimer(cmdParams[1], message)
return
}
case "bttvemotes":
if userLevel < 1000 {

View file

@ -62,7 +62,7 @@ func (app *Application) InitialTimers() {
app.Logger.Info(timer)
// Iterate over the slice of channels and join each.
// Iterate over each timer and add them to the scheduler.
for _, v := range timer {
app.Logger.Infow("Initial timers:",
"Name", v.Name,
@ -80,3 +80,16 @@ func (app *Application) InitialTimers() {
func (app *Application) newTimer(channel, text string) {
common.Send(channel, text, app.TwitchClient)
}
// DeleteCommand takes in a name value and deletes the command from the database if it exists.
func (app *Application) DeleteTimer(name string, message twitch.PrivateMessage) {
err := app.Models.Timers.Delete(name)
if err != nil {
common.Send(message.Channel, "Something went wrong FeelsBadMan", app.TwitchClient)
app.Logger.Error(err)
return
}
reply := fmt.Sprintf("Deleted timer %s", name)
common.Send(message.Channel, reply, app.TwitchClient)
}

View file

@ -41,6 +41,7 @@ type Models struct {
Get(name string) (*Timer, error)
Insert(timer *Timer) error
GetAll() ([]*Timer, error)
Delete(name string) error
}
}

View file

@ -127,3 +127,31 @@ func (t TimerModel) GetAll() ([]*Timer, error) {
return timers, nil
}
// Delete takes in a command name and queries the database for an entry with
// the same name and tries to delete that entry.
func (t TimerModel) Delete(name string) error {
// Prepare the statement.
query := `
DELETE FROM timers
WHERE name = $1`
// Execute the query returning the number of affected rows.
result, err := t.DB.Exec(query, name)
if err != nil {
return err
}
// Check how many rows were affected.
rowsAffected, err := result.RowsAffected()
if err != nil {
return err
}
// We want atleast 1, if it is 0 the entry did not exist.
if rowsAffected == 0 {
return ErrRecordNotFound
}
return nil
}

View file

@ -7,11 +7,6 @@ CREATE TABLE IF NOT EXISTS timers (
);
INSERT INTO timers (name,"text",channel,repeat) VALUES
('nourylul-2m','2m timer','nourylul','2m'),
('nourylul-3m','3 minute timer xD','nourylul','3m'),
('nourylul-5m','5minute timer lulw','nourylul','5m'),
('nourylul-10m','10 minute timer xD','nourylul','10m'),
('nourylul-15m',' every 15 minutes :)','nourylul','15m'),
('nourybot-4m',' 4 minute timer xD','nourybot','4m'),
('nourybot-10m','10 minute timer xd','nourybot','10m'),
('nourybot-20m',' 20 minutes XD','nourybot','20m'),