migrate lastfm_username to the user model instead of seperate database table

This commit is contained in:
lyx0 2023-03-05 17:42:34 +00:00
parent 012dfc03a6
commit d5710d727f
6 changed files with 119 additions and 41 deletions

View file

@ -196,7 +196,8 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else if cmdParams[1] == "lastfm" {
app.SetLastFMUser(cmdParams[2], message)
app.SetUserLastFM(cmdParams[2], message)
//app.SetLastFMUser(cmdParams[2], message)
return
} else if cmdParams[1] == "location" {
app.SetUserLocation(cmdParams[2], message)

View file

@ -1,12 +1,9 @@
package main
import (
"fmt"
"github.com/gempir/go-twitch-irc/v3"
"github.com/lyx0/nourybot/internal/commands"
"github.com/lyx0/nourybot/internal/common"
"github.com/lyx0/nourybot/internal/data"
"go.uber.org/zap"
)
@ -18,7 +15,7 @@ func (app *Application) CheckLastFM(message twitch.PrivateMessage) {
sugar.Infow("Twitchlogin: ",
"twitchLogin:", twitchLogin,
)
u, err := app.Models.LastFMUsers.Get(twitchLogin)
lastfmUser, err := app.Models.Users.GetLastFM(twitchLogin)
if err != nil {
sugar.Errorw("No LastFM account registered for: ",
"twitchLogin:", twitchLogin,
@ -29,38 +26,38 @@ func (app *Application) CheckLastFM(message twitch.PrivateMessage) {
}
target := message.Channel
user := u.LastFMUser
sugar.Infow("Twitchlogin: ",
"twitchLogin:", twitchLogin,
"user:", user,
"user:", lastfmUser,
)
commands.LastFmUserRecent(target, user, app.TwitchClient)
commands.LastFmUserRecent(target, lastfmUser, app.TwitchClient)
}
func (app *Application) SetLastFMUser(lastfmUser string, message twitch.PrivateMessage) {
sugar := zap.NewExample().Sugar()
defer sugar.Sync()
user := &data.LastFMUser{
TwitchLogin: message.User.Name,
TwitchID: message.User.ID,
LastFMUser: lastfmUser,
}
sugar.Infow("User:: ",
"user:", user,
)
err := app.Models.LastFMUsers.Insert(user)
if err != nil {
if err != nil {
reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err)
common.Send(message.Channel, reply, app.TwitchClient)
return
}
} else {
reply := fmt.Sprintf("Successfully set your lastfm account as %v", lastfmUser)
common.Send(message.Channel, reply, app.TwitchClient)
return
}
}
//func (app *Application) SetLastFMUser(lastfmUser string, message twitch.PrivateMessage) {
// sugar := zap.NewExample().Sugar()
// defer sugar.Sync()
//
// user := &data.LastFMUser{
// TwitchLogin: message.User.Name,
// TwitchID: message.User.ID,
// LastFMUser: lastfmUser,
// }
// sugar.Infow("User:: ",
// "user:", user,
// )
//
// err := app.Models.LastFMUsers.Insert(user)
// if err != nil {
// if err != nil {
// reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err)
// common.Send(message.Channel, reply, app.TwitchClient)
// return
// }
// } else {
// reply := fmt.Sprintf("Successfully set your lastfm account as %v", lastfmUser)
// common.Send(message.Channel, reply, app.TwitchClient)
// return
// }
//}
//

View file

@ -124,6 +124,27 @@ func (app *Application) SetUserLocation(location string, message twitch.PrivateM
}
}
// SetUserLastFM tries to update the database record for the supplied
// login name with the new level.
func (app *Application) SetUserLastFM(lastfmUser string, message twitch.PrivateMessage) {
login := message.User.Name
app.Logger.Infow("SetUserLastFM",
"lastfmUser", lastfmUser,
"login", login,
)
err := app.Models.Users.SetLastFM(login, lastfmUser)
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("Successfully set your lastfm username to %v", lastfmUser)
common.Send(message.Channel, reply, app.TwitchClient)
return
}
}
// GetUserLevel takes in a login name and queries the database for an entry
// with such a name value. If there is one it returns the level value as an integer.
// Returns 0 on an error which is the level for unregistered users.

View file

@ -29,6 +29,8 @@ type Models struct {
Get(login string) (*User, error)
SetLevel(login string, level int) error
SetLocation(login, location string) error
SetLastFM(login, lastfmUser string) error
GetLastFM(login string) (string, error)
Delete(login string) error
}
Commands interface {

View file

@ -7,12 +7,13 @@ import (
)
type User struct {
ID int `json:"id"`
AddedAt time.Time `json:"-"`
Login string `json:"login"`
TwitchID string `json:"twitchid"`
Level int `json:"level"`
Location string `json:"location,omitempty"`
ID int `json:"id"`
AddedAt time.Time `json:"-"`
Login string `json:"login"`
TwitchID string `json:"twitchid"`
Level int `json:"level"`
Location string `json:"location,omitempty"`
LastFMUsername string `json:"lastfm_username,omitempty"`
}
type UserModel struct {
@ -77,6 +78,59 @@ func (u UserModel) SetLocation(login, location string) error {
return nil
}
// SetLocation searches the database for a record with the provided login value
// and if that exists sets the location to the supplied
func (u UserModel) SetLastFM(login, lastfmUser string) error {
query := `
UPDATE users
SET lastfm_username = $2
WHERE login = $1`
result, err := u.DB.Exec(query, login, lastfmUser)
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
}
// SetLocation searches the database for a record with the provided login value
// and if that exists sets the location to the supplied
func (u UserModel) GetLastFM(login string) (string, error) {
query := `
SELECT lastfm_username
FROM users
WHERE login = $1`
var user User
err := u.DB.QueryRow(query, login).Scan(
&user.LastFMUsername,
)
if err != nil {
switch {
case errors.Is(err, sql.ErrNoRows):
return "", ErrRecordNotFound
default:
return "", err
}
}
return user.LastFMUsername, nil
}
// Setlevel searches the database for a record with the provided login value
// and if that exists sets the level to the supplied level value.
func (u UserModel) SetLevel(login string, level int) error {

View file

@ -4,7 +4,8 @@ CREATE TABLE IF NOT EXISTS users (
login text UNIQUE NOT NULL,
twitchid text NOT NULL,
level integer NOT NULL,
location text
location text,
lastfm_username text
);
INSERT INTO users (added_at,login,twitchid,"level") VALUES
@ -14,3 +15,5 @@ INSERT INTO users (added_at,login,twitchid,"level") VALUES
(NOW(),'xnoury','197780373',500),
(NOW(),'noemience','135447564',500);
UPDATE users SET location = 'vilnius' WHERE login = 'nourylul';
UPDATE users SET lastfm_username = 'nouryqt' WHERE login = 'nourylul';