Skip to content

Commit

Permalink
Reverse travel
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-stanek committed Oct 25, 2023
1 parent bf3fb0e commit 0555ca2
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
15 changes: 15 additions & 0 deletions app/AccountancyModule/TravelModule/presenters/DefaultPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Assert\Assertion;
use Model\Services\PdfRenderer;
use Model\Travel\Commands\Command\DuplicateTravel;
use Model\Travel\Commands\Command\ReverseTravel;
use Model\Travel\Travel\TransportType;
use Model\TravelService;
use Model\UserService;
Expand Down Expand Up @@ -171,6 +172,20 @@ public function handleDuplicateTravel(int $commandId, int $travelId): void
$this->redirect('this');
}

public function handleReverseTravel(int $commandId, int $travelId): void
{
if (! $this->isCommandEditable($commandId)) {
$this->flashMessage('Nemáte oprávnění obrátit cestu.', 'danger');
$this->redirect('default');
}

$this->commandBus->handle(new ReverseTravel($commandId, $travelId));

$this->flashMessage('Cesta byla obrácena.', 'success');

$this->redirect('this');
}

public function handleOpenCommand(int $commandId): void
{
if (! $this->isCommandAccessible($commandId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<td n:if="$isEditable">
<div class="btn-group">
<a n:href="duplicateTravel! $command->id, $t->id" class="btn btn-sm btn-info" title="duplikovat cestu"><i class="far fa-copy"></i></a>
<a n:href="reverseTravel! $command->id, $t->id" class="btn btn-sm btn-info" title="obrátit cestu"><i class="far fa-exchange-alt"></i></a>
<a n:href="editTravel! $t->id" class="ajax btn btn-sm btn-primary"><i class="far fa-edit"></i></a>
<a n:href="removeTravel! $command->id, $t->id" class="btn btn-sm btn-danger" onclick="return confirm('Opravdu chcete smazat cestu?')" title="smazat cestu"><i class="far fa-trash-alt"></i></a>
</div>
Expand Down
18 changes: 17 additions & 1 deletion app/model/Travel/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,23 @@ public function duplicateTravel(int $id): void
} elseif ($travel instanceof TransportTravel) {
$this->addTransportTravel($travel->getPrice(), $travel->getDetails());
} else {
throw new ShouldNotHappen('Uknown travel type');
throw new ShouldNotHappen('Unknown travel type');
}
}

/** @throws TravelNotFound */
public function reverseTravel(int $id): void
{
$travel = $this->getTravel($id);
$details = $travel->getDetails();
$reversedDetails = new TravelDetails($details->getDate(), $details->getTransportType(), $details->getEndPlace(), $details->getStartPlace());

if ($travel instanceof VehicleTravel) {
$travel->update($travel->getDistance(), $reversedDetails);
} elseif ($travel instanceof TransportTravel) {
$travel->update($travel->getPrice(), $reversedDetails);
} else {
throw new ShouldNotHappen('Unknown travel type');
}
}

Expand Down
23 changes: 23 additions & 0 deletions app/model/Travel/Commands/Command/ReverseTravel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Model\Travel\Commands\Command;

/** @see ReverseTravelHandler */
final class ReverseTravel
{
public function __construct(private int $commandId, private int $travelId)
{
}

public function getCommandId(): int
{
return $this->commandId;
}

public function getTravelId(): int
{
return $this->travelId;
}
}
24 changes: 24 additions & 0 deletions app/model/Travel/Handlers/Command/ReverseTravelHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Model\Travel\Handlers\Command;

use Model\Travel\Commands\Command\ReverseTravel;
use Model\Travel\Repositories\ICommandRepository;

final class ReverseTravelHandler
{
public function __construct(private ICommandRepository $commands)
{
}

public function __invoke(ReverseTravel $command): void
{
$travelCommand = $this->commands->find($command->getCommandId());

$travelCommand->reverseTravel($command->getTravelId());

$this->commands->save($travelCommand);
}
}
1 change: 1 addition & 0 deletions frontend/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ library.add(
far.faTrashAlt,
far.faUser,
far.faQuestionCircle,
far.faExchangeAlt,

// Brands
fab.faGithub as IconDefinition,
Expand Down

0 comments on commit 0555ca2

Please sign in to comment.