- 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
77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
package sharelinks
|
|
|
|
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) Create(ctx context.Context, l *models.ShareLink) error {
|
|
return s.pool.QueryRow(ctx,
|
|
`INSERT INTO share_links (id, album_id, token, created_by, expires_at)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING created_at`,
|
|
l.ID, l.AlbumID, l.Token, l.CreatedBy, l.ExpiresAt,
|
|
).Scan(&l.CreatedAt)
|
|
}
|
|
|
|
func (s *Store) ListByAlbum(ctx context.Context, albumID string) ([]models.ShareLink, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`SELECT id, album_id, token, created_by, created_at, expires_at, revoked_at
|
|
FROM share_links WHERE album_id = $1 ORDER BY created_at DESC`, albumID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
out := []models.ShareLink{}
|
|
for rows.Next() {
|
|
var l models.ShareLink
|
|
if err := rows.Scan(&l.ID, &l.AlbumID, &l.Token, &l.CreatedBy, &l.CreatedAt, &l.ExpiresAt, &l.RevokedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, l)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *Store) GetByToken(ctx context.Context, token string) (*models.ShareLink, error) {
|
|
var l models.ShareLink
|
|
err := s.pool.QueryRow(ctx,
|
|
`SELECT id, album_id, token, created_by, created_at, expires_at, revoked_at
|
|
FROM share_links WHERE token = $1`, token).
|
|
Scan(&l.ID, &l.AlbumID, &l.Token, &l.CreatedBy, &l.CreatedAt, &l.ExpiresAt, &l.RevokedAt)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &l, nil
|
|
}
|
|
|
|
func (s *Store) Revoke(ctx context.Context, id, albumID string) error {
|
|
tag, err := s.pool.Exec(ctx,
|
|
`UPDATE share_links SET revoked_at = now() WHERE id = $1 AND album_id = $2 AND revoked_at IS NULL`,
|
|
id, albumID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|