Commit 28e5481503ba65efb2aecebb2eae0d23ba7dfe36

Authored by Андрей Ларионов
1 parent 0a94fb81dd
Exists in master

Рефакторинг кода и устранение бага в сортировке данных

Showing 8 changed files with 145 additions and 4 deletions Side-by-side Diff

app/Classes/FilterData.php
... ... @@ -0,0 +1,61 @@
  1 +<?php
  2 +
  3 +
  4 +namespace App\Classes;
  5 +
  6 +
  7 +class FilterData
  8 +{
  9 + protected $builder;
  10 + protected $request;
  11 +
  12 + public function __construct($builder, $request) {
  13 + $this->builder = $builder;
  14 + $this->request = $request;
  15 + }
  16 +
  17 + public function apply() {
  18 + foreach ($this->filters() as $filter => $value) {
  19 + if (method_exists($this, $filter)) {
  20 + $this->filter($value);
  21 + }
  22 + }
  23 + return $this->builder;
  24 + }
  25 +
  26 + public function area($value) {
  27 + return $this->builder->where('area_id', '=', $value);
  28 + }
  29 +
  30 + public function type_area($value) {
  31 + return $this->builder->where('type_area_id', '=', $value);
  32 + }
  33 +
  34 + public function format_area($value) {
  35 + return $this->builder->where('format_area_id', '=', $value);
  36 + }
  37 +
  38 + public function area_m2_min($value) {
  39 + return $this->builder->where('area', '>', $value);
  40 + }
  41 +
  42 + public function area_m2_max($value) {
  43 + return $this->builder->where('area', '<', $value);
  44 + }
  45 +
  46 + public function price_min($value) {
  47 + return $this->builder->where('price', '>', $value);
  48 + }
  49 +
  50 + public function price_max($value) {
  51 + return $this->builder->where('price', '<', $value);
  52 + }
  53 +
  54 + public function address($value) {
  55 + return $this->builder->where('address', 'LIKE', "%".$value."%");
  56 + }
  57 +
  58 + public function filters() {
  59 + return $this->request->all();
  60 + }
  61 +}
app/Classes/SortData.php
... ... @@ -0,0 +1,54 @@
  1 +<?php
  2 +
  3 +
  4 +namespace App\Classes;
  5 +
  6 +
  7 +class SortData
  8 +{
  9 + protected $builder;
  10 + protected $request;
  11 +
  12 + public function __construct($builder, $request) {
  13 + $this->builder = $builder;
  14 + $this->request = $request;
  15 + }
  16 +
  17 + public function apply() {
  18 + foreach ($this->sorter() as $filter => $value) {
  19 + if (method_exists($this, $filter)) {
  20 + $this->filter($value);
  21 + }
  22 + }
  23 + return $this->builder;
  24 + }
  25 +
  26 + public function sort_price($value) {
  27 + switch ($value) {
  28 + case 1: $this->builder = $this->builder->orderBy('price');break;
  29 + case 2: $this->builder = $this->builder->orderByDesc('price');break;
  30 + default: $this->builder = $this->builder->orderBy('price');break;
  31 + }
  32 + }
  33 +
  34 + public function sort_new($value) {
  35 + switch ($value) {
  36 + case 1: $this->builder = $this->builder->orderByDesc('created_at');break;
  37 + case 2: $this->builder = $this->builder->orderBy('created_at');break;
  38 + default: $this->builder = $this->builder->orderByDesc('created_at');break;
  39 + }
  40 + }
  41 +
  42 + public function sort_area($value) {
  43 + switch ($value) {
  44 + case 1: $this->builder = $this->builder->orderByDesc('area');break;
  45 + case 2: $this->builder = $this->builder->orderBy('area');break;
  46 + default: $this->builder = $this->builder->orderByDesc('area');break;
  47 + }
  48 + }
  49 +
  50 + public function sorter() {
  51 + return $this->request->all();
  52 + }
  53 +
  54 +}
app/Http/Controllers/MainController.php
... ... @@ -20,6 +20,7 @@ use Illuminate\Support\Facades\DB;
20 20 use Illuminate\Support\Facades\Mail;
21 21 use PhpParser\Node\Stmt\Switch_;
22 22 use Illuminate\Support\Facades\Response;
  23 +use App\Classes\FilterData;
23 24  
24 25 class MainController extends Controller
25 26 {
... ... @@ -406,6 +407,19 @@ class MainController extends Controller
406 407 return view('category_catalog', compact('title', 'cat', 'houses'));
407 408 }
408 409  
  410 + public function Test(Request $request) {
  411 + $house_arenda = House::with('areas')->
  412 + where('format_house', '=', 'Аренда');
  413 +
  414 + $house_arenda = $house_arenda->orderByDesc('price')->
  415 + orderByDesc('created_at')->
  416 + orderByDesc('area')->get();
  417 +
  418 +
  419 + return view('ajax.complex.arenda', compact('house_arenda'));
  420 +
  421 + }
  422 +
409 423  
410 424 /*
411 425 * Каталог
... ... @@ -416,6 +430,10 @@ class MainController extends Controller
416 430 $house_arenda = House::with('areas')->
417 431 where('format_house', '=', 'Аренда');
418 432 // условия поиска по объектам комплексов
  433 +
  434 + //$house_arenda = (new FilterData($house_arenda, $request))->apply();
  435 +
  436 +
419 437 if (!empty($request->area)) {
420 438 $house_arenda = $house_arenda->where('area_id', '=', $request->area);
421 439 }
... ... @@ -448,6 +466,7 @@ class MainController extends Controller
448 466 $house_arenda = $house_arenda->where('address', 'LIKE', "%".$request->address."%");
449 467 }
450 468  
  469 +
451 470 if ($request->view == 'arenda') {
452 471 switch ($request->sort_price) {
453 472 case 1: $house_arenda = $house_arenda->orderBy('price');break;
app/Providers/ComposerServiceProvider.php
... ... @@ -34,7 +34,8 @@ class ComposerServiceProvider extends ServiceProvider
34 34 }
35 35 );
36 36  
37   - $views = ['layout.site', 'index', 'about', 'house.post', 'catalog', 'category_catalog'];
  37 + $views = ['layout.site', 'index', 'about', 'house.post', 'catalog', 'category_catalog',
  38 + 'catalogs.elemhouse', 'catalogs.house_mini'];
38 39 View::composer($views, function($view) {
39 40 $view->with(['items_contact' => Contact::limit(1)->get()]);
40 41 });
resources/views/catalog.blade.php
... ... @@ -35,7 +35,7 @@
35 35  
36 36 });
37 37  
38   - console.log('-------------------');
  38 + console.log('-----Загрузка ajax аренда---------');
39 39 $.ajax({
40 40 type: "GET",
41 41 url: "{{ route('catalog') }}",
... ... @@ -55,6 +55,7 @@
55 55 }
56 56 });
57 57  
  58 + console.log('---ajax продажи---');
58 59 $.ajax({
59 60 type: "GET",
60 61 url: "{{ route('catalog') }}",
... ... @@ -74,6 +75,7 @@
74 75 }
75 76 });
76 77  
  78 + console.log('---ajax бизнес---');
77 79 $.ajax({
78 80 type: "GET",
79 81 url: "{{ route('catalog') }}",
... ... @@ -93,6 +95,7 @@
93 95 }
94 96 });
95 97  
  98 + console.log('---ajax арендованные---');
96 99 $.ajax({
97 100 type: "GET",
98 101 url: "{{ route('catalog') }}",
... ... @@ -101,7 +104,7 @@
101 104 "&area_m2_min=<? if (isset($_GET['area_m2_min'])) echo $_GET['area_m2_min'];?>&area_m2_max=<? if (isset($_GET['area_m2_max'])) echo $_GET['area_m2_max'];?>"+
102 105 "&price_min=<? if (isset($_GET['price_min'])) echo $_GET['price_min'];?>&price_max=<? if (isset($_GET['price_max'])) echo $_GET['price_max'];?>&address=<? if (isset($_GET['address'])) echo $_GET['address'];?>",
103 106 success: function(data) {
104   - console.log('Успешно обновлены данные таблиц ');
  107 + console.log('Успешно обновлены данные таблиц АРЕНДОВАННЫЕ ');
105 108 $('#arendovannie_block').html(data);
106 109 //.append(data);
107 110 },
resources/views/catalogs/elemhouse.blade.php
... ... @@ -17,7 +17,7 @@
17 17 <use xlink:href="{{ asset('images/sprite.svg#card-favorites') }}"></use>
18 18 </svg>
19 19 </span>
20   - <a class="card__label card__label-messenger" href="tel:{{ $items_contact[0]->telephone }}">
  20 + <a class="card__label card__label-messenger" href="tel:<?=$items_contact[0]->telephone;?>">
21 21 <svg width="25" height="25">
22 22 <use xlink:href="{{ asset('images/sprite.svg#card-messenger') }}"></use>
23 23 </svg></a></div>
resources/views/layout/site.blade.php
... ... @@ -4,6 +4,7 @@
4 4 <meta charset="UTF-8">
5 5 <meta http-equiv="X-UA-Compatible" content="IE=edge">
6 6 <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7 + <meta name="csrf-token" content="<?=csrf_token() ?>">
7 8 <title>{{$title}}</title>
8 9 <link rel="shortcut icon" href="{{ asset('images/favicon.png')}}" type="image/x-icon">
9 10 <link rel="preload" href="{{ asset('fonts/Manrope-ExtraLight.woff2') }}" as="font" type="font/woff2" crossorigin>
... ... @@ -37,6 +37,8 @@ Route::get(&#39;contact&#39;,[MainController::class, &#39;contact&#39;])-&gt;name(&#39;contact&#39;);
37 37 //Страница каталог
38 38 Route::get('catalog',[MainController::class, 'catalog'])->name('catalog');
39 39  
  40 +Route::get('test', [MainController::class, 'test'])->name('test');
  41 +
40 42 //Страница новости
41 43 Route::get('news',[MainController::class, 'news'])->name('news');
42 44