1
|
|
|
<?php |
2
|
|
|
namespace NOSQL\Api\base; |
3
|
|
|
|
4
|
|
|
use NOSQL\Dto\Model\ResultsetDto; |
5
|
|
|
use NOSQL\Models\NOSQLActiveRecord; |
6
|
|
|
use NOSQL\Models\NOSQLQuery; |
7
|
|
|
use NOSQL\Services\Base\NOSQLManagerTrait; |
8
|
|
|
use PSFS\base\dto\JsonResponse; |
9
|
|
|
use PSFS\base\Logger; |
10
|
|
|
use PSFS\base\types\CustomApi; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class NOSQLBase |
14
|
|
|
* @package NOSQL\Api\base |
15
|
|
|
* @method NOSQLActiveRecord getModel() |
16
|
|
|
* @property NOSQLActiveRecord $model |
17
|
|
|
*/ |
18
|
|
|
abstract class NOSQLBase extends CustomApi { |
19
|
|
|
use NOSQLManagerTrait; |
20
|
|
|
|
21
|
|
|
const NOSQL_MODEL_PRIMARY_KEY = '_id'; |
22
|
|
|
/** |
23
|
|
|
* @Injectable |
24
|
|
|
* @var \NOSQL\Services\ParserService |
25
|
|
|
*/ |
26
|
|
|
protected $parser; |
27
|
|
|
/** |
28
|
|
|
* @var \MongoDB\Database |
29
|
|
|
*/ |
30
|
|
|
protected $con; |
31
|
|
|
|
32
|
|
|
public function getModelTableMap() |
33
|
|
|
{ |
34
|
|
|
return null; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param int $status |
39
|
|
|
*/ |
40
|
|
|
public function closeTransaction($status) { } |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @throws \NOSQL\Exceptions\NOSQLValidationException |
44
|
|
|
* @throws \ReflectionException |
45
|
|
|
*/ |
46
|
|
|
public function init() |
47
|
|
|
{ |
48
|
|
|
parent::init(); |
49
|
|
|
$this->con = $this->parser->createConnection($this->getDomain()); |
50
|
|
|
$this->model = $this->parser->hydrateModelFromRequest($this->data, get_called_class()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getApi() |
57
|
|
|
{ |
58
|
|
|
$class = explode('\\', get_called_class()); |
59
|
|
|
return $class[count($class)-1]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getDomain() |
66
|
|
|
{ |
67
|
|
|
$class = explode('\\', get_called_class()); |
68
|
|
|
return $class[0]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function modelList() |
72
|
|
|
{ |
73
|
|
|
$success = true; |
74
|
|
|
$code = 200; |
75
|
|
|
try { |
76
|
|
|
$className = get_called_class(); |
77
|
|
|
$modelName = $className::MODEL_CLASS; |
78
|
|
|
$results = NOSQLQuery::find($modelName, $this->query, $this->con); |
79
|
|
|
} catch (\Exception $exception) { |
80
|
|
|
$results = new ResultsetDto(false); |
81
|
|
|
$success = false; |
82
|
|
|
$code = 404; |
83
|
|
|
Logger::log($exception->getMessage(), LOG_WARNING, $this->query); |
84
|
|
|
} |
85
|
|
|
return $this->json(new JsonResponse($results->items, $success, $results->count, ceil($results->count / $results->limit)), $code); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function get($pk) |
89
|
|
|
{ |
90
|
|
|
$success = true; |
91
|
|
|
$code = 200; |
92
|
|
|
try { |
93
|
|
|
$this->feedModel($pk); |
94
|
|
|
} catch (\Exception $exception) { |
95
|
|
|
$this->model = null; |
96
|
|
|
$success = false; |
97
|
|
|
$code = 404; |
98
|
|
|
Logger::log($exception->getMessage(), LOG_WARNING, [$pk]); |
99
|
|
|
} |
100
|
|
|
return $this->json(new JsonResponse(null !== $this->model ? $this->getModel()->toArray() : [], $success), $code); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function post() |
104
|
|
|
{ |
105
|
|
|
$success = false; |
106
|
|
|
$code = $message = null; |
107
|
|
|
try { |
108
|
|
|
$success = $this->getModel()->save($this->con); |
109
|
|
|
} catch (\Exception $exception) { |
110
|
|
|
$success = false; |
111
|
|
|
$message = $exception->getMessage(); |
112
|
|
|
Logger::log($message, LOG_WARNING, $this->getModel()->toArray()); |
113
|
|
|
} finally { |
114
|
|
|
$code = $success && null === $code ? 200 : 400; |
115
|
|
|
} |
116
|
|
|
return $this->json(new JsonResponse($this->getModel()->toArray(), $success, null, null, $message), $code); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function put($pk) |
120
|
|
|
{ |
121
|
|
|
$code = $message = null; |
122
|
|
|
try { |
123
|
|
|
$this->feedModel($pk); |
124
|
|
|
$this->getModel()->feed($this->getRequest()->getData()); |
125
|
|
|
$success = $this->getModel()->update($this->con); |
126
|
|
|
} catch (\Exception $exception) { |
127
|
|
|
$this->model = null; |
128
|
|
|
$success = false; |
129
|
|
|
$message = $exception->getMessage(); |
130
|
|
|
Logger::log($message, LOG_WARNING, [$pk]); |
131
|
|
|
} finally { |
132
|
|
|
$code = $success && null === $code ? 200 : 400; |
133
|
|
|
} |
134
|
|
|
return $this->json(new JsonResponse(null !== $this->model ? $this->getModel()->toArray() : [], $success, null, null, $message), $code); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function delete($pk = null) |
138
|
|
|
{ |
139
|
|
|
$code = 200; |
140
|
|
|
try { |
141
|
|
|
$this->feedModel($pk); |
142
|
|
|
$success = $this->getModel()->delete($this->con); |
143
|
|
|
} catch (\Exception $exception) { |
144
|
|
|
$this->model = null; |
145
|
|
|
$success = false; |
146
|
|
|
$code = 404; |
147
|
|
|
Logger::log($exception->getMessage(), LOG_WARNING, [$pk]); |
148
|
|
|
} |
149
|
|
|
return $this->json(new JsonResponse([], $success), $code); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function bulk() |
153
|
|
|
{ |
154
|
|
|
$code = 200; |
155
|
|
|
try { |
156
|
|
|
$className = get_called_class(); |
157
|
|
|
$modelName = $className::MODEL_CLASS; |
158
|
|
|
$this->model = new $modelName(); |
159
|
|
|
$inserts = $this->model->bulkInsert($this->getRequest()->getData()); |
160
|
|
|
$success = $inserts > 0; |
161
|
|
|
} catch (\Exception $exception) { |
162
|
|
|
$inserts = 0; |
163
|
|
|
$success = false; |
164
|
|
|
$code = 404; |
165
|
|
|
Logger::log($exception->getMessage(), LOG_WARNING); |
166
|
|
|
} |
167
|
|
|
return $this->json(new JsonResponse($success, $success, $inserts), $code); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param $pk |
172
|
|
|
* @throws \PSFS\base\exception\ApiException |
173
|
|
|
*/ |
174
|
|
|
private function feedModel($pk) |
175
|
|
|
{ |
176
|
|
|
$className = get_called_class(); |
177
|
|
|
$modelName = $className::MODEL_CLASS; |
178
|
|
|
$this->model = NOSQLQuery::findPk($modelName, $pk); |
179
|
|
|
} |
180
|
|
|
} |