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.

163 lines
4.0 KiB

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