-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseModelAPIController.php
executable file
·177 lines (158 loc) · 4.43 KB
/
BaseModelAPIController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use Validator;
class BaseModelAPIController extends BaseController {
protected $meta = false;
protected $base_route;
public function getMeta() {
if ( $this->meta ) {
return $this->meta;
}
$route = \Route::getCurrentRoute()->getName();
$this->base_route = $route = substr( $route, 4, strrpos( $route, '.' ) - 4 );
return $this->meta = config( 'model' )[ $route ];
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index( Request $request ) {
$meta = $this->getMeta();
$model = $meta[ 'model' ];
$res = array();
/**
* @var \Illuminate\Database\Eloquent\Builder $query
*/
$query = $model::query();
if ( $request->has( 'order' ) ) {
foreach ( $request->input( 'order' ) as $field => $dir ) {
$query->orderBy( $field, $dir );
}
}
if ( $request->has( 'filter' ) ) {
$scope = Str::Camel( $request->input( 'filter' ) );
$method = Str::Camel( 'scope_' . $request->input( 'filter' ) );
if ( method_exists( $model, $method ) ) {
$query->$scope();
} else {
return [ 'error' => 'Unknown filter' ];
}
}
if ( $request->has( 'take' ) ) {
$query->take( $request->input( 'take' ) );
}
if ( $request->has( 'where' ) ) {
foreach ( $request->input( 'where' ) as $item ) {
$query->where( $item[ 'col' ], $item[ 'op' ], $item[ 'val' ], $item[ 'bool' ] );
}
}
$res[ 'status' ] = 'success';
$res[ 'code' ] = 200;
$res[ 'data' ] = $query->get();
return $res;
}
/**
* Display the specified resource.
*
* @param int $id
*
* @return Response
*/
public function show( Request $request, $item ) {
$with_relations = [ ];
$result = $item->toArray();
if ( $request->has( 'with' ) ) {
$with = $request->get( 'with' );
if ( is_string( $with ) ) {
$with = explode( '|', $with );
}
$with_relations = $with;
foreach ( $with as $relation ) {
$result[ $relation ] = $item->$relation;
}
}
return [ 'status' => 'success', 'code' => 200, "data" => $result ];
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store( Request $request ) {
$meta = $this->getMeta();
$model = $meta[ 'model' ];
$validation = Validator::make( $request->input(), $model::$rules );
if ( $validation->passes() ) {
$item = $model::create( $request->input() );
return response( [ 'status' => 'success', 'code' => 201, 'data' => $item ], 201,
[ 'Location' => route( 'api_' . $this->base_route . '.show', $item->id ) ] );
} else {
return response( [ 'status' => 'error', 'code' => 422, 'error' => $validation->errors() ], 422 );
}
}
/**
* Update the specified resource in storage.
*
* @param int $id
*
* @return Response
*/
public function update(Request $request, $item ) {
$model = get_class( $item );
if ( isset( $model::$rules_edit ) ) {
$rules = $model::$rules_edit;
} else {
$rules = $model::$rules;
}
foreach ( $rules as $key=>&$rule ) {
if ($request->has($key)) {
if ( substr( $rule, -1, 1 ) == ',' ) {
$rule .= $item->id;
}
} else {
unset( $rules[ $key ] );
}
}
$validation = Validator::make( $request->input(), $rules );
if ( $validation->passes() ) {
if ( $item->update( $request->input() ) ) {
return [ 'status' => 'success', 'code' => 200, 'data' => $item ];
} else {
return response( [ 'status' => 'fail', 'code' => 500, 'error' => [ 'Can`t update' ] ], 500 );
}
} else {
$errors = $validation->errors()->toArray();
foreach ($errors as $code=>&$message) {
$message = str_replace( '%attribute%', '"' . $model::getFieldLabel($code) . '"', $message );
}
return response( [ 'status' => 'error', 'code' => 422, 'error' => $errors ], 422 );
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
*
* @return Response
*/
public function destroy( Request $request, $item ) {
$item->delete();
return [ 'status' => 'success' ];
}
public function rootOptions( Request $request ) {
$headers = [
'Allow' => 'HEAD,GET,POST,OPTIONS',
];
//config('api_application')
return response( [ 'status' => 'success', 'code' => 200 ], 200, $headers );
}
public function itemOptions( Request $request ) {
$headers = [
'Allow' => 'HEAD,GET,PUT,POST,DELETE,OPTIONS',
];
//config('api_application')
return response( [ 'status' => 'success', 'code' => 200 ], 200, $headers );
}
}