initial commit
This commit is contained in:
commit
5a75f99720
20 changed files with 2239 additions and 0 deletions
49
backend/db/db.go
Normal file
49
backend/db/db.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"github.com/mediocregopher/radix/v4"
|
||||
)
|
||||
|
||||
var sq = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar)
|
||||
|
||||
type DB struct {
|
||||
*pgxpool.Pool
|
||||
|
||||
Redis radix.Client
|
||||
}
|
||||
|
||||
func New(dsn string) (*DB, error) {
|
||||
pool, err := pgxpool.Connect(context.Background(), dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
redis, err := (&radix.PoolConfig{}).New(context.Background(), "tcp", os.Getenv("REDIS"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
db := &DB{
|
||||
Pool: pool,
|
||||
Redis: redis,
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// MultiCmd executes the given Redis commands in order.
|
||||
// If any return an error, the function is aborted.
|
||||
func (db *DB) MultiCmd(ctx context.Context, cmds ...radix.Action) error {
|
||||
for _, cmd := range cmds {
|
||||
err := db.Redis.Do(ctx, cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
68
backend/db/user.go
Normal file
68
backend/db/user.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
|
||||
"emperror.dev/errors"
|
||||
"github.com/georgysavva/scany/pgxscan"
|
||||
"github.com/jackc/pgconn"
|
||||
"github.com/rs/xid"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID xid.ID
|
||||
Username string
|
||||
DisplayName *string
|
||||
Bio *string
|
||||
|
||||
AvatarSource *string
|
||||
AvatarURL *string
|
||||
Links []string
|
||||
|
||||
Discord *string
|
||||
}
|
||||
|
||||
// usernames must match this regex
|
||||
var usernameRegex = regexp.MustCompile(`[\w-.]{2,40}`)
|
||||
|
||||
const (
|
||||
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")
|
||||
)
|
||||
|
||||
// CreateUser creates a user with the given username.
|
||||
func (db *DB) CreateUser(ctx context.Context, username string) (u User, err error) {
|
||||
// check if the username is valid
|
||||
// if not, return an error depending on what failed
|
||||
if !usernameRegex.MatchString(username) {
|
||||
if len(username) < 2 {
|
||||
return u, ErrUsernameTooShort
|
||||
} else if len(username) > 40 {
|
||||
return u, ErrUsernameTooLong
|
||||
}
|
||||
|
||||
return u, ErrInvalidUsername
|
||||
}
|
||||
|
||||
sql, args, err := sq.Insert("users").Columns("id", "username").Values(xid.New(), username).Suffix("RETURNING *").ToSql()
|
||||
if err != nil {
|
||||
return u, errors.Wrap(err, "building sql")
|
||||
}
|
||||
|
||||
err = pgxscan.Get(ctx, db, &u, sql, args...)
|
||||
if err != nil {
|
||||
if v, ok := errors.Cause(err).(*pgconn.PgError); ok {
|
||||
if v.Code == "23505" { // unique constraint violation
|
||||
return u, ErrUsernameTaken
|
||||
}
|
||||
}
|
||||
|
||||
return u, errors.Cause(err)
|
||||
}
|
||||
|
||||
return u, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue