123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- namespace App\Http\Controllers\Admin\Inform;
- use Illuminate\Http\Request;
- use App\Http\Requests;
- use Prettus\Validator\Contracts\ValidatorInterface;
- use Prettus\Validator\Exceptions\ValidatorException;
- use App\Http\Requests\UserMessageCreateRequest;
- use App\Http\Requests\UserMessageUpdateRequest;
- use App\Contracts\Repositories\Inform\UserMessageRepository;
- use App\Repositories\Validators\Inform\UserMessageValidator;
- /**
- * Class UserMessagesController.
- *
- * @package namespace App\Http\Controllers\Admin\Inform;
- */
- class UserMessagesController extends Controller
- {
- /**
- * @var UserMessageRepository
- */
- protected $repository;
- /**
- * @var UserMessageValidator
- */
- protected $validator;
- /**
- * UserMessagesController constructor.
- *
- * @param UserMessageRepository $repository
- * @param UserMessageValidator $validator
- */
- public function __construct(UserMessageRepository $repository, UserMessageValidator $validator)
- {
- $this->repository = $repository;
- $this->validator = $validator;
- }
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
- $userMessages = $this->repository->all();
- if (request()->wantsJson()) {
- return response()->json([
- 'data' => $userMessages,
- ]);
- }
- return view('userMessages.index', compact('userMessages'));
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param UserMessageCreateRequest $request
- *
- * @return \Illuminate\Http\Response
- *
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function store(UserMessageCreateRequest $request)
- {
- try {
- $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
- $userMessage = $this->repository->create($request->all());
- $response = [
- 'message' => 'UserMessage created.',
- 'data' => $userMessage->toArray(),
- ];
- if ($request->wantsJson()) {
- return response()->json($response);
- }
- return redirect()->back()->with('message', $response['message']);
- } catch (ValidatorException $e) {
- if ($request->wantsJson()) {
- return response()->json([
- 'error' => true,
- 'message' => $e->getMessageBag()
- ]);
- }
- return redirect()->back()->withErrors($e->getMessageBag())->withInput();
- }
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- *
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- $userMessage = $this->repository->find($id);
- if (request()->wantsJson()) {
- return response()->json([
- 'data' => $userMessage,
- ]);
- }
- return view('userMessages.show', compact('userMessage'));
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- *
- * @return \Illuminate\Http\Response
- */
- public function edit($id)
- {
- $userMessage = $this->repository->find($id);
- return view('userMessages.edit', compact('userMessage'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param UserMessageUpdateRequest $request
- * @param string $id
- *
- * @return Response
- *
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function update(UserMessageUpdateRequest $request, $id)
- {
- try {
- $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);
- $userMessage = $this->repository->update($request->all(), $id);
- $response = [
- 'message' => 'UserMessage updated.',
- 'data' => $userMessage->toArray(),
- ];
- if ($request->wantsJson()) {
- return response()->json($response);
- }
- return redirect()->back()->with('message', $response['message']);
- } catch (ValidatorException $e) {
- if ($request->wantsJson()) {
- return response()->json([
- 'error' => true,
- 'message' => $e->getMessageBag()
- ]);
- }
- return redirect()->back()->withErrors($e->getMessageBag())->withInput();
- }
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- *
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- $deleted = $this->repository->delete($id);
- if (request()->wantsJson()) {
- return response()->json([
- 'message' => 'UserMessage deleted.',
- 'deleted' => $deleted,
- ]);
- }
- return redirect()->back()->with('message', 'UserMessage deleted.');
- }
- }
|