FEAT: Change Catagories to a list
Build, Push and Deploy CMS / build-push-deploy (push) Successful in 2m17s

This commit is contained in:
FahdShalhoub
2026-08-16 22:29:35 +03:00
parent 96cabc5716
commit 724351c271
7 changed files with 126 additions and 65 deletions
+37 -13
View File
@@ -6,6 +6,8 @@ import (
"fmt"
"thamanyah/cms/v2/internal/db"
"time"
"github.com/google/uuid"
)
var DB DBClient
@@ -14,7 +16,7 @@ type Video struct {
ID string
Title string
Description string
Category string
Categories []string
Tags string
FileName string
StorageKey string
@@ -34,26 +36,48 @@ type DBConcrete struct {
}
func (svc DBConcrete) CreateVideo(ctx context.Context, v Video) (Video, error) {
db, err := sql.Open("postgres", svc.ConnectionString)
sqlDB, err := sql.Open("postgres", svc.ConnectionString)
if err != nil {
return Video{}, err
}
defer db.Close()
defer sqlDB.Close()
var categoryID int64
if err := db.QueryRowContext(ctx, `SELECT id FROM categories WHERE name = $1`, v.Category).Scan(&categoryID); err != nil {
return Video{}, fmt.Errorf("looking up category %q: %w", v.Category, err)
id, err := uuid.NewV7()
if err != nil {
return Video{}, fmt.Errorf("generating video id: %w", err)
}
row := db.QueryRowContext(ctx, `
INSERT INTO videos (title, description, category_id, tags, file_name, storage_key, mediaconvert_job_id, status, size_bytes)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
tx, err := sqlDB.BeginTx(ctx, nil)
if err != nil {
return Video{}, err
}
defer tx.Rollback()
row := tx.QueryRowContext(ctx, `
INSERT INTO videos (id, title, description, tags, file_name, storage_key, mediaconvert_job_id, status, size_bytes)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING created_at, updated_at
`, v.Title, v.Description, categoryID, v.Tags, v.FileName, v.StorageKey, v.MediaConvertJobID, v.Status, v.SizeBytes)
`, id, v.Title, v.Description, v.Tags, v.FileName, v.StorageKey, v.MediaConvertJobID, v.Status, v.SizeBytes)
if err := row.Scan(&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)`, id, categoryID); err != nil {
return Video{}, fmt.Errorf("linking category %q: %w", category, err)
}
}
if err := tx.Commit(); err != nil {
return Video{}, err
}
v.ID = id.String()
return v, nil
}
@@ -71,13 +95,13 @@ func (svc DBConcrete) Migrate() {
// 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) {
db, err := sql.Open("postgres", svc.ConnectionString)
sqlDB, err := sql.Open("postgres", svc.ConnectionString)
if err != nil {
panic(fmt.Errorf("db: cannot open connection: %w", err))
}
defer db.Close()
defer sqlDB.Close()
if err := db.PingContext(ctx); err != nil {
if err := sqlDB.PingContext(ctx); err != nil {
panic(fmt.Errorf("db: cannot connect: %w", err))
}
}