fix(BE): show crud
This commit is contained in:
parent
32460249f4
commit
6794991b3f
7 changed files with 163 additions and 38 deletions
|
@ -14,6 +14,7 @@ use App\Traits\Show\ShowInstancesTrait;
|
|||
use App\Traits\Show\ShowTrait;
|
||||
use Illuminate\Http\Request;
|
||||
use Exception;
|
||||
use Log;
|
||||
|
||||
# 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
|
||||
|
@ -46,8 +47,9 @@ class ShowController extends Controller
|
|||
$showDays = $showData['show_days'];
|
||||
$showDJs = $showData['show_djs'];
|
||||
$this->createShow($showData, $showDays, $showDJs);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'status' => 'success',
|
||||
'message' => 'Show saved successfully!'
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
|
@ -55,16 +57,38 @@ class ShowController extends Controller
|
|||
}
|
||||
}
|
||||
|
||||
public function show(ShowResource $show)
|
||||
public function show(Request $request, $showId)
|
||||
{
|
||||
return new ShowResource($show);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public function update(ShowRequest $request, Show $show)
|
||||
public function update(Request $request, int $showId)
|
||||
{
|
||||
$show->update($request->validated());
|
||||
try {
|
||||
$showData = $request->all();
|
||||
$showDJs = $showData['show_djs'];
|
||||
$this->updateShow($showData, $showId, $showDJs);
|
||||
|
||||
return new ShowResource($show);
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Show saved successfully!'
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
|
||||
return response()->json(['error' => 'Failed to update show'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy(Request $request)
|
||||
|
|
|
@ -9,12 +9,16 @@ use App\Http\Resources\ShowDaysResource;
|
|||
use App\Models\Show\Show;
|
||||
use App\Models\Show\ShowDays;
|
||||
use App\Traits\Show\ShowDaysTrait;
|
||||
use App\Traits\Show\ShowInstancesTrait;
|
||||
use Illuminate\Http\ReqshowDaysFlatest;
|
||||
use Illuminate\Http\Request;
|
||||
use Log;
|
||||
|
||||
class ShowDaysController extends Controller
|
||||
{
|
||||
use ShowDaysTrait;
|
||||
use ShowInstancesTrait;
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$queryParams = collect($request->except('flattenShowDays'));
|
||||
|
@ -33,11 +37,21 @@ class ShowDaysController extends Controller
|
|||
return new ShowDaysResource($ShowDays);
|
||||
}
|
||||
|
||||
public function update(ShowDaysRequest $request, ShowDays $ShowDays)
|
||||
public function update(Request $request, $showId)
|
||||
{
|
||||
$ShowDays->update($request->validated());
|
||||
try {
|
||||
$showDaysRules = $request->all();
|
||||
$this->updateShowDays($showDaysRules, $showId);
|
||||
|
||||
return new ShowDaysResource($ShowDays);
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Show rules saved successfully!'
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
|
||||
return response()->json(['error' => 'Failed to update show days rules'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy(ShowDays $ShowDays)
|
||||
|
|
|
@ -7,16 +7,24 @@ use App\Http\Controllers\Controller;
|
|||
use App\Http\Requests\ShowInstancesRequest;
|
||||
use App\Http\Resources\ShowInstancesResource;
|
||||
use App\Models\ShowInstances\ShowInstances;
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
class ShowInstancesController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$queryParams = collect($request->except('withShow'));
|
||||
$queryParams = collect($request->except('withShow'));
|
||||
$showInstanceFilter = (new ShowInstances())->searchFilter($queryParams);
|
||||
if($request->withShow) $showInstanceFilter = $showInstanceFilter->with('show');
|
||||
return response()->json($showInstanceFilter->get());
|
||||
if ($request->withShow) {
|
||||
$showInstanceFilter = $showInstanceFilter->with('show')->with('show.showDjs');
|
||||
}
|
||||
$showInstances = $showInstanceFilter->get();
|
||||
return response()->json($showInstances->sortBy('starts')->values());
|
||||
}
|
||||
|
||||
public function store(ShowInstancesRequest $request)
|
||||
|
@ -24,22 +32,52 @@ class ShowInstancesController extends Controller
|
|||
return new ShowInstancesResource(ShowInstances::create($request->validated()));
|
||||
}
|
||||
|
||||
public function show(ShowInstances $showInstances)
|
||||
public function show(Request $request, $showInstanceId)
|
||||
{
|
||||
return new ShowInstancesResource($showInstances);
|
||||
try {
|
||||
$showInstance = ShowInstances::findOrFail($showInstanceId);
|
||||
if ($request->has('withShow')) {
|
||||
$showInstance->load('show');
|
||||
}
|
||||
return response()->json($showInstance);
|
||||
} catch (Exception $e) {
|
||||
return response()->json(['message' => 'Show instance not found ' . $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function update(ShowInstancesRequest $request, ShowInstances $showInstances)
|
||||
/**
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function update(Request $request)
|
||||
{
|
||||
$showInstances->update($request->validated());
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$showInstanceData = $request->all();
|
||||
$showInstance = ShowInstances::find($request->id);
|
||||
$showInstanceData['modified_instance'] = true;
|
||||
|
||||
return new ShowInstancesResource($showInstances);
|
||||
$showInstance->fill($showInstanceData);
|
||||
$showInstance->save();
|
||||
|
||||
DB::commit();
|
||||
return response()->json(['message' => 'Shows Instances updated 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());
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy(ShowInstances $showInstances)
|
||||
public function destroy(Request $request)
|
||||
{
|
||||
$showInstances->delete();
|
||||
|
||||
return response()->json();
|
||||
try {
|
||||
$showInstancesIds = $request->input('showInstancesIds');
|
||||
ShowInstances::destroy($showInstancesIds);
|
||||
return response()->json(['message' => 'Shows Instances deleted']);
|
||||
} catch (Exception $e) {
|
||||
return response()->json(['message' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,9 +13,13 @@ class ShowInstances extends Model
|
|||
{
|
||||
protected $table = 'cc_show_instances';
|
||||
|
||||
protected $dateFormat = 'c';
|
||||
protected $dates = ['starts', 'ends'];
|
||||
|
||||
protected $fillable = [
|
||||
'starts',
|
||||
'ends',
|
||||
'description',
|
||||
'show_id',
|
||||
'record',
|
||||
'rebroadcast',
|
||||
|
@ -28,6 +32,8 @@ class ShowInstances extends Model
|
|||
protected $casts = [
|
||||
'created' => 'timestamp',
|
||||
'last_scheduled' => 'timestamp',
|
||||
'starts' => 'datetime',
|
||||
'ends' => 'datetime',
|
||||
'modified_instance' => 'boolean',
|
||||
'autoplaylist_built' => 'boolean',
|
||||
];
|
||||
|
|
|
@ -6,7 +6,12 @@ use App\Enums\ShowRepeatType;
|
|||
use App\Helpers\CarbonShowRepetition;
|
||||
use App\Helpers\Preferences;
|
||||
use App\Lib\RabbitMQSender;
|
||||
use App\Models\Show\Show;
|
||||
use App\Models\Show\ShowDays;
|
||||
use DB;
|
||||
use Exception;
|
||||
use Illuminate\Support\Collection;
|
||||
use Throwable;
|
||||
|
||||
trait ShowDaysTrait
|
||||
{
|
||||
|
@ -49,10 +54,31 @@ trait ShowDaysTrait
|
|||
$baseProps = $showDays->first()->toArray();
|
||||
$earliestDate = $showDays->min('first_show');
|
||||
$daysArray = $showDays->pluck('day')->toArray();
|
||||
|
||||
$repeatType = ShowRepeatType::fromValue($baseProps['repeat_type'])->name;
|
||||
return array_merge($baseProps, [
|
||||
'first_show' => $earliestDate,
|
||||
'day' => $daysArray,
|
||||
'repeat_type' => $repeatType,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function updateShowDays(array $showDaysRules, int $showId): void
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$show = Show::with('ShowDays')->findOrFail($showId);
|
||||
$show->showDays()->delete();
|
||||
$this->createShowDays($show, $showDaysRules);
|
||||
$this->manageShowInstances($show);
|
||||
DB::commit();
|
||||
} catch (Exception $e) {
|
||||
DB::rollBack();
|
||||
throw new Exception($e->getMessage());
|
||||
} catch (Throwable $e) {
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -23,25 +23,19 @@ trait ShowDjTrait
|
|||
}
|
||||
|
||||
try {
|
||||
// Fetch current DJs for the show
|
||||
$currentDjs = $show->showDjs()->pluck('subjs_id')->toArray();
|
||||
|
||||
// Compute the difference
|
||||
$djsToAdd = array_diff($showDjs, $currentDjs);
|
||||
$djsToRemove = array_diff($currentDjs, $showDjs);
|
||||
|
||||
// Remove DJs not in the list
|
||||
foreach ($djsToRemove as $djId) {
|
||||
$show->showDjs()->where('subjs_id', $djId)->delete();
|
||||
}
|
||||
|
||||
// Add new DJs
|
||||
foreach ($djsToAdd as $djId) {
|
||||
$show->showDjs()->make(['subjs_id' => $djId]);
|
||||
if($djsToAdd || $djsToRemove){
|
||||
$show->showDjs()->whereIn('subjs_id', $djsToRemove)->delete();
|
||||
foreach ($djsToAdd as $djId) {
|
||||
$show->showDjs()->create(['subjs_id' => $djId]);
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -2,10 +2,13 @@
|
|||
|
||||
namespace App\Traits\Show;
|
||||
|
||||
use App\Http\Resources\ShowResource;
|
||||
use App\Lib\RabbitMQSender;
|
||||
use App\Models\Show\Show;
|
||||
use App\Models\Show\ShowHosts;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Throwable;
|
||||
|
||||
trait ShowTrait
|
||||
{
|
||||
|
@ -15,7 +18,7 @@ trait ShowTrait
|
|||
* @param ShowHosts[] $showDJs
|
||||
*
|
||||
* @return string|void
|
||||
* @throws \Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public function createShow(array $showData, array $showDays, array|null $showDJs)
|
||||
{
|
||||
|
@ -30,11 +33,31 @@ trait ShowTrait
|
|||
$show->save();
|
||||
DB::commit();
|
||||
RabbitMQSender::SendMessageToPypo('update_schedule', []);
|
||||
} catch (\Exception $e) {
|
||||
} catch (Exception $e) {
|
||||
DB::rollBack();
|
||||
throw new \Exception($e->getMessage());
|
||||
} catch (\Throwable $e) {
|
||||
throw new \Exception($e->getMessage());
|
||||
throw new Exception($e->getMessage());
|
||||
} catch (Throwable $e) {
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function updateShow(array $showData, int $showId, array|null $showDJs){
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$show = Show::findOrFail($showData['id']);
|
||||
$show->update($showData);
|
||||
if (isset($showDJs)) {
|
||||
$this->manageShowDjs($show, $showDJs);
|
||||
}
|
||||
DB::commit();
|
||||
} catch (Exception $e) {
|
||||
DB::rollBack();
|
||||
throw new Exception($e->getMessage());
|
||||
} catch (Throwable $e) {
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue