diff --git a/app/controller/ArticleCategoryController.php b/app/controller/ArticleCategoryController.php new file mode 100644 index 0000000..07e8a09 --- /dev/null +++ b/app/controller/ArticleCategoryController.php @@ -0,0 +1,53 @@ + '', + ]); + validate(ArticleCategoryValidate::class)->check($params, 'save'); + return success($service->saveData($params)); + } + + public function update(ArticleCategoryService $service): Response + { + $params = requestOnly([ + 'id' => '', + 'name' => '', + ]); + validate(ArticleCategoryValidate::class)->check($params, 'update'); + return success($service->updateData(ArrayUtils::filterNotEmpty($params))); + } + + public function index(ArticleCategoryService $service): Response + { + $params = requestOnly([ + 'name' => '', + 'begin_time' => '', + 'end_time' => '' + ]); + return success($service->listData($params)); + } + + public function read(ArticleCategoryService $service): Response + { + $id = input('id'); + return success($service->readData($id)); + } + + public function delete(ArticleCategoryService $service): Response + { + $id = input('id'); + return success($service->deleteData($id)); + } + + +} \ No newline at end of file diff --git a/app/route/route.php b/app/route/route.php index 651b105..114c8ae 100644 --- a/app/route/route.php +++ b/app/route/route.php @@ -1,6 +1,7 @@ setParams(['perm' => ['creationTaskDelete']]); }); + + //文章发呢类 + Route::group('/articleCategory', function () { + //新增 + Route::post('/save', [ArticleCategoryController::class, 'save'])->setParams(['perm' => ['articleCategorySave']]); + //修改 + Route::post('/update', [ArticleCategoryController::class, 'update'])->setParams(['perm' => ['articleCategoryUpdate']]); + //列表 + Route::get('/index', [ArticleCategoryController::class, 'index'])->setParams(['perm' => ['articleCategoryIndex']]); + //详情 + Route::get('/read', [ArticleCategoryController::class, 'read'])->setParams(['perm' => ['articleCategoryRead']]); + //删除 + Route::post('/delete', [ArticleCategoryController::class, 'delete'])->setParams(['perm' => ['articleCategoryDelete']]); + }); }); })->middleware([ AdminAuthorizationMiddleware::class, diff --git a/app/service/ArticleCategoryService.php b/app/service/ArticleCategoryService.php new file mode 100644 index 0000000..23e41c1 --- /dev/null +++ b/app/service/ArticleCategoryService.php @@ -0,0 +1,114 @@ +dao = $dao; + } + + /** + * 保存信息 + * @param array $params + * @return array + */ + public function saveData(array $params): array + { + Db::startTrans(); + try { + $data = $this->dao->save($params); + Db::commit(); + } catch (\Exception $exception) { + Db::rollback(); + throw new ApiException($exception->getMessage()); + } + return $data->toArray(); + } + + /** + * 修改信息 + * @param array $params + * @return array + */ + public function updateData(array $params): array + { + // 落库 + Db::startTrans(); + try { + $this->dao->update(['id' => $params['id']], $params); + Db::commit(); + } catch (\Exception $exception) { + Db::rollback(); + throw new ApiException($exception->getMessage()); + } + return $params; + } + + /** + * 获取列表 + * @param array $params + * @return array + */ + public function listData(array $params): array + { + $query = [ + 'delete_time' => 0 + ]; + if (isNotBlank($params['name'])) { + $query[] = ['name', 'like', '%' . $params['name'] . '%']; + } + if (isNotBlank($params['begin_time'])) { + $query[] = ['create_time', '>=', strtotime($params['begin_time'])]; + } + if (isNotBlank($params['end_time'])) { + $query[] = ['create_time', '<=', strtotime($params['end_time'] . ' 23:59:59')]; + } + $list = $this->dao->getList($query); + $count = $this->dao->getCount($query); + return compact('list', 'count'); + } + + /** + * 获取信息 + * @param mixed $id + * @return array + */ + public function readData(mixed $id): array + { + $category = $this->dao->get(['id' => $id]); + if (empty($category)) { + throw new ApiException('数据不存在'); + } + return $category->toArray(); + } + + /** + * 删除信息 + * @param mixed $id + * @return array + */ + public function deleteData(mixed $id): array + { + // 落库 + Db::startTrans(); + try { + $this->dao->update($id, ['delete_time' => time()]); + Db::commit(); + } catch (\Exception $exception) { + Db::rollback(); + throw new ApiException($exception->getMessage()); + } + return ['id' => $id]; + } + +} \ No newline at end of file diff --git a/app/validate/ArticleCategoryValidate.php b/app/validate/ArticleCategoryValidate.php new file mode 100644 index 0000000..2c6ad4f --- /dev/null +++ b/app/validate/ArticleCategoryValidate.php @@ -0,0 +1,22 @@ + [ + 'name' => 'require', + ], + 'update' => [ + 'id' => 'require', + 'name' => 'require', + ], + ]; + protected $message = [ + 'id.require' => 'ID不能为空', + 'name.require' => '文章分类不能为空', + ]; +} \ No newline at end of file