2022-10-27 11:55:16 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use App\Entity\Cpu;
|
|
|
|
use App\Form\CpuType;
|
|
|
|
use App\Traits\FormControllerTrait;
|
|
|
|
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;
|
|
|
|
|
|
|
|
#[Route('/cpu')]
|
|
|
|
class CpuController extends AbstractController {
|
|
|
|
use FormControllerTrait;
|
|
|
|
|
|
|
|
protected const ENTITY = Cpu::class;
|
|
|
|
protected const TEMPLATE_PATH = 'cpu/';
|
|
|
|
protected const ROUTE_PREFIX = 'cpu_';
|
|
|
|
protected const FORM = CpuType::class;
|
|
|
|
|
|
|
|
public function __construct(private readonly EntityManagerInterface $entityManager)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:46:35 -04:00
|
|
|
#[Route('/', name: 'cpu_index', methods: ['GET'])]
|
2022-10-27 11:55:16 -04:00
|
|
|
public function index(): Response
|
|
|
|
{
|
2022-11-03 10:46:44 -04:00
|
|
|
$template = self::TEMPLATE_PATH . 'index.html.twig';
|
|
|
|
|
|
|
|
$items = $this->entityManager->getRepository(self::ENTITY)->findBy([], [
|
|
|
|
'productLine' => 'ASC',
|
|
|
|
'model' => 'ASC',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$amd = array_filter($items, static fn (Cpu $cpu) => $cpu->getBrand()->getName() === 'AMD' && $cpu->isReceived());
|
|
|
|
$intel = array_filter($items, static fn (Cpu $cpu) => $cpu->getBrand()->getName() === 'Intel' && $cpu->isReceived());
|
|
|
|
$others = array_filter($items, static fn (Cpu $cpu) => ( ! in_array($cpu->getBrand()->getName(), ['AMD','Intel'])) && $cpu->isReceived());
|
|
|
|
$notReceived = array_filter($items, static fn(CPU $cpu) => $cpu->isReceived() === FALSE);
|
|
|
|
|
|
|
|
return $this->render($template, [
|
|
|
|
'all' => [
|
|
|
|
'not_acquired' => $notReceived,
|
|
|
|
'amd' => $amd,
|
|
|
|
'intel' => $intel,
|
|
|
|
'others' => $others,
|
|
|
|
],
|
|
|
|
]);
|
2022-10-27 11:55:16 -04:00
|
|
|
}
|
|
|
|
|
2022-10-28 08:46:35 -04:00
|
|
|
#[Route('/new', name: 'cpu_new', methods: ['GET', 'POST'])]
|
2022-10-27 11:55:16 -04:00
|
|
|
public function new(Request $request): Response
|
|
|
|
{
|
|
|
|
return $this->itemCreate($request, 'cpu');
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:46:35 -04:00
|
|
|
#[Route('/{id}', name: 'cpu_show', methods: ['GET'])]
|
2022-10-27 11:55:16 -04:00
|
|
|
public function show(Cpu $cpu): Response
|
|
|
|
{
|
|
|
|
return $this->itemView($cpu, 'cpu');
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:46:35 -04:00
|
|
|
#[Route('/{id}/edit', name: 'cpu_edit', methods: ['GET', 'POST'])]
|
2022-10-27 11:55:16 -04:00
|
|
|
public function edit(Request $request, Cpu $cpu): Response
|
|
|
|
{
|
|
|
|
return $this->itemUpdate($request, $cpu, 'cpu');
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:46:35 -04:00
|
|
|
#[Route('/{id}', name: 'cpu_delete', methods: ['POST'])]
|
2022-10-27 11:55:16 -04:00
|
|
|
public function delete(Request $request, Cpu $cpu): Response
|
|
|
|
{
|
|
|
|
return $this->deleteCSRF($request, $cpu);
|
|
|
|
}
|
|
|
|
}
|