-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTasksController.php
More file actions
306 lines (275 loc) · 10 KB
/
TasksController.php
File metadata and controls
306 lines (275 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* TasksController
*
* REST controller for CalDAV task operations on OpenRegister objects.
* Follows the FilesController pattern for sub-resource endpoints.
*
* @category Controller
* @package OCA\OpenRegister\Controller
* @author Conduction Development Team <dev@conduction.nl>
* @copyright 2024 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
* @version GIT: <git-id>
* @link https://OpenRegister.app
*/
declare(strict_types=1);
namespace OCA\OpenRegister\Controller;
use Exception;
use OCA\OpenRegister\Service\ObjectService;
use OCA\OpenRegister\Service\TaskService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
/**
* TasksController handles task operations for objects in registers.
*
* Provides REST API endpoints for managing CalDAV tasks (VTODOs)
* associated with OpenRegister objects.
*
* @category Controller
* @package OCA\OpenRegister\Controller
*/
class TasksController extends Controller
{
/**
* Task service for CalDAV VTODO operations.
*
* @var TaskService
*/
private readonly TaskService $taskService;
/**
* Object service for object validation.
*
* @var ObjectService
*/
private readonly ObjectService $objectService;
/**
* Constructor.
*
* @param string $appName Application name
* @param IRequest $request HTTP request object
* @param TaskService $taskService Task service for VTODO operations
* @param ObjectService $objectService Object service for object validation
*
* @return void
*/
public function __construct(
string $appName,
IRequest $request,
TaskService $taskService,
ObjectService $objectService
) {
parent::__construct(appName: $appName, request: $request);
$this->taskService = $taskService;
$this->objectService = $objectService;
}//end __construct()
/**
* List all tasks linked to a specific object.
*
* @param string $register The register slug or identifier
* @param string $schema The schema slug or identifier
* @param string $id The ID of the object
*
* @return JSONResponse JSON response with tasks list
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function index(
string $register,
string $schema,
string $id
): JSONResponse {
try {
$object = $this->validateObject(register: $register, schema: $schema, id: $id);
if ($object === null) {
return new JSONResponse(
data: ['error' => 'Object not found'],
statusCode: 404
);
}
$tasks = $this->taskService->getTasksForObject($object->getUuid());
return new JSONResponse(data: ['results' => $tasks, 'total' => count($tasks)]);
} catch (DoesNotExistException $e) {
return new JSONResponse(data: ['error' => 'Object not found'], statusCode: 404);
} catch (Exception $e) {
// No VTODO calendar = no tasks; return empty for listing.
if (str_contains($e->getMessage(), 'No VTODO-supporting calendar') === true) {
return new JSONResponse(data: ['results' => [], 'total' => 0]);
}
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 500);
}//end try
}//end index()
/**
* Create a new task linked to a specific object.
*
* @param string $register The register slug or identifier
* @param string $schema The schema slug or identifier
* @param string $id The ID of the object
*
* @return JSONResponse JSON response with the created task
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function create(
string $register,
string $schema,
string $id
): JSONResponse {
try {
$object = $this->validateObject(register: $register, schema: $schema, id: $id);
if ($object === null) {
return new JSONResponse(
data: ['error' => 'Object not found'],
statusCode: 404
);
}
$data = $this->request->getParams();
// Validate required fields.
if (empty($data['summary']) === true) {
return new JSONResponse(
data: ['error' => 'Task summary is required'],
statusCode: 400
);
}
$task = $this->taskService->createTask(
registerId: (int) $object->getRegister(),
schemaId: (int) $object->getSchema(),
objectUuid: $object->getUuid(),
objectTitle: $object->getName() ?? $object->getUuid(),
data: $data
);
return new JSONResponse(data: $task, statusCode: 201);
} catch (DoesNotExistException $e) {
return new JSONResponse(data: ['error' => 'Object not found'], statusCode: 404);
} catch (Exception $e) {
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 400);
}//end try
}//end create()
/**
* Update an existing task.
*
* @param string $register The register slug or identifier
* @param string $schema The schema slug or identifier
* @param string $id The ID of the object
* @param string $taskId The URI of the task to update
*
* @return JSONResponse JSON response with the updated task
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function update(
string $register,
string $schema,
string $id,
string $taskId
): JSONResponse {
try {
$object = $this->validateObject(register: $register, schema: $schema, id: $id);
if ($object === null) {
return new JSONResponse(
data: ['error' => 'Object not found'],
statusCode: 404
);
}
$data = $this->request->getParams();
// The taskId from the URL is the task URI. We need the calendarId too.
$calendarId = $data['calendarId'] ?? null;
if ($calendarId === null) {
// Try to find the task in the user's calendar to get the calendarId.
$tasks = $this->taskService->getTasksForObject($object->getUuid());
foreach ($tasks as $existingTask) {
if ($existingTask['id'] === $taskId) {
$calendarId = $existingTask['calendarId'];
break;
}
}
}
if ($calendarId === null) {
return new JSONResponse(
data: ['error' => 'Task not found'],
statusCode: 404
);
}
$task = $this->taskService->updateTask($calendarId, $taskId, $data);
return new JSONResponse(data: $task);
} catch (DoesNotExistException $e) {
return new JSONResponse(data: ['error' => 'Object not found'], statusCode: 404);
} catch (Exception $e) {
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 400);
}//end try
}//end update()
/**
* Delete a task.
*
* @param string $register The register slug or identifier
* @param string $schema The schema slug or identifier
* @param string $id The ID of the object
* @param string $taskId The URI of the task to delete
*
* @return JSONResponse JSON response confirming deletion
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function destroy(
string $register,
string $schema,
string $id,
string $taskId
): JSONResponse {
try {
$object = $this->validateObject(register: $register, schema: $schema, id: $id);
if ($object === null) {
return new JSONResponse(
data: ['error' => 'Object not found'],
statusCode: 404
);
}
// Find the task to get its calendarId.
$tasks = $this->taskService->getTasksForObject($object->getUuid());
$calendarId = null;
foreach ($tasks as $existingTask) {
if ($existingTask['id'] === $taskId) {
$calendarId = $existingTask['calendarId'];
break;
}
}
if ($calendarId === null) {
return new JSONResponse(
data: ['error' => 'Task not found'],
statusCode: 404
);
}
$this->taskService->deleteTask($calendarId, $taskId);
return new JSONResponse(data: ['success' => true]);
} catch (DoesNotExistException $e) {
return new JSONResponse(data: ['error' => 'Object not found'], statusCode: 404);
} catch (Exception $e) {
return new JSONResponse(data: ['error' => $e->getMessage()], statusCode: 400);
}//end try
}//end destroy()
/**
* Validate that the object exists and return it.
*
* @param string $register The register slug or identifier
* @param string $schema The schema slug or identifier
* @param string $id The object ID
*
* @return \OCA\OpenRegister\Db\ObjectEntity|null The object or null
*/
private function validateObject(
string $register,
string $schema,
string $id
): ?\OCA\OpenRegister\Db\ObjectEntity {
$this->objectService->setSchema($schema);
$this->objectService->setRegister($register);
$this->objectService->setObject($id);
return $this->objectService->getObject();
}//end validateObject()
}//end class