From 576658cd2c91414dc13e8b1b96a61cc7aad4ad98 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Thu, 20 May 2021 23:30:34 +0800 Subject: [PATCH] SearchBoxRepository --- app/Console/Commands/Test.php | 6 +- app/Exceptions/Handler.php | 9 ++- app/Models/Category.php | 2 + app/Models/{Searchbox.php => SearchBox.php} | 15 +++- app/Models/SearchBoxField.php | 40 ++++++++++ app/Models/SearchboxField.php | 15 ---- app/Repositories/SearchBoxRepository.php | 84 +++++++++++++++++++++ lang/en/lang_admanage.php | 2 +- lang/en/lang_catmanage.php | 4 +- resources/lang/zh_CN/torrent.php | 4 +- 10 files changed, 156 insertions(+), 25 deletions(-) rename app/Models/{Searchbox.php => SearchBox.php} (50%) create mode 100644 app/Models/SearchBoxField.php delete mode 100644 app/Models/SearchboxField.php create mode 100644 app/Repositories/SearchBoxRepository.php diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index 49927b8c..8a335748 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -7,6 +7,7 @@ use App\Models\ExamProgress; use App\Models\ExamUser; use App\Models\User; use App\Repositories\ExamRepository; +use App\Repositories\SearchBoxRepository; use Carbon\Carbon; use Illuminate\Console\Command; use Illuminate\Support\Facades\DB; @@ -61,7 +62,10 @@ class Test extends Command // $r = $disk->put('/', base_path('composer.json')); // $r = DB::table('users')->where('id', 1)->update(['modcomment' => DB::raw("concat_ws(',', 'ddddd', modcomment)")]); - $r = format_description('[em4] [em27]'); +// $r = format_description('[em4] [em27]'); + + $rep = new SearchBoxRepository(); + $r = $rep->initSearchBoxField(4); dd($r); } diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 2bb44954..bdf56389 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -76,15 +76,18 @@ class Handler extends ExceptionHandler protected function prepareJsonResponse($request, Throwable $e) { $data = $request->all(); - if (config('app.debug')) { + $httpStatusCode = $this->getHttpStatusCode($e); + if ($httpStatusCode == 200) { $msg = $e->getMessage() ?: get_class($e); - $data['trace'] = $e->getTraceAsString(); } else { $msg = 'Server Error'; } + if (config('app.debug')) { + $data['trace'] = $e->getTraceAsString(); + } return new JsonResponse( fail($msg, $data), - $this->getHttpStatusCode($e), + $httpStatusCode, $this->isHttpException($e) ? $e->getHeaders() : [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ); diff --git a/app/Models/Category.php b/app/Models/Category.php index d357708f..6801cfe3 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -5,5 +5,7 @@ namespace App\Models; class Category extends NexusModel { + protected $table = 'categories'; + protected $fillable = ['mode', 'name', 'class_name', 'image', 'sort_index', 'icon_id']; } diff --git a/app/Models/Searchbox.php b/app/Models/SearchBox.php similarity index 50% rename from app/Models/Searchbox.php rename to app/Models/SearchBox.php index 331693ec..a32ce336 100644 --- a/app/Models/Searchbox.php +++ b/app/Models/SearchBox.php @@ -2,7 +2,7 @@ namespace App\Models; -class Searchbox extends NexusModel +class SearchBox extends NexusModel { protected $table = 'searchbox'; @@ -12,4 +12,17 @@ class Searchbox extends NexusModel 'custom_fields', 'custom_fields_display_name', 'custom_fields_display' ]; + public function categories(): \Illuminate\Database\Eloquent\Relations\HasMany + { + return $this->hasMany(Category::class, 'mode'); + } + + public function normal_fields(): \Illuminate\Database\Eloquent\Relations\HasMany + { + return $this->hasMany(SearchBoxField::class, 'searchbox_id'); + } + + + + } diff --git a/app/Models/SearchBoxField.php b/app/Models/SearchBoxField.php new file mode 100644 index 00000000..db0de371 --- /dev/null +++ b/app/Models/SearchBoxField.php @@ -0,0 +1,40 @@ + ['text' => 'Source', 'model' => Source::class], + self::FIELD_TYPE_MEDIUM => ['text' => 'Medium', 'model' => Media::class], + self::FIELD_TYPE_CODEC => ['text' => 'Codec', 'model' => Codec::class], + self::FIELD_TYPE_AUDIO_CODEC => ['text' => 'Audio codec', 'model' => AudioCodec::class], + self::FIELD_TYPE_STANDARD => ['text' => 'Standard', 'model' => Standard::class], + self::FIELD_TYPE_PROCESSING => ['text' => 'Processing', 'model' => Processing::class], + self::FIELD_TYPE_TEAM => ['text' => 'Team', 'model' => Team::class], + self::FIELD_TYPE_CUSTOM => ['text' => 'Custom', ], + ]; + + + public function searchBox(): \Illuminate\Database\Eloquent\Relations\BelongsTo + { + return $this->belongsTo(SearchBox::class, 'searchbox_id'); + } + + + + +} diff --git a/app/Models/SearchboxField.php b/app/Models/SearchboxField.php deleted file mode 100644 index f95f3165..00000000 --- a/app/Models/SearchboxField.php +++ /dev/null @@ -1,15 +0,0 @@ -belongsTo(Searchbox::class, 'searchbox_id'); - } -} diff --git a/app/Repositories/SearchBoxRepository.php b/app/Repositories/SearchBoxRepository.php new file mode 100644 index 00000000..d6d081b2 --- /dev/null +++ b/app/Repositories/SearchBoxRepository.php @@ -0,0 +1,84 @@ +getSortFieldAndType($params); + $query->orderBy($sortField, $sortType); + return $query->paginate(); + } + + public function store(array $params) + { + $result = SearchBox::query()->create($params); + return $result; + } + + public function update(array $params, $id) + { + $result = SearchBox::query()->findOrFail($id); + $result->update($params); + return $result; + } + + public function getDetail($id) + { + $result = SearchBox::query()->findOrFail($id); + return $result; + } + + public function delete($id) + { + $result = SearchBox::query()->findOrFail($id); + $success = $result->delete(); + return $success; + } + + public function buildSearchBox($id) + { + $searchBox = SearchBox::query()->with(['categories', 'normal_fields'])->findOrFail($id); + $fieldData = []; + foreach ($searchBox->normal_fields as $normalField) { + $fieldType = $normalField->field_type; + $info = SearchBoxField::$fieldTypes[$fieldType] ?? null; + if ($info) { + /** @var NexusModel $model */ + $model = new $info[$fieldType]['model']; + $fieldData[$fieldType] = $model::query()->get(); + } + } + $searchBox->setRelation('normal_fields_data', $fieldData); + return $searchBox; + } + + public function initSearchBoxField($id) + { + $searchBox = SearchBox::query()->findOrFail($id); + foreach (SearchBoxField::$fieldTypes as $fieldType => $info) { + if ($fieldType == SearchBoxField::FIELD_TYPE_CUSTOM) { + continue; + } + $name = str_replace('_', '', "show{$fieldType}"); + $log = "name: $name, fieldType: $fieldType"; + $searchBox->normal_fields()->where('field_type', $fieldType)->delete(); + if ($searchBox->{$name}) { + $searchBox->normal_fields()->create([ + 'field_type' => $fieldType, + ]); + do_log("$log, create."); + } else { + do_log("$log, delete."); + } + } + } + + +} diff --git a/lang/en/lang_admanage.php b/lang/en/lang_admanage.php index 1dfdf6d7..228c4cf3 100644 --- a/lang/en/lang_admanage.php +++ b/lang/en/lang_admanage.php @@ -8,7 +8,7 @@ $lang_admanage = array 'text_header' => "Header", 'text_footer' => "Footer", 'text_below_navigation' => "Below Navigation", - 'text_below_searchbox' => "Below Searchbox", + 'text_below_searchbox' => "Below SearchBox", 'text_torrent_detail' => "Torrent Detail", 'text_comment_page' => "Comment Page", 'text_inter_overforums' => "Inter Overforums", diff --git a/lang/en/lang_catmanage.php b/lang/en/lang_catmanage.php index d833338e..598edd68 100644 --- a/lang/en/lang_catmanage.php +++ b/lang/en/lang_catmanage.php @@ -5,7 +5,7 @@ $lang_catmanage = array 'head_category_management' => "Category Management", 'text_category_management' => "Category Management", 'text_manage' => "Manage", - 'text_searchbox' => "Searchbox", + 'text_searchbox' => "SearchBox", 'text_category_icons' => "Category icon pack", 'text_second_icons' => "Second icons", 'text_categories' => "Categories", @@ -49,7 +49,7 @@ $lang_catmanage = array 'submit_submit' => "Submit", 'text_subcategory_name_note' => "Don't use long name. Recommend less than 10 letters.", 'text_order_note' => "Ascendantly, i.e. '0' comes first.", - 'row_searchbox_name' => "Searchbox Name", + 'row_searchbox_name' => "SearchBox Name", 'text_searchbox_name_note' => "Allowed Characters: [a-z] (in lower case), [0-9], [_./].", 'row_show_sub_category' => "Show sub-category", 'text_show_sub_category_note' => "Check the sub-categories you want to enable.", diff --git a/resources/lang/zh_CN/torrent.php b/resources/lang/zh_CN/torrent.php index 7a909dee..f0dc4975 100644 --- a/resources/lang/zh_CN/torrent.php +++ b/resources/lang/zh_CN/torrent.php @@ -15,9 +15,9 @@ return [ 'basic_team' => '制作组', 'size' => '大小', 'comments_label' => '评论', - 'times_completed_label' => '完成次数', + 'times_completed_label' => '完成', 'peers_count_label' => '同伴', - 'thank_users_count_label' => '说谢谢', + 'thank_users_count_label' => '谢谢', 'numfiles_label' => '文件', ] ];