UserMessagesController.php 5.1 KB

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