sintonia_webapp/app/Http/Controllers/Show/ShowController.php
2025-07-04 11:40:13 +02:00

127 lines
3.7 KiB
PHP

<?php
namespace App\Http\Controllers\Show;
use App\Filters\Show\ShowFilters;
use App\Http\Controllers\Controller;
use App\Models\Show\Show;
use App\Traits\ScheduleTrait;
use App\Traits\Show\ShowDaysTrait;
use App\Traits\Show\ShowDjTrait;
use App\Traits\Show\ShowInstancesTrait;
use App\Traits\Show\ShowTrait;
use Illuminate\Http\Request;
use Exception;
use Illuminate\Support\Facades\DB;
use Log;
use Throwable;
# TODO Expose the show instance generation for user interaction and pypo queue
# When pypo requests the schedule up to a certain date, generate the shows up to that date
class ShowController extends Controller
{
use ShowTrait;
use ShowDaysTrait;
use ShowInstancesTrait;
use ShowDjTrait;
public function index(Request $request)
{
$pagination = 20;
if(isset($request->per_page) || is_null($request)) {
$pagination = $request->per_page;
}
$shows = Show::searchFilter($request);
if($request->has('showType')) {
$showType = $request->get('showType');
$shows = ($showType == 'show')
? $shows->doesntHave('spotShow')
: $shows->has('spotShow');
}
return $shows->orderBy('name')
->paginate($pagination)
->toJson();
}
/**
* @throws \Exception
*/
public function store(Request $request)
{
try {
$showData = $request->all();
$showDays = $showData['show_days'];
$showDJs = $showData['show_djs'];
$this->createShow($showData, $showDays, $showDJs);
return response()->json([
'status' => 'success',
'message' => 'Show saved successfully!'
]);
} catch (Exception $e) {
return response()->json(['message' => $e->getMessage()], 500);
}
}
public function show(Request $request, $showId)
{
try {
$show = Show::findOrFail($showId);
if ($request->has('withDjs')) {
$show->setAttribute(
'show_djs',
$show->showDjs()->pluck('subjs_id')->toArray()
);
}
return response()->json($show);
} catch (Exception $e) {
return response()->json(['message' => 'Show not found ' . $e->getMessage()], 500);
}
}
/**
* @throws Throwable
*/
public function update(Request $request, int $showId)
{
try {
DB::beginTransaction();
$showData = $request->all();
$showDJs = $showData['show_djs'];
$this->updateShow($showData, $showId, $showDJs);
DB::commit();
return response()->json([
'status' => 'success',
'message' => 'Show saved successfully!'
]);
} catch (\Exception $e) {
DB::rollBack();
Log::error($e->getMessage());
return response()->json(['error' => 'Failed to update show'], 500);
} catch (Throwable $e) {
throw new Exception($e->getMessage());
}
}
/**
* @throws Throwable
*/
public function destroy(Request $request, int $showId)
{
try {
DB::beginTransaction();
Show::destroy($showId);
DB::commit();
return response()->json(['message' => 'Shows deleted']);
} catch (Exception $e) {
DB::rollBack();
Log::error($e->getMessage());
return response()->json(['error' => 'Show not deleted'], 500);
} catch (Throwable $e) {
throw new Exception($e->getMessage());
}
}
}