<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\UserType;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Repository\DemandeRepository;
/**
* @Route("/user")
*/
class UserController extends AbstractController
{
/**
* @Route("/index", name="app_user_index", methods={"GET"})
*
*/
public function index(UserRepository $userRepository): Response
{
return $this->render('user/index.html.twig', [
'users' => $userRepository->findAll(),
]);
}
/**
* @Route("/new", name="app_user_new", methods={"GET", "POST"})
*/
public function new(Request $request, UserRepository $userRepository, UserPasswordEncoderInterface $encoder): Response
{
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$hash = $encoder->encodePassword($user, $user->getPassword());
$user->setPassword($hash);
$userRepository->add($user, true);
return $this->redirectToRoute('app_livre_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('user/new.html.twig', [
'user' => $user,
'form' => $form,
]);
}
/**
* @Route("/profile", name="profile", methods={"GET"})
*/
public function profile(): Response
{
return $this->render('user/profile.html.twig', [
]);
}
/**
* @Route("/{id}", name="app_user_show", methods={"GET"})
*/
public function show(User $user): Response
{
$livres = $user->getLivres();
return $this->render('user/show.html.twig', [
'user' => $user,
'livres' => $livres,
]);
}
/**
* @Route("/echange/{id}/{demande}", name="app_user_echange", methods={"GET"})
*/
public function echange(User $user, $demande, DemandeRepository $demandeRepo): Response
{
$livres = $user->getLivres();
$demandeobject = $demandeRepo->findBy(['id' => $demande]);
// dd($demandeobject);
return $this->render('user/echange.html.twig', [
'user' => $user,
'livres' => $livres,
'demande' => $demande,
'demandeob' => $demandeobject[0],
]);
}
/**
* @Route("/{id}/edit", name="app_user_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, User $user, UserRepository $userRepository): Response
{
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$userRepository->add($user, true);
return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('user/edit.html.twig', [
'user' => $user,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="app_user_delete", methods={"POST"})
*/
public function delete(Request $request, User $user, UserRepository $userRepository): Response
{
if ($this->isCsrfTokenValid('delete'.$user->getId(), $request->request->get('_token'))) {
$userRepository->remove($user, true);
}
return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
}
}