|
|
<?php
namespace App\Http\Controllers;
use App\SearchDisplace\Searchers\SearcherFactory; use App\SearchDisplace\Searchers\SearchersCollection; use App\SearchDisplace\Searchers\SearchersStorage;
class SearcherController extends Controller { public function index() { // @TODO Add filters
if (request()->has('q')) { $searchers = []; } else { $searchers = (new SearchersCollection())->all(); }
if (request()->wantsJson()) { return response()->json([ 'searchers' => $searchers, ], 200); }
return view('pages.searchers.index', [ 'searchers' => $searchers, ]); }
public function create() { return view('pages.searchers.create'); }
public function store() { request()->validate([ 'name' => 'required', 'tag' => 'nullable', 'rows' => 'required|array', 'rows.*' => 'array', ]);
try { $factory = new SearcherFactory( request()->get('name'), request()->get('rows'), request()->get('tag') );
$searcher = $factory->create();
return response()->json([ 'status' => 'success', 'searcher' => $searcher, ], 200); } catch (\Exception $exception) { return response()->json([ 'status' => 'fail', 'trace' => $exception->getTrace(), 'message' => $exception->getMessage(), ], 400); } }
public function show($id) { $searchersCollection = new SearchersCollection();
try { $searcher = $searchersCollection->get($id);
if ( ! $searcher) { abort(404); }
if (request()->wantsJson()) { return response()->json([ 'searcher' => $searcher, ], 200); }
return view('pages.searchers.show', [ 'searcher' => $searcher, ]); } catch (\Exception $exception) { abort(400); } }
public function edit($id) { $searchersCollection = new SearchersCollection();
try { $searcher = $searchersCollection->get($id);
if ( ! $searcher) { abort(404); }
return view('pages.searchers.edit', [ 'searcher' => $searcher, ]); } catch (\Exception $exception) { abort(400); } }
public function update($id) { request()->validate([ 'name' => 'required', 'tag' => 'nullable', 'rows' => 'required|array', 'rows.*' => 'array', ]);
try { $factory = new SearcherFactory( request()->get('name'), request()->get('rows'), request()->get('tag') );
$searcher = $factory->update($id);
return response()->json([ 'status' => 'success', 'searcher' => $searcher, ], 200); } catch (\Exception $exception) { return response()->json([ 'status' => 'fail', 'trace' => $exception->getTrace(), 'message' => $exception->getMessage(), ], 400); } }
public function destroy($id) { $searcherStorage = new SearchersStorage();
if ( ! $searcherStorage->has($id)) { abort(404); }
$deleted = $searcherStorage->destroy($id);
if ( ! $deleted) { return response()->json([], 400); }
return response()->json([], 200); } }
|