|
10 | 10 | namespace OCA\DAV\Controller; |
11 | 11 |
|
12 | 12 | use OCA\DAV\AppInfo\Application; |
| 13 | +use OCA\DAV\Service\ExampleContactService; |
13 | 14 | use OCA\DAV\Service\ExampleEventService; |
14 | 15 | use OCP\AppFramework\ApiController; |
15 | 16 | use OCP\AppFramework\Http; |
16 | 17 | use OCP\AppFramework\Http\Attribute\FrontpageRoute; |
17 | 18 | use OCP\AppFramework\Http\Attribute\NoCSRFRequired; |
18 | 19 | use OCP\AppFramework\Http\DataDownloadResponse; |
19 | 20 | use OCP\AppFramework\Http\JSONResponse; |
20 | | -use OCP\Files\AppData\IAppDataFactory; |
21 | | -use OCP\Files\IAppData; |
22 | | -use OCP\Files\NotFoundException; |
23 | | -use OCP\IAppConfig; |
24 | | -use OCP\IConfig; |
25 | 21 | use OCP\IRequest; |
26 | 22 | use Psr\Log\LoggerInterface; |
27 | 23 |
|
28 | 24 | class ExampleContentController extends ApiController { |
29 | | - private IAppData $appData; |
30 | | - |
31 | 25 | public function __construct( |
32 | 26 | IRequest $request, |
33 | | - private IConfig $config, |
34 | | - private IAppConfig $appConfig, |
35 | | - private IAppDataFactory $appDataFactory, |
36 | | - private LoggerInterface $logger, |
37 | | - private ExampleEventService $exampleEventService, |
| 27 | + private readonly LoggerInterface $logger, |
| 28 | + private readonly ExampleEventService $exampleEventService, |
| 29 | + private readonly ExampleContactService $exampleContactService, |
38 | 30 | ) { |
39 | 31 | parent::__construct(Application::APP_ID, $request); |
40 | | - $this->appData = $this->appDataFactory->get('dav'); |
41 | 32 | } |
42 | 33 |
|
43 | | - public function setEnableDefaultContact($allow) { |
44 | | - if ($allow === 'yes' && !$this->defaultContactExists()) { |
| 34 | + #[FrontpageRoute(verb: 'PUT', url: '/api/defaultcontact/config')] |
| 35 | + public function setEnableDefaultContact(bool $allow): JSONResponse { |
| 36 | + if ($allow && !$this->exampleContactService->defaultContactExists()) { |
45 | 37 | try { |
46 | | - $this->setCard(); |
| 38 | + $this->exampleContactService->setCard(); |
47 | 39 | } catch (\Exception $e) { |
48 | 40 | $this->logger->error('Could not create default contact', ['exception' => $e]); |
49 | 41 | return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
50 | 42 | } |
51 | 43 | } |
52 | | - $this->config->setAppValue(Application::APP_ID, 'enableDefaultContact', $allow); |
| 44 | + $this->exampleContactService->setDefaultContactEnabled($allow); |
53 | 45 | return new JSONResponse([], Http::STATUS_OK); |
54 | 46 | } |
55 | 47 |
|
56 | 48 | #[NoCSRFRequired] |
| 49 | + #[FrontpageRoute(verb: 'GET', url: '/api/defaultcontact/contact')] |
57 | 50 | public function getDefaultContact(): DataDownloadResponse { |
58 | | - $cardData = $this->getCard() |
| 51 | + $cardData = $this->exampleContactService->getCard() |
59 | 52 | ?? file_get_contents(__DIR__ . '/../ExampleContentFiles/exampleContact.vcf'); |
60 | 53 | return new DataDownloadResponse($cardData, 'example_contact.vcf', 'text/vcard'); |
61 | 54 | } |
62 | 55 |
|
| 56 | + #[FrontpageRoute(verb: 'PUT', url: '/api/defaultcontact/contact')] |
63 | 57 | public function setDefaultContact(?string $contactData = null) { |
64 | | - if (!$this->config->getAppValue(Application::APP_ID, 'enableDefaultContact', 'yes')) { |
| 58 | + if (!$this->exampleContactService->isDefaultContactEnabled()) { |
65 | 59 | return new JSONResponse([], Http::STATUS_FORBIDDEN); |
66 | 60 | } |
67 | | - $this->setCard($contactData); |
| 61 | + $this->exampleContactService->setCard($contactData); |
68 | 62 | return new JSONResponse([], Http::STATUS_OK); |
69 | 63 | } |
70 | 64 |
|
71 | | - private function getCard(): ?string { |
72 | | - try { |
73 | | - $folder = $this->appData->getFolder('defaultContact'); |
74 | | - } catch (NotFoundException $e) { |
75 | | - return null; |
76 | | - } |
77 | | - |
78 | | - if (!$folder->fileExists('defaultContact.vcf')) { |
79 | | - return null; |
80 | | - } |
81 | | - |
82 | | - return $folder->getFile('defaultContact.vcf')->getContent(); |
83 | | - } |
84 | | - |
85 | | - private function setCard(?string $cardData = null) { |
86 | | - try { |
87 | | - $folder = $this->appData->getFolder('defaultContact'); |
88 | | - } catch (NotFoundException $e) { |
89 | | - $folder = $this->appData->newFolder('defaultContact'); |
90 | | - } |
91 | | - |
92 | | - $isCustom = true; |
93 | | - if (is_null($cardData)) { |
94 | | - $cardData = file_get_contents(__DIR__ . '/../ExampleContentFiles/exampleContact.vcf'); |
95 | | - $isCustom = false; |
96 | | - } |
97 | | - |
98 | | - if (!$cardData) { |
99 | | - throw new \Exception('Could not read exampleContact.vcf'); |
100 | | - } |
101 | | - |
102 | | - $file = (!$folder->fileExists('defaultContact.vcf')) ? $folder->newFile('defaultContact.vcf') : $folder->getFile('defaultContact.vcf'); |
103 | | - $file->putContent($cardData); |
104 | | - |
105 | | - $this->appConfig->setValueBool(Application::APP_ID, 'hasCustomDefaultContact', $isCustom); |
106 | | - } |
107 | | - |
108 | | - private function defaultContactExists(): bool { |
109 | | - try { |
110 | | - $folder = $this->appData->getFolder('defaultContact'); |
111 | | - } catch (NotFoundException $e) { |
112 | | - return false; |
113 | | - } |
114 | | - return $folder->fileExists('defaultContact.vcf'); |
115 | | - } |
116 | | - |
117 | 65 | #[FrontpageRoute(verb: 'POST', url: '/api/exampleEvent/enable')] |
118 | 66 | public function setCreateExampleEvent(bool $enable): JSONResponse { |
119 | 67 | $this->exampleEventService->setCreateExampleEvent($enable); |
|
0 commit comments