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 App\Traits\Show\ShowTrait;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Log;
|
||||||
|
|
||||||
# TODO Expose the show instance generation for user interaction and pypo queue
|
# 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
|
# When pypo requests the schedule up to a certain date, generate the shows up to that date
|
||||||
|
@ -46,6 +47,7 @@ class ShowController extends Controller
|
||||||
$showDays = $showData['show_days'];
|
$showDays = $showData['show_days'];
|
||||||
$showDJs = $showData['show_djs'];
|
$showDJs = $showData['show_djs'];
|
||||||
$this->createShow($showData, $showDays, $showDJs);
|
$this->createShow($showData, $showDays, $showDJs);
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status' => 'success',
|
'status' => 'success',
|
||||||
'message' => 'Show saved successfully!'
|
'message' => 'Show saved successfully!'
|
||||||
|
@ -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)
|
public function destroy(Request $request)
|
||||||
|
|
|
@ -9,12 +9,16 @@ use App\Http\Resources\ShowDaysResource;
|
||||||
use App\Models\Show\Show;
|
use App\Models\Show\Show;
|
||||||
use App\Models\Show\ShowDays;
|
use App\Models\Show\ShowDays;
|
||||||
use App\Traits\Show\ShowDaysTrait;
|
use App\Traits\Show\ShowDaysTrait;
|
||||||
|
use App\Traits\Show\ShowInstancesTrait;
|
||||||
use Illuminate\Http\ReqshowDaysFlatest;
|
use Illuminate\Http\ReqshowDaysFlatest;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Log;
|
||||||
|
|
||||||
class ShowDaysController extends Controller
|
class ShowDaysController extends Controller
|
||||||
{
|
{
|
||||||
use ShowDaysTrait;
|
use ShowDaysTrait;
|
||||||
|
use ShowInstancesTrait;
|
||||||
|
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$queryParams = collect($request->except('flattenShowDays'));
|
$queryParams = collect($request->except('flattenShowDays'));
|
||||||
|
@ -33,11 +37,21 @@ class ShowDaysController extends Controller
|
||||||
return new ShowDaysResource($ShowDays);
|
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)
|
public function destroy(ShowDays $ShowDays)
|
||||||
|
|
|
@ -7,7 +7,12 @@ use App\Http\Controllers\Controller;
|
||||||
use App\Http\Requests\ShowInstancesRequest;
|
use App\Http\Requests\ShowInstancesRequest;
|
||||||
use App\Http\Resources\ShowInstancesResource;
|
use App\Http\Resources\ShowInstancesResource;
|
||||||
use App\Models\ShowInstances\ShowInstances;
|
use App\Models\ShowInstances\ShowInstances;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Exception;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
class ShowInstancesController extends Controller
|
class ShowInstancesController extends Controller
|
||||||
{
|
{
|
||||||
|
@ -15,8 +20,11 @@ class ShowInstancesController extends Controller
|
||||||
{
|
{
|
||||||
$queryParams = collect($request->except('withShow'));
|
$queryParams = collect($request->except('withShow'));
|
||||||
$showInstanceFilter = (new ShowInstances())->searchFilter($queryParams);
|
$showInstanceFilter = (new ShowInstances())->searchFilter($queryParams);
|
||||||
if($request->withShow) $showInstanceFilter = $showInstanceFilter->with('show');
|
if ($request->withShow) {
|
||||||
return response()->json($showInstanceFilter->get());
|
$showInstanceFilter = $showInstanceFilter->with('show')->with('show.showDjs');
|
||||||
|
}
|
||||||
|
$showInstances = $showInstanceFilter->get();
|
||||||
|
return response()->json($showInstances->sortBy('starts')->values());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function store(ShowInstancesRequest $request)
|
public function store(ShowInstancesRequest $request)
|
||||||
|
@ -24,22 +32,52 @@ class ShowInstancesController extends Controller
|
||||||
return new ShowInstancesResource(ShowInstances::create($request->validated()));
|
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();
|
try {
|
||||||
|
$showInstancesIds = $request->input('showInstancesIds');
|
||||||
return response()->json();
|
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 $table = 'cc_show_instances';
|
||||||
|
|
||||||
|
protected $dateFormat = 'c';
|
||||||
|
protected $dates = ['starts', 'ends'];
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'starts',
|
'starts',
|
||||||
'ends',
|
'ends',
|
||||||
|
'description',
|
||||||
'show_id',
|
'show_id',
|
||||||
'record',
|
'record',
|
||||||
'rebroadcast',
|
'rebroadcast',
|
||||||
|
@ -28,6 +32,8 @@ class ShowInstances extends Model
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'created' => 'timestamp',
|
'created' => 'timestamp',
|
||||||
'last_scheduled' => 'timestamp',
|
'last_scheduled' => 'timestamp',
|
||||||
|
'starts' => 'datetime',
|
||||||
|
'ends' => 'datetime',
|
||||||
'modified_instance' => 'boolean',
|
'modified_instance' => 'boolean',
|
||||||
'autoplaylist_built' => 'boolean',
|
'autoplaylist_built' => 'boolean',
|
||||||
];
|
];
|
||||||
|
|
|
@ -6,7 +6,12 @@ use App\Enums\ShowRepeatType;
|
||||||
use App\Helpers\CarbonShowRepetition;
|
use App\Helpers\CarbonShowRepetition;
|
||||||
use App\Helpers\Preferences;
|
use App\Helpers\Preferences;
|
||||||
use App\Lib\RabbitMQSender;
|
use App\Lib\RabbitMQSender;
|
||||||
|
use App\Models\Show\Show;
|
||||||
|
use App\Models\Show\ShowDays;
|
||||||
|
use DB;
|
||||||
|
use Exception;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
trait ShowDaysTrait
|
trait ShowDaysTrait
|
||||||
{
|
{
|
||||||
|
@ -49,10 +54,31 @@ trait ShowDaysTrait
|
||||||
$baseProps = $showDays->first()->toArray();
|
$baseProps = $showDays->first()->toArray();
|
||||||
$earliestDate = $showDays->min('first_show');
|
$earliestDate = $showDays->min('first_show');
|
||||||
$daysArray = $showDays->pluck('day')->toArray();
|
$daysArray = $showDays->pluck('day')->toArray();
|
||||||
|
$repeatType = ShowRepeatType::fromValue($baseProps['repeat_type'])->name;
|
||||||
return array_merge($baseProps, [
|
return array_merge($baseProps, [
|
||||||
'first_show' => $earliestDate,
|
'first_show' => $earliestDate,
|
||||||
'day' => $daysArray,
|
'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 {
|
try {
|
||||||
// Fetch current DJs for the show
|
|
||||||
$currentDjs = $show->showDjs()->pluck('subjs_id')->toArray();
|
$currentDjs = $show->showDjs()->pluck('subjs_id')->toArray();
|
||||||
|
|
||||||
// Compute the difference
|
|
||||||
$djsToAdd = array_diff($showDjs, $currentDjs);
|
$djsToAdd = array_diff($showDjs, $currentDjs);
|
||||||
$djsToRemove = array_diff($currentDjs, $showDjs);
|
$djsToRemove = array_diff($currentDjs, $showDjs);
|
||||||
|
|
||||||
// Remove DJs not in the list
|
if($djsToAdd || $djsToRemove){
|
||||||
foreach ($djsToRemove as $djId) {
|
$show->showDjs()->whereIn('subjs_id', $djsToRemove)->delete();
|
||||||
$show->showDjs()->where('subjs_id', $djId)->delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add new DJs
|
|
||||||
foreach ($djsToAdd as $djId) {
|
foreach ($djsToAdd as $djId) {
|
||||||
$show->showDjs()->make(['subjs_id' => $djId]);
|
$show->showDjs()->create(['subjs_id' => $djId]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return $e->getMessage();
|
return $e->getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,10 +2,13 @@
|
||||||
|
|
||||||
namespace App\Traits\Show;
|
namespace App\Traits\Show;
|
||||||
|
|
||||||
|
use App\Http\Resources\ShowResource;
|
||||||
use App\Lib\RabbitMQSender;
|
use App\Lib\RabbitMQSender;
|
||||||
use App\Models\Show\Show;
|
use App\Models\Show\Show;
|
||||||
use App\Models\Show\ShowHosts;
|
use App\Models\Show\ShowHosts;
|
||||||
|
use Exception;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
trait ShowTrait
|
trait ShowTrait
|
||||||
{
|
{
|
||||||
|
@ -15,7 +18,7 @@ trait ShowTrait
|
||||||
* @param ShowHosts[] $showDJs
|
* @param ShowHosts[] $showDJs
|
||||||
*
|
*
|
||||||
* @return string|void
|
* @return string|void
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function createShow(array $showData, array $showDays, array|null $showDJs)
|
public function createShow(array $showData, array $showDays, array|null $showDJs)
|
||||||
{
|
{
|
||||||
|
@ -30,11 +33,31 @@ trait ShowTrait
|
||||||
$show->save();
|
$show->save();
|
||||||
DB::commit();
|
DB::commit();
|
||||||
RabbitMQSender::SendMessageToPypo('update_schedule', []);
|
RabbitMQSender::SendMessageToPypo('update_schedule', []);
|
||||||
} catch (\Exception $e) {
|
} catch (Exception $e) {
|
||||||
DB::rollBack();
|
DB::rollBack();
|
||||||
throw new \Exception($e->getMessage());
|
throw new Exception($e->getMessage());
|
||||||
} catch (\Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
throw new \Exception($e->getMessage());
|
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