CRUD for domains & playing with symfony stimulus

This commit is contained in:
2023-10-14 03:15:46 -06:00
parent 3ed53ac05e
commit 39e94d8bdc
13 changed files with 622 additions and 368 deletions
+85
View File
@@ -0,0 +1,85 @@
<?php
namespace App\Controller;
use App\Entity\Domain;
use App\Form\DomainType;
use App\Repository\DomainRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\String\ByteString;
#[Route('/domain')]
#[IsGranted('IS_AUTHENTICATED_FULLY')]
class DomainController extends AbstractController
{
#[Route('/', name: 'app_domain_index', methods: ['GET'])]
public function index(DomainRepository $domainRepository): Response
{
return $this->render('domain/index.html.twig', [
'domains' => $domainRepository->findAll(),
]);
}
#[Route('/new', name: 'app_domain_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$domain = new Domain();
$domain->setOwnerToken(ByteString::fromRandom(64)->toString());
$form = $this->createForm(DomainType::class, $domain);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($domain);
$entityManager->flush();
return $this->redirectToRoute('app_domain_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('domain/new.html.twig', [
'domain' => $domain,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_domain_show', methods: ['GET'])]
public function show(Domain $domain): Response
{
return $this->render('domain/show.html.twig', [
'domain' => $domain,
]);
}
#[Route('/{id}/edit', name: 'app_domain_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Domain $domain, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(DomainType::class, $domain);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('app_domain_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('domain/edit.html.twig', [
'domain' => $domain,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_domain_delete', methods: ['POST'])]
public function delete(Request $request, Domain $domain, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$domain->getId(), $request->request->get('_token'))) {
$entityManager->remove($domain);
$entityManager->flush();
}
return $this->redirectToRoute('app_domain_index', [], Response::HTTP_SEE_OTHER);
}
}
+16 -34
View File
@@ -13,6 +13,13 @@ use Gedmo\Mapping\Annotation\Timestampable;
#[ORM\Entity(repositoryClass: DomainRepository::class)]
class Domain
{
const SORT_POLICIES = [
'Recent First' => 'created:desc',
'Oldest First' => 'created:asc',
'Rating Desc' => 'rating:desc',
'Rating Asc' => 'rating:asc',
];
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
@@ -41,15 +48,11 @@ class Domain
#[Timestampable(on: "create")]
private ?DateTimeInterface $createdAt = null;
#[ORM\OneToMany(mappedBy: 'domain', targetEntity: Comment::class)]
private Collection $comments;
#[ORM\OneToMany(mappedBy: 'domain', targetEntity: DomainPage::class)]
private Collection $domainPages;
public function __construct()
{
$this->comments = new ArrayCollection();
$this->domainPages = new ArrayCollection();
}
@@ -111,6 +114,15 @@ class Domain
return $this->defaultSortPolicy;
}
public function getDefaultSortPolicyName(): ?string
{
if(!$this->getDefaultSortPolicy()) {
return null;
}
return array_flip(Domain::SORT_POLICIES)[$this->getDefaultSortPolicy()] ?? null;
}
public function setDefaultSortPolicy(string $defaultSortPolicy): static
{
$this->defaultSortPolicy = $defaultSortPolicy;
@@ -142,36 +154,6 @@ class Domain
return $this;
}
/**
* @return Collection<int, Comment>
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): static
{
if (!$this->comments->contains($comment)) {
$this->comments->add($comment);
$comment->setDomain($this);
}
return $this;
}
public function removeComment(Comment $comment): static
{
if ($this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if ($comment->getDomain() === $this) {
$comment->setDomain(null);
}
}
return $this;
}
/**
* @return Collection<int, DomainPage>
*/
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace App\Form;
use App\Entity\Domain;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class DomainType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('enabled', CheckboxType::class, [
'attr' => ['checked' => 'checked', 'value' => '1']
])
->add('domain', TextType::class, [
'attr' => [
'placeholder' => 'example.com'
]
])
->add('name', TextType::class, [
'attr' => [
'placeholder' => "John Doe's Blog"
]
])
->add('defaultSortPolicy', ChoiceType::class, [
'choices' => Domain::SORT_POLICIES,
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Domain::class,
]);
}
}