searchParamsFetcher = $searchParamsFetcher; $this->recordServiceContainer = $recordServiceContainer; $this->config = $config; $this->user = $user; $this->acl = $acl; parent::__construct( $container, $user, $acl, $aclManager, $config, $preferences, $metadata, $serviceFactory ); } protected function getEntityType(): string { return $this->name; } protected function getRecordService(?string $entityType = null): RecordService { return $this->recordServiceContainer->get($entityType ?? $this->getEntityType()); } /** * Read a record. */ public function getActionRead(Request $request, Response $response): StdClass { if (method_exists($this, 'actionRead')) { // For backward compatibility. return (object) $this->actionRead($request->getRouteParams(), $request->getParsedBody(), $request); } $id = $request->getRouteParam('id'); $entity = $this->getRecordService()->read($id); if (!$entity) { throw new NotFound(); } return $entity->getValueMap(); } /** * Create a record. */ public function postActionCreate(Request $request, Response $response): StdClass { if (method_exists($this, 'actionCreate')) { // For backward compatibility. return (object) $this->actionCreate($request->getRouteParams(), $request->getParsedBody(), $request); } $data = $request->getParsedBody(); $entity = $this->getRecordService()->create($data); return $entity->getValueMap(); } public function patchActionUpdate(Request $request, Response $response): StdClass { return $this->putActionUpdate($request, $response); } /** * Update a record. */ public function putActionUpdate(Request $request, Response $response): StdClass { if (method_exists($this, 'actionUpdate')) { // For backward compatibility. return (object) $this->actionUpdate($request->getRouteParams(), $request->getParsedBody(), $request); } $id = $request->getRouteParam('id'); $data = $request->getParsedBody(); $entity = $this->getRecordService()->update($id, $data); return $entity->getValueMap(); } /** * List records. */ public function getActionList(Request $request, Response $response): StdClass { if (method_exists($this, 'actionList')) { // For backward compatibility. return (object) $this->actionList($request->getRouteParams(), $request->getParsedBody(), $request); } $searchParams = $this->fetchSearchParamsFromRequest($request); $result = $this->getRecordService()->find($searchParams); if ($result instanceof RecordCollection) { return (object) [ 'total' => $result->getTotal(), 'list' => $result->getValueMapList(), ]; } if (is_array($result)) { return (object) [ 'total' => $result['total'], 'list' => isset($result['collection']) ? $result['collection']->getValueMapList() : $result['list'], ]; } return (object) [ 'total' => $result->total, 'list' => isset($result->collection) ? $result->collection->getValueMapList() : $result->list, ]; } /** * Delete a record. */ public function deleteActionDelete(Request $request, Response $response): bool { if (method_exists($this, 'actionDelete')) { // For backward compatibility. return (object) $this->actionDelete($request->getRouteParams(), $request->getParsedBody(), $request); } $id = $request->getRouteParam('id'); $this->getRecordService()->delete($id); return true; } protected function fetchSearchParamsFromRequest(Request $request): SearchParams { return $this->searchParamsFetcher->fetch($request); } public function postActionGetDuplicateAttributes(Request $request): StdClass { $id = $request->getParsedBody()->id ?? null; if (!$id) { throw new BadRequest(); } return $this->getRecordService()->getDuplicateAttributes($id); } public function postActionRestoreDeleted(Request $request): bool { if (!$this->user->isAdmin()) { throw new Forbidden(); } $id = $request->getParsedBody()->id ?? null; if (!$id) { throw new BadRequest(); } $this->getRecordService()->restoreDeleted($id); return true; } /** * @deprecated */ protected function getEntityManager() { return $this->entityManager; } }