|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Controller; |
| 6 | + |
| 7 | +use App\Dto\Request\Company\CompanyRequestDto; |
| 8 | +use App\Entity\Company; |
| 9 | +use App\Repository\CompanyRepository; |
| 10 | +use App\UseCase\Company\CreateCompany; |
| 11 | +use App\UseCase\Company\UpdateCompany; |
| 12 | +use Doctrine\ORM\EntityManagerInterface; |
| 13 | +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 14 | +use Symfony\Component\HttpFoundation\File\UploadedFile; |
| 15 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 16 | +use Symfony\Component\HttpFoundation\Request; |
| 17 | +use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; |
| 18 | +use Symfony\Component\Routing\Annotation\Route; |
| 19 | +use Symfony\Component\Security\Http\Attribute\IsGranted; |
| 20 | + |
| 21 | +class CompanyController extends AbstractController |
| 22 | +{ |
| 23 | + public function __construct( |
| 24 | + private readonly EntityManagerInterface $entityManager, |
| 25 | + private readonly CompanyRepository $companyRepository, |
| 26 | + private readonly CreateCompany $createCompany, |
| 27 | + private readonly UpdateCompany $updateCompany, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + #[Route('/companies', name: 'list_companies', methods: ['GET'])] |
| 32 | + public function list(): JsonResponse |
| 33 | + { |
| 34 | + $companies = $this->companyRepository->findAll(); |
| 35 | + |
| 36 | + return $this->json($companies, 200, [], ['groups' => Company::GROUP_LIST]); |
| 37 | + } |
| 38 | + |
| 39 | + #[Route('/companies/{id}', name: 'get_company', methods: ['GET'])] |
| 40 | + #[IsGranted('ROLE_RIGHT_COMPANY_READ')] |
| 41 | + public function get(Company $company): JsonResponse |
| 42 | + { |
| 43 | + return $this->json($company, 200, [], ['groups' => Company::GROUP_DETAILS]); |
| 44 | + } |
| 45 | + |
| 46 | + #[Route('/companies', name: 'create_company', methods: ['POST'])] |
| 47 | + #[IsGranted('ROLE_RIGHT_COMPANY_CREATE')] |
| 48 | + public function create(#[MapRequestPayload] CompanyRequestDto $companyRequestDto, Request $request): JsonResponse |
| 49 | + { |
| 50 | + $identityFile = $request->files->get('identityFile'); |
| 51 | + if ($identityFile !== null) { |
| 52 | + \assert($identityFile instanceof UploadedFile); |
| 53 | + $companyRequestDto->setIndentityFile($identityFile); |
| 54 | + } |
| 55 | + $company = $this->createCompany->create($companyRequestDto); |
| 56 | + $this->entityManager->flush(); |
| 57 | + |
| 58 | + return $this->json($company, 200, [], ['groups' => Company::GROUP_DETAILS]); |
| 59 | + } |
| 60 | + |
| 61 | + #[Route('/companies/{id}', name: 'update_company', methods: ['POST'])] |
| 62 | + #[IsGranted('ROLE_RIGHT_COMPANY_UPDATE')] |
| 63 | + public function update(Company $company, #[MapRequestPayload] CompanyRequestDto $companyRequestDto, Request $request): JsonResponse |
| 64 | + { |
| 65 | + $identityFile = $request->files->get('identityFile'); |
| 66 | + if ($identityFile !== null) { |
| 67 | + \assert($identityFile instanceof UploadedFile); |
| 68 | + $companyRequestDto->setIndentityFile($identityFile); |
| 69 | + } |
| 70 | + $company = $this->updateCompany->update($company, $companyRequestDto); |
| 71 | + $this->entityManager->flush(); |
| 72 | + |
| 73 | + return $this->json($company, 200, [], ['groups' => Company::GROUP_DETAILS]); |
| 74 | + } |
| 75 | +} |
0 commit comments