Skip to content

Commit a45639e

Browse files
committed
Updates to code structure placement order
1 parent 093fa91 commit a45639e

1 file changed

Lines changed: 54 additions & 54 deletions

File tree

src/Controller/Api/ApiController.php

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ public function getUserCalendars(Request $request, string $username, ManagerRegi
174174
$response = [
175175
'status' => 'success',
176176
'data' => [
177-
'user_calendars' => $calendars ?? [],
178-
'shared_calendars' => $sharedCalendars ?? [],
179-
'subscriptions' => $subscriptions ?? [],
177+
'user_calendars' => $calendars,
178+
'shared_calendars' => $sharedCalendars,
179+
'subscriptions' => $subscriptions,
180180
],
181181
'timestamp' => $this->getTimestamp(),
182182
];
@@ -236,42 +236,38 @@ public function getUserCalendarDetails(Request $request, string $username, int $
236236
return $this->json($response, 200);
237237
}
238238

239-
#[Route('/calendars/{username}/{calendar_id}/edit', name: 'calendar_edit', methods: ['POST'], requirements: ['calendar_id' => "\d+", 'username' => '[a-zA-Z0-9_-]+'])]
240-
public function editUserCalendar(Request $request, string $username, int $calendar_id, ManagerRegistry $doctrine): JsonResponse
239+
/**
240+
* Creates a new calendar for a specific user.
241+
*
242+
* @param Request $request The HTTP POST request
243+
* @param string $username The username of the user for whom the calendar is to be created
244+
*
245+
* @return JsonResponse A JSON response indicating the success or failure of the operation
246+
*/
247+
#[Route('/calendars/{username}/create', name: 'calendar_create', methods: ['POST'], requirements: ['username' => '[a-zA-Z0-9_-]+'])]
248+
public function createNewUserCalendar(Request $request, string $username, ManagerRegistry $doctrine): JsonResponse
241249
{
242250
if (!$doctrine->getRepository(Principal::class)->findOneByUri(Principal::PREFIX.$username)) {
243251
return $this->json(['status' => 'error', 'message' => 'User Not Found', 'timestamp' => $this->getTimestamp()], 404);
244252
}
245253

246-
$ownerInstance = $doctrine->getRepository(CalendarInstance::class)->findOneBy([
247-
'id' => $calendar_id,
248-
'principalUri' => Principal::PREFIX.$username,
249-
]);
250-
251-
if (!$ownerInstance) {
252-
return $this->json(['status' => 'error', 'message' => 'Invalid Calendar ID', 'timestamp' => $this->getTimestamp()], 400);
253-
}
254-
255-
$calendarInstance = $doctrine->getRepository(CalendarInstance::class)->findOneById($calendar_id);
256-
if (!$calendarInstance) {
257-
return $this->json(['status' => 'error', 'message' => 'Calendar Instance Not Found', 'timestamp' => $this->getTimestamp()], 404);
258-
}
259-
260254
$calendarName = $request->get('name');
261255
if (1 !== preg_match('/^[a-zA-Z0-9 _-]{0,64}$/', $calendarName)) {
262256
return $this->json(['status' => 'error', 'message' => 'Invalid Calendar Name', 'timestamp' => $this->getTimestamp()], 400);
263257
}
258+
$calendarURI = strtolower(str_replace(' ', '_', $calendarName));
264259

265260
$calendarDescription = $request->get('description', '');
266261
if (1 !== preg_match('/^[a-zA-Z0-9 _-]{0,256}$/', $calendarDescription)) {
267262
return $this->json(['status' => 'error', 'message' => 'Invalid Calendar Description', 'timestamp' => $this->getTimestamp()], 400);
268263
}
269264

270265
$entityManager = $doctrine->getManager();
271-
$calendarInstance->setDisplayName($calendarName);
272-
$calendarInstance->setDescription($calendarDescription);
266+
$calendarInstance = new CalendarInstance();
267+
$calendar = new Calendar();
268+
$calendarInstance->setCalendar($calendar);
273269

274-
$calendarComponents = explode(',', $calendarInstance->getCalendar()->getComponents());
270+
$calendarComponents = [];
275271
if ('true' === $request->get('events_support', 'true')) {
276272
$calendarComponents[] = Calendar::COMPONENT_EVENTS;
277273
}
@@ -283,44 +279,61 @@ public function editUserCalendar(Request $request, string $username, int $calend
283279
}
284280
$calendarInstance->getCalendar()->setComponents(implode(',', $calendarComponents));
285281

282+
$calendarInstance
283+
->setCalendar($calendar)
284+
->setAccess(CalendarInstance::ACCESS_SHAREDOWNER)
285+
->setDescription($calendarDescription)
286+
->setDisplayName($calendarName)
287+
->setUri($calendarURI)
288+
->setPrincipalUri(Principal::PREFIX.$username);
289+
286290
$entityManager->persist($calendarInstance);
287291
$entityManager->flush();
288292

289-
return $this->json(['status' => 'success', 'timestamp' => $this->getTimestamp()], 200);
293+
$response = [
294+
'status' => 'success',
295+
'timestamp' => $this->getTimestamp(),
296+
];
297+
298+
return $this->json($response, 200);
290299
}
291300

292-
/**
293-
* Creates a new calendar for a specific user.
294-
*
295-
* @param Request $request The HTTP POST request
296-
* @param string $username The username of the user for whom the calendar is to be created
297-
*
298-
* @return JsonResponse A JSON response indicating the success or failure of the operation
299-
*/
300-
#[Route('/calendars/{username}/create', name: 'calendar_create', methods: ['POST'], requirements: ['username' => '[a-zA-Z0-9_-]+'])]
301-
public function createNewUserCalendar(Request $request, string $username, ManagerRegistry $doctrine): JsonResponse
301+
#[Route('/calendars/{username}/{calendar_id}/edit', name: 'calendar_edit', methods: ['POST'], requirements: ['calendar_id' => "\d+", 'username' => '[a-zA-Z0-9_-]+'])]
302+
public function editUserCalendar(Request $request, string $username, int $calendar_id, ManagerRegistry $doctrine): JsonResponse
302303
{
303304
if (!$doctrine->getRepository(Principal::class)->findOneByUri(Principal::PREFIX.$username)) {
304305
return $this->json(['status' => 'error', 'message' => 'User Not Found', 'timestamp' => $this->getTimestamp()], 404);
305306
}
306307

308+
$ownerInstance = $doctrine->getRepository(CalendarInstance::class)->findOneBy([
309+
'id' => $calendar_id,
310+
'principalUri' => Principal::PREFIX.$username,
311+
]);
312+
313+
if (!$ownerInstance) {
314+
return $this->json(['status' => 'error', 'message' => 'Invalid Calendar ID', 'timestamp' => $this->getTimestamp()], 400);
315+
}
316+
317+
$calendarInstance = $doctrine->getRepository(CalendarInstance::class)->findOneById($calendar_id);
318+
if (!$calendarInstance) {
319+
return $this->json(['status' => 'error', 'message' => 'Calendar Instance Not Found', 'timestamp' => $this->getTimestamp()], 404);
320+
}
321+
307322
$calendarName = $request->get('name');
308323
if (1 !== preg_match('/^[a-zA-Z0-9 _-]{0,64}$/', $calendarName)) {
309324
return $this->json(['status' => 'error', 'message' => 'Invalid Calendar Name', 'timestamp' => $this->getTimestamp()], 400);
310325
}
311-
$calendarURI = strtolower(str_replace(' ', '_', $calendarName));
312326

313327
$calendarDescription = $request->get('description', '');
314328
if (1 !== preg_match('/^[a-zA-Z0-9 _-]{0,256}$/', $calendarDescription)) {
315329
return $this->json(['status' => 'error', 'message' => 'Invalid Calendar Description', 'timestamp' => $this->getTimestamp()], 400);
316330
}
317331

318332
$entityManager = $doctrine->getManager();
319-
$calendarInstance = new CalendarInstance();
320-
$calendar = new Calendar();
321-
$calendarInstance->setCalendar($calendar);
333+
$calendarInstance->setDisplayName($calendarName);
334+
$calendarInstance->setDescription($calendarDescription);
322335

323-
$calendarComponents = [];
336+
$calendarComponents = explode(',', $calendarInstance->getCalendar()->getComponents());
324337
if ('true' === $request->get('events_support', 'true')) {
325338
$calendarComponents[] = Calendar::COMPONENT_EVENTS;
326339
}
@@ -332,23 +345,10 @@ public function createNewUserCalendar(Request $request, string $username, Manage
332345
}
333346
$calendarInstance->getCalendar()->setComponents(implode(',', $calendarComponents));
334347

335-
$calendarInstance
336-
->setCalendar($calendar)
337-
->setAccess(CalendarInstance::ACCESS_SHAREDOWNER)
338-
->setDescription($calendarDescription)
339-
->setDisplayName($calendarName)
340-
->setUri($calendarURI)
341-
->setPrincipalUri(Principal::PREFIX.$username);
342-
343348
$entityManager->persist($calendarInstance);
344349
$entityManager->flush();
345350

346-
$response = [
347-
'status' => 'success',
348-
'timestamp' => $this->getTimestamp(),
349-
];
350-
351-
return $this->json($response, 200);
351+
return $this->json(['status' => 'success', 'timestamp' => $this->getTimestamp()], 200);
352352
}
353353

354354
/**
@@ -373,7 +373,7 @@ public function getUserCalendarsShares(Request $request, string $username, int $
373373
]);
374374

375375
if (!$ownerInstance) {
376-
return $this->json(['status' => 'error', 'message' => 'Invalid Calendar ID', 'timestamp' => $this->getTimestamp()], 400);
376+
return $this->json(['status' => 'error', 'message' => 'Invalid Calendar ID/Username', 'timestamp' => $this->getTimestamp()], 400);
377377
}
378378

379379
$instances = $doctrine->getRepository(CalendarInstance::class)->findSharedInstancesOfInstance($calendar_id, true);
@@ -422,7 +422,7 @@ public function setUserCalendarsShare(Request $request, string $username, int $c
422422
]);
423423

424424
if (!$ownerInstance) {
425-
return $this->json(['status' => 'error', 'message' => 'Invalid Calendar ID', 'timestamp' => $this->getTimestamp()], 400);
425+
return $this->json(['status' => 'error', 'message' => 'Invalid Calendar ID and User ID', 'timestamp' => $this->getTimestamp()], 400);
426426
}
427427

428428
$userId = $request->get('user_id');

0 commit comments

Comments
 (0)