3 namespace App\Http\Controllers;
5 use App\Models\Channel;
6 use App\Models\Episode;
8 use Illuminate\Http\Request;
10 class EpisodeController extends Controller
13 public function addRestream(Request $request, Episode $episode) {
14 $this->authorize('addRestream', $episode);
15 $validatedData = $request->validate([
16 'channel_id' => 'numeric|exists:App\Models\Channel,id',
19 $channel = Channel::find($validatedData['channel_id']);
20 $this->authorize('addEpisode', $channel);
22 foreach ($episode->channels as $c) {
23 if ($c->id == $channel->id) {
24 throw new \Exception('channel already exists on episode');
28 $episode->channels()->attach($channel);
30 return $episode->load('channels')->toJson();
33 public function removeRestream(Request $request, Episode $episode) {
34 $this->authorize('removeRestream', $episode);
35 $validatedData = $request->validate([
36 'channel_id' => 'numeric|exists:App\Models\Channel,id',
39 $channel = Channel::find($validatedData['channel_id']);
40 $this->authorize('removeEpisode', $channel);
42 $episode->channels()->detach($channel);
44 return $episode->load('channels')->toJson();
47 public function search(Request $request) {
48 $validatedData = $request->validate([
49 'after' => 'nullable|date',
50 'before' => 'nullable|date',
51 'event' => 'nullable|array',
52 'event.*' => 'numeric',
54 $after = isset($validatedData['after']) ? $validatedData['after'] : Carbon::now()->sub(2, 'hours');
55 $before = isset($validatedData['before']) ? $validatedData['before'] : Carbon::now()->add(1, 'days');
56 $episodes = Episode::with(['channels', 'event', 'players', 'players.user'])
57 ->select('episodes.*')
58 ->join('events', 'episodes.event_id', '=', 'events.id')
59 ->where('episodes.confirmed', '=', true)
60 ->where('episodes.start', '>=', $after)
61 ->where('episodes.start', '<=', $before)
62 ->where('events.visible', '=', true)
63 ->orderBy('episodes.start')
65 if (!empty($validatedData['event'])) {
66 $episodes = $episodes->whereIn('episodes.event_id', $validatedData['event']);
68 if ($request->user() && $request->user()->isPrivileged()) {
69 $episodes = $episodes->with(['crew', 'crew.user']);
71 $episodes = $episodes->with([
72 'crew' => function ($query) {
73 $query->where('confirmed', true);
78 return $episodes->get()->toJson();
81 public function single(Request $request, Episode $episode) {
82 $this->authorize('view', $episode);
83 return $episode->toJson();