Initial commit: Lookbook personal collection app
Pinterest-like app for saving images, videos, quotes, and embeds. Features: - Go backend with PostgreSQL, SSR templates - Console-based admin auth (login/logout via browser console) - Item types: images, videos (ffmpeg transcoding), quotes, embeds - Media stored as BLOBs in PostgreSQL - OpenGraph metadata extraction for links - Embed detection for YouTube, Vimeo, Twitter/X - Masonry grid layout, item detail pages - Tag system with filtering - Refresh metadata endpoint with change warnings - Replace media endpoint for updating item images/videos
This commit is contained in:
commit
cdcc5b5293
45 changed files with 4634 additions and 0 deletions
158
internal/data/media/queries.go
Normal file
158
internal/data/media/queries.go
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
package media
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Row struct {
|
||||
ID int64
|
||||
ItemID int64
|
||||
MediaType string // 'original', 'thumbnail'
|
||||
ContentType string // MIME type
|
||||
Data []byte
|
||||
Width *int
|
||||
Height *int
|
||||
SourceURL *string // Original URL the media was fetched from
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type CreateParams struct {
|
||||
ItemID int64
|
||||
MediaType string
|
||||
ContentType string
|
||||
Data []byte
|
||||
Width *int
|
||||
Height *int
|
||||
SourceURL *string
|
||||
}
|
||||
|
||||
// QCreate creates a new media record.
|
||||
func QCreate(ctx context.Context, db *sql.DB, p CreateParams) (Row, error) {
|
||||
query := `
|
||||
INSERT INTO media (item_id, media_type, content_type, data, width, height, source_url)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id, item_id, media_type, content_type, data, width, height, source_url, created_at
|
||||
`
|
||||
|
||||
var row Row
|
||||
err := db.QueryRowContext(ctx, query,
|
||||
p.ItemID, p.MediaType, p.ContentType, p.Data, p.Width, p.Height, p.SourceURL,
|
||||
).Scan(
|
||||
&row.ID, &row.ItemID, &row.MediaType, &row.ContentType, &row.Data,
|
||||
&row.Width, &row.Height, &row.SourceURL, &row.CreatedAt,
|
||||
)
|
||||
return row, err
|
||||
}
|
||||
|
||||
// QFindByID finds a media record by ID.
|
||||
func QFindByID(ctx context.Context, db *sql.DB, id int64) (*Row, error) {
|
||||
query := `
|
||||
SELECT id, item_id, media_type, content_type, data, width, height, source_url, created_at
|
||||
FROM media
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
var row Row
|
||||
err := db.QueryRowContext(ctx, query, id).Scan(
|
||||
&row.ID, &row.ItemID, &row.MediaType, &row.ContentType, &row.Data,
|
||||
&row.Width, &row.Height, &row.SourceURL, &row.CreatedAt,
|
||||
)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
// QFindByItemID finds all media for an item.
|
||||
func QFindByItemID(ctx context.Context, db *sql.DB, itemID int64) ([]Row, error) {
|
||||
query := `
|
||||
SELECT id, item_id, media_type, content_type, data, width, height, source_url, created_at
|
||||
FROM media
|
||||
WHERE item_id = $1
|
||||
ORDER BY media_type ASC
|
||||
`
|
||||
|
||||
rows, err := db.QueryContext(ctx, query, itemID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var media []Row
|
||||
for rows.Next() {
|
||||
var row Row
|
||||
if err := rows.Scan(
|
||||
&row.ID, &row.ItemID, &row.MediaType, &row.ContentType, &row.Data,
|
||||
&row.Width, &row.Height, &row.SourceURL, &row.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
media = append(media, row)
|
||||
}
|
||||
return media, rows.Err()
|
||||
}
|
||||
|
||||
// QFindThumbnailByItemID finds the thumbnail for an item.
|
||||
func QFindThumbnailByItemID(ctx context.Context, db *sql.DB, itemID int64) (*Row, error) {
|
||||
query := `
|
||||
SELECT id, item_id, media_type, content_type, data, width, height, source_url, created_at
|
||||
FROM media
|
||||
WHERE item_id = $1 AND media_type = 'thumbnail'
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
var row Row
|
||||
err := db.QueryRowContext(ctx, query, itemID).Scan(
|
||||
&row.ID, &row.ItemID, &row.MediaType, &row.ContentType, &row.Data,
|
||||
&row.Width, &row.Height, &row.SourceURL, &row.CreatedAt,
|
||||
)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
// QFindOriginalByItemID finds the original media for an item.
|
||||
func QFindOriginalByItemID(ctx context.Context, db *sql.DB, itemID int64) (*Row, error) {
|
||||
query := `
|
||||
SELECT id, item_id, media_type, content_type, data, width, height, source_url, created_at
|
||||
FROM media
|
||||
WHERE item_id = $1 AND media_type = 'original'
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
var row Row
|
||||
err := db.QueryRowContext(ctx, query, itemID).Scan(
|
||||
&row.ID, &row.ItemID, &row.MediaType, &row.ContentType, &row.Data,
|
||||
&row.Width, &row.Height, &row.SourceURL, &row.CreatedAt,
|
||||
)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
// QDelete deletes a media record by ID.
|
||||
func QDelete(ctx context.Context, db *sql.DB, id int64) error {
|
||||
query := `DELETE FROM media WHERE id = $1`
|
||||
_, err := db.ExecContext(ctx, query, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// QDeleteByItemID deletes all media for an item.
|
||||
func QDeleteByItemID(ctx context.Context, db *sql.DB, itemID int64) error {
|
||||
query := `DELETE FROM media WHERE item_id = $1`
|
||||
_, err := db.ExecContext(ctx, query, itemID)
|
||||
return err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue