|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Data 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 2019 Marcel Scherello |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace OCA\Analytics\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use OCA\Analytics\Activity\ActivityManager; |
|
15
|
|
|
use OCA\Analytics\Service\DataService; |
|
16
|
|
|
use OCA\Analytics\Service\DatasetService; |
|
17
|
|
|
use OCP\AppFramework\ApiController; |
|
18
|
|
|
use OCP\AppFramework\Http; |
|
19
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
20
|
|
|
use OCP\ILogger; |
|
21
|
|
|
use OCP\IRequest; |
|
22
|
|
|
use OCP\IUserSession; |
|
23
|
|
|
|
|
24
|
|
|
class ApiDataController extends ApiController |
|
25
|
|
|
{ |
|
26
|
|
|
const UNKNOWN = 9999; |
|
27
|
|
|
const MISSING_PARAM = 9001; |
|
28
|
|
|
const NOT_FOUND = 9404; |
|
29
|
|
|
const NOT_ALLOWED = 9405; |
|
30
|
|
|
protected $errors = []; |
|
31
|
|
|
private $logger; |
|
32
|
|
|
private $userSession; |
|
33
|
|
|
private $DataService; |
|
34
|
|
|
private $ActivityManager; |
|
35
|
|
|
private $DatasetService; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct( |
|
38
|
|
|
$appName, |
|
39
|
|
|
IRequest $request, |
|
40
|
|
|
ILogger $logger, |
|
41
|
|
|
DataService $DataService, |
|
42
|
|
|
IUserSession $userSession, |
|
43
|
|
|
ActivityManager $ActivityManager, |
|
44
|
|
|
DatasetService $DatasetService |
|
45
|
|
|
) |
|
46
|
|
|
{ |
|
47
|
|
|
parent::__construct( |
|
48
|
|
|
$appName, |
|
49
|
|
|
$request, |
|
50
|
|
|
'POST', |
|
51
|
|
|
); |
|
52
|
|
|
$this->logger = $logger; |
|
53
|
|
|
$this->userSession = $userSession; |
|
54
|
|
|
$this->DataService = $DataService; |
|
55
|
|
|
$this->ActivityManager = $ActivityManager; |
|
56
|
|
|
$this->DatasetService = $DatasetService; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @CORS |
|
61
|
|
|
* @NoCSRFRequired |
|
62
|
|
|
* @NoAdminRequired |
|
63
|
|
|
* @param int $datasetId |
|
64
|
|
|
* @return DataResponse |
|
65
|
|
|
*/ |
|
66
|
|
|
public function addData(int $datasetId) |
|
67
|
|
|
{ |
|
68
|
|
|
$params = $this->request->getParams(); |
|
69
|
|
|
//$this->logger->error($params); |
|
70
|
|
|
$datasetMetadata = $this->DatasetService->getOwnDataset($datasetId); |
|
71
|
|
|
|
|
72
|
|
|
if (empty($datasetMetadata)) { |
|
73
|
|
|
$this->errors[] = 'Unknown report or dataset'; |
|
74
|
|
|
return $this->requestResponse(false, self::NOT_FOUND, implode(',', $this->errors)); |
|
75
|
|
|
} elseif ((int)$datasetMetadata['type'] !== 2) { |
|
76
|
|
|
$this->errors[] = 'Report does not allow data maintenance'; |
|
77
|
|
|
return $this->requestResponse(false, self::NOT_ALLOWED, implode(',', $this->errors)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (!isset($params['dimension1'])) { |
|
81
|
|
|
$this->errors[] = 'Dimension 1 required'; |
|
82
|
|
|
} elseif (!isset($params['dimension2'])) { |
|
83
|
|
|
$this->errors[] = 'Dimension 2 required'; |
|
84
|
|
|
} elseif (!isset($params['dimension3'])) { |
|
85
|
|
|
$this->errors[] = 'Dimension 3 required'; |
|
86
|
|
|
} |
|
87
|
|
|
if (!empty($this->errors)) { |
|
88
|
|
|
return $this->requestResponse(false, self::MISSING_PARAM, implode(',', $this->errors)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$action = $this->DataService->update($datasetId, $params['dimension1'], $params['dimension2'], $params['dimension3']); |
|
92
|
|
|
$this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD_API); |
|
93
|
|
|
|
|
94
|
|
|
return $this->requestResponse( |
|
95
|
|
|
true, |
|
96
|
|
|
Http::STATUS_OK, |
|
97
|
|
|
'Data ' . $action . ' successfull'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param bool $success |
|
102
|
|
|
* @param int|null $code |
|
103
|
|
|
* @param string|null $message |
|
104
|
|
|
* @return DataResponse |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function requestResponse($success, $code = null, $message = null) |
|
107
|
|
|
{ |
|
108
|
|
|
if (!$success) { |
|
109
|
|
|
if ($code === null) { |
|
110
|
|
|
$code = self::UNKNOWN; |
|
111
|
|
|
} |
|
112
|
|
|
$array = [ |
|
113
|
|
|
'success' => false, |
|
114
|
|
|
'error' => ['code' => $code, |
|
115
|
|
|
'message' => $message |
|
116
|
|
|
] |
|
117
|
|
|
]; |
|
118
|
|
|
} else { |
|
119
|
|
|
$array = [ |
|
120
|
|
|
'success' => true, |
|
121
|
|
|
'message' => $message |
|
122
|
|
|
]; |
|
123
|
|
|
} |
|
124
|
|
|
$response = new DataResponse(); |
|
125
|
|
|
$response->setData($array)->render(); |
|
126
|
|
|
return $response; |
|
127
|
|
|
} |
|
128
|
|
|
// curl -u admin:bwGnD-tSTPS-mJM7j-6WBFD-M9WxS -d '{"dimension1": "x", "dimension2": "x", "dimension3": "333,3"}' -X POST -H "Content-Type: application/json" http://nc16/nextcloud/apps/analytics/api/1.0/adddata/10 |
|
129
|
|
|
} |