70 lines
1.9 KiB
PHP
70 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Stock;
|
|
use App\Form\StockType;
|
|
use App\Repository\StockRepository;
|
|
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;
|
|
|
|
class StockController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
private readonly EntityManagerInterface $entityManager,
|
|
private readonly StockRepository $stockRepository
|
|
)
|
|
{
|
|
}
|
|
|
|
#[Route('/stock/add', name: 'stock_add')]
|
|
public function add(Request $request): Response
|
|
{
|
|
$stock = new Stock();
|
|
$form = $this->createForm(StockType::class, $stock);
|
|
$form->handleRequest($request);
|
|
|
|
if($form->isSubmitted() && $form->isValid())
|
|
{
|
|
$this->entityManager->persist($stock);
|
|
$this->entityManager->flush();
|
|
}
|
|
|
|
return $this->render('stock/add.html.twig', [
|
|
'form' => $form,
|
|
]);
|
|
}
|
|
|
|
#[Route('/stock/list', name: 'stock_list')]
|
|
public function list(): Response
|
|
{
|
|
$stocks = $this->stockRepository->findAll();
|
|
|
|
return $this->render('stock/list.html.twig', [
|
|
'stocks' => $stocks,
|
|
]);
|
|
}
|
|
|
|
#[Route('/stock/update/{id}', name: 'stock_update')]
|
|
public function update(int $id, Request $request): Response
|
|
{
|
|
$stock = $this->stockRepository->find($id);
|
|
$form = $this->createForm(StockType::class, $stock);
|
|
$form->handleRequest($request);
|
|
|
|
if($form->isSubmitted() && $form->isValid())
|
|
{
|
|
$this->entityManager->persist($stock);
|
|
$this->entityManager->flush();
|
|
}
|
|
|
|
return $this->render('stock/add.html.twig', [
|
|
'form' => $form,
|
|
]);
|
|
}
|
|
|
|
|
|
} |