add updating user level

This commit is contained in:
lyx0 2022-08-10 00:14:54 +02:00
parent 332de23f64
commit b91dc8d60a
5 changed files with 64 additions and 0 deletions

View file

@ -75,6 +75,18 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
app.AddUser(cmdParams[1], cmdParams[2], message)
return
}
case "edituser": // ()edituser level nourylul 1000
if message.User.ID != "31437432" { // Limit to myself for now.
return
} else if msgLen < 4 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else if cmdParams[1] == "level" {
app.EditUserLevel(cmdParams[2], cmdParams[3], message)
return
} else {
return
}
case "deletechannel":
if message.User.ID != "31437432" { // Limit to myself for now.
return

View file

@ -4,4 +4,5 @@ import "errors"
var (
ErrUserLevelNotInteger = errors.New("user level must be a number")
ErrRecordNotFound = errors.New("user not found in the database")
)

View file

@ -54,3 +54,26 @@ func (app *Application) DeleteUser(login string, message twitch.PrivateMessage)
reply := fmt.Sprintf("Deleted user %s", login)
common.Send(message.Channel, reply, app.TwitchClient)
}
func (app *Application) EditUserLevel(user, lvl string, message twitch.PrivateMessage) {
level, err := strconv.Atoi(lvl)
if err != nil {
app.Logger.Error(err)
common.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrUserLevelNotInteger), app.TwitchClient)
return
}
err = app.Models.Users.SetLevel(user, level)
if err != nil {
common.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound), app.TwitchClient)
app.Logger.Error(err)
return
} else {
reply := fmt.Sprintf("Updated user %s to level %v", user, level)
common.Send(message.Channel, reply, app.TwitchClient)
return
}
}

View file

@ -23,6 +23,7 @@ type Models struct {
Users interface {
Insert(user *User) error
Get(login string) (*User, error)
SetLevel(login string, level int) error
Delete(login string) error
}
}

View file

@ -48,6 +48,33 @@ func (u UserModel) Insert(user *User) error {
return nil
}
func (u UserModel) SetLevel(login string, level int) error {
query := `
UPDATE users
SET level = $2
WHERE login = $1`
// err := u.DB.QueryRow(query, args...).Scan(&user)
result, err := u.DB.Exec(query, login, level)
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
}
func (u UserModel) Get(login string) (*User, error) {
query := `
SELECT id, added_at, login, twitchid, level