|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Modules\Core\BaseClasses; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use App\Modules\Core\Http\Resources\General as GeneralResource; |
|
8
|
|
|
|
|
9
|
|
|
class BaseApiController extends Controller |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Array of eager loaded relations. |
|
13
|
|
|
* |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $relations; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var object |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $service; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Path of the model resource. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $modelResource; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Path of the sotre form request. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $storeFormRequest; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Init new object. |
|
39
|
|
|
* |
|
40
|
|
|
* @param mixed $service |
|
41
|
|
|
* @return void |
|
|
|
|
|
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct($service) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->service = $service; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Fetch all records with relations from storage. |
|
50
|
|
|
* |
|
51
|
|
|
* @param Request $request |
|
52
|
|
|
* @return \Illuminate\Http\Response |
|
53
|
|
|
*/ |
|
54
|
|
|
public function index(Request $request) |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->modelResource::collection($this->service->list($request->relations, $request->query(), $request->query('perPage'), $request->query('sortBy'), $request->query('desc'), $request->query('trashed'))); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Fetch the single object with relations from storage. |
|
61
|
|
|
* |
|
62
|
|
|
* @param Request $request |
|
63
|
|
|
* @param integer $id Id of the requested model. |
|
64
|
|
|
* @return \Illuminate\Http\Response |
|
65
|
|
|
*/ |
|
66
|
|
|
public function show(Request $request, $id) |
|
67
|
|
|
{ |
|
68
|
|
|
return new $this->modelResource($this->service->find($id, $request->relations)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Insert the given model to storage. |
|
73
|
|
|
* |
|
74
|
|
|
* @return \Illuminate\Http\Response |
|
75
|
|
|
*/ |
|
76
|
|
|
public function store() |
|
77
|
|
|
{ |
|
78
|
|
|
$data = \App::make($this->storeFormRequest)->validated(); |
|
79
|
|
|
return new $this->modelResource($this->service->save($data)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Update the given model to storage. |
|
84
|
|
|
* |
|
85
|
|
|
* @param integer $id |
|
86
|
|
|
* @return \Illuminate\Http\Response |
|
87
|
|
|
*/ |
|
88
|
|
|
public function update($id) |
|
89
|
|
|
{ |
|
90
|
|
|
$data = \App::make($this->storeFormRequest)->validated(); |
|
91
|
|
|
$data['id'] = $id; |
|
92
|
|
|
return new $this->modelResource($this->service->save($data)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Delete by the given id from storage. |
|
97
|
|
|
* |
|
98
|
|
|
* @param integer $id Id of the deleted model. |
|
99
|
|
|
* @return \Illuminate\Http\Response |
|
100
|
|
|
*/ |
|
101
|
|
|
public function destroy($id) |
|
102
|
|
|
{ |
|
103
|
|
|
return new GeneralResource($this->service->delete($id)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Restore the deleted model. |
|
108
|
|
|
* |
|
109
|
|
|
* @param integer $id Id of the restored model. |
|
110
|
|
|
* @return \Illuminate\Http\Response |
|
111
|
|
|
*/ |
|
112
|
|
|
public function restore($id) |
|
113
|
|
|
{ |
|
114
|
|
|
return new GeneralResource($this->service->restore($id)); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.