can write and read notes

This commit is contained in:
2025-05-17 22:47:20 +01:00
parent a8d5c4dcdb
commit 6327c3509e
12 changed files with 352 additions and 4 deletions

View File

@@ -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,
]);
}
}

66
src/Entity/Note.php Normal file
View File

@@ -0,0 +1,66 @@
<?php
namespace App\Entity;
use App\Repository\NoteRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: NoteRepository::class)]
class Note
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $content = null;
#[ORM\Column(type: Types::DATE_MUTABLE)]
private ?\DateTime $published_date = null;
#[ORM\Column(length: 255)]
private ?string $slug = null;
public function getId(): ?int
{
return $this->id;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): static
{
$this->content = $content;
return $this;
}
public function getPublishedDate(): ?\DateTime
{
return $this->published_date;
}
public function setPublishedDate(\DateTime $published_date): static
{
$this->published_date = $published_date;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): static
{
$this->slug = $slug;
return $this;
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Form\Type;
use App\Entity\Note;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class NoteType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('content', TextType::class)
->add('post', SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Note::class,
]);
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace App\Repository;
use App\Entity\Note;
use DateInterval;
use DateTime;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Note>
*/
class NoteRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Note::class);
}
public function countWherePublishedOnDate(DateTime $date): int {
$min = new DateTime($date->format('c'));
$min->setTime(0, 0, 0, 0);
$max = new DateTime($min->format('c'));
$max->add(DateInterval::createFromDateString('1 day'));
$wherePublishedOnDate = $this->createQueryBuilder('n')
->andWhere('n.published_date >= :min')
->andWhere('n.published_date < :max')
->setParameter('min', $min)
->setParameter('max', $max)
->getQuery()
->execute();
return count($wherePublishedOnDate);
}
// /**
// * @return Note[] Returns an array of Note objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('n')
// ->andWhere('n.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('n.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Note
// {
// return $this->createQueryBuilder('n')
// ->andWhere('n.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}