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:
@@ -0,0 +1,30 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
func CreateDBConnection(connectionString string) *sql.DB {
|
||||
sqlDB, err := sql.Open("postgres", connectionString)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("db: cannot open connection: %w", err))
|
||||
}
|
||||
|
||||
return sqlDB
|
||||
}
|
||||
|
||||
func CloseConnection(SQLDB *sql.DB) {
|
||||
err := SQLDB.Close()
|
||||
if err != nil {
|
||||
log.Fatalf("Error Closing DB Connection: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func AssertSuccessfulConnection(ctx context.Context, SQLDB *sql.DB) {
|
||||
if err := SQLDB.PingContext(ctx); err != nil {
|
||||
panic(fmt.Errorf("db: cannot connect: %w", err))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"thamanyah/cms/v2/internal/models"
|
||||
)
|
||||
|
||||
type CatagoriesRepository interface {
|
||||
ListCategories(ctx context.Context) ([]models.Category, error)
|
||||
ListCategoriesIDs(ctx context.Context) ([]int16, error)
|
||||
}
|
||||
|
||||
var CatagoriesRepo CatagoriesRepository
|
||||
|
||||
type ConcreteCatagoriesRepository struct {
|
||||
SQLDB *sql.DB
|
||||
}
|
||||
|
||||
func (svc ConcreteCatagoriesRepository) ListCategories(ctx context.Context) ([]models.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 []models.Category
|
||||
for rows.Next() {
|
||||
var category models.Category
|
||||
if err := rows.Scan(&category.ID, &category.Name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
categories = append(categories, category)
|
||||
}
|
||||
return categories, rows.Err()
|
||||
}
|
||||
|
||||
func (svc ConcreteCatagoriesRepository) 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()
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"thamanyah/cms/v2/internal/models"
|
||||
)
|
||||
|
||||
type VideoRepository interface {
|
||||
CreateVideo(ctx context.Context, v models.Video) (models.Video, error)
|
||||
}
|
||||
|
||||
var VideoRepo VideoRepository
|
||||
|
||||
type ConcreteVideoRepository struct {
|
||||
SQLDB *sql.DB
|
||||
}
|
||||
|
||||
func (svc ConcreteVideoRepository) CreateVideo(ctx context.Context, v models.Video) (models.Video, error) {
|
||||
tx, err := svc.SQLDB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return models.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 models.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 models.Video{}, fmt.Errorf("linking categories %v: %w", v.CategoryIDs, err)
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return models.Video{}, err
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
Reference in New Issue
Block a user