69 lines
2 KiB
PHP
69 lines
2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\StoreStreamRequest;
|
|
use App\Http\Requests\UpdateStreamRequest;
|
|
use App\Http\Resources\StreamResource;
|
|
use App\Models\Scenario;
|
|
use App\Models\Stream;
|
|
use App\Repositories\StreamRepository;
|
|
use App\Services\Streams\StatsService;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
class StreamController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly StreamRepository $streamRepository,
|
|
private readonly StatsService $statsService
|
|
) {}
|
|
|
|
public function index(Scenario $scenario): JsonResponse
|
|
{
|
|
$streams = $this->streamRepository->getForScenario($scenario);
|
|
|
|
return response()->json([
|
|
'streams' => StreamResource::collection($streams),
|
|
'stats' => $this->statsService->getSummaryStats($scenario),
|
|
]);
|
|
}
|
|
|
|
public function store(StoreStreamRequest $request, Scenario $scenario): JsonResponse
|
|
{
|
|
$stream = $this->streamRepository->create($scenario, $request->validated());
|
|
|
|
return response()->json([
|
|
'stream' => new StreamResource($stream),
|
|
'message' => 'Stream created successfully.',
|
|
], 201);
|
|
}
|
|
|
|
public function update(UpdateStreamRequest $request, Stream $stream): JsonResponse
|
|
{
|
|
$stream = $this->streamRepository->update($stream, $request->validated());
|
|
|
|
return response()->json([
|
|
'stream' => new StreamResource($stream),
|
|
'message' => 'Stream updated successfully.',
|
|
]);
|
|
}
|
|
|
|
public function destroy(Stream $stream): JsonResponse
|
|
{
|
|
$this->streamRepository->delete($stream);
|
|
|
|
return response()->json([
|
|
'message' => 'Stream deleted successfully.',
|
|
]);
|
|
}
|
|
|
|
public function toggle(Stream $stream): JsonResponse
|
|
{
|
|
$stream = $this->streamRepository->toggleActive($stream);
|
|
|
|
return response()->json([
|
|
'stream' => new StreamResource($stream),
|
|
'message' => $stream->is_active ? 'Stream activated.' : 'Stream deactivated.',
|
|
]);
|
|
}
|
|
}
|