Allow ceating and editing snips

This commit is contained in:
Tim 2023-04-04 00:36:35 +02:00
parent 921dcf1e97
commit dce2fbff42
7 changed files with 75 additions and 32 deletions

View File

@ -3,19 +3,28 @@
namespace App\Controller;
use App\Entity\Snip;
use App\Form\SnipType;
use App\Repository\SnipRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/snip', name: 'snip')]
class SnipController extends AbstractController
{
public function __construct(
private readonly SnipRepository $repository,
)
{
}
#[Route('/', name: '_index')]
public function index(SnipRepository $repository): Response
public function index(): Response
{
return $this->render('snip/index.html.twig', [
'snips' => $repository->findAll(),
'snips' => $this->repository->findAll(),
]);
}
@ -26,4 +35,37 @@ class SnipController extends AbstractController
'snip' => $snip,
]);
}
#[Route('/edit/{snip}', name: '_edit')]
public function edit(Snip $snip, Request $request): Response
{
$form = $this->createForm(SnipType::class, $snip);
$form->add('Save', SubmitType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$snip->setCreatedAtTodayNoSeconds()
->setCreatedBy($this->getUser());
$this->repository->save($snip);
$this->addFlash('success', sprintf('Snip "%s" saved successfully', $snip));
return $this->redirectToRoute('snip_single', [
'snip' => $snip->getId(),
]);
}
return $this->render('snip/edit.html.twig', [
'snip' => $snip,
'form' => $form->createView(),
]);
}
#[Route('/new', name: '_new')]
public function new(Request $request): Response
{
$snip = new Snip();
return $this->edit($snip, $request);
}
}

View File

@ -19,6 +19,11 @@ class Snip
#[ORM\Column(length: 255)]
private ?string $name = null;
public function __toString(): string
{
return $this->name ?? '';
}
public function getId(): ?int
{
return $this->id;

View File

@ -34,6 +34,11 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[ORM\Column(length: 255)]
private ?string $email = null;
public function __toString(): string
{
return $this->name ?? '';
}
public function getId(): ?int
{
return $this->id;

View File

@ -4,6 +4,7 @@ namespace App\Form;
use App\Entity\Snip;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
@ -13,8 +14,10 @@ class SnipType extends AbstractType
{
$builder
->add('name')
->add('createdAt')
->add('createdBy')
->add('content', TextareaType::class, [
'attr' => ['rows' => 20],
'mapped' => false,
])
;
}

View File

@ -21,7 +21,7 @@ class SnipRepository extends ServiceEntityRepository
parent::__construct($registry, Snip::class);
}
public function save(Snip $entity, bool $flush = false): void
public function save(Snip $entity, bool $flush = true): void
{
$this->getEntityManager()->persist($entity);
@ -30,7 +30,7 @@ class SnipRepository extends ServiceEntityRepository
}
}
public function remove(Snip $entity, bool $flush = false): void
public function remove(Snip $entity, bool $flush = true): void
{
$this->getEntityManager()->remove($entity);
@ -38,29 +38,4 @@ class SnipRepository extends ServiceEntityRepository
$this->getEntityManager()->flush();
}
}
// /**
// * @return Snip[] Returns an array of Snip objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('s')
// ->andWhere('s.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('s.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Snip
// {
// return $this->createQueryBuilder('s')
// ->andWhere('s.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}

View File

@ -0,0 +1,12 @@
{% extends 'base/base.html.twig' %}
{% block body %}
<h2>{{ snip }}</h2>
<div class="container">
<div class="row">
<div class="col-md-12">
{{ form(form) }}
</div>
</div>
</div>
{% endblock %}

View File

@ -7,10 +7,11 @@
<h1>Snippets</h1>
</div>
</div>
<a class="btn btn-primary" href="{{ path('snip_new') }}">Create a new Snip</a>
<div class="row">
<div class="list-group">
{% for snip in snips %}
<a class="list-group-item">
<a class="list-group-item" href="{{ path('snip_edit', {snip: snip.id}) }}">
{{ snip }}
</a>
{% endfor %}