diff options
| author | polo <ordipolo@gmx.fr> | 2026-06-10 20:56:25 +0200 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2026-06-10 20:56:25 +0200 |
| commit | ae217a6e4b0c29346381e2a410fd7810cb33ce3f (patch) | |
| tree | 206f604c9297deabf78bb33f31a1e6b9303d2258 /src/controller/CalendarController.php | |
| parent | 99d2616a08c98e7067cdb12f0bcbd4ac0cffaeb0 (diff) | |
| download | cms-ae217a6e4b0c29346381e2a410fd7810cb33ce3f.tar.gz cms-ae217a6e4b0c29346381e2a410fd7810cb33ce3f.tar.bz2 cms-ae217a6e4b0c29346381e2a410fd7810cb33ce3f.zip | |
Tous les contrôleurs retournent une Response!
Diffstat (limited to 'src/controller/CalendarController.php')
| -rw-r--r-- | src/controller/CalendarController.php | 30 |
1 files changed, 11 insertions, 19 deletions
diff --git a/src/controller/CalendarController.php b/src/controller/CalendarController.php index b60ff11..1a62afa 100644 --- a/src/controller/CalendarController.php +++ b/src/controller/CalendarController.php | |||
| @@ -5,10 +5,11 @@ declare(strict_types=1); | |||
| 5 | 5 | ||
| 6 | use Doctrine\ORM\EntityManager; | 6 | use Doctrine\ORM\EntityManager; |
| 7 | use App\Entity\Event; | 7 | use App\Entity\Event; |
| 8 | use Symfony\Component\HttpFoundation\JsonResponse; | ||
| 8 | 9 | ||
| 9 | class CalendarController | 10 | class CalendarController |
| 10 | { | 11 | { |
| 11 | static public function getData(EntityManager $entityManager): void | 12 | static public function getData(EntityManager $entityManager): JsonResponse |
| 12 | { | 13 | { |
| 13 | // bornes début et fin du calendrier affiché à l'heure locale | 14 | // bornes début et fin du calendrier affiché à l'heure locale |
| 14 | // noter que la vue "planning" est similaire à la vue "semaine" | 15 | // noter que la vue "planning" est similaire à la vue "semaine" |
| @@ -33,50 +34,41 @@ class CalendarController | |||
| 33 | $events[] = $event->toArray(); | 34 | $events[] = $event->toArray(); |
| 34 | } | 35 | } |
| 35 | 36 | ||
| 36 | header('Content-Type: application/json'); | 37 | return new JsonResponse($events); |
| 37 | echo json_encode($events); | ||
| 38 | die; | ||
| 39 | } | 38 | } |
| 40 | 39 | ||
| 41 | static public function newEvent(array $json, EntityManager $entityManager):void | 40 | static public function newEvent(array $json, EntityManager $entityManager): JsonResponse |
| 42 | { | 41 | { |
| 43 | try{ | 42 | try{ |
| 44 | $event = new Event($json); | 43 | $event = new Event($json); |
| 45 | } | 44 | } |
| 46 | catch(InvalidArgumentException $e){ | 45 | catch(InvalidArgumentException $e){ |
| 47 | echo json_encode(['success' => false, 'error' => $e->getMessage()]); | 46 | return new JsonResponse(['success' => false, 'error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST); // code 400 |
| 48 | http_response_code(400); | ||
| 49 | die; | ||
| 50 | } | 47 | } |
| 51 | $entityManager->persist($event); | 48 | $entityManager->persist($event); |
| 52 | $entityManager->flush(); | 49 | $entityManager->flush(); |
| 53 | 50 | ||
| 54 | echo json_encode(['success' => true, 'id' => $event->getId()]); | 51 | return new JsonResponse(['success' => true, 'id' => $event->getId()]); |
| 55 | die; | ||
| 56 | } | 52 | } |
| 57 | static public function updateEvent(array $json, EntityManager $entityManager):void | 53 | static public function updateEvent(array $json, EntityManager $entityManager): JsonResponse |
| 58 | { | 54 | { |
| 59 | $event = $entityManager->find('App\Entity\Event', (int)$json['id']); | 55 | $event = $entityManager->find('App\Entity\Event', (int)$json['id']); |
| 60 | try{ | 56 | try{ |
| 61 | $event->securedUpdateFromJSON($json); | 57 | $event->securedUpdateFromJSON($json); |
| 62 | } | 58 | } |
| 63 | catch(InvalidArgumentException $e){ | 59 | catch(InvalidArgumentException $e){ |
| 64 | echo json_encode(['success' => false, 'error' => $e->getMessage()]); | 60 | return new JsonResponse(['success' => false, 'error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST); // code 400 |
| 65 | http_response_code(400); | ||
| 66 | die; | ||
| 67 | } | 61 | } |
| 68 | $entityManager->flush(); | 62 | $entityManager->flush(); |
| 69 | 63 | ||
| 70 | echo json_encode(['success' => true]); | 64 | return new JsonResponse(['success' => true]); |
| 71 | die; | ||
| 72 | } | 65 | } |
| 73 | static public function removeEvent(array $json, EntityManager $entityManager):void | 66 | static public function removeEvent(array $json, EntityManager $entityManager): JsonResponse |
| 74 | { | 67 | { |
| 75 | $event = $entityManager->find('App\Entity\Event', (int)$json['id']); | 68 | $event = $entityManager->find('App\Entity\Event', (int)$json['id']); |
| 76 | $entityManager->remove($event); | 69 | $entityManager->remove($event); |
| 77 | $entityManager->flush(); | 70 | $entityManager->flush(); |
| 78 | 71 | ||
| 79 | echo json_encode(['success' => true]); | 72 | return new JsonResponse(['success' => true]); |
| 80 | die; | ||
| 81 | } | 73 | } |
| 82 | } \ No newline at end of file | 74 | } \ No newline at end of file |
