|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Blok\Repository\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
|
6
|
|
|
use Blok\Repository\AbstractEloquentRepository; |
|
7
|
|
|
use Blok\Repository\Contracts\ApiControllerContract; |
|
8
|
|
|
use Blok\Repository\Traits\Modelable; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
use Illuminate\Http\Request; |
|
11
|
|
|
use Illuminate\Validation\ValidationException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class AbstractApiController |
|
15
|
|
|
* |
|
16
|
|
|
* Default route controller to don't rewriting the same things again and again |
|
17
|
|
|
* |
|
18
|
|
|
* @package App\Http\Controllers |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class AbstractApiController extends Controller implements ApiControllerContract |
|
21
|
|
|
{ |
|
22
|
|
|
use Modelable; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var AbstractEloquentRepository |
|
26
|
|
|
*/ |
|
27
|
|
|
public $model; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(){ |
|
30
|
|
|
$this->app = app(); |
|
31
|
|
|
$this->makeModel(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Display a listing of the resource. |
|
36
|
|
|
* |
|
37
|
|
|
* @param Request $request |
|
38
|
|
|
* |
|
39
|
|
|
* @return mixed |
|
40
|
|
|
* @throws Exception |
|
41
|
|
|
*/ |
|
42
|
|
|
public function index(Request $request) |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->model->paginate(50, $request->toArray()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Show the form settings |
|
49
|
|
|
* |
|
50
|
|
|
* @return \Illuminate\Http\Response |
|
51
|
|
|
*/ |
|
52
|
|
|
public function create(){ |
|
53
|
|
|
return $this->model->getForm('create'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Store a newly created resource in storage. |
|
58
|
|
|
* |
|
59
|
|
|
* @param Request $request |
|
60
|
|
|
* @return mixed |
|
61
|
|
|
*/ |
|
62
|
|
|
public function store(Request $request) |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->model->create($request->all()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Display the specified resource. |
|
69
|
|
|
* |
|
70
|
|
|
* @param $id |
|
71
|
|
|
* |
|
72
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
|
73
|
|
|
*/ |
|
74
|
|
|
public function show($id) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->model->find($id); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Show the form info |
|
81
|
|
|
* |
|
82
|
|
|
* @param $id |
|
83
|
|
|
* |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
|
|
public function edit($id){ |
|
87
|
|
|
return $this->model->getForm('edit', $id); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Update the specified resource in storage. |
|
92
|
|
|
* |
|
93
|
|
|
* @param \Illuminate\Http\Request $request |
|
94
|
|
|
* @param $id |
|
95
|
|
|
* |
|
96
|
|
|
* @return \Illuminate\Http\JsonResponse|mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
public function update(Request $request, $id) |
|
99
|
|
|
{ |
|
100
|
|
|
try { |
|
101
|
|
|
return $this->model->update($request->all(), $id); |
|
102
|
|
|
} catch (ValidationException $e) { |
|
103
|
|
|
return response()->json($e->errors(), 422); |
|
104
|
|
|
} catch (Exception $e) { |
|
105
|
|
|
return response()->json($e->getMessage(), 500); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Remove the specified resource from storage. |
|
111
|
|
|
* |
|
112
|
|
|
* @param $id |
|
113
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|mixed |
|
114
|
|
|
*/ |
|
115
|
|
|
public function destroy($id) |
|
116
|
|
|
{ |
|
117
|
|
|
try { |
|
118
|
|
|
return $this->model->delete($id); |
|
119
|
|
|
} catch (Exception $e) { |
|
120
|
|
|
return response($e->getMessage(), 500); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|