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

201 lines
5.2 KiB
PHP
Raw Normal View History

<?php
2017-06-11 11:17:45 +08:00
namespace App\Http\Controllers\Admin;
use App\Http\Requests\CreateFareRequest;
2018-03-23 06:48:57 +08:00
use App\Http\Requests\ImportRequest;
use App\Http\Requests\UpdateFareRequest;
use App\Interfaces\Controller;
use App\Repositories\FareRepository;
2018-03-23 06:48:57 +08:00
use App\Services\ExportService;
use App\Services\ImportService;
use Flash;
2018-02-21 12:33:09 +08:00
use Illuminate\Http\Request;
2018-03-23 06:48:57 +08:00
use Log;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;
2018-03-23 06:48:57 +08:00
use Storage;
/**
* Class FareController
* @package App\Http\Controllers\Admin
*/
class FareController extends Controller
{
2018-03-23 06:48:57 +08:00
private $fareRepo,
$importSvc;
2018-02-21 12:33:09 +08:00
/**
* FareController constructor.
* @param FareRepository $fareRepo
2018-03-23 06:48:57 +08:00
* @param ImportService $importSvc
2018-02-21 12:33:09 +08:00
*/
public function __construct(
2018-03-23 06:48:57 +08:00
FareRepository $fareRepo,
ImportService $importSvc
2018-02-21 12:33:09 +08:00
) {
2018-03-23 06:48:57 +08:00
$this->fareRepo = $fareRepo;
$this->importSvc = $importSvc;
}
/**
* Display a listing of the Fare.
* @param Request $request
* @return Response
2018-02-21 12:33:09 +08:00
* @throws \Prettus\Repository\Exceptions\RepositoryException
*/
public function index(Request $request)
{
2018-03-23 06:48:57 +08:00
$this->fareRepo->pushCriteria(new RequestCriteria($request));
$fares = $this->fareRepo->all();
return view('admin.fares.index')
->with('fares', $fares);
}
/**
* Show the form for creating a new Fare.
*
* @return Response
*/
public function create()
{
return view('admin.fares.create');
}
/**
* Store a newly created Fare in storage.
* @param CreateFareRequest $request
* @return Response
2018-02-21 12:33:09 +08:00
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/
public function store(CreateFareRequest $request)
{
$input = $request->all();
2018-03-23 06:48:57 +08:00
$fare = $this->fareRepo->create($input);
2018-03-23 06:48:57 +08:00
Flash::success('Fare saved successfully.');
return redirect(route('admin.fares.index'));
}
/**
* Display the specified Fare.
* @param int $id
* @return Response
*/
public function show($id)
{
2018-03-23 06:48:57 +08:00
$fare = $this->fareRepo->findWithoutFail($id);
if (empty($fare)) {
Flash::error('Fare not found');
return redirect(route('admin.fares.index'));
}
return view('admin.fares.show')->with('fare', $fare);
}
/**
* Show the form for editing the specified Fare.
* @param int $id
* @return Response
*/
public function edit($id)
{
2018-03-23 06:48:57 +08:00
$fare = $this->fareRepo->findWithoutFail($id);
if (empty($fare)) {
Flash::error('Fare not found');
return redirect(route('admin.fares.index'));
}
return view('admin.fares.edit')->with('fare', $fare);
}
/**
* Update the specified Fare in storage.
* @param int $id
* @param UpdateFareRequest $request
* @return Response
2018-02-21 12:33:09 +08:00
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/
public function update($id, UpdateFareRequest $request)
{
2018-03-23 06:48:57 +08:00
$fare = $this->fareRepo->findWithoutFail($id);
if (empty($fare)) {
Flash::error('Fare not found');
return redirect(route('admin.fares.index'));
}
2017-06-11 11:17:45 +08:00
2018-03-23 06:48:57 +08:00
$fare = $this->fareRepo->update($request->all(), $id);
2018-03-23 06:48:57 +08:00
Flash::success('Fare updated successfully.');
return redirect(route('admin.fares.index'));
}
/**
* Remove the specified Fare from storage.
* @param int $id
* @return Response
*/
public function destroy($id)
{
2018-03-23 06:48:57 +08:00
$fare = $this->fareRepo->findWithoutFail($id);
if (empty($fare)) {
Flash::error('Fare not found');
return redirect(route('admin.fares.index'));
}
2017-06-11 11:17:45 +08:00
2018-03-23 06:48:57 +08:00
$this->fareRepo->delete($id);
2018-03-23 06:48:57 +08:00
Flash::success('Fare deleted successfully.');
return redirect(route('admin.fares.index'));
}
2018-03-23 06:48:57 +08:00
/**
* Run the aircraft exporter
* @param Request $request
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
* @throws \League\Csv\Exception
*/
public function export(Request $request)
{
$exporter = app(ExportService::class);
$fares = $this->fareRepo->all();
$path = $exporter->exportFares($fares);
return response()
->download($path, 'fares.csv', [
'content-type' => 'text/csv',
])
->deleteFileAfterSend(true);
}
/**
*
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws \Illuminate\Validation\ValidationException
*/
public function import(Request $request)
{
$logs = [
'success' => [],
'failed' => [],
];
if ($request->isMethod('post')) {
ImportRequest::validate($request);
$path = Storage::putFileAs(
'import', $request->file('csv_file'), 'import_fares.csv'
);
$path = storage_path('app/'.$path);
Log::info('Uploaded fares import file to '.$path);
$logs = $this->importSvc->importFares($path);
}
return view('admin.fares.import', [
'logs' => $logs,
]);
}
}