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