mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
add data models for lastfm
This commit is contained in:
parent
05c22e0093
commit
ea6fcbe416
72
internal/data/lastfm.go
Normal file
72
internal/data/lastfm.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type LastFMUser struct {
|
||||
ID int `json:"id"`
|
||||
TwitchLogin string `json:"twitch_login"`
|
||||
TwitchID string `json:"twitch_id"`
|
||||
LastFMUser string `json:"lastfm_username"`
|
||||
}
|
||||
|
||||
type LastFMUserModel struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
func (l LastFMUserModel) Get(login string) (*LastFMUser, error) {
|
||||
query := `
|
||||
SELECT id, twitch_login, twitch_id, lastfm_username
|
||||
FROM lastfm_users
|
||||
WHERE twitch_login = $1`
|
||||
|
||||
var lastfm LastFMUser
|
||||
|
||||
err := l.DB.QueryRow(query, login).Scan(
|
||||
&lastfm.ID,
|
||||
&lastfm.TwitchLogin,
|
||||
&lastfm.TwitchID,
|
||||
&lastfm.LastFMUser,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, ErrRecordNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &lastfm, nil
|
||||
}
|
||||
|
||||
func (l LastFMUserModel) Insert(lastfm *LastFMUser) error {
|
||||
query := `
|
||||
INSERT into lastfm_users(twitch_login, twitch_id, lastfm_username)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (twitch_id)
|
||||
DO NOTHING
|
||||
RETURNING id;
|
||||
`
|
||||
|
||||
args := []interface{}{lastfm.TwitchLogin, lastfm.TwitchID, lastfm.LastFMUser}
|
||||
|
||||
result, err := l.DB.Exec(query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
return ErrCommandRecordAlreadyExists
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -38,6 +38,10 @@ type Models struct {
|
|||
SetHelp(name, helptext string) error
|
||||
Delete(name string) error
|
||||
}
|
||||
LastFMUsers interface {
|
||||
Get(name string) (*LastFMUser, error)
|
||||
Insert(lastfm *LastFMUser) error
|
||||
}
|
||||
Timers interface {
|
||||
Get(name string) (*Timer, error)
|
||||
Insert(timer *Timer) error
|
||||
|
@ -52,6 +56,7 @@ func NewModels(db *sql.DB) Models {
|
|||
Channels: ChannelModel{DB: db},
|
||||
Users: UserModel{DB: db},
|
||||
Commands: CommandModel{DB: db},
|
||||
LastFMUsers: LastFMUserModel{DB: db},
|
||||
Timers: TimerModel{DB: db},
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue