model = ColorcardModel::class; $this->selectField = ['id', 'card_class', 'company', 'price', 'description', 'cover', 'status', 'created_at', 'updated_at']; $this->queryField = ['card_class' => '=', 'company' => '=', 'description' => 'like', 'status' => '=']; $this->media = MediaUrlService::getInstance(); } /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ public function list(): array { $this->with = ['cardClassInfo', 'companyInfo']; $result = $this->getPageList(); foreach ($result['items'] as &$item) { $item['card_class_name'] = $item['card_class_info']['name'] ?? ''; $item['company_name'] = $item['company_info']['name'] ?? ''; unset($item['card_class_info'], $item['company_info']); $item['cover'] = $this->media->toPublic($item['cover'] ?? ''); } unset($item); return $result; } public function option(): mixed { $this->optionField = ['id', 'description as name']; return $this->getOption(); } /** * @throws Exception */ public function detail($id): mixed { $info = $this->getDetail($id); $info->cover = $this->media->toPublic($info->cover); return $info; } public function create($params): mixed { if (array_key_exists('cover', $params)) { $params['cover'] = $this->media->firstOf($params['cover']); } return $this->insert($params); } /** * @throws Exception */ public function update($id, $params): mixed { if (array_key_exists('cover', $params)) { $params['cover'] = $this->media->firstOf($params['cover']); } return $this->save($id, $params); } /** * @throws Exception */ public function delete($id): mixed { return $this->del(is_array($id) ? $id : [$id]); } /** * @throws Exception */ public function status($id, $status): mixed { return $this->save($id, ['status' => (int) $status]); } }