REFACTOR: Changed Video ID Creation To DB
Build, Push and Deploy CMS / build-push-deploy (push) Successful in 2m11s
Build, Push and Deploy CMS / build-push-deploy (push) Successful in 2m11s
This commit is contained in:
@@ -6,8 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"thamanyah/cms/v2/internal/db"
|
"thamanyah/cms/v2/internal/db"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var DB DBClient
|
var DB DBClient
|
||||||
@@ -43,11 +41,6 @@ func (svc DBConcrete) CreateVideo(ctx context.Context, v Video) (Video, error) {
|
|||||||
}
|
}
|
||||||
defer sqlDB.Close()
|
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)
|
tx, err := sqlDB.BeginTx(ctx, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Video{}, err
|
return Video{}, err
|
||||||
@@ -55,12 +48,12 @@ func (svc DBConcrete) CreateVideo(ctx context.Context, v Video) (Video, error) {
|
|||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
row := tx.QueryRowContext(ctx, `
|
row := tx.QueryRowContext(ctx, `
|
||||||
INSERT INTO videos (id, title, description, tags, file_name, storage_key, mediaconvert_job_id, status, size_bytes)
|
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, $9)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||||
RETURNING created_at, updated_at
|
RETURNING id, created_at, updated_at
|
||||||
`, id, v.Title, v.Description, v.Tags, v.FileName, v.StorageKey, v.MediaConvertJobID, v.Status, v.SizeBytes)
|
`, 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
|
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 {
|
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)
|
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)
|
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
|
return Video{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
v.ID = id.String()
|
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user