From 5854d86875dc378274b3a798d90f4943eb042ca8 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Thu, 20 Feb 2025 19:20:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86categoryId=E7=9B=B8=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Admin/Services/Article.php | 5 ++--- app/Http/Admin/Services/Help.php | 7 ++----- app/Http/Admin/Services/Question.php | 5 ++--- app/Services/Logic/Article/ArticleDataTrait.php | 3 +-- .../Logic/Question/QuestionDataTrait.php | 3 +-- app/Validators/Article.php | 12 +++++++++--- app/Validators/Course.php | 16 ++++++++++------ app/Validators/Help.php | 12 +++++++++--- app/Validators/Question.php | 12 +++++++++--- 9 files changed, 45 insertions(+), 30 deletions(-) diff --git a/app/Http/Admin/Services/Article.php b/app/Http/Admin/Services/Article.php index 0b8dd5ae..9f516762 100644 --- a/app/Http/Admin/Services/Article.php +++ b/app/Http/Admin/Services/Article.php @@ -199,9 +199,8 @@ class Article extends Service $data['published'] = $validator->checkPublishStatus($post['published']); } - if (isset($post['category_id']) && !empty($post['category_id'])) { - $category = $validator->checkCategory($post['category_id']); - $data['category_id'] = $category->id; + if (isset($post['category_id'])) { + $data['category_id'] = $validator->checkCategoryId($post['category_id']); } if (isset($post['xm_tag_ids'])) { diff --git a/app/Http/Admin/Services/Help.php b/app/Http/Admin/Services/Help.php index 0a681b20..2fde91de 100644 --- a/app/Http/Admin/Services/Help.php +++ b/app/Http/Admin/Services/Help.php @@ -63,12 +63,10 @@ class Help extends Service $data = []; - $category = $validator->checkCategory($post['category_id']); - $data['title'] = $validator->checkTitle($post['title']); $data['content'] = $validator->checkContent($post['content']); $data['priority'] = $validator->checkPriority($post['priority']); - $data['category_id'] = $category->id; + $data['category_id'] = $validator->checkCategoryId($post['category_id']); $help = new HelpModel(); @@ -88,8 +86,7 @@ class Help extends Service $data = []; if (isset($post['category_id'])) { - $category = $validator->checkCategory($post['category_id']); - $data['category_id'] = $category->id; + $data['category_id'] = $validator->checkCategoryId($post['category_id']); } if (isset($post['title'])) { diff --git a/app/Http/Admin/Services/Question.php b/app/Http/Admin/Services/Question.php index 87ca6273..5dd98be7 100644 --- a/app/Http/Admin/Services/Question.php +++ b/app/Http/Admin/Services/Question.php @@ -186,9 +186,8 @@ class Question extends Service $data['published'] = $validator->checkPublishStatus($post['published']); } - if (isset($post['category_id']) && !empty($post['category_id'])) { - $category = $validator->checkCategory($post['category_id']); - $data['category_id'] = $category->id; + if (isset($post['category_id'])) { + $data['category_id'] = $validator->checkCategoryId($post['category_id']); } if (isset($post['xm_tag_ids'])) { diff --git a/app/Services/Logic/Article/ArticleDataTrait.php b/app/Services/Logic/Article/ArticleDataTrait.php index 5128c79f..73ceb94a 100644 --- a/app/Services/Logic/Article/ArticleDataTrait.php +++ b/app/Services/Logic/Article/ArticleDataTrait.php @@ -34,8 +34,7 @@ trait ArticleDataTrait $data['content'] = $validator->checkContent($post['content']); if (isset($post['category_id'])) { - $category = $validator->checkCategory($post['category_id']); - $data['category_id'] = $category->id; + $data['category_id'] = $validator->checkCategoryId($post['category_id']); } if (isset($post['source_type'])) { diff --git a/app/Services/Logic/Question/QuestionDataTrait.php b/app/Services/Logic/Question/QuestionDataTrait.php index 66a13906..fc73362b 100644 --- a/app/Services/Logic/Question/QuestionDataTrait.php +++ b/app/Services/Logic/Question/QuestionDataTrait.php @@ -33,8 +33,7 @@ trait QuestionDataTrait $data['content'] = $validator->checkContent($post['content']); if (isset($post['category_id'])) { - $category = $validator->checkCategory($post['category_id']); - $data['category_id'] = $category->id; + $data['category_id'] = $validator->checkCategoryId($post['category_id']); } if (isset($post['closed'])) { diff --git a/app/Validators/Article.php b/app/Validators/Article.php index 2d29ec76..cbbe4acc 100644 --- a/app/Validators/Article.php +++ b/app/Validators/Article.php @@ -66,11 +66,17 @@ class Article extends Validator } } - public function checkCategory($id) + public function checkCategoryId($id) { - $validator = new Category(); + $result = 0; - return $validator->checkCategory($id); + if ($id > 0) { + $validator = new Category(); + $category = $validator->checkCategory($id); + $result = $category->id; + } + + return $result; } public function checkTitle($title) diff --git a/app/Validators/Course.php b/app/Validators/Course.php index 2a3ad61c..292c3c02 100644 --- a/app/Validators/Course.php +++ b/app/Validators/Course.php @@ -68,24 +68,28 @@ class Course extends Validator public function checkCategoryId($id) { + $result = 0; + if ($id > 0) { $validator = new Category(); $category = $validator->checkCategory($id); - return $category->id; - } else { - return 0; + $result = $category->id; } + + return $result; } public function checkTeacherId($id) { + $result = 0; + if ($id > 0) { $validator = new User(); $user = $validator->checkTeacher($id); - return $user->id; - } else { - return 0; + $result = $user->id; } + + return $result; } public function checkModel($model) diff --git a/app/Validators/Help.php b/app/Validators/Help.php index 63b715f7..e160573c 100644 --- a/app/Validators/Help.php +++ b/app/Validators/Help.php @@ -27,11 +27,17 @@ class Help extends Validator return $help; } - public function checkCategory($id) + public function checkCategoryId($id) { - $validator = new Category(); + $result = 0; - return $validator->checkCategory($id); + if ($id > 0) { + $validator = new Category(); + $category = $validator->checkCategory($id); + $result = $category->id; + } + + return $result; } public function checkTitle($title) diff --git a/app/Validators/Question.php b/app/Validators/Question.php index 28ac33ad..e6099cd7 100644 --- a/app/Validators/Question.php +++ b/app/Validators/Question.php @@ -65,11 +65,17 @@ class Question extends Validator } } - public function checkCategory($id) + public function checkCategoryId($id) { - $validator = new Category(); + $result = 0; - return $validator->checkCategory($id); + if ($id > 0) { + $validator = new Category(); + $category = $validator->checkCategory($id); + $result = $category->id; + } + + return $result; } public function checkTitle($title)