can write and read notes
This commit is contained in:
@@ -1,8 +1,15 @@
|
||||
<?php
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Note;
|
||||
use App\Form\Type\NoteType;
|
||||
use DateTime;
|
||||
use DateTimeZone;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bridge\Twig\Attribute\Template;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
class GuiController extends AbstractController {
|
||||
@@ -14,4 +21,66 @@ class GuiController extends AbstractController {
|
||||
'description' => 'Joe Carstairs\' personal website',
|
||||
];
|
||||
}
|
||||
|
||||
#[Route('/notes', name: 'notes')]
|
||||
#[Template('/notes.html.twig')]
|
||||
public function notes(): Array {
|
||||
return [
|
||||
'title' => 'Joe Carstairs\' notes',
|
||||
'description' => 'Joe Carstairs\' notes',
|
||||
];
|
||||
}
|
||||
|
||||
#[Route('/note/{slug}', name: 'note')]
|
||||
#[Template('/note.html.twig')]
|
||||
public function note(
|
||||
EntityManagerInterface $entityManager,
|
||||
string $slug,
|
||||
): array {
|
||||
$repository = $entityManager->getRepository(Note::class);
|
||||
$note = $repository->findOneBy(['slug' => $slug]);
|
||||
|
||||
return [
|
||||
'title' => 'Joe Carstairs\' notes',
|
||||
'description' => 'Joe Carstairs\' notes',
|
||||
'note' => $note,
|
||||
];
|
||||
}
|
||||
|
||||
#[Route('/notes/write')]
|
||||
#[Template('/write_note.html.twig')]
|
||||
public function writeNote(
|
||||
EntityManagerInterface $entityManager,
|
||||
Request $request,
|
||||
): Response {
|
||||
$note = new Note();
|
||||
$form = $this->createForm(NoteType::class, $note);
|
||||
|
||||
$form->handleRequest($request);
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$note = $form->getData();
|
||||
|
||||
$now = new DateTime('now');
|
||||
$note->setPublishedDate($now);
|
||||
|
||||
$num_existing_notes_today = $entityManager->getRepository(Note::class)->countWherePublishedOnDate($now);
|
||||
$note->getPublishedDate()->setTimezone(new DateTimeZone('Europe/London'));
|
||||
$slug = $note->getPublishedDate()->format('Y-m-d');
|
||||
if ($num_existing_notes_today > 0) {
|
||||
$slug = $slug . '-' . $num_existing_notes_today;
|
||||
}
|
||||
$note->setSlug($slug);
|
||||
|
||||
$entityManager->persist($note);
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('note', ['slug' => $note->getSlug()]);
|
||||
}
|
||||
|
||||
return $this->render('/write_note.html.twig', [
|
||||
'title' => 'Write for Joe Carstairs',
|
||||
'description' => 'The authoring page for Joe Carstairs\' personal website',
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user