delete channel from database

This commit is contained in:
lyx0 2022-08-09 19:50:46 +02:00
parent 3af6d60641
commit 3f16128812
4 changed files with 50 additions and 1 deletions

View file

@ -29,5 +29,16 @@ func AddChannel(login string, message twitch.PrivateMessage, app *Application) {
reply := fmt.Sprintf("Joined channel %s", login)
common.Send(message.Channel, reply, app.TwitchClient)
}
func DeleteChannel(login string, message twitch.PrivateMessage, app *Application) {
err := app.Models.Channels.Delete(login)
if err != nil {
common.Send(message.Channel, "Something went wrong FeelsBadMan", app.TwitchClient)
app.Logger.Error(err)
return
}
reply := fmt.Sprintf("Deleted channel %s", login)
common.Send(message.Channel, reply, app.TwitchClient)
}

View file

@ -64,6 +64,17 @@ func handleCommand(message twitch.PrivateMessage, app *Application) {
AddChannel(cmdParams[1], message, app)
return
}
case "deletechannel":
if message.User.ID != "31437432" { // Limit to myself for now.
return
} else if msgLen < 2 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else {
// ()addchannel noemience
DeleteChannel(cmdParams[1], message, app)
return
}
case "bttv":
if msgLen < 2 {
common.Send(target, "Not enough arguments provided. Usage: ()bttv <emote name>", app.TwitchClient)

View file

@ -56,3 +56,29 @@ func (c ChannelModel) Get(login string) (*Channel, error) {
return &channel, nil
}
func (c ChannelModel) Delete(login string) error {
// Prepare the statement.
query := `
DELETE FROM channels
WHERE login = $1`
// Execute the query returning the number of affected rows.
result, err := c.DB.Exec(query, login)
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

@ -15,6 +15,7 @@ type Models struct {
Channels interface {
Insert(channel *Channel) error
Get(login string) (*Channel, error)
Delete(login string) error
}
}