- rename Go module, binary, systemd unit, PG_DATABASE default, frontend package and docs from shared-album to galo - add theme-aware halo emblem and favicon (new ui-branding capability) - add make build-release (versioned, stripped, static binary) and run the binary from the repo root
122 lines
3.3 KiB
Go
122 lines
3.3 KiB
Go
package members
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"galo/internal/models"
|
|
)
|
|
|
|
var ErrNotFound = errors.New("not found")
|
|
|
|
type Store struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewStore(pool *pgxpool.Pool) *Store { return &Store{pool: pool} }
|
|
|
|
func (s *Store) Role(ctx context.Context, albumID, userID string) (string, error) {
|
|
var role string
|
|
err := s.pool.QueryRow(ctx,
|
|
`SELECT role FROM album_members WHERE album_id = $1 AND user_id = $2`,
|
|
albumID, userID).Scan(&role)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return "", nil
|
|
}
|
|
return "", err
|
|
}
|
|
return role, nil
|
|
}
|
|
|
|
func (s *Store) List(ctx context.Context, albumID string) ([]models.AlbumMember, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`SELECT m.album_id, m.user_id, m.role, m.invited_by, m.joined_at,
|
|
u.id, u.email, u.name, COALESCE(u.avatar_url, '')
|
|
FROM album_members m
|
|
JOIN users u ON u.id = m.user_id
|
|
WHERE m.album_id = $1
|
|
ORDER BY m.joined_at`, albumID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
out := []models.AlbumMember{}
|
|
for rows.Next() {
|
|
var m models.AlbumMember
|
|
if err := rows.Scan(&m.AlbumID, &m.UserID, &m.Role, &m.InvitedBy, &m.JoinedAt,
|
|
&m.User.ID, &m.User.Email, &m.User.Name, &m.User.AvatarURL); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, m)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *Store) Add(ctx context.Context, albumID, userID, role string, invitedBy *string) error {
|
|
_, err := s.pool.Exec(ctx,
|
|
`INSERT INTO album_members (album_id, user_id, role, invited_by)
|
|
VALUES ($1, $2, $3, $4)
|
|
ON CONFLICT (album_id, user_id) DO UPDATE SET role = EXCLUDED.role`,
|
|
albumID, userID, role, invitedBy)
|
|
return err
|
|
}
|
|
|
|
func (s *Store) UpdateRole(ctx context.Context, albumID, userID, role string) error {
|
|
tag, err := s.pool.Exec(ctx,
|
|
`UPDATE album_members SET role = $3 WHERE album_id = $1 AND user_id = $2`,
|
|
albumID, userID, role)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) Remove(ctx context.Context, albumID, userID string) error {
|
|
tag, err := s.pool.Exec(ctx,
|
|
`DELETE FROM album_members WHERE album_id = $1 AND user_id = $2`, albumID, userID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) CreateInvite(ctx context.Context, inv *models.Invite) error {
|
|
return s.pool.QueryRow(ctx,
|
|
`INSERT INTO invites (id, album_id, token, role, created_by, expires_at, max_uses)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
RETURNING created_at`,
|
|
inv.ID, inv.AlbumID, inv.Token, inv.Role, inv.CreatedBy, inv.ExpiresAt, inv.MaxUses,
|
|
).Scan(&inv.CreatedAt)
|
|
}
|
|
|
|
func (s *Store) GetInviteByToken(ctx context.Context, token string) (*models.Invite, error) {
|
|
var inv models.Invite
|
|
err := s.pool.QueryRow(ctx,
|
|
`SELECT id, album_id, token, role, created_by, expires_at, max_uses, uses, created_at
|
|
FROM invites WHERE token = $1`, token).
|
|
Scan(&inv.ID, &inv.AlbumID, &inv.Token, &inv.Role, &inv.CreatedBy, &inv.ExpiresAt, &inv.MaxUses, &inv.Uses, &inv.CreatedAt)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &inv, nil
|
|
}
|
|
|
|
func (s *Store) IncrementUses(ctx context.Context, id string) error {
|
|
_, err := s.pool.Exec(ctx, `UPDATE invites SET uses = uses + 1 WHERE id = $1`, id)
|
|
return err
|
|
}
|