From 785154cec637897361a8448ba60c937fa31e344c Mon Sep 17 00:00:00 2001 From: FahdShalhoub Date: Sun, 16 Aug 2026 22:49:50 +0300 Subject: [PATCH] REFACTOR: Changed Video ID Creation To DB --- cms/internal/services/db.go | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/cms/internal/services/db.go b/cms/internal/services/db.go index 93dee34..a18d596 100644 --- a/cms/internal/services/db.go +++ b/cms/internal/services/db.go @@ -6,8 +6,6 @@ import ( "fmt" "thamanyah/cms/v2/internal/db" "time" - - "github.com/google/uuid" ) var DB DBClient @@ -43,11 +41,6 @@ func (svc DBConcrete) CreateVideo(ctx context.Context, v Video) (Video, error) { } defer sqlDB.Close() - id, err := uuid.NewV7() - if err != nil { - return Video{}, fmt.Errorf("generating video id: %w", err) - } - tx, err := sqlDB.BeginTx(ctx, nil) if err != nil { return Video{}, err @@ -55,12 +48,12 @@ func (svc DBConcrete) CreateVideo(ctx context.Context, v Video) (Video, error) { 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 - `, id, v.Title, v.Description, v.Tags, v.FileName, v.StorageKey, v.MediaConvertJobID, v.Status, v.SizeBytes) + 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.CreatedAt, &v.UpdatedAt); err != nil { + if err := row.Scan(&v.ID, &v.CreatedAt, &v.UpdatedAt); err != nil { return Video{}, err } @@ -69,7 +62,7 @@ func (svc DBConcrete) CreateVideo(ctx context.Context, v Video) (Video, error) { 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 { + 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) } } @@ -78,7 +71,6 @@ func (svc DBConcrete) CreateVideo(ctx context.Context, v Video) (Video, error) { return Video{}, err } - v.ID = id.String() return v, nil }