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;
11 use Illuminate\Support\Facades\DB;
13 class EpisodeController extends Controller
16 public function addRestream(Request $request, Episode $episode) {
17 $this->authorize('addRestream', $episode);
18 $validatedData = $request->validate([
19 'accept_comms' => 'boolean',
20 'accept_tracker' => 'boolean',
21 'channel_id' => 'numeric|exists:App\Models\Channel,id',
24 $channel = Channel::find($validatedData['channel_id']);
25 $this->authorize('addEpisode', $channel);
27 foreach ($episode->channels as $c) {
28 if ($c->id == $channel->id) {
29 throw new \Exception('channel already exists on episode');
33 $validatedProps = $request->validate([
34 'accept_comms' => 'boolean',
35 'accept_tracker' => 'boolean',
38 $episode->channels()->attach($channel, $validatedProps);
40 return $episode->load('channels')->toJson();
43 public function crewManage(Request $request, Episode $episode) {
44 $this->authorize('editRestream', $episode);
45 $validatedData = $request->validate([
46 'add' => 'numeric|exists:App\Models\User,id',
47 'channel_id' => 'numeric|exists:App\Models\Channel,id',
48 'confirm' => 'nullable|numeric|exists:App\Models\EpisodeCrew,id',
49 'remove' => 'numeric|exists:App\Models\EpisodeCrew,id',
50 'role' => 'string|in:commentary,setup,tracking',
51 'unconfirm' => 'nullable|numeric|exists:App\Models\EpisodeCrew,id',
54 $channel = Channel::find($validatedData['channel_id']);
55 $this->authorize('editRestream', $channel);
57 if (isset($validatedData['add'])) {
58 $crew = $episode->crew()
59 ->where('user_id', '=', $validatedData['add'])
60 ->where('role', '=', $validatedData['role'])
63 $add_user = User::findOrFail($validatedData['add']);
64 $crew = new EpisodeCrew();
65 $crew->channel()->associate($channel);
66 $crew->episode()->associate($episode);
67 $crew->user()->associate($add_user);
68 $crew->role = $validatedData['role'];
70 $crew->confirmed = true;
74 if (isset($validatedData['confirm'])) {
75 $crew = EpisodeCrew::find($validatedData['confirm']);
76 $crew->confirmed = true;
80 if (isset($validatedData['remove'])) {
81 $crew = EpisodeCrew::find($validatedData['remove']);
87 if (isset($validatedData['unconfirm'])) {
88 $crew = EpisodeCrew::find($validatedData['unconfirm']);
89 $crew->confirmed = false;
93 $user = $request->user();
94 if ($user->isPrivileged()) {
95 return $episode->load(['crew', 'crew.user'])->toJson();
97 return $episode->load([
98 'crew' => function ($query) use ($user) {
99 $query->where('confirmed', true);
100 $query->orWhere('user_id', '=', $user->id);
101 $query->orWhereIn('channel_id', $user->channel_crews->pluck('channel_id'));
108 public function crewSignup(Request $request, Episode $episode) {
109 if (!$request->user()) {
110 throw new \Exception('requires user to sign up');
112 $validatedData = $request->validate([
113 'as' => 'string|in:commentary,tracking',
114 'channel_id' => 'numeric|exists:App\Models\Channel,id',
117 $channel = $episode->channels()->find($validatedData['channel_id']);
119 throw new \Exception('channel not found');
122 $as = $validatedData['as'];
123 if ($as == 'commentary' && !$channel->pivot->accept_comms) {
124 throw new \Exception('channel not looking for commentary');
126 if ($as == 'tracking' && !$channel->pivot->accept_tracker) {
127 throw new \Exception('channel not looking for trackers');
130 $user = $request->user();
132 foreach ($episode->crew as $crew) {
133 if ($crew->user_id == $user->id && $crew->role == $as) {
134 throw new \Exception('already signed up');
138 $episode->crew()->create([
139 'channel_id' => $channel->id,
141 'user_id' => $user->id,
144 if ($user->isPrivileged()) {
145 return $episode->load(['crew', 'crew.user'])->toJson();
147 return $episode->load([
148 'crew' => function ($query) use ($user) {
149 $query->where('confirmed', true);
150 $query->orWhere('user_id', '=', $user->id);
151 $query->orWhereIn('channel_id', $user->channel_crews->pluck('channel_id'));
158 public function editRestream(Request $request, Episode $episode) {
159 $this->authorize('editRestream', $episode);
160 $validatedData = $request->validate([
161 'channel_id' => 'numeric|exists:App\Models\Channel,id',
164 $channel = Channel::find($validatedData['channel_id']);
165 $this->authorize('editRestream', $channel);
167 $validatedChanges = $request->validate([
168 'accept_comms' => 'boolean',
169 'accept_tracker' => 'boolean',
172 $episode->channels()->updateExistingPivot($channel, $validatedChanges);
174 return $episode->load('channels')->toJson();
177 public function removeRestream(Request $request, Episode $episode) {
178 $this->authorize('removeRestream', $episode);
179 $validatedData = $request->validate([
180 'channel_id' => 'numeric|exists:App\Models\Channel,id',
183 $channel = Channel::find($validatedData['channel_id']);
184 $this->authorize('removeEpisode', $channel);
186 $episode->channels()->detach($channel);
188 return $episode->load('channels')->toJson();
191 public function search(Request $request) {
192 $validatedData = $request->validate([
193 'after' => 'nullable|date',
194 'before' => 'nullable|date',
195 'event' => 'nullable|array',
196 'event.*' => 'numeric',
198 $after = isset($validatedData['after']) ? $validatedData['after'] : Carbon::now();
199 $before = isset($validatedData['before']) ? $validatedData['before'] : Carbon::now()->add(1, 'days');
200 $episodes = Episode::with(['channels', 'event', 'players', 'players.user'])
201 ->select('episodes.*')
202 ->join('events', 'episodes.event_id', '=', 'events.id')
203 ->where('episodes.confirmed', '=', true)
204 ->where(DB::raw('DATE_ADD(`episodes`.`start`, INTERVAL COALESCE(`episodes`.`estimate`, 0) SECOND)'), '>=', $after)
205 ->where('episodes.start', '<=', $before)
206 ->where('events.visible', '=', true)
207 ->orderBy('episodes.start')
209 if (!empty($validatedData['event'])) {
210 $episodes = $episodes->whereIn('episodes.event_id', $validatedData['event']);
212 if ($request->user() && $request->user()->isPrivileged()) {
213 $episodes = $episodes->with(['crew', 'crew.user']);
214 } else if ($request->user()) {
215 $episodes = $episodes->with([
216 'crew' => function ($query) use ($request) {
217 $query->where('confirmed', true);
218 $query->orWhere('user_id', '=', $request->user()->id);
219 $query->orWhereIn('channel_id', $request->user()->channel_crews->pluck('channel_id'));
224 $episodes = $episodes->with([
225 'crew' => function ($query) {
226 $query->where('confirmed', true);
231 return $episodes->get()->toJson();
234 public function single(Request $request, Episode $episode) {
235 $this->authorize('view', $episode);
236 return $episode->toJson();