Skip to content
Permalink
Browse files

Implement deleting notes

  • Loading branch information
karol-dabrowski committed Feb 9, 2020
1 parent 107692a commit 626453d3d7d5c8867f24769ed657a8a67995a116
@@ -0,0 +1,19 @@
<?php
namespace App\Command;

use Ramsey\Uuid\Uuid;

class DeleteNote
{
private $id;

public function __construct(Uuid $noteId)
{
$this->id = $noteId;
}

public function getId(): Uuid
{
return $this->id;
}
}
@@ -0,0 +1,31 @@
<?php
namespace App\Command\Handler;

use App\Command\DeleteNote;
use App\Repository\NoteRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class DeleteNoteHandler implements MessageHandlerInterface
{
private $notes;
private $em;

public function __construct(NoteRepository $noteRepository, EntityManagerInterface $em)
{
$this->notes = $noteRepository;
$this->em = $em;
}

public function __invoke(DeleteNote $command)
{
$noteId = $command->getId();
$note = $this->notes->findOneById($noteId);

if(!$note) {
throw new \InvalidArgumentException("Note with provided id does not isset");
}

$this->em->remove($note);
}
}
@@ -2,6 +2,7 @@
namespace App\Controller;

use App\Command\AddNote;
use App\Command\DeleteNote;
use App\Query\GetSomething;
use Ramsey\Uuid\Uuid;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -33,11 +34,19 @@ public function add(Request $request, MessageBusInterface $commandBus)
}

/**
* @Route("/note/delete/{noteId}", methods={"POST"}, requirements={"noteId"="\d+"})
* @Route("/note/delete/{noteId}", methods={"POST"})
*/
public function delete(int $noteId)
public function delete(string $noteId, MessageBusInterface $commandBus)
{
dd($noteId);
//TODO Delete note with id $noteId
try {
$commandBus->dispatch(
new DeleteNote(Uuid::fromString($noteId))
);
} catch (\Exception $e) {
//TODO Handle exception
dd($e->getMessage());
}

return $this->redirectToRoute('index');
}
}
@@ -5,6 +5,7 @@
use App\Entity\Note;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Ramsey\Uuid\Uuid;

class NoteRepository extends ServiceEntityRepository
{
@@ -22,4 +23,9 @@ public function getNoteByTitle(string $title): ?Note
{
return $this->findOneBy(['title' => $title]);
}

public function findOneById(Uuid $id): ?Note
{
return $this->findOneBy(['id' => $id]);
}
}

0 comments on commit 626453d

Please sign in to comment.
You can’t perform that action at this time.