Repo for the search and displace core module including the interface to select files and search and displace operations to run on them.
https://searchanddisplace.com
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
3.8 KiB
158 lines
3.8 KiB
<?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);
|
|
}
|
|
}
|