Skip to content

Commit

Permalink
Mazání skupin a družin (#941)
Browse files Browse the repository at this point in the history
* remove groups

* format
  • Loading branch information
jan-stanek committed Feb 14, 2023
1 parent eb2e9ac commit 9a2eabe
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 36 deletions.
40 changes: 29 additions & 11 deletions app/AdminModule/UsersModule/Components/PatrolsGridControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace App\AdminModule\UsersModule\Components;

use App\Model\User\Commands\RemovePatrol;
use App\Model\User\Patrol;
use App\Model\User\Repositories\PatrolRepository;
use App\Services\CommandBus;
use App\Utils\Helpers;
use Nette\Application\AbortException;
use Nette\Application\UI\Control;
use Nette\Localization\Translator;
use Nette\Utils\Html;
Expand All @@ -24,8 +27,9 @@
class PatrolsGridControl extends Control
{
public function __construct(
private CommandBus $commandBus,
private Translator $translator,
private PatrolRepository $repository
private PatrolRepository $patrolRepository
) {
}

Expand All @@ -49,7 +53,7 @@ public function createComponentPatrolsGrid(string $name): DataGrid
{
$grid = new DataGrid($this, $name);
$grid->setTranslator($this->translator);
$grid->setDataSource($this->repository->createQueryBuilder('p')->where('p.confirmed = true'));
$grid->setDataSource($this->patrolRepository->createQueryBuilder('p')->where('p.confirmed = true'));
$grid->setDefaultSort(['displayName' => 'ASC']);
$grid->setColumnsHideable();
$grid->setItemsPerPageList([25, 50, 100, 250, 500]);
Expand Down Expand Up @@ -78,21 +82,35 @@ public function createComponentPatrolsGrid(string $name): DataGrid
})
->setSortable();

$grid->addColumnText('userRoles', 'Počet osob')
$grid->addColumnNumber('userRoles', 'Počet osob')
->setRenderer(static fn (Patrol $p) => count($p->getUsersRoles())); // je to správné číslo?
// $grid->addAction('detail', 'admin.common.detail', 'Patrols:detail') // destinace
// ->setClass('btn btn-xs btn-primary');

// $grid->addAction('delete', '', 'delete!')
// ->setIcon('trash')
// ->setTitle('admin.common.delete')
// ->setClass('btn btn-xs btn-danger')
// ->addAttributes([
// 'data-toggle' => 'confirmation',
// 'data-content' => $this->translator->translate('admin.users.users_delete_confirm'),
// ]);
$grid->addAction('delete', '', 'delete!')
->setIcon('trash')
->setTitle('admin.common.delete')
->setClass('btn btn-xs btn-danger')
->addAttributes([
'data-toggle' => 'confirmation',
'data-content' => $this->translator->translate('Opravdu chcete družinu odstranit?'),
]);

return $grid;
}

/**
* Zpracuje odstranění družiny.
*
* @throws AbortException
*/
public function handleDelete(int $id): void
{
$patrol = $this->patrolRepository->findById($id);
$this->commandBus->handle(new RemovePatrol($patrol));
$p = $this->getPresenter();
$p->flashMessage('Družina byla úspěšně odstraněna.', 'success');
$p->redirect('this');
}
}
51 changes: 26 additions & 25 deletions app/AdminModule/UsersModule/Components/TroopsGridControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace App\AdminModule\UsersModule\Components;

use App\Model\Acl\Role;
use App\Model\User\Commands\RemoveTroop;
use App\Model\User\Repositories\TroopRepository;
use App\Model\User\Troop;
use App\Services\CommandBus;
use App\Utils\Helpers;
use Doctrine\ORM\QueryBuilder;
use Nette\Application\AbortException;
Expand All @@ -27,8 +29,9 @@
class TroopsGridControl extends Control
{
public function __construct(
private CommandBus $commandBus,
private Translator $translator,
private TroopRepository $repository
private TroopRepository $troopRepository
) {
}

Expand All @@ -52,7 +55,7 @@ public function createComponentPatrolsGrid(string $name): DataGrid
{
$grid = new DataGrid($this, $name);
$grid->setTranslator($this->translator);
$grid->setDataSource($this->repository->createQueryBuilder('p'));
$grid->setDataSource($this->troopRepository->createQueryBuilder('p'));
$grid->setDefaultSort(['displayName' => 'ASC']);
$grid->setColumnsHideable();
$grid->setItemsPerPageList([25, 50, 100, 250, 500]);
Expand Down Expand Up @@ -135,33 +138,31 @@ public function createComponentPatrolsGrid(string $name): DataGrid
$grid->addAction('detail', 'admin.common.detail', 'Troops:detail')
->setClass('btn btn-xs btn-primary');

// $grid->addAction('delete', '', 'delete!')
// ->setIcon('trash')
// ->setTitle('admin.common.delete')
// ->setClass('btn btn-xs btn-danger')
// ->addAttributes([
// 'data-toggle' => 'confirmation',
// 'data-content' => $this->translator->translate('admin.users.users_delete_confirm'),
// ]);
$grid->addAction('delete', '', 'delete!')
->setIcon('trash')
->setTitle('admin.common.delete')
->setClass('btn btn-xs btn-danger')
->addAttributes([
'data-toggle' => 'confirmation',
'data-content' => $this->translator->translate('Opravdu chcete skupinu odstranit?'),
]);

return $grid;
}

// /**
// * Zpracuje odstranění externí skupiny.
// *
// * @throws AbortException
// */
// public function handleDelete(int $id): void
// {
// $rec = $this->repository->findById($id);
//
// $this->repository->remove($rec);
//
// $p = $this->getPresenter();
// $p->flashMessage('Skupina smazána.', 'success');
// $p->redirect('this');
// }
/**
* Zpracuje odstranění skupiny.
*
* @throws AbortException
*/
public function handleDelete(int $id): void
{
$troop = $this->troopRepository->findById($id);
$this->commandBus->handle(new RemoveTroop($troop));
$p = $this->getPresenter();
$p->flashMessage('Skupina byla úspěšně odstraněna.', 'success');
$p->redirect('this');
}

/**
* Vygeneruje potvrzení o přijetí platby.
Expand Down
38 changes: 38 additions & 0 deletions app/Model/User/Commands/Handlers/RemovePatrolHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Model\User\Commands\Handlers;

use App\Model\User\Commands\RemovePatrol;
use App\Model\User\Repositories\PatrolRepository;
use App\Model\User\Repositories\UserGroupRoleRepository;
use App\Model\User\Repositories\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class RemovePatrolHandler implements MessageHandlerInterface
{
public function __construct(
private EntityManagerInterface $em,
private UserGroupRoleRepository $userGroupRoleRepository,
private PatrolRepository $patrolRepository,
private UserRepository $userRepository
) {
}

public function __invoke(RemovePatrol $command): void
{
$this->em->wrapInTransaction(function () use ($command): void {
foreach ($command->getPatrol()->getUsersRoles() as $userRole) {
$user = $userRole->getUser();
$this->userGroupRoleRepository->remove($userRole);
if ($user->getRoles()->isEmpty() && $user->getGroupRoles()->isEmpty()) {
$this->userRepository->remove($user);
}
}

$this->patrolRepository->remove($command->getPatrol());
});
}
}
45 changes: 45 additions & 0 deletions app/Model/User/Commands/Handlers/RemoveTroopHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace App\Model\User\Commands\Handlers;

use App\Model\User\Commands\RemovePatrol;
use App\Model\User\Commands\RemoveTroop;
use App\Model\User\Repositories\TroopRepository;
use App\Model\User\Repositories\UserGroupRoleRepository;
use App\Model\User\Repositories\UserRepository;
use App\Services\CommandBus;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class RemoveTroopHandler implements MessageHandlerInterface
{
public function __construct(
private CommandBus $commandBus,
private EntityManagerInterface $em,
private UserGroupRoleRepository $userGroupRoleRepository,
private TroopRepository $troopRepository,
private UserRepository $userRepository
) {
}

public function __invoke(RemoveTroop $command): void
{
$this->em->wrapInTransaction(function () use ($command): void {
foreach ($command->getTroop()->getPatrols() as $patrol) {
$this->commandBus->handle(new RemovePatrol($patrol));
}

foreach ($command->getTroop()->getUsersRoles() as $userRole) {
$user = $userRole->getUser();
$this->userGroupRoleRepository->remove($userRole);
if ($user->getRoles()->isEmpty() && $user->getGroupRoles()->isEmpty()) {
$this->userRepository->remove($user);
}
}

$this->troopRepository->remove($command->getTroop());
});
}
}
19 changes: 19 additions & 0 deletions app/Model/User/Commands/RemovePatrol.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Model\User\Commands;

use App\Model\User\Patrol;

class RemovePatrol
{
public function __construct(private Patrol $patrol)
{
}

public function getPatrol(): Patrol
{
return $this->patrol;
}
}
19 changes: 19 additions & 0 deletions app/Model/User/Commands/RemoveTroop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Model\User\Commands;

use App\Model\User\Troop;

class RemoveTroop
{
public function __construct(private Troop $troop)
{
}

public function getTroop(): Troop
{
return $this->troop;
}
}
6 changes: 6 additions & 0 deletions app/Model/User/Repositories/PatrolRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ public function save(Patrol $patrol): void
$this->em->persist($patrol);
$this->em->flush();
}

public function remove(Patrol $patrol): void
{
$this->em->remove($patrol);
$this->em->flush();
}
}
6 changes: 6 additions & 0 deletions app/Model/User/Repositories/TroopRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,10 @@ public function save(Troop $troop): void
$this->em->persist($troop);
$this->em->flush();
}

public function remove(Troop $troop): void
{
$this->em->remove($troop);
$this->em->flush();
}
}

0 comments on commit 9a2eabe

Please sign in to comment.