REFACTOR: Optimized Catagory IDs
Build, Push and Deploy CMS / build-push-deploy (push) Successful in 2m19s
Build, Push and Deploy CMS / build-push-deploy (push) Successful in 2m19s
This commit is contained in:
+21
-16
@@ -5,17 +5,23 @@ import (
|
||||
"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
|
||||
Categories []string
|
||||
CategoryIDs []int16
|
||||
Tags string
|
||||
FileName string
|
||||
StorageKey string
|
||||
@@ -28,7 +34,7 @@ type Video struct {
|
||||
|
||||
type DBClient interface {
|
||||
CreateVideo(ctx context.Context, v Video) (Video, error)
|
||||
ListCategories(ctx context.Context) ([]string, error)
|
||||
ListCategories(ctx context.Context) ([]Category, error)
|
||||
}
|
||||
|
||||
type DBConcrete struct {
|
||||
@@ -62,14 +68,13 @@ func (svc DBConcrete) CreateVideo(ctx context.Context, v Video) (Video, error) {
|
||||
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)`, v.ID, categoryID); err != nil {
|
||||
return Video{}, fmt.Errorf("linking category %q: %w", category, 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 {
|
||||
@@ -79,20 +84,20 @@ func (svc DBConcrete) CreateVideo(ctx context.Context, v Video) (Video, error) {
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (svc DBConcrete) ListCategories(ctx context.Context) ([]string, error) {
|
||||
rows, err := svc.SQLDB.QueryContext(ctx, `SELECT name FROM categories ORDER BY name`)
|
||||
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 []string
|
||||
var categories []Category
|
||||
for rows.Next() {
|
||||
var name string
|
||||
if err := rows.Scan(&name); err != nil {
|
||||
var category Category
|
||||
if err := rows.Scan(&category.ID, &category.Name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
categories = append(categories, name)
|
||||
categories = append(categories, category)
|
||||
}
|
||||
return categories, rows.Err()
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ package services
|
||||
// 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.VideoResponse.Status.
|
||||
// and extending the `enums` annotation on api.CompleteResponse.Status.
|
||||
type VideoStatus string
|
||||
|
||||
const (
|
||||
|
||||
Reference in New Issue
Block a user