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
|
@ -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