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

3 years ago
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\SearchDisplace\Searchers\SearcherFactory;
  4. use App\SearchDisplace\Searchers\SearchersCollection;
  5. use App\SearchDisplace\Searchers\SearchersStorage;
  6. class SearcherController extends Controller
  7. {
  8. public function index()
  9. {
  10. // @TODO Add filters
  11. if (request()->has('q')) {
  12. $searchers = [];
  13. } else {
  14. $searchers = (new SearchersCollection())->all();
  15. }
  16. if (request()->wantsJson()) {
  17. return response()->json([
  18. 'searchers' => $searchers,
  19. ], 200);
  20. }
  21. return view('pages.searchers.index', [
  22. 'searchers' => $searchers,
  23. ]);
  24. }
  25. public function create()
  26. {
  27. return view('pages.searchers.create');
  28. }
  29. public function store()
  30. {
  31. request()->validate([
  32. 'name' => 'required',
  33. 'tag' => 'nullable',
  34. 'rows' => 'required|array',
  35. 'rows.*' => 'array',
  36. ]);
  37. try {
  38. $factory = new SearcherFactory(
  39. request()->get('name'),
  40. request()->get('rows'),
  41. request()->get('tag')
  42. );
  43. $searcher = $factory->create();
  44. return response()->json([
  45. 'status' => 'success',
  46. 'searcher' => $searcher,
  47. ], 200);
  48. } catch (\Exception $exception) {
  49. return response()->json([
  50. 'status' => 'fail',
  51. 'trace' => $exception->getTrace(),
  52. 'message' => $exception->getMessage(),
  53. ], 400);
  54. }
  55. }
  56. public function show($id)
  57. {
  58. $searchersCollection = new SearchersCollection();
  59. try {
  60. $searcher = $searchersCollection->get($id);
  61. if ( ! $searcher) {
  62. abort(404);
  63. }
  64. if (request()->wantsJson()) {
  65. return response()->json([
  66. 'searcher' => $searcher,
  67. ], 200);
  68. }
  69. return view('pages.searchers.show', [
  70. 'searcher' => $searcher,
  71. ]);
  72. } catch (\Exception $exception) {
  73. abort(400);
  74. }
  75. }
  76. public function edit($id)
  77. {
  78. $searchersCollection = new SearchersCollection();
  79. try {
  80. $searcher = $searchersCollection->get($id);
  81. if ( ! $searcher) {
  82. abort(404);
  83. }
  84. return view('pages.searchers.edit', [
  85. 'searcher' => $searcher,
  86. ]);
  87. } catch (\Exception $exception) {
  88. abort(400);
  89. }
  90. }
  91. public function update($id)
  92. {
  93. request()->validate([
  94. 'name' => 'required',
  95. 'tag' => 'nullable',
  96. 'rows' => 'required|array',
  97. 'rows.*' => 'array',
  98. ]);
  99. try {
  100. $factory = new SearcherFactory(
  101. request()->get('name'),
  102. request()->get('rows'),
  103. request()->get('tag')
  104. );
  105. $searcher = $factory->update($id);
  106. return response()->json([
  107. 'status' => 'success',
  108. 'searcher' => $searcher,
  109. ], 200);
  110. } catch (\Exception $exception) {
  111. return response()->json([
  112. 'status' => 'fail',
  113. 'trace' => $exception->getTrace(),
  114. 'message' => $exception->getMessage(),
  115. ], 400);
  116. }
  117. }
  118. public function destroy($id)
  119. {
  120. $searcherStorage = new SearchersStorage();
  121. if ( ! $searcherStorage->has($id)) {
  122. abort(404);
  123. }
  124. $deleted = $searcherStorage->destroy($id);
  125. if ( ! $deleted) {
  126. return response()->json([], 400);
  127. }
  128. return response()->json([], 200);
  129. }
  130. }