|
|
|
@@ -3,6 +3,7 @@ package repositories
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
"thamanyah/cms/v2/internal/models"
|
|
|
|
@@ -14,6 +15,66 @@ type VideoRepository struct {
|
|
|
|
|
|
|
|
|
|
var VideoRepo VideoRepository
|
|
|
|
|
|
|
|
|
|
// UpdateVideoStatusByJobID moves the video a transcoding job belongs to into a
|
|
|
|
|
// new status. It returns ErrVideoNotFound when no video carries that job id,
|
|
|
|
|
// which the consumer treats as a message to drop rather than a fault.
|
|
|
|
|
func (svc VideoRepository) UpdateVideoStatusByJobID(ctx context.Context, jobID string, status models.VideoStatus) error {
|
|
|
|
|
result, err := svc.SQLDB.ExecContext(ctx, `
|
|
|
|
|
UPDATE videos
|
|
|
|
|
SET status = $1, updated_at = now()
|
|
|
|
|
WHERE mediaconvert_job_id = $2
|
|
|
|
|
`, string(status), jobID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
affected, err := result.RowsAffected()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if affected == 0 {
|
|
|
|
|
return ErrVideoNotFound
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetVideoByID reads one video and the ids of the categories it is filed
|
|
|
|
|
// under. It returns ErrVideoNotFound when no video has that id.
|
|
|
|
|
func (svc VideoRepository) GetVideoByID(ctx context.Context, id string) (models.Video, error) {
|
|
|
|
|
var v models.Video
|
|
|
|
|
|
|
|
|
|
err := svc.SQLDB.QueryRowContext(ctx, `
|
|
|
|
|
SELECT id, title, description, tags, file_name, storage_key, mediaconvert_job_id, status, size_bytes, created_at, updated_at
|
|
|
|
|
FROM videos
|
|
|
|
|
WHERE id = $1
|
|
|
|
|
`, id).Scan(&v.ID, &v.Title, &v.Description, &v.Tags, &v.FileName, &v.StorageKey,
|
|
|
|
|
&v.MediaConvertJobID, &v.Status, &v.SizeBytes, &v.CreatedAt, &v.UpdatedAt)
|
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
return models.Video{}, ErrVideoNotFound
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return models.Video{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rows, err := svc.SQLDB.QueryContext(ctx,
|
|
|
|
|
`SELECT category_id FROM video_categories WHERE video_id = $1 ORDER BY category_id`, v.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return models.Video{}, err
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var categoryID int16
|
|
|
|
|
if err := rows.Scan(&categoryID); err != nil {
|
|
|
|
|
return models.Video{}, err
|
|
|
|
|
}
|
|
|
|
|
v.CategoryIDs = append(v.CategoryIDs, categoryID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return v, rows.Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VideoExistsWithStorageKey reports whether a video has already been
|
|
|
|
|
// registered under a storage key. It is served by videos_storage_key_key, the
|
|
|
|
|
// index Postgres builds for the column's UNIQUE constraint.
|
|
|
|
|