-
-
Notifications
You must be signed in to change notification settings - Fork 655
Expand file tree
/
Copy pathUpdateEventAction.php
More file actions
58 lines (52 loc) · 1.92 KB
/
UpdateEventAction.php
File metadata and controls
58 lines (52 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace HiEvents\Http\Actions\Events;
use HiEvents\DomainObjects\EventDomainObject;
use HiEvents\Exceptions\CannotChangeCurrencyException;
use HiEvents\Exceptions\OrganizerNotFoundException;
use HiEvents\Http\Actions\BaseAction;
use HiEvents\Http\Request\Event\UpdateEventRequest;
use HiEvents\Resources\Event\EventResource;
use HiEvents\Services\Application\Handlers\Event\DTO\UpdateEventDTO;
use HiEvents\Services\Application\Handlers\Event\UpdateEventHandler;
use Illuminate\Http\JsonResponse;
use Illuminate\Validation\ValidationException;
use Throwable;
class UpdateEventAction extends BaseAction
{
public function __construct(
private readonly UpdateEventHandler $updateEventHandler
)
{
}
/**
* @throws Throwable|ValidationException
*/
public function __invoke(UpdateEventRequest $request, int $eventId): JsonResponse
{
$this->isActionAuthorized($eventId, EventDomainObject::class);
$authorisedUser = $this->getAuthenticatedUser();
try {
$event = $this->updateEventHandler->handle(
UpdateEventDTO::fromArray(
array_merge(
$request->validated(),
[
'id' => $eventId,
'account_id' => $this->getAuthenticatedAccountId(),
'user_id' => $authorisedUser->getId(),
]
)
)
);
} catch (CannotChangeCurrencyException $exception) {
throw ValidationException::withMessages([
'currency' => $exception->getMessage(),
]);
} catch (OrganizerNotFoundException $exception) {
throw ValidationException::withMessages([
'organizer_id' => $exception->getMessage(),
]);
}
return $this->resourceResponse(EventResource::class, $event);
}
}