Implement basic editing of offer
This commit is contained in:
parent
47033e8677
commit
54486d008c
2 changed files with 55 additions and 1 deletions
|
@ -28,7 +28,7 @@ class OfferController extends AbstractController
|
|||
#[Route('/offers', name: 'offers')]
|
||||
public function index(Environment $twig, OfferingRepository $offerRepository): Response
|
||||
{
|
||||
return new Response($twig->render('app/index.html.twig', [
|
||||
return new Response($twig->render('offer/index.html.twig', [
|
||||
'offers' => $offerRepository->findAll(),
|
||||
]));
|
||||
}
|
||||
|
@ -80,4 +80,39 @@ class OfferController extends AbstractController
|
|||
'wishes' => $wishRepository->findByUser($offer->getByUser()),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/offer/edit/{id}', name: 'edit_offer')]
|
||||
public function editOffer(Offering $offer, OfferingRepository $offerRepository, Request $request, string $photoDir): Response
|
||||
{
|
||||
$form = $this->createForm(OfferingFormType::class, $offer);
|
||||
$user = $this->getUser();
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$offer->setByUser($user);
|
||||
$offer->setCreatedAt(new \DateTime());
|
||||
|
||||
if ($photo = $form['photo']->getData()) {
|
||||
$filename = bin2hex(random_bytes(6)).'.'.$photo->guessExtension();
|
||||
try {
|
||||
$photo->move($photoDir, $filename);
|
||||
} catch (FileException $e) {
|
||||
// unable to upload the photo, give up
|
||||
$this->addFlash("error", "There was an error uploading the photo: ".$e);
|
||||
return $this->redirectToRoute('new_offer');
|
||||
}
|
||||
$offer->setPhotoFilename($filename);
|
||||
}
|
||||
|
||||
$this->entityManager->persist($offer);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
return $this->render('offer/edit.html.twig', [
|
||||
'user' => $this->getUser(),
|
||||
'offer' => $offer,
|
||||
'offer_form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue