allow a user to set his own location

This commit is contained in:
lyx0 2023-03-04 22:37:31 +00:00
parent 2e7b115795
commit 012dfc03a6
5 changed files with 61 additions and 6 deletions

View file

@ -198,6 +198,9 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
} else if cmdParams[1] == "lastfm" { } else if cmdParams[1] == "lastfm" {
app.SetLastFMUser(cmdParams[2], message) app.SetLastFMUser(cmdParams[2], message)
return return
} else if cmdParams[1] == "location" {
app.SetUserLocation(cmdParams[2], message)
return
} else { } else {
return return
} }

View file

@ -103,6 +103,27 @@ func (app *Application) EditUserLevel(login, lvl string, message twitch.PrivateM
} }
} }
// EditUserLevel tries to update the database record for the supplied
// login name with the new level.
func (app *Application) SetUserLocation(location string, message twitch.PrivateMessage) {
login := message.User.Name
app.Logger.Infow("SetUserLocation",
"location", location,
"login", login,
)
err := app.Models.Users.SetLocation(login, location)
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 location to %v", location)
common.Send(message.Channel, reply, app.TwitchClient)
return
}
}
// GetUserLevel takes in a login name and queries the database for an entry // 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. // 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. // Returns 0 on an error which is the level for unregistered users.

View file

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

View file

@ -12,6 +12,7 @@ type User struct {
Login string `json:"login"` Login string `json:"login"`
TwitchID string `json:"twitchid"` TwitchID string `json:"twitchid"`
Level int `json:"level"` Level int `json:"level"`
Location string `json:"location,omitempty"`
} }
type UserModel struct { type UserModel struct {
@ -49,6 +50,33 @@ func (u UserModel) Insert(user *User) error {
return nil 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) SetLocation(login, location string) error {
query := `
UPDATE users
SET location = $2
WHERE login = $1`
result, err := u.DB.Exec(query, login, location)
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
}
// Setlevel searches the database for a record with the provided login value // Setlevel searches the database for a record with the provided login value
// and if that exists sets the level to the supplied level value. // and if that exists sets the level to the supplied level value.
func (u UserModel) SetLevel(login string, level int) error { func (u UserModel) SetLevel(login string, level int) error {
@ -80,7 +108,7 @@ func (u UserModel) SetLevel(login string, level int) error {
// Get searches the database for a login name and returns the user struct on success. // Get searches the database for a login name and returns the user struct on success.
func (u UserModel) Get(login string) (*User, error) { func (u UserModel) Get(login string) (*User, error) {
query := ` query := `
SELECT id, added_at, login, twitchid, level SELECT id, added_at, login, twitchid, level, location
FROM users FROM users
WHERE login = $1` WHERE login = $1`
@ -92,6 +120,7 @@ func (u UserModel) Get(login string) (*User, error) {
&user.Login, &user.Login,
&user.TwitchID, &user.TwitchID,
&user.Level, &user.Level,
&user.Location,
) )
if err != nil { if err != nil {

View file

@ -3,7 +3,8 @@ CREATE TABLE IF NOT EXISTS users (
added_at timestamp(0) with time zone NOT NULL DEFAULT NOW(), added_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
login text UNIQUE NOT NULL, login text UNIQUE NOT NULL,
twitchid text NOT NULL, twitchid text NOT NULL,
level integer NOT NULL level integer NOT NULL,
location text
); );
INSERT INTO users (added_at,login,twitchid,"level") VALUES INSERT INTO users (added_at,login,twitchid,"level") VALUES