Implement basic wishlist

This commit is contained in:
Jannis Portmann 2021-05-04 11:52:20 +02:00
parent d80d667e0d
commit f2dd4da50b
9 changed files with 315 additions and 0 deletions

View file

@ -2,12 +2,27 @@
namespace App\Controller;
use App\Entity\Wish;
use App\Form\WishFormType;
use App\Repository\WishRepository;
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 Twig\Environment;
class UserController extends AbstractController
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
#[Route('/user', name: 'user_page')]
public function user(): Response
{
@ -15,4 +30,30 @@ class UserController extends AbstractController
'user' => $this->getUser(),
]);
}
#[Route('/wishlist', name: 'wishlist')]
public function wishlist(Request $request, WishRepository $wishRepository): Response
{
$wish = new Wish();
$form = $this->createForm(WishFormType::class, $wish);
$user = $this->getUser();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$wish->setByUser($user);
$this->entityManager->persist($wish);
$this->entityManager->flush();
$this->addFlash("success", "Successfully added the new offering!");
return $this->redirectToRoute('wishlist');
}
return $this->render('user/wish.html.twig', [
'user' => $this->getUser(),
'wishes' => $wishRepository->findByUser($user),
'wish_form' => $form->createView(),
]);
}
}