From b91dc8d60a09b171522831b2fc128e0a7364b19d Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Wed, 10 Aug 2022 00:14:54 +0200 Subject: [PATCH] add updating user level --- cmd/bot/command.go | 12 ++++++++++++ cmd/bot/models.go | 1 + cmd/bot/user.go | 23 +++++++++++++++++++++++ internal/data/models.go | 1 + internal/data/users.go | 27 +++++++++++++++++++++++++++ 5 files changed, 64 insertions(+) diff --git a/cmd/bot/command.go b/cmd/bot/command.go index a323c39..4e44944 100644 --- a/cmd/bot/command.go +++ b/cmd/bot/command.go @@ -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 diff --git a/cmd/bot/models.go b/cmd/bot/models.go index 621c093..4459d52 100644 --- a/cmd/bot/models.go +++ b/cmd/bot/models.go @@ -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") ) diff --git a/cmd/bot/user.go b/cmd/bot/user.go index 1ee09e6..ff93f68 100644 --- a/cmd/bot/user.go +++ b/cmd/bot/user.go @@ -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 + } + +} diff --git a/internal/data/models.go b/internal/data/models.go index 0d3f54b..caf96bb 100644 --- a/internal/data/models.go +++ b/internal/data/models.go @@ -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 } } diff --git a/internal/data/users.go b/internal/data/users.go index 8f04083..c06cae4 100644 --- a/internal/data/users.go +++ b/internal/data/users.go @@ -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