REFACTOR: Seperated Catagories And Video DB Repositories
Build, Push and Deploy CMS / build-push-deploy (push) Successful in 2m45s
Build, Push and Deploy CMS / build-push-deploy (push) Successful in 2m45s
This commit is contained in:
@@ -1,141 +0,0 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"thamanyah/cms/v2/internal/db"
|
||||
"time"
|
||||
)
|
||||
|
||||
var DB DBClient
|
||||
|
||||
type Category struct {
|
||||
ID int16
|
||||
Name string
|
||||
}
|
||||
|
||||
type Video struct {
|
||||
ID string
|
||||
Title string
|
||||
Description string
|
||||
CategoryIDs []int16
|
||||
Tags string
|
||||
FileName string
|
||||
StorageKey string
|
||||
MediaConvertJobID string
|
||||
Status VideoStatus
|
||||
SizeBytes int64
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type DBClient interface {
|
||||
CreateVideo(ctx context.Context, v Video) (Video, error)
|
||||
ListCategories(ctx context.Context) ([]Category, error)
|
||||
ListCategoriesIDs(ctx context.Context) ([]int16, error)
|
||||
}
|
||||
|
||||
type DBConcrete struct {
|
||||
SQLDB *sql.DB
|
||||
}
|
||||
|
||||
func CreateConcreteDbClient(connectionString string) DBConcrete {
|
||||
sqlDB, err := sql.Open("postgres", connectionString)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("db: cannot open connection: %w", err))
|
||||
}
|
||||
return DBConcrete{
|
||||
SQLDB: sqlDB,
|
||||
}
|
||||
}
|
||||
|
||||
func (svc DBConcrete) CreateVideo(ctx context.Context, v Video) (Video, error) {
|
||||
tx, err := svc.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, string(v.Status), v.SizeBytes)
|
||||
|
||||
if err := row.Scan(&v.ID, &v.CreatedAt, &v.UpdatedAt); err != nil {
|
||||
return Video{}, err
|
||||
}
|
||||
|
||||
rows := make([]string, 0, len(v.CategoryIDs))
|
||||
for _, categoryID := range v.CategoryIDs {
|
||||
rows = append(rows, fmt.Sprintf("($1, %d)", categoryID))
|
||||
}
|
||||
|
||||
if _, err := tx.ExecContext(ctx, `INSERT INTO video_categories (video_id, category_id) VALUES `+strings.Join(rows, ", "), v.ID); err != nil {
|
||||
return Video{}, fmt.Errorf("linking categories %v: %w", v.CategoryIDs, err)
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return Video{}, err
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (svc DBConcrete) ListCategories(ctx context.Context) ([]Category, error) {
|
||||
rows, err := svc.SQLDB.QueryContext(ctx, `SELECT id, name FROM categories ORDER BY name`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var categories []Category
|
||||
for rows.Next() {
|
||||
var category Category
|
||||
if err := rows.Scan(&category.ID, &category.Name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
categories = append(categories, category)
|
||||
}
|
||||
return categories, rows.Err()
|
||||
}
|
||||
|
||||
func (svc DBConcrete) ListCategoriesIDs(ctx context.Context) ([]int16, error) {
|
||||
rows, err := svc.SQLDB.QueryContext(ctx, `SELECT id FROM categories`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var categories []int16
|
||||
for rows.Next() {
|
||||
var category int16
|
||||
if err := rows.Scan(&category); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
categories = append(categories, category)
|
||||
}
|
||||
return categories, rows.Err()
|
||||
}
|
||||
|
||||
func (svc DBConcrete) Migrate() {
|
||||
if err := db.Migrate(svc.SQLDB); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (svc DBConcrete) CloseConnection() {
|
||||
err := svc.SQLDB.Close()
|
||||
if err != nil {
|
||||
log.Fatalf("Error Closing DB Connection: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (svc DBConcrete) AssertSuccessfulConnection(ctx context.Context) {
|
||||
if err := svc.SQLDB.PingContext(ctx); err != nil {
|
||||
panic(fmt.Errorf("db: cannot connect: %w", err))
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package services
|
||||
|
||||
// VideoStatus is the transcoding lifecycle state of a video, stored verbatim
|
||||
// in videos.status. The set is closed on both sides: the database rejects
|
||||
// anything outside it via the videos_status_check constraint. Adding a state
|
||||
// means adding a constant here, widening that constraint in a new migration,
|
||||
// and extending the `enums` annotation on api.CompleteResponse.Status.
|
||||
type VideoStatus string
|
||||
|
||||
const (
|
||||
// VideoStatusProcessing is set when the MediaConvert job is queued. Every
|
||||
// video starts here, and nothing moves it on yet — see Known gaps.
|
||||
VideoStatusProcessing VideoStatus = "processing"
|
||||
// VideoStatusReady means transcoding finished and the output is in the
|
||||
// encoded bucket, reachable through CloudFront.
|
||||
VideoStatusReady VideoStatus = "ready"
|
||||
// VideoStatusFailed means the MediaConvert job ended in an error.
|
||||
VideoStatusFailed VideoStatus = "failed"
|
||||
)
|
||||
|
||||
func (s VideoStatus) String() string {
|
||||
return string(s)
|
||||
}
|
||||
Reference in New Issue
Block a user