add frontend template + GET /users/{userRef} route
This commit is contained in:
parent
5a75f99720
commit
580449440a
28 changed files with 1393 additions and 12 deletions
|
@ -2,8 +2,11 @@ package db
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"emperror.dev/errors"
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"github.com/mediocregopher/radix/v4"
|
||||
|
@ -47,3 +50,69 @@ func (db *DB) MultiCmd(ctx context.Context, cmds ...radix.Action) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetJSON sets the given key to v marshaled as JSON.
|
||||
func (db *DB) SetJSON(ctx context.Context, key string, v any, args ...string) error {
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "marshaling json")
|
||||
}
|
||||
|
||||
cmdArgs := make([]string, 0, len(args)+2)
|
||||
cmdArgs = append(cmdArgs, key, string(b))
|
||||
cmdArgs = append(cmdArgs, args...)
|
||||
|
||||
err = db.Redis.Do(ctx, radix.Cmd(nil, "SET", cmdArgs...))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "writing to Redis")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetJSON gets the given key as a JSON object.
|
||||
func (db *DB) GetJSON(ctx context.Context, key string, v any) error {
|
||||
var b []byte
|
||||
|
||||
err := db.Redis.Do(ctx, radix.Cmd(&b, "GET", key))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "reading from Redis")
|
||||
}
|
||||
|
||||
if b == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if v == nil {
|
||||
return fmt.Errorf("nil pointer passed into GetJSON")
|
||||
}
|
||||
|
||||
err = json.Unmarshal(b, v)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "unmarshaling json")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDelJSON gets the given key as a JSON object and deletes it.
|
||||
func (db *DB) GetDelJSON(ctx context.Context, key string, v any) error {
|
||||
var b []byte
|
||||
|
||||
err := db.Redis.Do(ctx, radix.Cmd(&b, "GETDEL", key))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "reading from Redis")
|
||||
}
|
||||
|
||||
if b == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if v == nil {
|
||||
return fmt.Errorf("nil pointer passed into GetDelJSON")
|
||||
}
|
||||
|
||||
err = json.Unmarshal(b, v)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "unmarshaling json")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -5,8 +5,10 @@ import (
|
|||
"regexp"
|
||||
|
||||
"emperror.dev/errors"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/georgysavva/scany/pgxscan"
|
||||
"github.com/jackc/pgconn"
|
||||
"github.com/jackc/pgx/v4"
|
||||
"github.com/rs/xid"
|
||||
)
|
||||
|
||||
|
@ -27,8 +29,9 @@ type User struct {
|
|||
var usernameRegex = regexp.MustCompile(`[\w-.]{2,40}`)
|
||||
|
||||
const (
|
||||
ErrUsernameTaken = errors.Sentinel("username is already taken")
|
||||
ErrUserNotFound = errors.Sentinel("user not found")
|
||||
|
||||
ErrUsernameTaken = errors.Sentinel("username is already taken")
|
||||
ErrInvalidUsername = errors.Sentinel("username contains invalid characters")
|
||||
ErrUsernameTooShort = errors.Sentinel("username is too short")
|
||||
ErrUsernameTooLong = errors.Sentinel("username is too long")
|
||||
|
@ -66,3 +69,69 @@ func (db *DB) CreateUser(ctx context.Context, username string) (u User, err erro
|
|||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// DiscordUser fetches a user by Discord user ID.
|
||||
func (db *DB) DiscordUser(ctx context.Context, discordID string) (u User, err error) {
|
||||
sql, args, err := sq.Select("*").From("users").Where("discord = ?", discordID).ToSql()
|
||||
if err != nil {
|
||||
return u, errors.Wrap(err, "building sql")
|
||||
}
|
||||
|
||||
err = pgxscan.Get(ctx, db, &u, sql, args...)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == pgx.ErrNoRows {
|
||||
return u, ErrUserNotFound
|
||||
}
|
||||
return u, errors.Cause(err)
|
||||
}
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (u *User) UpdateFromDiscord(ctx context.Context, db pgxscan.Querier, du *discordgo.User) error {
|
||||
builder := sq.Update("users").
|
||||
Set("discord_username", du.String()).
|
||||
Where("id = ?", u.ID).
|
||||
Suffix("RETURNING *")
|
||||
|
||||
if u.AvatarSource == nil || *u.AvatarSource == "discord" {
|
||||
builder = builder.
|
||||
Set("avatar_source", "discord").
|
||||
Set("avatar_url", du.AvatarURL("1024"))
|
||||
}
|
||||
|
||||
sql, args, err := builder.ToSql()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "building sql")
|
||||
}
|
||||
|
||||
return pgxscan.Get(ctx, db, u, sql, args...)
|
||||
}
|
||||
|
||||
// User gets a user by ID.
|
||||
func (db *DB) User(ctx context.Context, id xid.ID) (u User, err error) {
|
||||
err = pgxscan.Get(ctx, db, &u, "select * from users where id = $1", id)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == pgx.ErrNoRows {
|
||||
return u, ErrUserNotFound
|
||||
}
|
||||
|
||||
return u, errors.Cause(err)
|
||||
}
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// Username gets a user by username.
|
||||
func (db *DB) Username(ctx context.Context, name string) (u User, err error) {
|
||||
err = pgxscan.Get(ctx, db, &u, "select * from users where username = $1", name)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == pgx.ErrNoRows {
|
||||
return u, ErrUserNotFound
|
||||
}
|
||||
|
||||
return u, errors.Cause(err)
|
||||
}
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue