namespace, '/' . $this->resource_name, [ [ 'methods' => 'GET', 'callback' => [$this, 'index'], 'permission_callback' => [RsvRestPolicy::class, 'admin'], ], [ 'methods' => 'POST', 'callback' => [$this, 'create'], 'permission_callback' => [RsvRestPolicy::class, 'admin'], 'args' => self::input_args(RsvTimetable::schema()), ], ]); register_rest_route($this->namespace, '/' . $this->resource_name . '/(?P\d+)', [ [ 'methods' => 'PATCH', 'callback' => [$this, 'update'], 'permission_callback' => [RsvRestPolicy::class, 'admin'], 'args' => self::input_args(RsvTimetable::schema()), ], [ 'methods' => 'DELETE', 'callback' => [$this, 'destroy'], 'permission_callback' => [RsvRestPolicy::class, 'admin'], ], ]); } public function index(WP_REST_Request $request): WP_REST_Response { [$skip, $limit] = self::paging($request); $service = new RsvTimetableService(); return $this->paged_response($service->get_all($limit, $skip), $service->count_all()); } public function create(WP_REST_Request $request): WP_REST_Response { $service = new RsvTimetableService(); $id = $service->create(new RsvTimetable([ 'name' => $request->get_param('name'), 'block_size' => (int) $request->get_param('block_size'), 'maintainer_email' => $request->get_param('maintainer_email'), ])); return new WP_REST_Response(['id' => $id], 201); } public function update(WP_REST_Request $request): WP_REST_Response { $id = (int) $request->get_param('id'); $service = new RsvTimetableService(); $body = $request->get_json_params(); $timetable = $service->get($id); if ($timetable === null) { return new WP_REST_Response(['error' => 'Not found'], 404); } if (array_key_exists('name', $body)) $timetable->name = $body['name']; if (array_key_exists('block_size', $body)) $timetable->block_size = (int) $body['block_size']; if (array_key_exists('maintainer_email', $body)) $timetable->maintainer_email = $body['maintainer_email'] ?: null; if (array_key_exists('google_calendar_id', $body)) $timetable->google_calendar_id = $body['google_calendar_id'] ?: null; $service->update($id, $timetable); return new WP_REST_Response(['id' => $id], 200); } public function destroy(WP_REST_Request $request): WP_REST_Response { $id = (int) $request->get_param('id'); $service = new RsvTimetableService(); if ($service->get($id) === null) { return new WP_REST_Response(['error' => 'Not found'], 404); } $service->delete($id); return new WP_REST_Response(null, 204); } }