2021-04-22 17:44:16 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
2021-04-28 00:15:13 +02:00
|
|
|
use App\Entity\Offering;
|
|
|
|
use App\Form\OfferingFormType;
|
2021-04-26 17:31:24 +02:00
|
|
|
use App\Repository\OfferingRepository;
|
|
|
|
|
2021-04-30 17:08:34 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2021-04-22 17:44:16 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2021-04-30 17:08:34 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-04-22 17:44:16 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2021-04-26 17:31:24 +02:00
|
|
|
use Twig\Environment;
|
2021-04-22 17:44:16 +02:00
|
|
|
|
|
|
|
class AppController extends AbstractController
|
|
|
|
{
|
2021-04-30 17:08:34 +02:00
|
|
|
private $entityManager;
|
|
|
|
|
|
|
|
public function __construct(EntityManagerInterface $entityManager)
|
|
|
|
{
|
|
|
|
$this->entityManager = $entityManager;
|
|
|
|
}
|
|
|
|
|
2021-04-26 17:31:24 +02:00
|
|
|
#[Route('/', name: 'homepage')]
|
|
|
|
public function index(Environment $twig, OfferingRepository $offeringRepository): Response
|
2021-04-22 17:44:16 +02:00
|
|
|
{
|
2021-04-26 17:31:24 +02:00
|
|
|
return new Response($twig->render('app/index.html.twig', [
|
|
|
|
'offerings' => $offeringRepository->findAll(),
|
|
|
|
]));
|
2021-04-22 17:44:16 +02:00
|
|
|
}
|
2021-04-23 15:06:41 +02:00
|
|
|
|
|
|
|
#[Route('/user', name: 'user_page')]
|
|
|
|
public function user(): Response
|
|
|
|
{
|
2021-04-26 17:31:24 +02:00
|
|
|
return $this->render('app/user.html.twig', [
|
2021-04-23 15:06:41 +02:00
|
|
|
'user' => $this->getUser(),
|
|
|
|
]);
|
|
|
|
}
|
2021-04-28 00:15:13 +02:00
|
|
|
|
|
|
|
#[Route('/new', name: 'new_listing')]
|
2021-04-30 17:08:34 +02:00
|
|
|
public function new_listing(Request $request): Response
|
2021-04-28 00:15:13 +02:00
|
|
|
{
|
|
|
|
$offering = new Offering();
|
|
|
|
$form = $this->createForm(OfferingFormType::class, $offering);
|
2021-04-30 17:08:34 +02:00
|
|
|
$user = $this->getUser();
|
|
|
|
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
|
|
$offering->setByUser($user);
|
|
|
|
$offering->setCreatedAt(new \DateTime());
|
2021-04-28 00:15:13 +02:00
|
|
|
|
2021-04-30 17:08:34 +02:00
|
|
|
$this->entityManager->persist($offering);
|
|
|
|
$this->entityManager->flush();
|
2021-04-30 17:21:15 +02:00
|
|
|
|
|
|
|
$this->addFlash("success", "Successfully added the new offering!");
|
|
|
|
return $this->redirectToRoute('homepage');
|
2021-04-30 17:08:34 +02:00
|
|
|
}
|
2021-04-28 00:15:13 +02:00
|
|
|
|
|
|
|
return $this->render('app/new_listing.html.twig', [
|
|
|
|
'user' => $this->getUser(),
|
|
|
|
'offering_form' => $form->createView(),
|
|
|
|
]);
|
|
|
|
}
|
2021-04-22 17:44:16 +02:00
|
|
|
}
|