X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FHttp%2FControllers%2FEpisodeController.php;h=1a407a184e97039738e4b3743f888fec05a3f924;hb=638802eaf20d636c16d7ce337ace508708705f2c;hp=4e4eeda8dd943cdc8195dc451e9690bae24bca79;hpb=071885a30f24b980699b337d9cdb65952f8c6c42;p=alttp.git diff --git a/app/Http/Controllers/EpisodeController.php b/app/Http/Controllers/EpisodeController.php index 4e4eeda..1a407a1 100644 --- a/app/Http/Controllers/EpisodeController.php +++ b/app/Http/Controllers/EpisodeController.php @@ -2,16 +2,79 @@ namespace App\Http\Controllers; +use App\Models\Channel; use App\Models\Episode; +use Carbon\Carbon; use Illuminate\Http\Request; class EpisodeController extends Controller { + public function addRestream(Request $request, Episode $episode) { + $this->authorize('addRestream', $episode); + $validatedData = $request->validate([ + 'channel_id' => 'numeric|exists:App\Models\Channel,id', + ]); + + $channel = Channel::find($validatedData['channel_id']); + $this->authorize('addEpisode', $channel); + + foreach ($episode->channels as $c) { + if ($c->id == $channel->id) { + throw new \Exception('channel already exists on episode'); + } + } + + $episode->channels()->attach($channel); + + return $episode->load('channels')->toJson(); + } + + public function removeRestream(Request $request, Episode $episode) { + $this->authorize('removeRestream', $episode); + $validatedData = $request->validate([ + 'channel_id' => 'numeric|exists:App\Models\Channel,id', + ]); + + $channel = Channel::find($validatedData['channel_id']); + $this->authorize('removeEpisode', $channel); + + $episode->channels()->detach($channel); + + return $episode->load('channels')->toJson(); + } + public function search(Request $request) { - $episodes = Episode::with('event') - ->where('confirmed', '=', true) - ->where('event.visible', '=', true); + $validatedData = $request->validate([ + 'after' => 'nullable|date', + 'before' => 'nullable|date', + 'event' => 'nullable|array', + 'event.*' => 'numeric', + ]); + $after = isset($validatedData['after']) ? $validatedData['after'] : Carbon::now()->sub(2, 'hours'); + $before = isset($validatedData['before']) ? $validatedData['before'] : Carbon::now()->add(1, 'days'); + $episodes = Episode::with(['channels', 'event', 'players', 'players.user']) + ->select('episodes.*') + ->join('events', 'episodes.event_id', '=', 'events.id') + ->where('episodes.confirmed', '=', true) + ->where('episodes.start', '>=', $after) + ->where('episodes.start', '<=', $before) + ->where('events.visible', '=', true) + ->orderBy('episodes.start') + ->limit(1000); + if (!empty($validatedData['event'])) { + $episodes = $episodes->whereIn('episodes.event_id', $validatedData['event']); + } + if ($request->user() && $request->user()->isPrivileged()) { + $episodes = $episodes->with(['crew', 'crew.user']); + } else { + $episodes = $episodes->with([ + 'crew' => function ($query) { + $query->where('confirmed', true); + }, + 'crew.user', + ]); + } return $episodes->get()->toJson(); }