feat: implement shared-album multi-user photo album
Backend (Go/Gin/pgx): env config, pgx pool with retries, Garage S3 client (path-style, region garage), embedded golang-migrate migrations, JWT auth with refresh rotation, albums CRUD, members/roles/invites with no-escalation matrix, photo upload + async imaging/goexif WebP variants, cursor pagination, presigned URLs, likes/comments, SSE hub (album + personal), notifications, share links. Frontend (SvelteKit 2 + Svelte 5 + Tailwind v4): API wrapper with 401 refresh, auth store, all screens, Uploader/PhotoGrid/Lightbox/Comments, SSE client with reconnect + REST catch-up, three themes + system via data-theme. Deploy without Docker: Makefile builds the frontend into backend/static and embeds it (go:embed) into a single statically linked binary; systemd unit and nginx SSE example included. Use gen2brain/webp (-tags nodynamic) instead of cgo kolesa-team/go-webp to keep CGO_ENABLED=0 static builds; design.md/proposal.md updated accordingly.
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
package members
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"shared-album/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
|
||||
}
|
||||
Reference in New Issue
Block a user