]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/EpisodeController.php
basic channel crew
[alttp.git] / app / Http / Controllers / EpisodeController.php
1 <?php
2
3 namespace App\Http\Controllers;
4
5 use App\Models\Channel;
6 use App\Models\Episode;
7 use Carbon\Carbon;
8 use Illuminate\Http\Request;
9
10 class EpisodeController extends Controller
11 {
12
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',
17                 ]);
18
19                 $channel = Channel::find($validatedData['channel_id']);
20                 $this->authorize('addEpisode', $channel);
21
22                 foreach ($episode->channels as $c) {
23                         if ($c->id == $channel->id) {
24                                 throw new \Exception('channel already exists on episode');
25                         }
26                 }
27
28                 $episode->channels()->attach($channel);
29
30                 return $episode->load('channels')->toJson();
31         }
32
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',
37                 ]);
38
39                 $channel = Channel::find($validatedData['channel_id']);
40                 $this->authorize('removeEpisode', $channel);
41
42                 $episode->channels()->detach($channel);
43
44                 return $episode->load('channels')->toJson();
45         }
46
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',
53                 ]);
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')
64                         ->limit(1000);
65                 if (!empty($validatedData['event'])) {
66                         $episodes = $episodes->whereIn('episodes.event_id', $validatedData['event']);
67                 }
68                 if ($request->user() && $request->user()->isPrivileged()) {
69                         $episodes = $episodes->with(['crew', 'crew.user']);
70                 } else {
71                         $episodes = $episodes->with([
72                                 'crew' => function ($query) {
73                                         $query->where('confirmed', true);
74                                 },
75                                 'crew.user',
76                         ]);
77                 }
78                 return $episodes->get()->toJson();
79         }
80
81         public function single(Request $request, Episode $episode) {
82                 $this->authorize('view', $episode);
83                 return $episode->toJson();
84         }
85
86 }