mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
add user models
This commit is contained in:
parent
471b02559f
commit
fda0e7d8b8
|
@ -6,7 +6,8 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
ErrRecordNotFound = errors.New("record not found")
|
||||
ErrRecordNotFound = errors.New("record not found")
|
||||
ErrRecordAlreadyExists = errors.New("channel already in database")
|
||||
)
|
||||
|
||||
// struct Models wraps the models, making them callable
|
||||
|
@ -17,6 +18,11 @@ type Models struct {
|
|||
Get(login string) (*Channel, error)
|
||||
Delete(login string) error
|
||||
}
|
||||
User interface {
|
||||
Insert(channel *Channel) error
|
||||
Get(login string) (*Channel, error)
|
||||
Delete(login string) error
|
||||
}
|
||||
}
|
||||
|
||||
func NewModels(db *sql.DB) Models {
|
||||
|
|
103
internal/data/users.go
Normal file
103
internal/data/users.go
Normal file
|
@ -0,0 +1,103 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID int `json:"id"`
|
||||
AddedAt time.Time `json:"-"`
|
||||
Login string `json:"login"`
|
||||
TwitchID string `json:"twitchid"`
|
||||
Level int `json:"level"`
|
||||
}
|
||||
|
||||
type UserModel struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
func (u UserModel) Insert(user *User) error {
|
||||
query := `
|
||||
INSERT INTO users(login, twitchid, level)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (login)
|
||||
DO NOTHING
|
||||
RETURNING id, added_at;
|
||||
`
|
||||
|
||||
args := []interface{}{user.Login, user.TwitchID, 100}
|
||||
|
||||
// Execute the query returning the number of affected rows.
|
||||
result, err := u.DB.Exec(query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Check how many rows were affected.
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
return ErrRecordAlreadyExists
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u UserModel) Get(login string) (*User, error) {
|
||||
query := `
|
||||
SELECT id, added_at, login, twitchid, level
|
||||
FROM users
|
||||
WHERE login = $1`
|
||||
|
||||
var user User
|
||||
|
||||
err := u.DB.QueryRow(query, login).Scan(
|
||||
&user.ID,
|
||||
&user.AddedAt,
|
||||
&user.Login,
|
||||
&user.TwitchID,
|
||||
&user.Level,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, ErrRecordNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (u UserModel) Delete(login string) error {
|
||||
// Prepare the statement.
|
||||
query := `
|
||||
DELETE FROM users
|
||||
WHERE login = $1`
|
||||
|
||||
// Execute the query returning the number of affected rows.
|
||||
result, err := u.DB.Exec(query, login)
|
||||
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
|
||||
}
|
1
migrations/000002_create_users_table.down.sql
Normal file
1
migrations/000002_create_users_table.down.sql
Normal file
|
@ -0,0 +1 @@
|
|||
DROP TABLE IF EXISTS users;
|
7
migrations/000002_create_users_table.up.sql
Normal file
7
migrations/000002_create_users_table.up.sql
Normal file
|
@ -0,0 +1,7 @@
|
|||
CREATE TABLE IF NOT EXISTS users (
|
||||
id bigserial PRIMARY KEY,
|
||||
added_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
|
||||
login text NOT NULL,
|
||||
twitchid text UNIQUE NOT NULL,
|
||||
level integer NOT NULL
|
||||
);
|
Loading…
Reference in a new issue