1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Analytics |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the LICENSE.md file. |
7
|
|
|
* |
8
|
|
|
* @author Marcel Scherello <[email protected]> |
9
|
|
|
* @copyright 2020 Marcel Scherello |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OCA\Analytics\Controller; |
13
|
|
|
|
14
|
|
|
use OCA\Analytics\Activity\ActivityManager; |
15
|
|
|
use OCP\AppFramework\ApiController; |
16
|
|
|
use OCP\AppFramework\Http; |
17
|
|
|
use OCP\AppFramework\Http\DataResponse; |
18
|
|
|
use OCP\ILogger; |
19
|
|
|
use OCP\IRequest; |
20
|
|
|
use OCP\IUserSession; |
21
|
|
|
|
22
|
|
|
class ApiDataController extends ApiController |
23
|
|
|
{ |
24
|
|
|
const UNKNOWN = 9001; |
25
|
|
|
const MISSING_PARAM = 9002; |
26
|
|
|
const NOT_FOUND = 9003; |
27
|
|
|
const NOT_ALLOWED = 9004; |
28
|
|
|
|
29
|
|
|
protected $errors = []; |
30
|
|
|
private $logger; |
31
|
|
|
private $userSession; |
32
|
|
|
private $ActivityManager; |
33
|
|
|
private $DatasetController; |
34
|
|
|
private $StorageController; |
35
|
|
|
|
36
|
|
|
public function __construct( |
37
|
|
|
$appName, |
38
|
|
|
IRequest $request, |
39
|
|
|
ILogger $logger, |
40
|
|
|
IUserSession $userSession, |
41
|
|
|
ActivityManager $ActivityManager, |
42
|
|
|
DatasetController $DatasetController, |
43
|
|
|
StorageController $StorageController |
44
|
|
|
) |
45
|
|
|
{ |
46
|
|
|
parent::__construct( |
47
|
|
|
$appName, |
48
|
|
|
$request, |
49
|
|
|
'POST' |
50
|
|
|
); |
51
|
|
|
$this->logger = $logger; |
52
|
|
|
$this->userSession = $userSession; |
53
|
|
|
$this->ActivityManager = $ActivityManager; |
54
|
|
|
$this->DatasetController = $DatasetController; |
55
|
|
|
$this->StorageController = $StorageController; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* add data via there database names |
60
|
|
|
* @CORS |
61
|
|
|
* @NoCSRFRequired |
62
|
|
|
* @NoAdminRequired |
63
|
|
|
* @param int $datasetId |
64
|
|
|
* @return DataResponse |
65
|
|
|
* @throws \Exception |
66
|
|
|
*/ |
67
|
|
|
public function addData(int $datasetId) |
68
|
|
|
{ |
69
|
|
|
$params = $this->request->getParams(); |
70
|
|
|
//$this->logger->debug($datasetId); |
71
|
|
|
$datasetMetadata = $this->DatasetController->getOwnDataset($datasetId); |
72
|
|
|
|
73
|
|
|
if (empty($datasetMetadata)) { |
74
|
|
|
$this->errors[] = 'Unknown report or dataset'; |
75
|
|
|
return $this->requestResponse(false, self::NOT_FOUND, implode(',', $this->errors)); |
76
|
|
|
} elseif ((int)$datasetMetadata['type'] !== 2) { |
77
|
|
|
$this->errors[] = 'Report does not allow data maintenance'; |
78
|
|
|
return $this->requestResponse(false, self::NOT_ALLOWED, implode(',', $this->errors)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (!isset($params['dimension1'])) { |
82
|
|
|
$this->errors[] = 'Dimension 1 required'; |
83
|
|
|
} elseif (!isset($params['dimension2'])) { |
84
|
|
|
$this->errors[] = 'Dimension 2 required'; |
85
|
|
|
} elseif (!isset($params['dimension3'])) { |
86
|
|
|
$this->errors[] = 'Dimension 3 required'; |
87
|
|
|
} |
88
|
|
|
if (!empty($this->errors)) { |
89
|
|
|
return $this->requestResponse(false, self::MISSING_PARAM, implode(',', $this->errors)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->StorageController->update($datasetId, $params['dimension1'], $params['dimension2'], $params['dimension3']); |
93
|
|
|
$this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD_API); |
94
|
|
|
|
95
|
|
|
return $this->requestResponse( |
96
|
|
|
true, |
97
|
|
|
Http::STATUS_OK, |
98
|
|
|
'Data update successfull'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* add data via there real field names |
103
|
|
|
* @CORS |
104
|
|
|
* @NoCSRFRequired |
105
|
|
|
* @NoAdminRequired |
106
|
|
|
* @param int $datasetId |
107
|
|
|
* @return DataResponse |
108
|
|
|
* @throws \Exception |
109
|
|
|
*/ |
110
|
|
|
public function addDataV2(int $datasetId) |
111
|
|
|
{ |
112
|
|
|
$params = $this->request->getParams(); |
113
|
|
|
$datasetMetadata = $this->DatasetController->getOwnDataset($datasetId); |
114
|
|
|
|
115
|
|
|
if (empty($datasetMetadata)) { |
116
|
|
|
$this->errors[] = 'Unknown report or dataset'; |
117
|
|
|
return $this->requestResponse(false, self::NOT_FOUND, implode(',', $this->errors)); |
118
|
|
|
} elseif ((int)$datasetMetadata['type'] !== 2) { |
119
|
|
|
$this->errors[] = 'Report does not allow data maintenance'; |
120
|
|
|
return $this->requestResponse(false, self::NOT_ALLOWED, implode(',', $this->errors)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (!isset($params[$datasetMetadata['dimension1']])) { |
124
|
|
|
$this->errors[] = $datasetMetadata['dimension1'] . ' required'; |
125
|
|
|
} elseif (!isset($params[$datasetMetadata['dimension2']])) { |
126
|
|
|
$this->errors[] = $datasetMetadata['dimension2'] . ' required'; |
127
|
|
|
} elseif (!isset($params[$datasetMetadata['value']])) { |
128
|
|
|
$this->errors[] = $datasetMetadata['value'] . ' required'; |
129
|
|
|
} |
130
|
|
|
if (!empty($this->errors)) { |
131
|
|
|
return $this->requestResponse(false, self::MISSING_PARAM, implode(',', $this->errors)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->StorageController->update($datasetId, |
135
|
|
|
$params[$datasetMetadata['dimension1']], |
136
|
|
|
$params[$datasetMetadata['dimension2']], |
137
|
|
|
$params[$datasetMetadata['value']]); |
138
|
|
|
$this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD_API); |
139
|
|
|
|
140
|
|
|
return $this->requestResponse( |
141
|
|
|
true, |
142
|
|
|
Http::STATUS_OK, |
143
|
|
|
'Data update successfull'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param bool $success |
148
|
|
|
* @param int|null $code |
149
|
|
|
* @param string|null $message |
150
|
|
|
* @return DataResponse |
151
|
|
|
*/ |
152
|
|
|
protected function requestResponse($success, $code = null, $message = null) |
153
|
|
|
{ |
154
|
|
|
if (!$success) { |
155
|
|
|
if ($code === null) { |
156
|
|
|
$code = self::UNKNOWN; |
157
|
|
|
} |
158
|
|
|
$array = [ |
159
|
|
|
'success' => false, |
160
|
|
|
'error' => ['code' => $code, |
161
|
|
|
'message' => $message |
162
|
|
|
] |
163
|
|
|
]; |
164
|
|
|
} else { |
165
|
|
|
$array = [ |
166
|
|
|
'success' => true, |
167
|
|
|
'message' => $message |
168
|
|
|
]; |
169
|
|
|
} |
170
|
|
|
$response = new DataResponse(); |
171
|
|
|
$response->setData($array)->render(); |
172
|
|
|
return $response; |
173
|
|
|
} |
174
|
|
|
// curl -u Admin:2sroW-SxRcK-AmdsF-RYMJ5-CKSyf -d '{"dimension1": "x", "simension2": "x", "dimension3": "333,3"}' -X POST -H "Content-Type: application/json" http://nc18/nextcloud/apps/analytics/api/1.0/adddata/158 |
175
|
|
|
// curl -u Admin:2sroW-SxRcK-AmdsF-RYMJ5-CKSyf -d '{"Spalte 1": "x", "Spalte 2": "x", "toller wert": "333,3"}' -X POST -H "Content-Type: application/json" http://nc18/nextcloud/apps/analytics/api/2.0/adddata/158 |
176
|
|
|
} |