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