phpvms/app/Http/Controllers/Admin/AirlinesController.php

167 lines
4.3 KiB
PHP
Raw Normal View History

2017-06-09 09:02:52 +08:00
<?php
namespace App\Http\Controllers\Admin;
2019-07-16 03:51:35 +08:00
use App\Contracts\Controller;
2018-02-21 12:33:09 +08:00
use App\Http\Requests\CreateAirlineRequest;
use App\Http\Requests\UpdateAirlineRequest;
use App\Repositories\AirlineRepository;
use App\Services\AirlineService;
use App\Support\Countries;
2017-12-31 02:53:16 +08:00
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Laracasts\Flash\Flash;
2017-12-31 02:53:16 +08:00
use Prettus\Repository\Criteria\RequestCriteria;
2017-06-09 09:02:52 +08:00
class AirlinesController extends Controller
2017-06-09 09:02:52 +08:00
{
2017-06-20 00:50:25 +08:00
private $airlineRepo;
private $airlineSvc;
2017-06-09 09:02:52 +08:00
2018-02-21 12:33:09 +08:00
/**
* AirlinesController constructor.
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @param AirlineRepository $airlinesRepo
* @param $airlineSvc
2018-02-21 12:33:09 +08:00
*/
public function __construct(AirlineRepository $airlinesRepo, AirlineService $airlineSvc)
2018-08-27 00:40:04 +08:00
{
2017-06-20 00:50:25 +08:00
$this->airlineRepo = $airlinesRepo;
$this->airlineSvc = $airlineSvc;
2017-06-09 09:02:52 +08:00
}
/**
* Display a listing of the Airlines.
2018-08-27 00:40:04 +08:00
*
* @throws \Prettus\Repository\Exceptions\RepositoryException
2017-06-09 09:02:52 +08:00
*/
public function index(Request $request)
{
2017-06-20 00:50:25 +08:00
$this->airlineRepo->pushCriteria(new RequestCriteria($request));
$airlines = $this->airlineRepo->all();
2017-06-09 09:02:52 +08:00
2018-03-12 06:22:25 +08:00
return view('admin.airlines.index', [
'airlines' => $airlines,
]);
2017-06-09 09:02:52 +08:00
}
/**
* Show the form for creating a new Airlines.
*/
public function create()
{
return view('admin.airlines.create', [
'countries' => Countries::getSelectList(),
]);
2017-06-09 09:02:52 +08:00
}
/**
* Store a newly created Airlines in storage.
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:17:37 +08:00
* @throws \Prettus\Validator\Exceptions\ValidatorException
2017-06-09 09:02:52 +08:00
*/
2017-06-20 00:50:25 +08:00
public function store(CreateAirlineRequest $request)
2017-06-09 09:02:52 +08:00
{
$input = $request->all();
2017-06-20 00:50:25 +08:00
$airlines = $this->airlineRepo->create($input);
2017-06-09 09:02:52 +08:00
Flash::success('Airlines saved successfully.');
return redirect(route('admin.airlines.index'));
2017-06-09 09:02:52 +08:00
}
/**
* Display the specified Airlines.
2018-08-27 00:40:04 +08:00
*
* @param int $id
*
2017-06-11 11:17:45 +08:00
* @return mixed
2017-06-09 09:02:52 +08:00
*/
public function show($id)
{
2017-06-20 00:50:25 +08:00
$airlines = $this->airlineRepo->findWithoutFail($id);
2017-06-09 09:02:52 +08:00
if (empty($airlines)) {
Flash::error('Airlines not found');
return redirect(route('admin.airlines.index'));
2017-06-09 09:02:52 +08:00
}
return view('admin.airlines.show', [
'airlines' => $airlines,
]);
2017-06-09 09:02:52 +08:00
}
/**
* Show the form for editing the specified Airlines.
2018-08-27 00:40:04 +08:00
*
* @param int $id
*
2017-06-09 09:02:52 +08:00
* @return Response
*/
public function edit($id)
{
2017-11-23 10:03:49 +08:00
$airline = $this->airlineRepo->findWithoutFail($id);
2017-06-09 09:02:52 +08:00
2017-11-23 10:03:49 +08:00
if (empty($airline)) {
Flash::error('Airline not found');
return redirect(route('admin.airlines.index'));
2017-06-09 09:02:52 +08:00
}
return view('admin.airlines.edit', [
'airline' => $airline,
'countries' => Countries::getSelectList(),
]);
2017-06-09 09:02:52 +08:00
}
/**
* Update the specified Airlines in storage.
2018-08-27 00:40:04 +08:00
*
* @param int $id
2017-06-20 00:50:25 +08:00
* @param UpdateAirlineRequest $request
2018-08-27 00:40:04 +08:00
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
2018-08-27 00:40:04 +08:00
*
* @return Response
2017-06-09 09:02:52 +08:00
*/
2017-06-20 00:50:25 +08:00
public function update($id, UpdateAirlineRequest $request)
2017-06-09 09:02:52 +08:00
{
2017-06-20 00:50:25 +08:00
$airlines = $this->airlineRepo->findWithoutFail($id);
2017-06-09 09:02:52 +08:00
if (empty($airlines)) {
Flash::error('Airlines not found');
return redirect(route('admin.airlines.index'));
2017-06-09 09:02:52 +08:00
}
2017-06-20 00:50:25 +08:00
$airlines = $this->airlineRepo->update($request->all(), $id);
2017-06-09 09:02:52 +08:00
Flash::success('Airlines updated successfully.');
return redirect(route('admin.airlines.index'));
2017-06-09 09:02:52 +08:00
}
/**
* Remove the specified Airlines from storage.
2018-08-27 00:40:04 +08:00
*
* @param int $id
*
2017-06-09 09:02:52 +08:00
* @return Response
*/
public function destroy($id)
{
$airline = $this->airlineRepo->findWithoutFail($id);
2017-06-09 09:02:52 +08:00
if (empty($airline)) {
2017-06-09 09:02:52 +08:00
Flash::error('Airlines not found');
return redirect(route('admin.airlines.index'));
2017-06-09 09:02:52 +08:00
}
if (!$this->airlineSvc->canDeleteAirline($airline)) {
Flash::error('Airlines cannot be deleted; flights/PIREPs/subfleets exist');
return redirect(route('admin.airlines.index'));
}
2017-06-20 00:50:25 +08:00
$this->airlineRepo->delete($id);
2017-06-09 09:02:52 +08:00
Flash::success('Airlines deleted successfully.');
return redirect(route('admin.airlines.index'));
2017-06-09 09:02:52 +08:00
}
}