Created
September 10, 2020 19:45
-
-
Save valex/5d804d541f566aa8256db33f114531aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\Http\Requests\UpdateLineRequest; | |
use App\Repositories\CountriesRepository; | |
use App\Repositories\LineRepository; | |
use App\Repositories\SettingsRepository; | |
use App\Repositories\ViewRepository; | |
use App\Line; | |
use App\Transformers\LineFullTransformer; | |
use App\Transformers\StationTransformer; | |
use Illuminate\Http\Request; | |
class LinesController extends Controller | |
{ | |
protected $countriesRepository; | |
protected $lineRepository; | |
protected $settingsRepository; | |
protected $viewRepository; | |
/** | |
* Create a new controller instance. | |
* | |
* @return void | |
*/ | |
public function __construct(CountriesRepository $countriesRepository, | |
LineRepository $lineRepository, | |
SettingsRepository $settingsRepository, | |
ViewRepository $viewRepository) | |
{ | |
$this->countriesRepository = $countriesRepository; | |
$this->lineRepository = $lineRepository; | |
$this->settingsRepository = $settingsRepository; | |
$this->viewRepository = $viewRepository; | |
} | |
public function show(Line $line){ | |
$stations = $this->lineRepository->stations($line); | |
$take = $this->settingsRepository->get('page_size'); | |
$line->load(['views' => function ($query) use($take) { | |
$query->orderBy('video_quality', 'asc'); | |
$query->orderBy('duration', 'desc'); | |
$query->with('video'); | |
$query->take($take); | |
}]); | |
$orders = [ | |
'video_quality' => 'asc', | |
'duration' => 'desc' | |
]; | |
$views = $this->viewRepository->viewsForLine($line, [ | |
'take' => $take, | |
'skip' => 0, | |
'orders' => $orders, | |
'with' => ['video'] | |
]); | |
$data['views'] = $views; | |
$data['meta'] = [ | |
'title' => $line->name().', '.trans('main.line'), | |
'description' => 'Все станции и остановочные пункты на участке '.$line->name().'. Путешествие по участку железной дороги '.$line->name(), | |
]; | |
$data['line'] = $line; | |
$data['stations'] = $stations; | |
return view('lines.show', $data); | |
} | |
public function stationsForLine(Line $line){ | |
$order = 'asc'; | |
if(request('order') == 'desc'){ | |
$order = 'desc'; | |
} | |
$stations = $line->stations()->orderBy('distance_marker', $order)->get(); | |
return response()->json(fractal($stations, new StationTransformer())); | |
} | |
public function edit(Line $line){ | |
$data = [ | |
'meta' => [ | |
'title' => trans('main.Edit line') | |
], | |
]; | |
$data['line'] = $line; | |
$data['countriesSelectSource'] = $this->countriesRepository->selectSource(); | |
return view('lines.edit', $data); | |
} | |
public function update(UpdateLineRequest $request, Line $line){ | |
$stations = request('stations'); | |
// check the same timestamps | |
$hasTheSameMarks = false; | |
$sameMark = 0; | |
foreach ($stations as $key_i => $station_i){ | |
foreach ($stations as $key_j => $station_j){ | |
if($key_i == $key_j) | |
continue; | |
if($station_i['distance_marker'] == $station_j['distance_marker']){ | |
$hasTheSameMarks = true; | |
$sameMark = $station_i['distance_marker']; | |
break 2; | |
} | |
} | |
} | |
if(true === $hasTheSameMarks){ | |
$message = trans('main.Distance marks cannot be the same', ['mark' => $sameMark]); | |
return response()->json([ | |
'stations' => [$message] | |
], 422); | |
} | |
// SUCCESS | |
$line->country_id = request('country_id'); | |
$line->name = request('name'); | |
$line->insignificant = request('insignificant'); | |
$line->save(); | |
$stationsForSync = []; | |
foreach ($stations as $station){ | |
$stationsForSync[$station['id']] = [ | |
'distance_marker' => $station['distance_marker'] | |
]; | |
} | |
$line->stations()->sync($stationsForSync); | |
return response()->json(fractal($line, new LineFullTransformer())); | |
} | |
public function getFull(Line $line){ | |
return response()->json(fractal($line, new LineFullTransformer())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment