3 namespace App\Http\Controllers;
5 use App\Models\Channel;
6 use App\Models\Episode;
7 use App\Models\EpisodeCrew;
10 use Illuminate\Http\Request;
12 class EpisodeController extends Controller
15 public function addRestream(Request $request, Episode $episode) {
16 $this->authorize('addRestream', $episode);
17 $validatedData = $request->validate([
18 'accept_comms' => 'boolean',
19 'accept_tracker' => 'boolean',
20 'channel_id' => 'numeric|exists:App\Models\Channel,id',
23 $channel = Channel::find($validatedData['channel_id']);
24 $this->authorize('addEpisode', $channel);
26 foreach ($episode->channels as $c) {
27 if ($c->id == $channel->id) {
28 throw new \Exception('channel already exists on episode');
32 $validatedProps = $request->validate([
33 'accept_comms' => 'boolean',
34 'accept_tracker' => 'boolean',
37 $episode->channels()->attach($channel, $validatedProps);
39 return $episode->load('channels')->toJson();
42 public function crewManage(Request $request, Episode $episode) {
43 $this->authorize('editRestream', $episode);
44 $validatedData = $request->validate([
45 'add' => 'numeric|exists:App\Models\User,id',
46 'channel_id' => 'numeric|exists:App\Models\Channel,id',
47 'confirm' => 'nullable|numeric|exists:App\Models\EpisodeCrew,id',
48 'remove' => 'numeric|exists:App\Models\EpisodeCrew,id',
49 'role' => 'string|in:commentary,setup,tracking',
50 'unconfirm' => 'nullable|numeric|exists:App\Models\EpisodeCrew,id',
53 $channel = Channel::find($validatedData['channel_id']);
54 $this->authorize('editRestream', $channel);
56 if (isset($validatedData['add'])) {
57 $crew = $episode->crew()
58 ->where('user_id', '=', $validatedData['add'])
59 ->where('role', '=', $validatedData['role'])
62 $add_user = User::findOrFail($validatedData['add']);
63 $crew = new EpisodeCrew();
64 $crew->channel()->associate($channel);
65 $crew->episode()->associate($episode);
66 $crew->user()->associate($add_user);
67 $crew->role = $validatedData['role'];
69 $crew->confirmed = true;
73 if (isset($validatedData['confirm'])) {
74 $crew = EpisodeCrew::find($validatedData['confirm']);
75 $crew->confirmed = true;
79 if (isset($validatedData['remove'])) {
80 $crew = EpisodeCrew::find($validatedData['remove']);
86 if (isset($validatedData['unconfirm'])) {
87 $crew = EpisodeCrew::find($validatedData['unconfirm']);
88 $crew->confirmed = false;
92 $user = $request->user();
93 if ($user->isPrivileged()) {
94 return $episode->load(['crew', 'crew.user'])->toJson();
96 return $episode->load([
97 'crew' => function ($query) use ($user) {
98 $query->where('confirmed', true);
99 $query->orWhere('user_id', '=', $user->id);
100 $query->orWhereIn('channel_id', $user->channel_crews->pluck('channel_id'));
107 public function crewSignup(Request $request, Episode $episode) {
108 if (!$request->user()) {
109 throw new \Exception('requires user to sign up');
111 $validatedData = $request->validate([
112 'as' => 'string|in:commentary,tracking',
113 'channel_id' => 'numeric|exists:App\Models\Channel,id',
116 $channel = $episode->channels()->find($validatedData['channel_id']);
118 throw new \Exception('channel not found');
121 $as = $validatedData['as'];
122 if ($as == 'commentary' && !$channel->pivot->accept_comms) {
123 throw new \Exception('channel not looking for commentary');
125 if ($as == 'tracking' && !$channel->pivot->accept_tracker) {
126 throw new \Exception('channel not looking for trackers');
129 $user = $request->user();
131 foreach ($episode->crew as $crew) {
132 if ($crew->user_id == $user->id && $crew->role == $as) {
133 throw new \Exception('already signed up');
137 $episode->crew()->create([
138 'channel_id' => $channel->id,
140 'user_id' => $user->id,
143 if ($user->isPrivileged()) {
144 return $episode->load(['crew', 'crew.user'])->toJson();
146 return $episode->load([
147 'crew' => function ($query) use ($user) {
148 $query->where('confirmed', true);
149 $query->orWhere('user_id', '=', $user->id);
150 $query->orWhereIn('channel_id', $user->channel_crews->pluck('channel_id'));
157 public function editRestream(Request $request, Episode $episode) {
158 $this->authorize('editRestream', $episode);
159 $validatedData = $request->validate([
160 'channel_id' => 'numeric|exists:App\Models\Channel,id',
163 $channel = Channel::find($validatedData['channel_id']);
164 $this->authorize('editRestream', $channel);
166 $validatedChanges = $request->validate([
167 'accept_comms' => 'boolean',
168 'accept_tracker' => 'boolean',
171 $episode->channels()->updateExistingPivot($channel, $validatedChanges);
173 return $episode->load('channels')->toJson();
176 public function removeRestream(Request $request, Episode $episode) {
177 $this->authorize('removeRestream', $episode);
178 $validatedData = $request->validate([
179 'channel_id' => 'numeric|exists:App\Models\Channel,id',
182 $channel = Channel::find($validatedData['channel_id']);
183 $this->authorize('removeEpisode', $channel);
185 $episode->channels()->detach($channel);
187 return $episode->load('channels')->toJson();
190 public function search(Request $request) {
191 $validatedData = $request->validate([
192 'after' => 'nullable|date',
193 'before' => 'nullable|date',
194 'event' => 'nullable|array',
195 'event.*' => 'numeric',
197 $after = isset($validatedData['after']) ? $validatedData['after'] : Carbon::now()->sub(2, 'hours');
198 $before = isset($validatedData['before']) ? $validatedData['before'] : Carbon::now()->add(1, 'days');
199 $episodes = Episode::with(['channels', 'event', 'players', 'players.user'])
200 ->select('episodes.*')
201 ->join('events', 'episodes.event_id', '=', 'events.id')
202 ->where('episodes.confirmed', '=', true)
203 ->where('episodes.start', '>=', $after)
204 ->where('episodes.start', '<=', $before)
205 ->where('events.visible', '=', true)
206 ->orderBy('episodes.start')
208 if (!empty($validatedData['event'])) {
209 $episodes = $episodes->whereIn('episodes.event_id', $validatedData['event']);
211 if ($request->user() && $request->user()->isPrivileged()) {
212 $episodes = $episodes->with(['crew', 'crew.user']);
213 } else if ($request->user()) {
214 $episodes = $episodes->with([
215 'crew' => function ($query) use ($request) {
216 $query->where('confirmed', true);
217 $query->orWhere('user_id', '=', $request->user()->id);
218 $query->orWhereIn('channel_id', $request->user()->channel_crews->pluck('channel_id'));
223 $episodes = $episodes->with([
224 'crew' => function ($query) {
225 $query->where('confirmed', true);
230 return $episodes->get()->toJson();
233 public function single(Request $request, Episode $episode) {
234 $this->authorize('view', $episode);
235 return $episode->toJson();