- 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
143 lines
4.0 KiB
Go
143 lines
4.0 KiB
Go
package albums
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"time"
|
|
|
|
"galo/internal/models"
|
|
"galo/internal/sqliteutil"
|
|
)
|
|
|
|
// SQLiteStore is the SQLite implementation of Repository.
|
|
type SQLiteStore struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewSQLiteStore(db *sql.DB) *SQLiteStore { return &SQLiteStore{db: db} }
|
|
|
|
func (s *SQLiteStore) Create(ctx context.Context, a *models.Album) error {
|
|
var createdAt, updatedAt string
|
|
err := s.db.QueryRowContext(ctx,
|
|
`INSERT INTO albums (id, owner_id, title, description, cover_photo_id)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING created_at, updated_at`,
|
|
a.ID, a.OwnerID, a.Title, a.Description, a.CoverPhotoID,
|
|
).Scan(&createdAt, &updatedAt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
a.CreatedAt = sqliteutil.ParseTime(createdAt)
|
|
a.UpdatedAt = sqliteutil.ParseTime(updatedAt)
|
|
return nil
|
|
}
|
|
|
|
func (s *SQLiteStore) OwnerID(ctx context.Context, id string) (string, error) {
|
|
var ownerID string
|
|
err := s.db.QueryRowContext(ctx, `SELECT owner_id FROM albums WHERE id = $1`, id).Scan(&ownerID)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return "", ErrNotFound
|
|
}
|
|
return "", err
|
|
}
|
|
return ownerID, nil
|
|
}
|
|
|
|
const sqliteAlbumColumns = `a.id, a.owner_id, a.title, a.description, a.cover_photo_id,
|
|
a.created_at, a.updated_at, COALESCE(co.storage_key, ''),
|
|
(SELECT count(*) FROM photos WHERE album_id = a.id),
|
|
(SELECT count(*) FROM album_members WHERE album_id = a.id)`
|
|
|
|
const sqliteAlbumJoins = `
|
|
LEFT JOIN photo_objects cpo ON cpo.photo_id = a.cover_photo_id AND cpo.role = 'thumb'
|
|
LEFT JOIN objects co ON co.id = cpo.object_id`
|
|
|
|
func scanSQLiteAlbum(scan func(...any) error) (*models.Album, error) {
|
|
var a models.Album
|
|
var cover sql.NullString
|
|
var createdAt, updatedAt string
|
|
if err := scan(&a.ID, &a.OwnerID, &a.Title, &a.Description, &cover, &createdAt, &updatedAt,
|
|
&a.CoverThumbKey, &a.PhotosCount, &a.MembersCount); err != nil {
|
|
return nil, err
|
|
}
|
|
if cover.Valid {
|
|
a.CoverPhotoID = &cover.String
|
|
}
|
|
a.CreatedAt = sqliteutil.ParseTime(createdAt)
|
|
a.UpdatedAt = sqliteutil.ParseTime(updatedAt)
|
|
return &a, nil
|
|
}
|
|
|
|
func (s *SQLiteStore) GetByID(ctx context.Context, id string) (*models.Album, error) {
|
|
row := s.db.QueryRowContext(ctx,
|
|
`SELECT `+sqliteAlbumColumns+` FROM albums a`+sqliteAlbumJoins+` WHERE a.id = $1`, id)
|
|
a, err := scanSQLiteAlbum(row.Scan)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
func (s *SQLiteStore) ListForUser(ctx context.Context, userID string) ([]models.Album, error) {
|
|
rows, err := s.db.QueryContext(ctx,
|
|
`SELECT `+sqliteAlbumColumns+` FROM albums a
|
|
LEFT JOIN album_members m ON m.album_id = a.id AND m.user_id = $1`+sqliteAlbumJoins+`
|
|
WHERE a.owner_id = $1 OR m.user_id IS NOT NULL
|
|
ORDER BY a.created_at DESC`, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []models.Album{}
|
|
for rows.Next() {
|
|
a, err := scanSQLiteAlbum(rows.Scan)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, *a)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *SQLiteStore) Update(ctx context.Context, a *models.Album) error {
|
|
var updatedAt string
|
|
err := s.db.QueryRowContext(ctx,
|
|
`UPDATE albums
|
|
SET title = $2, description = $3, cover_photo_id = $4, updated_at = $5
|
|
WHERE id = $1
|
|
RETURNING updated_at`,
|
|
a.ID, a.Title, a.Description, a.CoverPhotoID, sqliteutil.FormatTime(time.Now().UTC()),
|
|
).Scan(&updatedAt)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return ErrNotFound
|
|
}
|
|
return err
|
|
}
|
|
a.UpdatedAt = sqliteutil.ParseTime(updatedAt)
|
|
return nil
|
|
}
|
|
|
|
func (s *SQLiteStore) Delete(ctx context.Context, id string) error {
|
|
res, err := s.db.ExecContext(ctx, `DELETE FROM albums WHERE id = $1`, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n, _ := res.RowsAffected(); n == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *SQLiteStore) TrashPhotosByAlbum(ctx context.Context, albumID string) error {
|
|
_, err := s.db.ExecContext(ctx,
|
|
`UPDATE photos SET deleted_at = $2 WHERE album_id = $1 AND deleted_at IS NULL`,
|
|
albumID, sqliteutil.FormatTime(time.Now().UTC()))
|
|
return err
|
|
}
|