FEAT: Change Catagories to a list
Build, Push and Deploy CMS / build-push-deploy (push) Successful in 2m17s
Build, Push and Deploy CMS / build-push-deploy (push) Successful in 2m17s
This commit is contained in:
+37
-13
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user