101 lines
4.5 KiB
Go
101 lines
4.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/example/sndit/backend/internal/config"
|
|
"github.com/example/sndit/backend/internal/db"
|
|
"github.com/google/uuid"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
const (
|
|
demoEmail = "demo@example.com"
|
|
demoPassword = "DemoPassword123!"
|
|
)
|
|
|
|
func main() {
|
|
cfg := config.Load()
|
|
ctx := context.Background()
|
|
database, err := db.New(ctx, cfg.DBDriver, cfg.DatabaseDSN())
|
|
if err != nil {
|
|
log.Fatalf("database unavailable: %v", err)
|
|
}
|
|
defer database.Close()
|
|
|
|
if err := seed(ctx, database); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
log.Printf("seeded %s with the Noah Bianchi demo gallery", demoEmail)
|
|
}
|
|
|
|
func seed(ctx context.Context, database *sql.DB) error {
|
|
passwordHash, err := bcrypt.GenerateFromPassword([]byte(demoPassword), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return fmt.Errorf("hash demo password: %w", err)
|
|
}
|
|
if _, err := database.ExecContext(ctx, `
|
|
INSERT INTO users (id, email, password_hash, name)
|
|
VALUES ($1, $2, $3, $4)
|
|
ON CONFLICT (email) DO UPDATE SET password_hash = EXCLUDED.password_hash, name = EXCLUDED.name, updated_at = CURRENT_TIMESTAMP
|
|
`, demoUserID, demoEmail, string(passwordHash), "Noah Bianchi"); err != nil {
|
|
return fmt.Errorf("seed demo user: %w", err)
|
|
}
|
|
|
|
if _, err := database.ExecContext(ctx, `
|
|
INSERT INTO galleries (
|
|
id, user_id, slug, title, client_name, description, status,
|
|
downloads_enabled, favorites_enabled, download_all_enabled, watermark_enabled,
|
|
cover_media_id, theme_config, branding_config, published_at
|
|
)
|
|
VALUES ($1, $2, $3, $4, $5, $6, 'published', TRUE, TRUE, TRUE, FALSE, $7, $8, $9, CURRENT_TIMESTAMP)
|
|
ON CONFLICT (slug) DO UPDATE SET
|
|
user_id = EXCLUDED.user_id, title = EXCLUDED.title, client_name = EXCLUDED.client_name,
|
|
description = EXCLUDED.description, status = EXCLUDED.status,
|
|
downloads_enabled = EXCLUDED.downloads_enabled, favorites_enabled = EXCLUDED.favorites_enabled,
|
|
download_all_enabled = EXCLUDED.download_all_enabled, watermark_enabled = EXCLUDED.watermark_enabled,
|
|
cover_media_id = EXCLUDED.cover_media_id, theme_config = EXCLUDED.theme_config,
|
|
branding_config = EXCLUDED.branding_config, published_at = EXCLUDED.published_at,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
`, demoGalleryID, demoUserID, "emma-james-wedding", "Emma & James", "Emma & James", "An early summer wedding, held close to the water and the people who make it home.", demoCoverID,
|
|
`{"mode":"light","layout":"editorial","accent":"#ad695b","font":"serif"}`,
|
|
`{"studioName":"Noah Bianchi","tagline":"Photographs for keeps.","websiteUrl":"https://example.com","instagramUrl":"https://instagram.com"}`); err != nil {
|
|
return fmt.Errorf("seed demo gallery: %w", err)
|
|
}
|
|
|
|
mediaRows := []struct {
|
|
id, filename, mime, externalURL string
|
|
sortOrder int
|
|
duration float64
|
|
}{
|
|
{demoCoverID.String(), "emma-james-01.jpg", "image/jpeg", "https://images.unsplash.com/photo-1519741497674-611481863552?auto=format&fit=crop&w=1800&q=88", 1, 0},
|
|
{demoPhotoID.String(), "emma-james-02.jpg", "image/jpeg", "https://images.unsplash.com/photo-1511285560929-80b456fea0bc?auto=format&fit=crop&w=1800&q=88", 2, 0},
|
|
{demoVideoID.String(), "emma-james-film.mp4", "video/mp4", "https://storage.googleapis.com/coverr-main/mp4/Mt_Baker.mp4", 3, 31.0},
|
|
}
|
|
for _, item := range mediaRows {
|
|
if _, err := database.ExecContext(ctx, `
|
|
INSERT INTO media (id, gallery_id, original_filename, mime_type, file_size, storage_key, external_url, processing_status, sort_order, duration_seconds)
|
|
VALUES ($1, $2, $3, $4, 0, $5, $6, 'READY', $7, $8)
|
|
ON CONFLICT (id) DO UPDATE SET
|
|
gallery_id = EXCLUDED.gallery_id, original_filename = EXCLUDED.original_filename,
|
|
mime_type = EXCLUDED.mime_type, storage_key = EXCLUDED.storage_key,
|
|
external_url = EXCLUDED.external_url, processing_status = EXCLUDED.processing_status,
|
|
sort_order = EXCLUDED.sort_order, duration_seconds = EXCLUDED.duration_seconds, updated_at = CURRENT_TIMESTAMP
|
|
`, uuid.MustParse(item.id), demoGalleryID, item.filename, item.mime, "demo/"+item.filename, item.externalURL, item.sortOrder, item.duration); err != nil {
|
|
return fmt.Errorf("seed demo media %s: %w", item.filename, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var (
|
|
demoUserID = uuid.MustParse("55555555-5555-4555-8555-555555555555")
|
|
demoGalleryID = uuid.MustParse("66666666-6666-4666-8666-666666666666")
|
|
demoCoverID = uuid.MustParse("77777777-7777-4777-8777-777777777777")
|
|
demoPhotoID = uuid.MustParse("88888888-8888-4888-8888-888888888888")
|
|
demoVideoID = uuid.MustParse("99999999-9999-4999-8999-999999999999")
|
|
)
|