diff --git a/app/Http/Controllers/Admin/DocumentCategoryCrudController.php b/app/Http/Controllers/Admin/DocumentCategoryCrudController.php new file mode 100644 index 0000000..acbe5cd --- /dev/null +++ b/app/Http/Controllers/Admin/DocumentCategoryCrudController.php @@ -0,0 +1,90 @@ +type('number'); + * - CRUD::addColumn(['name' => 'price', 'type' => 'number']); + */ + } + + /** + * Define what happens when the Create operation is loaded. + * + * @see https://backpackforlaravel.com/docs/crud-operation-create + * @return void + */ + protected function setupCreateOperation() + { + CRUD::setValidation(DocumentCategoryRequest::class); + + CRUD::field('title'); + + /** + * Fields can be defined using the fluent syntax or array syntax: + * - CRUD::field('price')->type('number'); + * - CRUD::addField(['name' => 'price', 'type' => 'number'])); + */ + } + + /** + * Define what happens when the Update operation is loaded. + * + * @see https://backpackforlaravel.com/docs/crud-operation-update + * @return void + */ + protected function setupUpdateOperation() + { + $this->setupCreateOperation(); + } + + protected function setupReorderOperation() + { + // define which model attribute will be shown on draggable elements + $this->crud->set('reorder.label', 'title'); + // define how deep the admin is allowed to nest the items + // for infinite levels, set it to 0 + $this->crud->set('reorder.max_level', 1); + } +} diff --git a/app/Http/Controllers/Admin/DocumentCrudController.php b/app/Http/Controllers/Admin/DocumentCrudController.php index e742439..11cf438 100644 --- a/app/Http/Controllers/Admin/DocumentCrudController.php +++ b/app/Http/Controllers/Admin/DocumentCrudController.php @@ -30,6 +30,7 @@ class DocumentCrudController extends CrudController protected function setupListOperation() { // TODO: remove setFromDb() and manually define Columns, maybe Filters + CRUD::column('document_category_id')->type('select')->entity('documentCategory')->model('App\Models\DocumentCategory')->attribute('title'); CRUD::column('title'); CRUD::column('file'); CRUD::column('page'); @@ -40,6 +41,7 @@ class DocumentCrudController extends CrudController $this->crud->setValidation(DocumentRequest::class); // TODO: remove setFromDb() and manually define Fields + CRUD::field('document_category_id')->type('select')->entity('documentCategory')->model('App\Models\DocumentCategory')->attribute('title'); CRUD::field('title'); CRUD::field('file')->type('upload')->upload(true); CRUD::field('page'); diff --git a/app/Http/Controllers/Api/DocumentCategoryController.php b/app/Http/Controllers/Api/DocumentCategoryController.php new file mode 100644 index 0000000..acf198e --- /dev/null +++ b/app/Http/Controllers/Api/DocumentCategoryController.php @@ -0,0 +1,15 @@ +get(); + return $this->respondWithCollection($categories, new CategoryTransformer($this->locale, 'trading')); + } +} diff --git a/app/Http/Controllers/Api/DocumentController.php b/app/Http/Controllers/Api/DocumentController.php index c34d611..7dafd38 100644 --- a/app/Http/Controllers/Api/DocumentController.php +++ b/app/Http/Controllers/Api/DocumentController.php @@ -11,14 +11,19 @@ class DocumentController extends ApiController public function index(Request $request) { $page = $request->page ?? null; + $category = $request->category ?? null; if($page){ - $documents = Document::where('page', $page)->orderBy('lft', 'desc')->get(); - if(count($documents) < 1){ + $documents = Document::where('page', $page)->orderBy('lft', 'desc'); + if(count($documents->get()) < 1){ return $this->errorNotFound(); } }else{ - $documents = Document::orderBy('lft', 'desc')->get(); + $documents = Document::orderBy('lft', 'desc'); } - return $this->respondWithCollection($documents, new DocumentTransformer($this->locale)); + if($category){ + $documents = $documents->where('document_category_id', $category); + } + + return $this->respondWithCollection($documents->get(), new DocumentTransformer($this->locale)); } } diff --git a/app/Http/Requests/DocumentCategoryRequest.php b/app/Http/Requests/DocumentCategoryRequest.php new file mode 100644 index 0000000..7acf224 --- /dev/null +++ b/app/Http/Requests/DocumentCategoryRequest.php @@ -0,0 +1,55 @@ +check(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return [ + // 'name' => 'required|min:5|max:255' + ]; + } + + /** + * Get the validation attributes that apply to the request. + * + * @return array + */ + public function attributes() + { + return [ + // + ]; + } + + /** + * Get the validation messages that apply to the request. + * + * @return array + */ + public function messages() + { + return [ + // + ]; + } +} diff --git a/app/Http/Requests/StoredocumentCategoryRequest.php b/app/Http/Requests/StoredocumentCategoryRequest.php new file mode 100644 index 0000000..55b4de4 --- /dev/null +++ b/app/Http/Requests/StoredocumentCategoryRequest.php @@ -0,0 +1,30 @@ +belongsTo(DocumentCategory::class); + } + } diff --git a/app/Models/DocumentCategory.php b/app/Models/DocumentCategory.php new file mode 100644 index 0000000..f7226ac --- /dev/null +++ b/app/Models/DocumentCategory.php @@ -0,0 +1,22 @@ +hasMany(Document::class); + } +} diff --git a/app/Policies/DocumentCategoryPolicy.php b/app/Policies/DocumentCategoryPolicy.php new file mode 100644 index 0000000..6a695be --- /dev/null +++ b/app/Policies/DocumentCategoryPolicy.php @@ -0,0 +1,94 @@ +id(); + $table->text('title'); + $table->integer('parent_id')->default(0)->nullable(); + $table->integer('lft')->default(0); + $table->integer('rgt')->default(0); + $table->integer('depth')->default(0); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('document_categories'); + } +} diff --git a/database/migrations/2022_12_20_114326_add_category_to_documents_table.php b/database/migrations/2022_12_20_114326_add_category_to_documents_table.php new file mode 100644 index 0000000..0e9e984 --- /dev/null +++ b/database/migrations/2022_12_20_114326_add_category_to_documents_table.php @@ -0,0 +1,35 @@ +after('id', function ($table) { + $table->unsignedBigInteger('document_category_id')->unsigned()->index()->nullable(); + $table->foreign('document_category_id')->references('id')->on('document_categories')->onDelete('cascade'); + }); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('documents', function (Blueprint $table) { + $table->dropColumn('document_category_id'); + }); + } +} diff --git a/database/seeders/DocumentCategorySeeder.php b/database/seeders/DocumentCategorySeeder.php new file mode 100644 index 0000000..b85047c --- /dev/null +++ b/database/seeders/DocumentCategorySeeder.php @@ -0,0 +1,18 @@ + {{trans('backpack::model.multimedia')}} + - diff --git a/routes/api.php b/routes/api.php index ee0a6d5..81c5126 100644 --- a/routes/api.php +++ b/routes/api.php @@ -14,6 +14,7 @@ use App\Http\Controllers\Api\MultimediaCategoryController; use App\Http\Controllers\Api\MultimediaController; use App\Http\Controllers\Api\PageController; use App\Http\Controllers\Api\VideoController; +use App\Http\Controllers\Api\DocumentCategoryController; /* |-------------------------------------------------------------------------- @@ -40,8 +41,10 @@ Route::get('categories/{id}/tradings', [TradingsController::class, 'selectedTrad Route::get('media/categories', [MultimediaCategoryController::class, 'index']); Route::get('medias/{category}', [MultimediaController::class, 'index']); -Route::get('news', [NewsController::class, 'index']); +Route::get('document/categories', [DocumentCategoryController::class, 'index']); Route::get('documents', [DocumentController::class, 'index']); + +Route::get('news', [NewsController::class, 'index']); Route::get('tariffs', [TarifController::class, 'index']); Route::get('contacts', [ContactController::class, 'index']); diff --git a/routes/backpack/custom.php b/routes/backpack/custom.php index 7d1b2fb..e5e2834 100644 --- a/routes/backpack/custom.php +++ b/routes/backpack/custom.php @@ -28,4 +28,5 @@ Route::group([ Route::crud('contact', 'ContactCrudController'); Route::crud('video', 'VideoCrudController'); Route::crud('page', 'PageCrudController'); + Route::crud('document-category', 'DocumentCategoryCrudController'); }); // this should be the absolute last line of this file \ No newline at end of file