Files
thamanyah/cms/internal/services/db.go
T
FahdShalhoub 785154cec6
Build, Push and Deploy CMS / build-push-deploy (push) Successful in 2m11s
REFACTOR: Changed Video ID Creation To DB
2026-08-16 22:49:50 +03:00

125 lines
3.2 KiB
Go

package services
import (
"context"
"database/sql"
"fmt"
"thamanyah/cms/v2/internal/db"
"time"
)
var DB DBClient
type Video struct {
ID string
Title string
Description string
Categories []string
Tags string
FileName string
StorageKey string
MediaConvertJobID string
Status string
SizeBytes int64
CreatedAt time.Time
UpdatedAt time.Time
}
type DBClient interface {
CreateVideo(ctx context.Context, v Video) (Video, error)
ListCategories(ctx context.Context) ([]string, error)
}
type DBConcrete struct {
ConnectionString string
}
func (svc DBConcrete) CreateVideo(ctx context.Context, v Video) (Video, error) {
sqlDB, err := sql.Open("postgres", svc.ConnectionString)
if err != nil {
return Video{}, err
}
defer sqlDB.Close()
tx, err := sqlDB.BeginTx(ctx, nil)
if err != nil {
return Video{}, err
}
defer tx.Rollback()
row := tx.QueryRowContext(ctx, `
INSERT INTO videos (title, description, tags, file_name, storage_key, mediaconvert_job_id, status, size_bytes)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id, created_at, updated_at
`, v.Title, v.Description, v.Tags, v.FileName, v.StorageKey, v.MediaConvertJobID, v.Status, v.SizeBytes)
if err := row.Scan(&v.ID, &v.CreatedAt, &v.UpdatedAt); err != nil {
return Video{}, err
}
for _, category := range v.Categories {
var categoryID int64
if err := tx.QueryRowContext(ctx, `SELECT id FROM categories WHERE name = $1`, category).Scan(&categoryID); err != nil {
return Video{}, fmt.Errorf("looking up category %q: %w", category, err)
}
if _, err := tx.ExecContext(ctx, `INSERT INTO video_categories (video_id, category_id) VALUES ($1, $2)`, v.ID, categoryID); err != nil {
return Video{}, fmt.Errorf("linking category %q: %w", category, err)
}
}
if err := tx.Commit(); err != nil {
return Video{}, err
}
return v, nil
}
func (svc DBConcrete) ListCategories(ctx context.Context) ([]string, error) {
sqlDB, err := sql.Open("postgres", svc.ConnectionString)
if err != nil {
return nil, err
}
defer sqlDB.Close()
rows, err := sqlDB.QueryContext(ctx, `SELECT name FROM categories ORDER BY name`)
if err != nil {
return nil, err
}
defer rows.Close()
var categories []string
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
return nil, err
}
categories = append(categories, name)
}
return categories, rows.Err()
}
func (svc DBConcrete) Migrate() {
sqlDB, err := sql.Open("postgres", svc.ConnectionString)
if err != nil {
panic(fmt.Errorf("failed to open db connection: %w", err))
}
if err := db.Migrate(sqlDB); err != nil {
panic(err)
}
sqlDB.Close()
}
// AssertSuccessfulConnection verifies the database is reachable, mirroring
// S3Concrete's boot-time check — panics rather than let the service come up broken.
func (svc DBConcrete) AssertSuccessfulConnection(ctx context.Context) {
sqlDB, err := sql.Open("postgres", svc.ConnectionString)
if err != nil {
panic(fmt.Errorf("db: cannot open connection: %w", err))
}
defer sqlDB.Close()
if err := sqlDB.PingContext(ctx); err != nil {
panic(fmt.Errorf("db: cannot connect: %w", err))
}
}