pronounss/backend/db/member.go

121 lines
3 KiB
Go
Raw Normal View History

package db
import (
"context"
"emperror.dev/errors"
"github.com/georgysavva/scany/pgxscan"
2022-10-03 10:59:30 +02:00
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/rs/xid"
)
2022-10-03 10:59:30 +02:00
const MaxMemberCount = 500
type Member struct {
2022-11-20 21:09:29 +01:00
ID xid.ID
UserID xid.ID
Name string
DisplayName *string
Bio *string
AvatarURLs []string `db:"avatar_urls"`
Links []string
}
2022-10-03 10:59:30 +02:00
const (
ErrMemberNotFound = errors.Sentinel("member not found")
ErrMemberNameInUse = errors.Sentinel("member name already in use")
)
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.
2022-11-20 21:09:29 +01:00
func (db *DB) CreateMember(ctx context.Context, tx pgx.Tx, userID xid.ID, name string, displayName *string, bio string, links []string) (m Member, err error) {
2022-10-03 10:59:30 +02:00
sql, args, err := sq.Insert("members").
2022-11-20 21:09:29 +01:00
Columns("user_id", "id", "name", "display_name", "bio", "links").
Values(userID, xid.New(), name, displayName, bio, links).
2022-10-03 10:59:30 +02:00
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
}