AreasController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Http\Controllers\Area;
  3. use App\Http\Controllers\Controller;
  4. use Prettus\Validator\Contracts\ValidatorInterface;
  5. use Prettus\Validator\Exceptions\ValidatorException;
  6. use App\Http\Requests\AreaCreateRequest;
  7. use App\Http\Requests\AreaUpdateRequest;
  8. use App\Repositories\Eloquent\AreaRepositoryEloquent;
  9. use App\Validators\AreaValidator;
  10. /**
  11. * Class AreasController.
  12. *
  13. * @package namespace App\Http\Controllers;
  14. */
  15. class AreasController extends Controller
  16. {
  17. /**
  18. * @var AreaRepositoryEloquent
  19. */
  20. protected $repository;
  21. /**
  22. * @var AreaValidator
  23. */
  24. protected $validator;
  25. /**
  26. * AreasController constructor.
  27. *
  28. * @param AreaRepositoryEloquent $repository
  29. * @param AreaValidator $validator
  30. */
  31. public function __construct(AreaRepositoryEloquent $repository, AreaValidator $validator)
  32. {
  33. $this->repository = $repository;
  34. $this->validator = $validator;
  35. }
  36. /**
  37. * Display a listing of the resource.
  38. *
  39. * @return \Illuminate\Http\Response
  40. */
  41. public function index()
  42. {
  43. $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
  44. $areas = $this->repository->all();
  45. if (request()->wantsJson()) {
  46. return response()->json([
  47. 'data' => $areas,
  48. ]);
  49. }
  50. return view('areas.index', compact('areas'));
  51. }
  52. /**
  53. * Store a newly created resource in storage.
  54. *
  55. * @param AreaCreateRequest $request
  56. *
  57. * @return \Illuminate\Http\Response
  58. *
  59. * @throws \Prettus\Validator\Exceptions\ValidatorException
  60. */
  61. public function store(AreaCreateRequest $request)
  62. {
  63. try {
  64. $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
  65. $area = $this->repository->create($request->all());
  66. $response = [
  67. 'message' => 'Area created.',
  68. 'data' => $area->toArray(),
  69. ];
  70. if ($request->wantsJson()) {
  71. return response()->json($response);
  72. }
  73. return redirect()->back()->with('message', $response['message']);
  74. } catch (ValidatorException $e) {
  75. if ($request->wantsJson()) {
  76. return response()->json([
  77. 'error' => true,
  78. 'message' => $e->getMessageBag()
  79. ]);
  80. }
  81. return redirect()->back()->withErrors($e->getMessageBag())->withInput();
  82. }
  83. }
  84. /**
  85. * Display the specified resource.
  86. *
  87. * @param int $id
  88. *
  89. * @return \Illuminate\Http\Response
  90. */
  91. public function show($id)
  92. {
  93. $area = $this->repository->find($id);
  94. if (request()->wantsJson()) {
  95. return response()->json([
  96. 'data' => $area,
  97. ]);
  98. }
  99. return view('areas.show', compact('area'));
  100. }
  101. /**
  102. * Show the form for editing the specified resource.
  103. *
  104. * @param int $id
  105. *
  106. * @return \Illuminate\Http\Response
  107. */
  108. public function edit($id)
  109. {
  110. $area = $this->repository->find($id);
  111. return view('areas.edit', compact('area'));
  112. }
  113. /**
  114. * Update the specified resource in storage.
  115. *
  116. * @param AreaUpdateRequest $request
  117. * @param string $id
  118. *
  119. * @return Response
  120. *
  121. * @throws \Prettus\Validator\Exceptions\ValidatorException
  122. */
  123. public function update(AreaUpdateRequest $request, $id)
  124. {
  125. try {
  126. $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);
  127. $area = $this->repository->update($request->all(), $id);
  128. $response = [
  129. 'message' => 'Area updated.',
  130. 'data' => $area->toArray(),
  131. ];
  132. if ($request->wantsJson()) {
  133. return response()->json($response);
  134. }
  135. return redirect()->back()->with('message', $response['message']);
  136. } catch (ValidatorException $e) {
  137. if ($request->wantsJson()) {
  138. return response()->json([
  139. 'error' => true,
  140. 'message' => $e->getMessageBag()
  141. ]);
  142. }
  143. return redirect()->back()->withErrors($e->getMessageBag())->withInput();
  144. }
  145. }
  146. /**
  147. * Remove the specified resource from storage.
  148. *
  149. * @param int $id
  150. *
  151. * @return \Illuminate\Http\Response
  152. */
  153. public function destroy($id)
  154. {
  155. $deleted = $this->repository->delete($id);
  156. if (request()->wantsJson()) {
  157. return response()->json([
  158. 'message' => 'Area deleted.',
  159. 'deleted' => $deleted,
  160. ]);
  161. }
  162. return redirect()->back()->with('message', 'Area deleted.');
  163. }
  164. }