1+ <?php
2+
3+ namespace App \Http \Controllers \Api ;
4+
5+ use App \Http \Controllers \Controller ;
6+ use App \Models \Recording ;
7+ use App \Models \Show ;
8+ use Illuminate \Http \Request ;
9+ use Illuminate \Support \Str ;
10+
11+ class RecordingApiController extends Controller
12+ {
13+ /**
14+ * Get shows that need to be recorded.
15+ * Returns shows where recordable=true, actual_start and actual_end are set, and no recording exists.
16+ */
17+ public function shows ()
18+ {
19+ $ shows = Show::with ('source ' )
20+ ->where ('recordable ' , true )
21+ ->whereNotNull ('actual_start ' )
22+ ->whereNotNull ('actual_end ' )
23+ ->whereDoesntHave ('recording ' )
24+ ->get ()
25+ ->map (function ($ show ) {
26+ return [
27+ 'show_id ' => $ show ->id ,
28+ 'source ' => $ show ->source ->slug ?? null ,
29+ 'show ' => $ show ->slug ,
30+ 'start ' => $ show ->actual_start ->toIso8601String (),
31+ 'end ' => $ show ->actual_end ->toIso8601String (),
32+ 'title ' => $ show ->title ,
33+ 'description ' => $ show ->description ,
34+ ];
35+ });
36+
37+ return response ()->json ([
38+ 'success ' => true ,
39+ 'data ' => $ shows ,
40+ 'count ' => $ shows ->count ()
41+ ]);
42+ }
43+
44+ /**
45+ * Create a new recording.
46+ */
47+ public function create (Request $ request )
48+ {
49+ $ validated = $ request ->validate ([
50+ 'show_id ' => 'nullable|exists:shows,id ' ,
51+ 'title ' => 'required|string|max:255 ' ,
52+ 'slug ' => 'nullable|string|max:255|unique:recordings,slug ' ,
53+ 'description ' => 'nullable|string ' ,
54+ 'm3u8_url ' => 'required|url ' ,
55+ 'duration ' => 'nullable|integer|min:0 ' ,
56+ 'date ' => 'nullable|date ' ,
57+ 'is_published ' => 'nullable|boolean ' ,
58+ ]);
59+
60+ // If show_id is provided, get the show details
61+ if (!empty ($ validated ['show_id ' ])) {
62+ $ show = Show::find ($ validated ['show_id ' ]);
63+
64+ // Use show details if not provided
65+ if (empty ($ validated ['date ' ]) && $ show ->actual_start ) {
66+ $ validated ['date ' ] = $ show ->actual_start ;
67+ }
68+
69+ // Calculate duration if not provided
70+ if (empty ($ validated ['duration ' ]) && $ show ->actual_start && $ show ->actual_end ) {
71+ $ validated ['duration ' ] = $ show ->actual_start ->diffInSeconds ($ show ->actual_end );
72+ }
73+
74+ // Use show description if not provided
75+ if (empty ($ validated ['description ' ]) && $ show ->description ) {
76+ $ validated ['description ' ] = $ show ->description ;
77+ }
78+ }
79+
80+ // Generate slug if not provided
81+ if (empty ($ validated ['slug ' ])) {
82+ $ baseSlug = Str::slug ($ validated ['title ' ]);
83+ $ slug = $ baseSlug ;
84+ $ count = 1 ;
85+
86+ while (Recording::where ('slug ' , $ slug )->exists ()) {
87+ $ slug = $ baseSlug . '- ' . $ count ;
88+ $ count ++;
89+ }
90+
91+ $ validated ['slug ' ] = $ slug ;
92+ }
93+
94+ // Default to published
95+ if (!isset ($ validated ['is_published ' ])) {
96+ $ validated ['is_published ' ] = true ;
97+ }
98+
99+ // Default date to now if not provided
100+ if (empty ($ validated ['date ' ])) {
101+ $ validated ['date ' ] = now ();
102+ }
103+
104+ // Create the recording
105+ $ recording = Recording::create ($ validated );
106+
107+ return response ()->json ([
108+ 'success ' => true ,
109+ 'data ' => $ recording ,
110+ 'message ' => 'Recording created successfully '
111+ ], 201 );
112+ }
113+
114+ /**
115+ * Get a recording by slug.
116+ */
117+ public function getBySlug ($ slug )
118+ {
119+ $ recording = Recording::where ('slug ' , $ slug )
120+ ->where ('is_published ' , true )
121+ ->firstOrFail ();
122+
123+ return response ()->json ([
124+ 'success ' => true ,
125+ 'data ' => $ recording
126+ ]);
127+ }
128+ }
0 commit comments