← Back
Editing: AdresseApiController.php
<?php namespace App\Controller\Api; use App\Entity\Adresse; use App\Entity\User; use App\Repository\AddressReminderRepository; use App\Repository\AdresseRepository; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; #[Route(path: '/api/adresse')] class AdresseApiController extends AbstractApiController { #[Route(path: '/', name: 'api_etape_get')] #[Route(path: '/list', name: 'api_address_list')] public function getAction(AdresseRepository $adresseRepository): JsonResponse { /** @var User $user */ $user = $this->getUser(); $adresseList = []; if (in_array('ROLE_AGENT', $user->getArrayRoles())) { $adresseList = $adresseRepository->getWithItemsByAgent($user); } if (in_array('ROLE_MANAGER', $user->getArrayRoles())) { $adresseList = $adresseRepository->getWithItemsByManager($user); } if (in_array('ROLE_SUPERAGENT', $user->getArrayRoles()) || in_array('ROLE_SUPERVISOR', $user->getArrayRoles())) { $adresseList = $adresseRepository->getWithItemsByAgency($user->getAgency()); } if (in_array('ROLE_ORGANIZATION', $user->getArrayRoles())) { $adresseList = $adresseRepository->getWithItemsByOrganization($user->getOrganization()); } if (in_array('ROLE_TENANT', $user->getArrayRoles())) { $adresseList = $adresseRepository->getWithItemsByTenant($user); } return $this->response($adresseList, ['show_address', 'show_address_list']); } #[Route(path: '/remind', name: 'api_adresse_remind')] public function getAddressesToRemind(AddressReminderRepository $addressReminderRepository): JsonResponse { $toRemind = $addressReminderRepository->findToRemind($this->getUser()); return $this->response($toRemind, 'show_reminder'); } #[Route(path: '/{id}', name: 'api_address_get')] public function getSingleAction(Adresse $address): JsonResponse { return $this->response($address, 'show_address_detail'); } #[Route(path: '/{id}/', name: 'api_adresse_patch', methods: ['PATCH'])] public function patchAction( $id, AdresseRepository $adresseRepository, Request $request, EntityManagerInterface $entityManager ): JsonResponse { try { $adresse = $adresseRepository->findWithZones($id); $receivedAdresse = json_decode($request->getContent()); $adresse->setAscenseurIsActive($receivedAdresse->ascenseurIsActive); $entityManager->persist($adresse); $entityManager->flush(); $status = 'OK'; $message = 'Panne raportée'; } catch (\Exception $e) { $status = 'KO'; $message = $e->getMessage(); } return $this->response(['status' => $status, 'message' => $message]); } }
Save File
Cancel