@@ -4,450 +4,450 @@ |
||
4 | 4 | |
5 | 5 | abstract class AbstractRepository implements RepositoryInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * The model implementation. |
|
9 | - * |
|
10 | - * @var model |
|
11 | - */ |
|
12 | - public $model; |
|
7 | + /** |
|
8 | + * The model implementation. |
|
9 | + * |
|
10 | + * @var model |
|
11 | + */ |
|
12 | + public $model; |
|
13 | 13 | |
14 | - /** |
|
15 | - * The config implementation. |
|
16 | - * |
|
17 | - * @var config |
|
18 | - */ |
|
19 | - protected $config; |
|
14 | + /** |
|
15 | + * The config implementation. |
|
16 | + * |
|
17 | + * @var config |
|
18 | + */ |
|
19 | + protected $config; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Create new AbstractRepository instance. |
|
23 | - */ |
|
24 | - public function __construct() |
|
25 | - { |
|
26 | - $this->config = \CoreConfig::getConfig(); |
|
27 | - $this->model = \App::make($this->getModel()); |
|
28 | - } |
|
21 | + /** |
|
22 | + * Create new AbstractRepository instance. |
|
23 | + */ |
|
24 | + public function __construct() |
|
25 | + { |
|
26 | + $this->config = \CoreConfig::getConfig(); |
|
27 | + $this->model = \App::make($this->getModel()); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * Fetch all records with relations from the storage. |
|
32 | - * |
|
33 | - * @param array $relations |
|
34 | - * @param string $sortBy |
|
35 | - * @param boolean $desc |
|
36 | - * @param array $columns |
|
37 | - * @return collection |
|
38 | - */ |
|
30 | + /** |
|
31 | + * Fetch all records with relations from the storage. |
|
32 | + * |
|
33 | + * @param array $relations |
|
34 | + * @param string $sortBy |
|
35 | + * @param boolean $desc |
|
36 | + * @param array $columns |
|
37 | + * @return collection |
|
38 | + */ |
|
39 | 39 | public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
40 | 40 | { |
41 | - $sort = $desc ? 'desc' : 'asc'; |
|
42 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
41 | + $sort = $desc ? 'desc' : 'asc'; |
|
42 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
43 | 43 | } |
44 | 44 | |
45 | - /** |
|
46 | - * Fetch all records with relations from storage in pages |
|
47 | - * that matche the given query. |
|
48 | - * |
|
49 | - * @param string $query |
|
50 | - * @param integer $perPage |
|
51 | - * @param array $relations |
|
52 | - * @param string $sortBy |
|
53 | - * @param boolean $desc |
|
54 | - * @param array $columns |
|
55 | - * @return collection |
|
56 | - */ |
|
57 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
58 | - { |
|
59 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
60 | - $conditionColumns = $this->model->getFillable(); |
|
61 | - $sort = $desc ? 'desc' : 'asc'; |
|
45 | + /** |
|
46 | + * Fetch all records with relations from storage in pages |
|
47 | + * that matche the given query. |
|
48 | + * |
|
49 | + * @param string $query |
|
50 | + * @param integer $perPage |
|
51 | + * @param array $relations |
|
52 | + * @param string $sortBy |
|
53 | + * @param boolean $desc |
|
54 | + * @param array $columns |
|
55 | + * @return collection |
|
56 | + */ |
|
57 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
58 | + { |
|
59 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
60 | + $conditionColumns = $this->model->getFillable(); |
|
61 | + $sort = $desc ? 'desc' : 'asc'; |
|
62 | 62 | |
63 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
64 | - $q->where(\DB::raw('LOWER(CAST(' . array_shift($conditionColumns) . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
65 | - foreach ($conditionColumns as $column) |
|
66 | - { |
|
67 | - $q->orWhere(\DB::raw('LOWER(CAST(' . $column . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
68 | - } |
|
69 | - foreach ($relations as $relation) |
|
70 | - { |
|
71 | - $relation = explode('.', $relation)[0]; |
|
72 | - if (\Core::$relation()) |
|
73 | - { |
|
74 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
63 | + $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
64 | + $q->where(\DB::raw('LOWER(CAST(' . array_shift($conditionColumns) . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
65 | + foreach ($conditionColumns as $column) |
|
66 | + { |
|
67 | + $q->orWhere(\DB::raw('LOWER(CAST(' . $column . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
68 | + } |
|
69 | + foreach ($relations as $relation) |
|
70 | + { |
|
71 | + $relation = explode('.', $relation)[0]; |
|
72 | + if (\Core::$relation()) |
|
73 | + { |
|
74 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
75 | 75 | |
76 | - $subModel->where(function ($q) use ($query, $relation){ |
|
76 | + $subModel->where(function ($q) use ($query, $relation){ |
|
77 | 77 | |
78 | - $subConditionColumns = \Core::$relation()->model->getFillable(); |
|
79 | - $q->where(\DB::raw('LOWER(CAST(' . array_shift($subConditionColumns) . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
80 | - foreach ($subConditionColumns as $subConditionColumn) |
|
81 | - { |
|
82 | - $q->orWhere(\DB::raw('LOWER(CAST(' . $subConditionColumn . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
83 | - } |
|
84 | - }); |
|
78 | + $subConditionColumns = \Core::$relation()->model->getFillable(); |
|
79 | + $q->where(\DB::raw('LOWER(CAST(' . array_shift($subConditionColumns) . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
80 | + foreach ($subConditionColumns as $subConditionColumn) |
|
81 | + { |
|
82 | + $q->orWhere(\DB::raw('LOWER(CAST(' . $subConditionColumn . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
83 | + } |
|
84 | + }); |
|
85 | 85 | |
86 | - }); |
|
87 | - } |
|
88 | - } |
|
89 | - }); |
|
86 | + }); |
|
87 | + } |
|
88 | + } |
|
89 | + }); |
|
90 | 90 | |
91 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
92 | - } |
|
91 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Fetch all records with relations from storage in pages. |
|
96 | - * |
|
97 | - * @param integer $perPage |
|
98 | - * @param array $relations |
|
99 | - * @param string $sortBy |
|
100 | - * @param boolean $desc |
|
101 | - * @param array $columns |
|
102 | - * @return collection |
|
103 | - */ |
|
104 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
105 | - { |
|
106 | - $sort = $desc ? 'desc' : 'asc'; |
|
107 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
108 | - } |
|
94 | + /** |
|
95 | + * Fetch all records with relations from storage in pages. |
|
96 | + * |
|
97 | + * @param integer $perPage |
|
98 | + * @param array $relations |
|
99 | + * @param string $sortBy |
|
100 | + * @param boolean $desc |
|
101 | + * @param array $columns |
|
102 | + * @return collection |
|
103 | + */ |
|
104 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
105 | + { |
|
106 | + $sort = $desc ? 'desc' : 'asc'; |
|
107 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * Fetch all records with relations based on |
|
112 | - * the given condition from storage in pages. |
|
113 | - * |
|
114 | - * @param array $conditions array of conditions |
|
115 | - * @param integer $perPage |
|
116 | - * @param array $relations |
|
117 | - * @param string $sortBy |
|
118 | - * @param boolean $desc |
|
119 | - * @param array $columns |
|
120 | - * @return collection |
|
121 | - */ |
|
122 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
123 | - { |
|
124 | - unset($conditions['page']); |
|
125 | - $conditions = $this->constructConditions($conditions); |
|
126 | - $sort = $desc ? 'desc' : 'asc'; |
|
127 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
128 | - } |
|
110 | + /** |
|
111 | + * Fetch all records with relations based on |
|
112 | + * the given condition from storage in pages. |
|
113 | + * |
|
114 | + * @param array $conditions array of conditions |
|
115 | + * @param integer $perPage |
|
116 | + * @param array $relations |
|
117 | + * @param string $sortBy |
|
118 | + * @param boolean $desc |
|
119 | + * @param array $columns |
|
120 | + * @return collection |
|
121 | + */ |
|
122 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
123 | + { |
|
124 | + unset($conditions['page']); |
|
125 | + $conditions = $this->constructConditions($conditions); |
|
126 | + $sort = $desc ? 'desc' : 'asc'; |
|
127 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * Save the given model to the storage. |
|
132 | - * |
|
133 | - * @param array $data |
|
134 | - * @param boolean $saveLog |
|
135 | - * @return object |
|
136 | - */ |
|
137 | - public function save(array $data, $saveLog = true) |
|
138 | - { |
|
139 | - $model = false; |
|
140 | - $modelClass = $this->model; |
|
141 | - $relations = []; |
|
142 | - $with = []; |
|
130 | + /** |
|
131 | + * Save the given model to the storage. |
|
132 | + * |
|
133 | + * @param array $data |
|
134 | + * @param boolean $saveLog |
|
135 | + * @return object |
|
136 | + */ |
|
137 | + public function save(array $data, $saveLog = true) |
|
138 | + { |
|
139 | + $model = false; |
|
140 | + $modelClass = $this->model; |
|
141 | + $relations = []; |
|
142 | + $with = []; |
|
143 | 143 | |
144 | - \DB::transaction(function () use (&$model, &$relations, &$with, $data, $saveLog, $modelClass) { |
|
145 | - /** |
|
146 | - * If the id is present in the data then select the model for updating, |
|
147 | - * else create new model. |
|
148 | - * @var array |
|
149 | - */ |
|
150 | - $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
151 | - if ( ! $model) |
|
152 | - { |
|
153 | - $error = \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
154 | - abort($error['status'], $error['message']); |
|
155 | - } |
|
144 | + \DB::transaction(function () use (&$model, &$relations, &$with, $data, $saveLog, $modelClass) { |
|
145 | + /** |
|
146 | + * If the id is present in the data then select the model for updating, |
|
147 | + * else create new model. |
|
148 | + * @var array |
|
149 | + */ |
|
150 | + $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
151 | + if ( ! $model) |
|
152 | + { |
|
153 | + $error = \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
154 | + abort($error['status'], $error['message']); |
|
155 | + } |
|
156 | 156 | |
157 | - /** |
|
158 | - * Construct the model object with the given data, |
|
159 | - * and if there is a relation add it to relations array, |
|
160 | - * then save the model. |
|
161 | - */ |
|
162 | - foreach ($data as $key => $value) |
|
163 | - { |
|
164 | - $relation = camel_case($key); |
|
165 | - if (method_exists($model, $relation)) |
|
166 | - { |
|
167 | - $with[] = $relation; |
|
168 | - if (class_basename($model->$relation) == 'Collection') |
|
169 | - { |
|
170 | - if ( ! $value || ! count($value)) |
|
171 | - { |
|
172 | - $relations[$relation] = 'delete'; |
|
173 | - } |
|
174 | - } |
|
175 | - if (is_array($value)) |
|
176 | - { |
|
177 | - foreach ($value as $attr => $val) |
|
178 | - { |
|
179 | - $relationBaseModel = \Core::$relation()->model; |
|
180 | - if (class_basename($model->$relation) == 'Collection') |
|
181 | - { |
|
182 | - $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
183 | - if ( ! $relationModel) |
|
184 | - { |
|
185 | - $error = \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
186 | - abort($error['status'], $error['message']); |
|
187 | - } |
|
157 | + /** |
|
158 | + * Construct the model object with the given data, |
|
159 | + * and if there is a relation add it to relations array, |
|
160 | + * then save the model. |
|
161 | + */ |
|
162 | + foreach ($data as $key => $value) |
|
163 | + { |
|
164 | + $relation = camel_case($key); |
|
165 | + if (method_exists($model, $relation)) |
|
166 | + { |
|
167 | + $with[] = $relation; |
|
168 | + if (class_basename($model->$relation) == 'Collection') |
|
169 | + { |
|
170 | + if ( ! $value || ! count($value)) |
|
171 | + { |
|
172 | + $relations[$relation] = 'delete'; |
|
173 | + } |
|
174 | + } |
|
175 | + if (is_array($value)) |
|
176 | + { |
|
177 | + foreach ($value as $attr => $val) |
|
178 | + { |
|
179 | + $relationBaseModel = \Core::$relation()->model; |
|
180 | + if (class_basename($model->$relation) == 'Collection') |
|
181 | + { |
|
182 | + $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
183 | + if ( ! $relationModel) |
|
184 | + { |
|
185 | + $error = \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
186 | + abort($error['status'], $error['message']); |
|
187 | + } |
|
188 | 188 | |
189 | - foreach ($val as $attr => $val) |
|
190 | - { |
|
191 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
192 | - { |
|
193 | - $relationModel->$attr = $val; |
|
194 | - } |
|
195 | - } |
|
196 | - $relations[$relation][] = $relationModel; |
|
197 | - } |
|
198 | - else |
|
199 | - { |
|
200 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
201 | - { |
|
202 | - $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
203 | - if ( ! $relationModel) |
|
204 | - { |
|
205 | - $error = \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
206 | - abort($error['status'], $error['message']); |
|
207 | - } |
|
189 | + foreach ($val as $attr => $val) |
|
190 | + { |
|
191 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
192 | + { |
|
193 | + $relationModel->$attr = $val; |
|
194 | + } |
|
195 | + } |
|
196 | + $relations[$relation][] = $relationModel; |
|
197 | + } |
|
198 | + else |
|
199 | + { |
|
200 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
201 | + { |
|
202 | + $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
203 | + if ( ! $relationModel) |
|
204 | + { |
|
205 | + $error = \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
206 | + abort($error['status'], $error['message']); |
|
207 | + } |
|
208 | 208 | |
209 | - if (array_search($attr, $relationModel->getFillable(), true) !== false) |
|
210 | - { |
|
211 | - $relationModel->$attr = $val; |
|
212 | - $relations[$relation] = $relationModel; |
|
213 | - } |
|
214 | - } |
|
215 | - } |
|
216 | - } |
|
217 | - } |
|
218 | - } |
|
219 | - else if (array_search($key, $model->getFillable(), true) !== false) |
|
220 | - { |
|
221 | - $model->$key = $value; |
|
222 | - } |
|
223 | - } |
|
224 | - $model->save(); |
|
209 | + if (array_search($attr, $relationModel->getFillable(), true) !== false) |
|
210 | + { |
|
211 | + $relationModel->$attr = $val; |
|
212 | + $relations[$relation] = $relationModel; |
|
213 | + } |
|
214 | + } |
|
215 | + } |
|
216 | + } |
|
217 | + } |
|
218 | + } |
|
219 | + else if (array_search($key, $model->getFillable(), true) !== false) |
|
220 | + { |
|
221 | + $model->$key = $value; |
|
222 | + } |
|
223 | + } |
|
224 | + $model->save(); |
|
225 | 225 | |
226 | - foreach ($relations as $key => $value) |
|
227 | - { |
|
228 | - if ($value == 'delete' && $model->$key()->count()) |
|
229 | - { |
|
230 | - $model->$key()->delete(); |
|
231 | - } |
|
232 | - else if (gettype($value) == 'array') |
|
233 | - { |
|
234 | - $ids = []; |
|
235 | - foreach ($value as $val) |
|
236 | - { |
|
237 | - switch (class_basename($model->$key())) |
|
238 | - { |
|
239 | - case 'HasMany': |
|
240 | - $foreignKeyName = explode('.', $model->$key()->getForeignKey())[1]; |
|
241 | - $val->$foreignKeyName = $model->id; |
|
242 | - $val->save(); |
|
243 | - $ids[] = $val->id; |
|
244 | - break; |
|
226 | + foreach ($relations as $key => $value) |
|
227 | + { |
|
228 | + if ($value == 'delete' && $model->$key()->count()) |
|
229 | + { |
|
230 | + $model->$key()->delete(); |
|
231 | + } |
|
232 | + else if (gettype($value) == 'array') |
|
233 | + { |
|
234 | + $ids = []; |
|
235 | + foreach ($value as $val) |
|
236 | + { |
|
237 | + switch (class_basename($model->$key())) |
|
238 | + { |
|
239 | + case 'HasMany': |
|
240 | + $foreignKeyName = explode('.', $model->$key()->getForeignKey())[1]; |
|
241 | + $val->$foreignKeyName = $model->id; |
|
242 | + $val->save(); |
|
243 | + $ids[] = $val->id; |
|
244 | + break; |
|
245 | 245 | |
246 | - case 'BelongsToMany': |
|
247 | - $val->save(); |
|
248 | - $ids[] = $val->id; |
|
249 | - break; |
|
250 | - } |
|
251 | - } |
|
252 | - switch (class_basename($model->$key())) |
|
253 | - { |
|
254 | - case 'HasMany': |
|
255 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
256 | - break; |
|
246 | + case 'BelongsToMany': |
|
247 | + $val->save(); |
|
248 | + $ids[] = $val->id; |
|
249 | + break; |
|
250 | + } |
|
251 | + } |
|
252 | + switch (class_basename($model->$key())) |
|
253 | + { |
|
254 | + case 'HasMany': |
|
255 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
256 | + break; |
|
257 | 257 | |
258 | - case 'BelongsToMany': |
|
259 | - $model->$key()->detach(); |
|
260 | - $model->$key()->attach($ids); |
|
261 | - break; |
|
262 | - } |
|
263 | - } |
|
264 | - else |
|
265 | - { |
|
266 | - switch (class_basename($model->$key())) |
|
267 | - { |
|
268 | - case 'BelongsTo': |
|
269 | - $value->save(); |
|
270 | - $model->$key()->associate($value); |
|
271 | - $model->save(); |
|
272 | - break; |
|
273 | - } |
|
274 | - } |
|
275 | - } |
|
276 | - $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
|
277 | - }); |
|
258 | + case 'BelongsToMany': |
|
259 | + $model->$key()->detach(); |
|
260 | + $model->$key()->attach($ids); |
|
261 | + break; |
|
262 | + } |
|
263 | + } |
|
264 | + else |
|
265 | + { |
|
266 | + switch (class_basename($model->$key())) |
|
267 | + { |
|
268 | + case 'BelongsTo': |
|
269 | + $value->save(); |
|
270 | + $model->$key()->associate($value); |
|
271 | + $model->save(); |
|
272 | + break; |
|
273 | + } |
|
274 | + } |
|
275 | + } |
|
276 | + $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
|
277 | + }); |
|
278 | 278 | |
279 | - $this->afterSave($model, $relations); |
|
279 | + $this->afterSave($model, $relations); |
|
280 | 280 | |
281 | - return $this->find($model->id, $with); |
|
282 | - } |
|
281 | + return $this->find($model->id, $with); |
|
282 | + } |
|
283 | 283 | |
284 | - /** |
|
285 | - * Save the given models to the storage. |
|
286 | - * |
|
287 | - * @param array $data |
|
288 | - * @return array |
|
289 | - */ |
|
290 | - public function saveMany(array $data) |
|
291 | - { |
|
292 | - $result = []; |
|
293 | - \DB::transaction(function () use (&$result, $data) { |
|
294 | - foreach ($data as $key => $value) |
|
295 | - { |
|
296 | - $result[] = $this->save($value); |
|
297 | - } |
|
298 | - }); |
|
299 | - return $result; |
|
300 | - } |
|
284 | + /** |
|
285 | + * Save the given models to the storage. |
|
286 | + * |
|
287 | + * @param array $data |
|
288 | + * @return array |
|
289 | + */ |
|
290 | + public function saveMany(array $data) |
|
291 | + { |
|
292 | + $result = []; |
|
293 | + \DB::transaction(function () use (&$result, $data) { |
|
294 | + foreach ($data as $key => $value) |
|
295 | + { |
|
296 | + $result[] = $this->save($value); |
|
297 | + } |
|
298 | + }); |
|
299 | + return $result; |
|
300 | + } |
|
301 | 301 | |
302 | - /** |
|
303 | - * Update record in the storage based on the given |
|
304 | - * condition. |
|
305 | - * |
|
306 | - * @param var $value condition value |
|
307 | - * @param array $data |
|
308 | - * @param string $attribute condition column name |
|
309 | - * @return void |
|
310 | - */ |
|
311 | - public function update($value, array $data, $attribute = 'id', $saveLog = true) |
|
312 | - { |
|
313 | - if ($attribute == 'id') |
|
314 | - { |
|
315 | - $model = $this->model->lockForUpdate()->find($value); |
|
316 | - $model ? $model->update($data) : 0; |
|
317 | - $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
318 | - } |
|
319 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
320 | - $model->update($data); |
|
321 | - $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
322 | - }); |
|
323 | - } |
|
302 | + /** |
|
303 | + * Update record in the storage based on the given |
|
304 | + * condition. |
|
305 | + * |
|
306 | + * @param var $value condition value |
|
307 | + * @param array $data |
|
308 | + * @param string $attribute condition column name |
|
309 | + * @return void |
|
310 | + */ |
|
311 | + public function update($value, array $data, $attribute = 'id', $saveLog = true) |
|
312 | + { |
|
313 | + if ($attribute == 'id') |
|
314 | + { |
|
315 | + $model = $this->model->lockForUpdate()->find($value); |
|
316 | + $model ? $model->update($data) : 0; |
|
317 | + $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
318 | + } |
|
319 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
320 | + $model->update($data); |
|
321 | + $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
322 | + }); |
|
323 | + } |
|
324 | 324 | |
325 | - /** |
|
326 | - * Delete record from the storage based on the given |
|
327 | - * condition. |
|
328 | - * |
|
329 | - * @param var $value condition value |
|
330 | - * @param string $attribute condition column name |
|
331 | - * @return void |
|
332 | - */ |
|
333 | - public function delete($value, $attribute = 'id', $saveLog = true) |
|
334 | - { |
|
335 | - if ($attribute == 'id') |
|
336 | - { |
|
337 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
338 | - $model = $this->model->lockForUpdate()->find($value); |
|
339 | - $model->delete(); |
|
340 | - $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
341 | - }); |
|
342 | - } |
|
343 | - else |
|
344 | - { |
|
345 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
346 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
347 | - $model->delete(); |
|
348 | - $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
349 | - }); |
|
350 | - }); |
|
351 | - } |
|
352 | - } |
|
325 | + /** |
|
326 | + * Delete record from the storage based on the given |
|
327 | + * condition. |
|
328 | + * |
|
329 | + * @param var $value condition value |
|
330 | + * @param string $attribute condition column name |
|
331 | + * @return void |
|
332 | + */ |
|
333 | + public function delete($value, $attribute = 'id', $saveLog = true) |
|
334 | + { |
|
335 | + if ($attribute == 'id') |
|
336 | + { |
|
337 | + \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
338 | + $model = $this->model->lockForUpdate()->find($value); |
|
339 | + $model->delete(); |
|
340 | + $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
341 | + }); |
|
342 | + } |
|
343 | + else |
|
344 | + { |
|
345 | + \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
346 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
347 | + $model->delete(); |
|
348 | + $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
349 | + }); |
|
350 | + }); |
|
351 | + } |
|
352 | + } |
|
353 | 353 | |
354 | - /** |
|
355 | - * Fetch records from the storage based on the given |
|
356 | - * id. |
|
357 | - * |
|
358 | - * @param integer $id |
|
359 | - * @param array $relations |
|
360 | - * @param array $columns |
|
361 | - * @return object |
|
362 | - */ |
|
363 | - public function find($id, $relations = [], $columns = array('*')) |
|
364 | - { |
|
365 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
366 | - } |
|
354 | + /** |
|
355 | + * Fetch records from the storage based on the given |
|
356 | + * id. |
|
357 | + * |
|
358 | + * @param integer $id |
|
359 | + * @param array $relations |
|
360 | + * @param array $columns |
|
361 | + * @return object |
|
362 | + */ |
|
363 | + public function find($id, $relations = [], $columns = array('*')) |
|
364 | + { |
|
365 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
366 | + } |
|
367 | 367 | |
368 | - /** |
|
369 | - * Fetch records from the storage based on the given |
|
370 | - * condition. |
|
371 | - * |
|
372 | - * @param array $conditions array of conditions |
|
373 | - * @param array $relations |
|
374 | - * @param string $sortBy |
|
375 | - * @param boolean $desc |
|
376 | - * @param array $columns |
|
377 | - * @return collection |
|
378 | - */ |
|
379 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
380 | - { |
|
381 | - $conditions = $this->constructConditions($conditions); |
|
382 | - $sort = $desc ? 'desc' : 'asc'; |
|
383 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
384 | - } |
|
368 | + /** |
|
369 | + * Fetch records from the storage based on the given |
|
370 | + * condition. |
|
371 | + * |
|
372 | + * @param array $conditions array of conditions |
|
373 | + * @param array $relations |
|
374 | + * @param string $sortBy |
|
375 | + * @param boolean $desc |
|
376 | + * @param array $columns |
|
377 | + * @return collection |
|
378 | + */ |
|
379 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
380 | + { |
|
381 | + $conditions = $this->constructConditions($conditions); |
|
382 | + $sort = $desc ? 'desc' : 'asc'; |
|
383 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
384 | + } |
|
385 | 385 | |
386 | - /** |
|
387 | - * Fetch the first record from the storage based on the given |
|
388 | - * condition. |
|
389 | - * |
|
390 | - * @param array $conditions array of conditions |
|
391 | - * @param array $relations |
|
392 | - * @param array $columns |
|
393 | - * @return object |
|
394 | - */ |
|
395 | - public function first($conditions, $relations = [], $columns = array('*')) |
|
396 | - { |
|
397 | - $conditions = $this->constructConditions($conditions); |
|
398 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
399 | - } |
|
386 | + /** |
|
387 | + * Fetch the first record from the storage based on the given |
|
388 | + * condition. |
|
389 | + * |
|
390 | + * @param array $conditions array of conditions |
|
391 | + * @param array $relations |
|
392 | + * @param array $columns |
|
393 | + * @return object |
|
394 | + */ |
|
395 | + public function first($conditions, $relations = [], $columns = array('*')) |
|
396 | + { |
|
397 | + $conditions = $this->constructConditions($conditions); |
|
398 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
399 | + } |
|
400 | 400 | |
401 | - /** |
|
402 | - * Build the conditions recursively for the retrieving methods. |
|
403 | - * @param array $conditions |
|
404 | - * @return array |
|
405 | - */ |
|
406 | - protected function constructConditions($conditions) |
|
407 | - { |
|
408 | - $conditionString = ''; |
|
409 | - $conditionValues = []; |
|
410 | - foreach ($conditions as $key => $value) |
|
411 | - { |
|
412 | - if ($key == 'and') |
|
413 | - { |
|
414 | - $conditionString .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
415 | - $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
|
416 | - } |
|
417 | - else if ($key == 'or') |
|
418 | - { |
|
419 | - $conditionString .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
420 | - $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
|
421 | - } |
|
422 | - else |
|
423 | - { |
|
424 | - $conditionString .= $key . '=? {op} '; |
|
425 | - $conditionValues[] = $value; |
|
426 | - } |
|
427 | - } |
|
428 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
429 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
430 | - } |
|
401 | + /** |
|
402 | + * Build the conditions recursively for the retrieving methods. |
|
403 | + * @param array $conditions |
|
404 | + * @return array |
|
405 | + */ |
|
406 | + protected function constructConditions($conditions) |
|
407 | + { |
|
408 | + $conditionString = ''; |
|
409 | + $conditionValues = []; |
|
410 | + foreach ($conditions as $key => $value) |
|
411 | + { |
|
412 | + if ($key == 'and') |
|
413 | + { |
|
414 | + $conditionString .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
415 | + $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
|
416 | + } |
|
417 | + else if ($key == 'or') |
|
418 | + { |
|
419 | + $conditionString .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
420 | + $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
|
421 | + } |
|
422 | + else |
|
423 | + { |
|
424 | + $conditionString .= $key . '=? {op} '; |
|
425 | + $conditionValues[] = $value; |
|
426 | + } |
|
427 | + } |
|
428 | + $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
429 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
430 | + } |
|
431 | 431 | |
432 | - /** |
|
433 | - * Abstract method that is called in after |
|
434 | - * the save method finish. |
|
435 | - * |
|
436 | - * @param object $model |
|
437 | - * @param array $relations |
|
438 | - * @return void |
|
439 | - */ |
|
440 | - protected function afterSave($model, $relations) |
|
441 | - { |
|
442 | - return false; |
|
443 | - } |
|
432 | + /** |
|
433 | + * Abstract method that is called in after |
|
434 | + * the save method finish. |
|
435 | + * |
|
436 | + * @param object $model |
|
437 | + * @param array $relations |
|
438 | + * @return void |
|
439 | + */ |
|
440 | + protected function afterSave($model, $relations) |
|
441 | + { |
|
442 | + return false; |
|
443 | + } |
|
444 | 444 | |
445 | - /** |
|
446 | - * Abstract method that return the necessary |
|
447 | - * information (full model namespace) |
|
448 | - * needed to preform the previous actions. |
|
449 | - * |
|
450 | - * @return string |
|
451 | - */ |
|
452 | - abstract protected function getModel(); |
|
445 | + /** |
|
446 | + * Abstract method that return the necessary |
|
447 | + * information (full model namespace) |
|
448 | + * needed to preform the previous actions. |
|
449 | + * |
|
450 | + * @return string |
|
451 | + */ |
|
452 | + abstract protected function getModel(); |
|
453 | 453 | } |
454 | 454 | \ No newline at end of file |
@@ -6,223 +6,223 @@ |
||
6 | 6 | |
7 | 7 | class BaseApiController extends Controller |
8 | 8 | { |
9 | - /** |
|
10 | - * The model implementation. |
|
11 | - * |
|
12 | - * @var model |
|
13 | - */ |
|
14 | - protected $model; |
|
15 | - |
|
16 | - /** |
|
17 | - * The config implementation. |
|
18 | - * |
|
19 | - * @var config |
|
20 | - */ |
|
21 | - protected $config; |
|
22 | - |
|
23 | - public function __construct() |
|
24 | - { |
|
25 | - \Session::set('timeZoneDiff', \Request::header('time-zone-diff') ?: 0); |
|
9 | + /** |
|
10 | + * The model implementation. |
|
11 | + * |
|
12 | + * @var model |
|
13 | + */ |
|
14 | + protected $model; |
|
15 | + |
|
16 | + /** |
|
17 | + * The config implementation. |
|
18 | + * |
|
19 | + * @var config |
|
20 | + */ |
|
21 | + protected $config; |
|
22 | + |
|
23 | + public function __construct() |
|
24 | + { |
|
25 | + \Session::set('timeZoneDiff', \Request::header('time-zone-diff') ?: 0); |
|
26 | 26 | |
27 | - $this->config = \CoreConfig::getConfig(); |
|
28 | - $this->model = property_exists($this, 'model') ? $this->model : false; |
|
29 | - $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
|
30 | - $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
|
31 | - $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
|
32 | - $this->relations = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false; |
|
33 | - $route = explode('@',\Route::currentRouteAction())[1]; |
|
34 | - $this->checkPermission(explode('_', snake_case($route))[1]); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Fetch all records with relations from model repository. |
|
39 | - * |
|
40 | - * @param string $sortBy |
|
41 | - * @param boolean $desc |
|
42 | - * @return \Illuminate\Http\Response |
|
43 | - */ |
|
44 | - public function getIndex() |
|
45 | - { |
|
46 | - if ($this->model) |
|
47 | - { |
|
48 | - $relations = $this->relations && $this->relations['all'] ? $this->relations['all'] : []; |
|
49 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->all($relations), 200); |
|
50 | - } |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Fetch the single object with relations from model repository. |
|
55 | - * |
|
56 | - * @param integer $id |
|
57 | - * @return \Illuminate\Http\Response |
|
58 | - */ |
|
59 | - public function getFind($id) |
|
60 | - { |
|
61 | - if ($this->model) |
|
62 | - { |
|
63 | - $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : []; |
|
64 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find($id, $relations), 200); |
|
65 | - } |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Paginate all records with relations from model repository |
|
70 | - * that matche the given query. |
|
71 | - * |
|
72 | - * @param string $query |
|
73 | - * @param integer $perPage |
|
74 | - * @param string $sortBy |
|
75 | - * @param boolean $desc |
|
76 | - * @return \Illuminate\Http\Response |
|
77 | - */ |
|
78 | - public function getSearch($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
79 | - { |
|
80 | - if ($this->model) |
|
81 | - { |
|
82 | - $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; |
|
83 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->search($query, $perPage, $relations, $sortBy, $desc), 200); |
|
84 | - } |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Fetch records from the storage based on the given |
|
89 | - * condition. |
|
90 | - * |
|
91 | - * @param \Illuminate\Http\Request $request |
|
92 | - * @param string $sortBy |
|
93 | - * @param boolean $desc |
|
94 | - * @return \Illuminate\Http\Response |
|
95 | - */ |
|
96 | - public function postFindby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
97 | - { |
|
98 | - if ($this->model) |
|
99 | - { |
|
100 | - $relations = $this->relations && $this->relations['findBy'] ? $this->relations['findBy'] : []; |
|
101 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->findBy($request->all(), $relations, $sortBy, $desc), 200); |
|
102 | - } |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Fetch the first record from the storage based on the given |
|
107 | - * condition. |
|
108 | - * |
|
109 | - * @param \Illuminate\Http\Request $request |
|
110 | - * @return \Illuminate\Http\Response |
|
111 | - */ |
|
112 | - public function postFirst(Request $request) |
|
113 | - { |
|
114 | - if ($this->model) |
|
115 | - { |
|
116 | - $relations = $this->relations && $this->relations['first'] ? $this->relations['first'] : []; |
|
117 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->first($request->all(), $relations), 200); |
|
118 | - } |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Paginate all records with relations from model repository. |
|
123 | - * |
|
124 | - * @param integer $perPage |
|
125 | - * @param string $sortBy |
|
126 | - * @param boolean $desc |
|
127 | - * @return \Illuminate\Http\Response |
|
128 | - */ |
|
129 | - public function getPaginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
130 | - { |
|
131 | - if ($this->model) |
|
132 | - { |
|
133 | - $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; |
|
134 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginate($perPage, $relations, $sortBy, $desc), 200); |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Fetch all records with relations based on |
|
140 | - * the given condition from storage in pages. |
|
141 | - * |
|
142 | - * @param \Illuminate\Http\Request $request |
|
143 | - * @param integer $perPage |
|
144 | - * @param string $sortBy |
|
145 | - * @param boolean $desc |
|
146 | - * @return \Illuminate\Http\Response |
|
147 | - */ |
|
148 | - public function postPaginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
149 | - { |
|
150 | - if ($this->model) |
|
151 | - { |
|
152 | - $relations = $this->relations && $this->relations['paginateBy'] ? $this->relations['paginateBy'] : []; |
|
153 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginateBy($request->all(), $perPage, $relations, $sortBy, $desc), 200); |
|
154 | - } |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Save the given model to repository. |
|
159 | - * |
|
160 | - * @param \Illuminate\Http\Request $request |
|
161 | - * @return \Illuminate\Http\Response |
|
162 | - */ |
|
163 | - public function postSave(Request $request) |
|
164 | - { |
|
165 | - foreach ($this->validationRules as &$rule) |
|
166 | - { |
|
167 | - if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) |
|
168 | - { |
|
169 | - $rule .= ',deleted_at,NULL'; |
|
170 | - } |
|
171 | - |
|
172 | - if ($request->has('id')) |
|
173 | - { |
|
174 | - $rule = str_replace('{id}', $request->get('id'), $rule); |
|
175 | - } |
|
176 | - else |
|
177 | - { |
|
178 | - $rule = str_replace(',{id}', '', $rule); |
|
179 | - } |
|
180 | - } |
|
27 | + $this->config = \CoreConfig::getConfig(); |
|
28 | + $this->model = property_exists($this, 'model') ? $this->model : false; |
|
29 | + $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
|
30 | + $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
|
31 | + $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
|
32 | + $this->relations = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false; |
|
33 | + $route = explode('@',\Route::currentRouteAction())[1]; |
|
34 | + $this->checkPermission(explode('_', snake_case($route))[1]); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Fetch all records with relations from model repository. |
|
39 | + * |
|
40 | + * @param string $sortBy |
|
41 | + * @param boolean $desc |
|
42 | + * @return \Illuminate\Http\Response |
|
43 | + */ |
|
44 | + public function getIndex() |
|
45 | + { |
|
46 | + if ($this->model) |
|
47 | + { |
|
48 | + $relations = $this->relations && $this->relations['all'] ? $this->relations['all'] : []; |
|
49 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->all($relations), 200); |
|
50 | + } |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Fetch the single object with relations from model repository. |
|
55 | + * |
|
56 | + * @param integer $id |
|
57 | + * @return \Illuminate\Http\Response |
|
58 | + */ |
|
59 | + public function getFind($id) |
|
60 | + { |
|
61 | + if ($this->model) |
|
62 | + { |
|
63 | + $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : []; |
|
64 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find($id, $relations), 200); |
|
65 | + } |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Paginate all records with relations from model repository |
|
70 | + * that matche the given query. |
|
71 | + * |
|
72 | + * @param string $query |
|
73 | + * @param integer $perPage |
|
74 | + * @param string $sortBy |
|
75 | + * @param boolean $desc |
|
76 | + * @return \Illuminate\Http\Response |
|
77 | + */ |
|
78 | + public function getSearch($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
79 | + { |
|
80 | + if ($this->model) |
|
81 | + { |
|
82 | + $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; |
|
83 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->search($query, $perPage, $relations, $sortBy, $desc), 200); |
|
84 | + } |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Fetch records from the storage based on the given |
|
89 | + * condition. |
|
90 | + * |
|
91 | + * @param \Illuminate\Http\Request $request |
|
92 | + * @param string $sortBy |
|
93 | + * @param boolean $desc |
|
94 | + * @return \Illuminate\Http\Response |
|
95 | + */ |
|
96 | + public function postFindby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
97 | + { |
|
98 | + if ($this->model) |
|
99 | + { |
|
100 | + $relations = $this->relations && $this->relations['findBy'] ? $this->relations['findBy'] : []; |
|
101 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->findBy($request->all(), $relations, $sortBy, $desc), 200); |
|
102 | + } |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Fetch the first record from the storage based on the given |
|
107 | + * condition. |
|
108 | + * |
|
109 | + * @param \Illuminate\Http\Request $request |
|
110 | + * @return \Illuminate\Http\Response |
|
111 | + */ |
|
112 | + public function postFirst(Request $request) |
|
113 | + { |
|
114 | + if ($this->model) |
|
115 | + { |
|
116 | + $relations = $this->relations && $this->relations['first'] ? $this->relations['first'] : []; |
|
117 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->first($request->all(), $relations), 200); |
|
118 | + } |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Paginate all records with relations from model repository. |
|
123 | + * |
|
124 | + * @param integer $perPage |
|
125 | + * @param string $sortBy |
|
126 | + * @param boolean $desc |
|
127 | + * @return \Illuminate\Http\Response |
|
128 | + */ |
|
129 | + public function getPaginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
130 | + { |
|
131 | + if ($this->model) |
|
132 | + { |
|
133 | + $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; |
|
134 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginate($perPage, $relations, $sortBy, $desc), 200); |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Fetch all records with relations based on |
|
140 | + * the given condition from storage in pages. |
|
141 | + * |
|
142 | + * @param \Illuminate\Http\Request $request |
|
143 | + * @param integer $perPage |
|
144 | + * @param string $sortBy |
|
145 | + * @param boolean $desc |
|
146 | + * @return \Illuminate\Http\Response |
|
147 | + */ |
|
148 | + public function postPaginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
149 | + { |
|
150 | + if ($this->model) |
|
151 | + { |
|
152 | + $relations = $this->relations && $this->relations['paginateBy'] ? $this->relations['paginateBy'] : []; |
|
153 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginateBy($request->all(), $perPage, $relations, $sortBy, $desc), 200); |
|
154 | + } |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Save the given model to repository. |
|
159 | + * |
|
160 | + * @param \Illuminate\Http\Request $request |
|
161 | + * @return \Illuminate\Http\Response |
|
162 | + */ |
|
163 | + public function postSave(Request $request) |
|
164 | + { |
|
165 | + foreach ($this->validationRules as &$rule) |
|
166 | + { |
|
167 | + if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) |
|
168 | + { |
|
169 | + $rule .= ',deleted_at,NULL'; |
|
170 | + } |
|
171 | + |
|
172 | + if ($request->has('id')) |
|
173 | + { |
|
174 | + $rule = str_replace('{id}', $request->get('id'), $rule); |
|
175 | + } |
|
176 | + else |
|
177 | + { |
|
178 | + $rule = str_replace(',{id}', '', $rule); |
|
179 | + } |
|
180 | + } |
|
181 | 181 | |
182 | - $this->validate($request, $this->validationRules); |
|
183 | - |
|
184 | - if ($this->model) |
|
185 | - { |
|
186 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->save($request->all()), 200); |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Delete by the given id from model repository. |
|
192 | - * |
|
193 | - * @param integer $id |
|
194 | - * @return \Illuminate\Http\Response |
|
195 | - */ |
|
196 | - public function getDelete($id) |
|
197 | - { |
|
198 | - if ($this->model) |
|
199 | - { |
|
200 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->delete($id), 200); |
|
201 | - } |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Check if the logged in user can do the given permission. |
|
206 | - * |
|
207 | - * @param string $permission |
|
208 | - * @return void |
|
209 | - */ |
|
210 | - private function checkPermission($permission) |
|
211 | - { |
|
212 | - $permission = $permission !== 'index' ? $permission : 'list'; |
|
213 | - if ($permission == 'method') |
|
214 | - { |
|
215 | - $error = \ErrorHandler::notFound('method'); |
|
216 | - abort($error['status'], $error['message']); |
|
217 | - } |
|
218 | - else if ( ! in_array($permission, $this->skipLoginCheck)) |
|
219 | - { |
|
220 | - \JWTAuth::parseToken()->authenticate(); |
|
221 | - if ( ! in_array($permission, $this->skipPermissionCheck) && ! \Core::users()->can($permission, $this->model)) |
|
222 | - { |
|
223 | - $error = \ErrorHandler::noPermissions(); |
|
224 | - abort($error['status'], $error['message']); |
|
225 | - } |
|
226 | - } |
|
227 | - } |
|
182 | + $this->validate($request, $this->validationRules); |
|
183 | + |
|
184 | + if ($this->model) |
|
185 | + { |
|
186 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->save($request->all()), 200); |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Delete by the given id from model repository. |
|
192 | + * |
|
193 | + * @param integer $id |
|
194 | + * @return \Illuminate\Http\Response |
|
195 | + */ |
|
196 | + public function getDelete($id) |
|
197 | + { |
|
198 | + if ($this->model) |
|
199 | + { |
|
200 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->delete($id), 200); |
|
201 | + } |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Check if the logged in user can do the given permission. |
|
206 | + * |
|
207 | + * @param string $permission |
|
208 | + * @return void |
|
209 | + */ |
|
210 | + private function checkPermission($permission) |
|
211 | + { |
|
212 | + $permission = $permission !== 'index' ? $permission : 'list'; |
|
213 | + if ($permission == 'method') |
|
214 | + { |
|
215 | + $error = \ErrorHandler::notFound('method'); |
|
216 | + abort($error['status'], $error['message']); |
|
217 | + } |
|
218 | + else if ( ! in_array($permission, $this->skipLoginCheck)) |
|
219 | + { |
|
220 | + \JWTAuth::parseToken()->authenticate(); |
|
221 | + if ( ! in_array($permission, $this->skipPermissionCheck) && ! \Core::users()->can($permission, $this->model)) |
|
222 | + { |
|
223 | + $error = \ErrorHandler::noPermissions(); |
|
224 | + abort($error['status'], $error['message']); |
|
225 | + } |
|
226 | + } |
|
227 | + } |
|
228 | 228 | } |
@@ -3,127 +3,127 @@ |
||
3 | 3 | interface RepositoryInterface |
4 | 4 | { |
5 | 5 | /** |
6 | - * Fetch all records with relations from the storage. |
|
7 | - * |
|
8 | - * @param array $relations |
|
9 | - * @param array $sortBy |
|
10 | - * @param array $desc |
|
11 | - * @param array $columns |
|
12 | - * @return collection |
|
13 | - */ |
|
6 | + * Fetch all records with relations from the storage. |
|
7 | + * |
|
8 | + * @param array $relations |
|
9 | + * @param array $sortBy |
|
10 | + * @param array $desc |
|
11 | + * @param array $columns |
|
12 | + * @return collection |
|
13 | + */ |
|
14 | 14 | public function all($relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
15 | 15 | |
16 | - /** |
|
17 | - * Fetch all records with relations from storage in pages |
|
18 | - * that matche the given query. |
|
19 | - * |
|
20 | - * @param string $query |
|
21 | - * @param integer $perPage |
|
22 | - * @param array $relations |
|
23 | - * @param array $sortBy |
|
24 | - * @param array $desc |
|
25 | - * @param array $columns |
|
26 | - * @return collection |
|
27 | - */ |
|
28 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
16 | + /** |
|
17 | + * Fetch all records with relations from storage in pages |
|
18 | + * that matche the given query. |
|
19 | + * |
|
20 | + * @param string $query |
|
21 | + * @param integer $perPage |
|
22 | + * @param array $relations |
|
23 | + * @param array $sortBy |
|
24 | + * @param array $desc |
|
25 | + * @param array $columns |
|
26 | + * @return collection |
|
27 | + */ |
|
28 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
29 | 29 | |
30 | 30 | /** |
31 | - * Fetch all records with relations from storage in pages. |
|
32 | - * |
|
33 | - * @param integer $perPage |
|
34 | - * @param array $relations |
|
35 | - * @param array $sortBy |
|
36 | - * @param array $desc |
|
37 | - * @param array $columns |
|
38 | - * @return collection |
|
39 | - */ |
|
40 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
31 | + * Fetch all records with relations from storage in pages. |
|
32 | + * |
|
33 | + * @param integer $perPage |
|
34 | + * @param array $relations |
|
35 | + * @param array $sortBy |
|
36 | + * @param array $desc |
|
37 | + * @param array $columns |
|
38 | + * @return collection |
|
39 | + */ |
|
40 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
41 | 41 | |
42 | 42 | /** |
43 | - * Fetch all records with relations based on |
|
44 | - * the given condition from storage in pages. |
|
45 | - * |
|
46 | - * @param array $conditions array of conditions |
|
47 | - * @param integer $perPage |
|
48 | - * @param array $relations |
|
49 | - * @param array $sortBy |
|
50 | - * @param array $desc |
|
51 | - * @param array $columns |
|
52 | - * @return collection |
|
53 | - */ |
|
54 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
43 | + * Fetch all records with relations based on |
|
44 | + * the given condition from storage in pages. |
|
45 | + * |
|
46 | + * @param array $conditions array of conditions |
|
47 | + * @param integer $perPage |
|
48 | + * @param array $relations |
|
49 | + * @param array $sortBy |
|
50 | + * @param array $desc |
|
51 | + * @param array $columns |
|
52 | + * @return collection |
|
53 | + */ |
|
54 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
55 | 55 | |
56 | - /** |
|
57 | - * Save the given model/models to the storage. |
|
58 | - * |
|
59 | - * @param array $data |
|
60 | - * @param boolean $saveLog |
|
61 | - * @return object |
|
62 | - */ |
|
63 | - public function save(array $data, $saveLog = true); |
|
56 | + /** |
|
57 | + * Save the given model/models to the storage. |
|
58 | + * |
|
59 | + * @param array $data |
|
60 | + * @param boolean $saveLog |
|
61 | + * @return object |
|
62 | + */ |
|
63 | + public function save(array $data, $saveLog = true); |
|
64 | 64 | |
65 | - /** |
|
66 | - * Insert multiple records to the storage. |
|
67 | - * |
|
68 | - * @param array $data |
|
69 | - * @return array |
|
70 | - */ |
|
71 | - public function saveMany(array $data); |
|
65 | + /** |
|
66 | + * Insert multiple records to the storage. |
|
67 | + * |
|
68 | + * @param array $data |
|
69 | + * @return array |
|
70 | + */ |
|
71 | + public function saveMany(array $data); |
|
72 | 72 | |
73 | 73 | /** |
74 | - * Update record in the storage based on the given |
|
75 | - * condition. |
|
76 | - * |
|
77 | - * @param [type] $value condition value |
|
78 | - * @param array $data |
|
79 | - * @param string $attribute condition column name |
|
80 | - * @return integer affected rows |
|
81 | - */ |
|
82 | - public function update($value, array $data, $attribute = 'id'); |
|
74 | + * Update record in the storage based on the given |
|
75 | + * condition. |
|
76 | + * |
|
77 | + * @param [type] $value condition value |
|
78 | + * @param array $data |
|
79 | + * @param string $attribute condition column name |
|
80 | + * @return integer affected rows |
|
81 | + */ |
|
82 | + public function update($value, array $data, $attribute = 'id'); |
|
83 | 83 | |
84 | 84 | /** |
85 | - * Delete record from the storage based on the given |
|
86 | - * condition. |
|
87 | - * |
|
88 | - * @param [type] $value condition value |
|
89 | - * @param string $attribute condition column name |
|
90 | - * @return integer affected rows |
|
91 | - */ |
|
92 | - public function delete($value, $attribute = 'id'); |
|
85 | + * Delete record from the storage based on the given |
|
86 | + * condition. |
|
87 | + * |
|
88 | + * @param [type] $value condition value |
|
89 | + * @param string $attribute condition column name |
|
90 | + * @return integer affected rows |
|
91 | + */ |
|
92 | + public function delete($value, $attribute = 'id'); |
|
93 | 93 | |
94 | 94 | /** |
95 | - * Fetch records from the storage based on the given |
|
96 | - * id. |
|
97 | - * |
|
98 | - * @param integer $id |
|
99 | - * @param array $relations |
|
100 | - * @param array $columns |
|
101 | - * @return object |
|
102 | - */ |
|
103 | - public function find($id, $relations = [], $columns = array('*')); |
|
95 | + * Fetch records from the storage based on the given |
|
96 | + * id. |
|
97 | + * |
|
98 | + * @param integer $id |
|
99 | + * @param array $relations |
|
100 | + * @param array $columns |
|
101 | + * @return object |
|
102 | + */ |
|
103 | + public function find($id, $relations = [], $columns = array('*')); |
|
104 | 104 | |
105 | 105 | /** |
106 | - * Fetch records from the storage based on the given |
|
107 | - * condition. |
|
108 | - * |
|
109 | - * @param array $conditions array of conditions |
|
110 | - * @param array $relations |
|
111 | - * @param array $sortBy |
|
112 | - * @param array $desc |
|
113 | - * @param array $columns |
|
114 | - * @return collection |
|
115 | - */ |
|
116 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
106 | + * Fetch records from the storage based on the given |
|
107 | + * condition. |
|
108 | + * |
|
109 | + * @param array $conditions array of conditions |
|
110 | + * @param array $relations |
|
111 | + * @param array $sortBy |
|
112 | + * @param array $desc |
|
113 | + * @param array $columns |
|
114 | + * @return collection |
|
115 | + */ |
|
116 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
117 | 117 | |
118 | - /** |
|
119 | - * Fetch the first record fro the storage based on the given |
|
120 | - * condition. |
|
121 | - * |
|
122 | - * @param array $conditions array of conditions |
|
123 | - * @param [type] $value condition value |
|
124 | - * @param array $relations |
|
125 | - * @param array $columns |
|
126 | - * @return object |
|
127 | - */ |
|
128 | - public function first($conditions, $relations = [], $columns = array('*')); |
|
118 | + /** |
|
119 | + * Fetch the first record fro the storage based on the given |
|
120 | + * condition. |
|
121 | + * |
|
122 | + * @param array $conditions array of conditions |
|
123 | + * @param [type] $value condition value |
|
124 | + * @param array $relations |
|
125 | + * @param array $columns |
|
126 | + * @return object |
|
127 | + */ |
|
128 | + public function first($conditions, $relations = [], $columns = array('*')); |
|
129 | 129 | } |
130 | 130 | \ No newline at end of file |