rafflesargentina /
l5-resource-controller
| 1 | <?php |
||
| 2 | |||
| 3 | namespace RafflesArgentina\ResourceController; |
||
| 4 | |||
| 5 | use DB; |
||
| 6 | |||
| 7 | use Illuminate\Http\Request; |
||
| 8 | |||
| 9 | use RafflesArgentina\ResourceController\Traits\WorksWithRelations; |
||
| 10 | use RafflesArgentina\ResourceController\Traits\WorksWithFileUploads; |
||
| 11 | use RafflesArgentina\ResourceController\Traits\FormatsResponseMessages; |
||
| 12 | use RafflesArgentina\ResourceController\Exceptions\ResourceControllerException; |
||
| 13 | |||
| 14 | class ResourceController extends AbstractResourceController |
||
| 15 | { |
||
| 16 | use WorksWithRelations, WorksWithFileUploads, FormatsResponseMessages; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Display a listing of the resource. |
||
| 20 | * |
||
| 21 | * @param Request $request The request object. |
||
| 22 | * |
||
| 23 | * @return mixed |
||
| 24 | */ |
||
| 25 | public function index(Request $request) |
||
| 26 | { |
||
| 27 | $this->getFormRequestInstance(); |
||
| 28 | |||
| 29 | $items = $this->getItemsCollection(); |
||
| 30 | |||
| 31 | if ($request->wantsJson()) { |
||
| 32 | return $this->validSuccessJsonResponse('Success', $items); |
||
| 33 | } |
||
| 34 | |||
| 35 | $view = $this->getViewLocation(__FUNCTION__); |
||
| 36 | $this->checkViewExists($view); |
||
| 37 | |||
| 38 | return response()->view($view, compact('items')); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Show the form for creating a new resource. |
||
| 43 | * |
||
| 44 | * @param Request $request The request object. |
||
| 45 | * |
||
| 46 | * @return mixed |
||
| 47 | */ |
||
| 48 | public function create(Request $request) |
||
| 49 | { |
||
| 50 | if ($request->wantsJson()) { |
||
| 51 | return $this->validNotFoundJsonResponse(); |
||
| 52 | } |
||
| 53 | |||
| 54 | $model = new $this->repository->model; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 55 | |||
| 56 | $view = $this->getViewLocation(__FUNCTION__); |
||
| 57 | $this->checkViewExists($view); |
||
| 58 | |||
| 59 | return response()->view($view, compact('model')); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Store a newly created resource in storage. |
||
| 64 | * |
||
| 65 | * @param Request $request The request object. |
||
| 66 | * |
||
| 67 | * @throws ResourceControllerException |
||
| 68 | * |
||
| 69 | * @return mixed |
||
| 70 | */ |
||
| 71 | public function store(Request $request) |
||
| 72 | { |
||
| 73 | $this->getFormRequestInstance(); |
||
| 74 | |||
| 75 | DB::beginTransaction(); |
||
| 76 | try { |
||
| 77 | $instance = $this->repository->create($request->all()); |
||
| 78 | $model = $instance[1]; |
||
| 79 | $number = $model->{$model->getRouteKeyName()}; |
||
| 80 | $mergedRequest = $this->uploadFiles($request, $model); |
||
| 81 | $this->updateOrCreateRelations($mergedRequest, $model); |
||
| 82 | } catch (\Exception $e) { |
||
| 83 | DB::rollback(); |
||
| 84 | |||
| 85 | $message = $this->storeFailedMessage($e->getMessage()); |
||
| 86 | throw new ResourceControllerException($message); |
||
| 87 | } |
||
| 88 | |||
| 89 | DB::commit(); |
||
| 90 | |||
| 91 | $message = $this->storeSuccessfulMessage($number); |
||
| 92 | |||
| 93 | if ($request->wantsJson()) { |
||
| 94 | $data = [ |
||
| 95 | $model |
||
| 96 | ]; |
||
| 97 | |||
| 98 | return $this->validSuccessJsonResponse($message, $data); |
||
| 99 | } |
||
| 100 | |||
| 101 | return redirect()->route($this->getRedirectionRoute()) |
||
| 102 | ->with($this->successFlashMessageKey, $message); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Display the specified resource. |
||
| 107 | * |
||
| 108 | * @param Request $request The request object. |
||
| 109 | * @param string $key The model key. |
||
| 110 | * |
||
| 111 | * @return mixed |
||
| 112 | */ |
||
| 113 | public function show(Request $request, $key) |
||
| 114 | { |
||
| 115 | $model = $this->findFirstByKey($key); |
||
| 116 | |||
| 117 | if (!$model) { |
||
| 118 | if ($request->wantsJson()) { |
||
| 119 | return $this->validNotFoundJsonResponse(); |
||
| 120 | } |
||
| 121 | abort(404); |
||
| 122 | } |
||
| 123 | |||
| 124 | if ($request->wantsJson()) { |
||
| 125 | return response()->json($model, 200, [], JSON_PRETTY_PRINT); |
||
| 126 | } |
||
| 127 | |||
| 128 | $view = $this->getViewLocation(__FUNCTION__); |
||
| 129 | $this->checkViewExists($view); |
||
| 130 | |||
| 131 | return response()->view($view, compact('model')); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Show the form for editing the specified resource. |
||
| 136 | * |
||
| 137 | * @param Request $request The request object. |
||
| 138 | * @param string $key The model key. |
||
| 139 | * |
||
| 140 | * @return mixed |
||
| 141 | */ |
||
| 142 | public function edit(Request $request, $key) |
||
| 143 | { |
||
| 144 | if ($request->wantsJson()) { |
||
| 145 | return $this->validNotFoundJsonResponse(); |
||
| 146 | } |
||
| 147 | |||
| 148 | $model = $this->findFirstByKey($key); |
||
| 149 | |||
| 150 | if (!$model) { |
||
| 151 | abort(404); |
||
| 152 | } |
||
| 153 | |||
| 154 | $view = $this->getViewLocation(__FUNCTION__); |
||
| 155 | $this->checkViewExists($view); |
||
| 156 | |||
| 157 | return response()->view($view, compact('model')); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Update the specified resource in storage. |
||
| 162 | * |
||
| 163 | * @param Request $request The request object. |
||
| 164 | * @param string $key The model key. |
||
| 165 | * |
||
| 166 | * @throws ResourceControllerException |
||
| 167 | * |
||
| 168 | * @return mixed |
||
| 169 | */ |
||
| 170 | public function update(Request $request, $key) |
||
| 171 | { |
||
| 172 | $this->getFormRequestInstance(); |
||
| 173 | |||
| 174 | $model = $this->findFirstByKey($key); |
||
| 175 | |||
| 176 | if (!$model) { |
||
| 177 | if ($request->wantsJson()) { |
||
| 178 | return $this->validNotFoundJsonResponse(); |
||
| 179 | } |
||
| 180 | abort(404); |
||
| 181 | } |
||
| 182 | |||
| 183 | DB::beginTransaction(); |
||
| 184 | |||
| 185 | try { |
||
| 186 | $instance = $this->repository->update($model, $request->all()); |
||
| 187 | $model = $instance[1]; |
||
| 188 | $mergedRequest = $this->uploadFiles($request, $model); |
||
| 189 | $this->updateOrCreateRelations($mergedRequest, $model); |
||
| 190 | } catch (\Exception $e) { |
||
| 191 | DB::rollback(); |
||
| 192 | |||
| 193 | $message = $this->updateFailedMessage($key, $e->getMessage()); |
||
| 194 | throw new ResourceControllerException($message); |
||
| 195 | } |
||
| 196 | |||
| 197 | DB::commit(); |
||
| 198 | |||
| 199 | $message = $this->updateSuccessfulMessage($key); |
||
| 200 | |||
| 201 | if ($request->wantsJson()) { |
||
| 202 | $data = [ |
||
| 203 | $model |
||
| 204 | ]; |
||
| 205 | |||
| 206 | return $this->validSuccessJsonResponse($message, $data); |
||
| 207 | } |
||
| 208 | |||
| 209 | return redirect()->route($this->getRedirectionRoute()) |
||
| 210 | ->with($this->successFlashMessageKey, $message); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Remove the specified resource from storage. |
||
| 215 | * |
||
| 216 | * @param Request $request The request object. |
||
| 217 | * @param string $key The model key. |
||
| 218 | * |
||
| 219 | * @throws ResourceControllerException |
||
| 220 | * |
||
| 221 | * @return mixed |
||
| 222 | */ |
||
| 223 | public function destroy(Request $request, $key) |
||
| 224 | { |
||
| 225 | $this->getFormRequestInstance(); |
||
| 226 | |||
| 227 | $model = $this->findFirstByKey($key); |
||
| 228 | |||
| 229 | if (!$model) { |
||
| 230 | if ($request->wantsJson()) { |
||
| 231 | return $this->validNotFoundJsonResponse(); |
||
| 232 | } |
||
| 233 | abort(404); |
||
| 234 | } |
||
| 235 | |||
| 236 | DB::beginTransaction(); |
||
| 237 | |||
| 238 | try { |
||
| 239 | $this->repository->delete($model); |
||
| 240 | } catch (\Exception $e) { |
||
| 241 | DB::rollback(); |
||
| 242 | |||
| 243 | $message = $this->destroyFailedMessage($key, $e->getMessage()); |
||
| 244 | throw new ResourceControllerException($message); |
||
| 245 | } |
||
| 246 | |||
| 247 | DB::commit(); |
||
| 248 | |||
| 249 | $message = $this->destroySuccessfulMessage($key); |
||
| 250 | |||
| 251 | if ($request->wantsJson()) { |
||
| 252 | $data = [ |
||
| 253 | $model |
||
| 254 | ]; |
||
| 255 | |||
| 256 | return $this->validSuccessJsonResponse($message, $data); |
||
| 257 | } |
||
| 258 | |||
| 259 | return redirect()->route($this->getRedirectionRoute()) |
||
| 260 | ->with($this->infoFlashMessageKey, $message); |
||
| 261 | } |
||
| 262 | } |
||
| 263 |