mandant-crm/backend/app/Modules/History/Controllers/HistoryController.php

56 lines
2 KiB
PHP
Raw Normal View History

<?php
namespace App\Modules\History\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Client\Models\Client;
use App\Modules\History\Actions\CreateHistoryEntryAction;
use App\Modules\History\Actions\UpdateHistoryEntryAction;
use App\Modules\History\DTOs\HistoryEntryData;
use App\Modules\History\Models\HistoryEntry;
use App\Modules\History\Requests\CreateHistoryEntryRequest;
use App\Modules\History\Requests\UpdateHistoryEntryRequest;
use App\Modules\History\Resources\HistoryEntryResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
class HistoryController extends Controller
{
public function index(Client $client): AnonymousResourceCollection
{
$this->authorize('viewAny', HistoryEntry::class);
$entries = $client->historyEntries()
->with(['author', 'updatedBy'])
->orderByDesc('created_at')
->get();
return HistoryEntryResource::collection($entries);
}
public function store(
CreateHistoryEntryRequest $request,
Client $client,
CreateHistoryEntryAction $action
): JsonResponse {
$this->authorize('create', HistoryEntry::class);
$entry = $action->handle($client, HistoryEntryData::fromCreateRequest($request), $request->user());
return HistoryEntryResource::make($entry->load(['author']))->response()->setStatusCode(201);
}
public function update(
UpdateHistoryEntryRequest $request,
HistoryEntry $historyEntry,
UpdateHistoryEntryAction $action
): HistoryEntryResource {
$this->authorize('update', $historyEntry);
$updated = $action->handle($historyEntry, HistoryEntryData::fromUpdateRequest($request), $request->user());
return HistoryEntryResource::make($updated->load(['author', 'updatedBy']));
}
public function destroy(HistoryEntry $historyEntry): JsonResponse
{
$this->authorize('delete', $historyEntry);
$historyEntry->delete();
return response()->json(null, 204);
}
}