From 329d3cc3d8b793403042340dbe3f4e490cdb9743 Mon Sep 17 00:00:00 2001 From: nobohan Date: Mon, 30 Aug 2021 11:02:53 +0200 Subject: [PATCH] rdv: add API entry point for POSTing calendar range --- .../Controller/CalendarRangeAPIController.php | 19 ++++--- .../ChillCalendarExtension.php | 3 +- .../Entity/CalendarRange.php | 6 +-- .../ChillCalendarBundle/chill.api.specs.yaml | 50 +++++++++++++++++++ 4 files changed, 66 insertions(+), 12 deletions(-) diff --git a/src/Bundle/ChillCalendarBundle/Controller/CalendarRangeAPIController.php b/src/Bundle/ChillCalendarBundle/Controller/CalendarRangeAPIController.php index be279ed36..672e9527c 100644 --- a/src/Bundle/ChillCalendarBundle/Controller/CalendarRangeAPIController.php +++ b/src/Bundle/ChillCalendarBundle/Controller/CalendarRangeAPIController.php @@ -18,16 +18,19 @@ class CalendarRangeAPIController extends ApiController */ public function availableRanges(Request $request, string $_format): JsonResponse { - if ($request->query->has('user')) { - $user = $request->query->get('user'); - } - $em = $this->getDoctrine()->getManager(); - $query = $em->createQuery( - 'SELECT c FROM ChillCalendarBundle:CalendarRange c - WHERE NOT EXISTS (SELECT cal.id FROM ChillCalendarBundle:Calendar cal WHERE cal.calendarRange = c.id)') - ; + $sql = 'SELECT c FROM ChillCalendarBundle:CalendarRange c + WHERE NOT EXISTS (SELECT cal.id FROM ChillCalendarBundle:Calendar cal WHERE cal.calendarRange = c.id)'; + + if ($request->query->has('user')) { + $user = $request->query->get('user'); + $sql = $sql . ' AND c.user = :user'; + $query = $em->createQuery($sql) + ->setParameter('user', $user); + } else { + $query = $em->createQuery($sql); + } $results = $query->getResult(); diff --git a/src/Bundle/ChillCalendarBundle/DependencyInjection/ChillCalendarExtension.php b/src/Bundle/ChillCalendarBundle/DependencyInjection/ChillCalendarExtension.php index 2927d4910..5f20f5968 100644 --- a/src/Bundle/ChillCalendarBundle/DependencyInjection/ChillCalendarExtension.php +++ b/src/Bundle/ChillCalendarBundle/DependencyInjection/ChillCalendarExtension.php @@ -72,7 +72,8 @@ class ChillCalendarExtension extends Extension implements PrependExtensionInterf '_entity' => [ 'methods' => [ Request::METHOD_GET => true, - Request::METHOD_HEAD => true + Request::METHOD_HEAD => true, + Request::METHOD_POST => true, ] ], ] diff --git a/src/Bundle/ChillCalendarBundle/Entity/CalendarRange.php b/src/Bundle/ChillCalendarBundle/Entity/CalendarRange.php index f9582c8c2..b83684a7c 100644 --- a/src/Bundle/ChillCalendarBundle/Entity/CalendarRange.php +++ b/src/Bundle/ChillCalendarBundle/Entity/CalendarRange.php @@ -25,19 +25,19 @@ class CalendarRange /** * @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\User") - * @Groups({"read"}) + * @groups({"read", "write"}) */ private User $user; /** * @ORM\Column(type="datetimetz_immutable") - * @groups({"read"}) + * @groups({"read", "write"}) */ private \DateTimeImmutable $startDate; /** * @ORM\Column(type="datetimetz_immutable") - * @groups({"read"}) + * @groups({"read", "write"}) */ private \DateTimeImmutable $endDate; diff --git a/src/Bundle/ChillCalendarBundle/chill.api.specs.yaml b/src/Bundle/ChillCalendarBundle/chill.api.specs.yaml index 8da280b36..1ca66375d 100644 --- a/src/Bundle/ChillCalendarBundle/chill.api.specs.yaml +++ b/src/Bundle/ChillCalendarBundle/chill.api.specs.yaml @@ -8,6 +8,28 @@ servers: - url: "/api" description: "Your current dev server" +components: + schemas: + Date: + type: object + properties: + datetime: + type: string + format: date-time + User: + type: object + properties: + id: + type: integer + type: + type: string + enum: + - user + username: + type: string + text: + type: string + paths: /1.0/calendar/calendar.json: get: @@ -48,6 +70,34 @@ paths: responses: 200: description: "ok" + post: + tags: + - calendar + summary: create a new calendar range + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + user: + $ref: '#/components/schemas/User' + startDate: + $ref: '#/components/schemas/Date' + endDate: + $ref: '#/components/schemas/Date' + responses: + 401: + description: "Unauthorized" + 404: + description: "Not found" + 200: + description: "OK" + 422: + description: "Unprocessable entity (validation errors)" + 400: + description: "transition cannot be applyed" /1.0/calendar/calendar-range/{id}.json: get: