2022-09-20 12:55:00 +02:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"emperror.dev/errors"
|
|
|
|
"github.com/georgysavva/scany/pgxscan"
|
2022-10-03 10:59:30 +02:00
|
|
|
"github.com/jackc/pgconn"
|
2022-09-20 12:55:00 +02:00
|
|
|
"github.com/jackc/pgx/v4"
|
|
|
|
"github.com/rs/xid"
|
|
|
|
)
|
|
|
|
|
2022-10-03 10:59:30 +02:00
|
|
|
const MaxMemberCount = 500
|
|
|
|
|
2022-09-20 12:55:00 +02:00
|
|
|
type Member struct {
|
2022-09-20 14:12:29 +02:00
|
|
|
ID xid.ID
|
|
|
|
UserID xid.ID
|
|
|
|
Name string
|
|
|
|
Bio *string
|
|
|
|
AvatarURLs []string `db:"avatar_urls"`
|
|
|
|
Links []string
|
2022-09-20 12:55:00 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 10:59:30 +02:00
|
|
|
const (
|
|
|
|
ErrMemberNotFound = errors.Sentinel("member not found")
|
|
|
|
ErrMemberNameInUse = errors.Sentinel("member name already in use")
|
|
|
|
)
|
2022-09-20 12:55:00 +02:00
|
|
|
|
|
|
|
func (db *DB) Member(ctx context.Context, id xid.ID) (m Member, err error) {
|
|
|
|
sql, args, err := sq.Select("*").From("members").Where("id = ?", id).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return m, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgxscan.Get(ctx, db, &m, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Cause(err) == pgx.ErrNoRows {
|
|
|
|
return m, ErrMemberNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, errors.Wrap(err, "retrieving member")
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) UserMember(ctx context.Context, userID xid.ID, memberRef string) (m Member, err error) {
|
|
|
|
sql, args, err := sq.Select("*").From("members").
|
|
|
|
Where("user_id = ? and (id = ? or name = ?)", userID, memberRef, memberRef).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return m, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgxscan.Get(ctx, db, &m, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Cause(err) == pgx.ErrNoRows {
|
|
|
|
return m, ErrMemberNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, errors.Wrap(err, "retrieving member")
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) UserMembers(ctx context.Context, userID xid.ID) (ms []Member, err error) {
|
|
|
|
sql, args, err := sq.Select("*").From("members").Where("user_id = ?", userID).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgxscan.Select(ctx, db, &ms, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "retrieving members")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ms == nil {
|
|
|
|
ms = make([]Member, 0)
|
|
|
|
}
|
|
|
|
return ms, nil
|
|
|
|
}
|
2022-10-03 10:59:30 +02:00
|
|
|
|
|
|
|
// CreateMember creates a member.
|
|
|
|
func (db *DB) CreateMember(ctx context.Context, tx pgx.Tx, userID xid.ID, name, bio string, links []string) (m Member, err error) {
|
|
|
|
sql, args, err := sq.Insert("members").
|
|
|
|
Columns("user_id", "id", "name", "bio", "links").
|
|
|
|
Values(userID, xid.New(), name, bio, links).
|
|
|
|
Suffix("RETURNING *").ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return m, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgxscan.Get(ctx, db, &m, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
pge := &pgconn.PgError{}
|
|
|
|
if errors.As(err, &pge) {
|
|
|
|
if pge.Code == "23505" {
|
|
|
|
return m, ErrMemberNameInUse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, errors.Wrap(err, "executing query")
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MemberCount returns the number of members that the given user has.
|
|
|
|
func (db *DB) MemberCount(ctx context.Context, userID xid.ID) (n int64, err error) {
|
|
|
|
sql, args, err := sq.Select("count(id)").From("members").Where("user_id = ?", userID).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.QueryRow(ctx, sql, args...).Scan(&n)
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrap(err, "executing query")
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|