1<?php
2
3declare(strict_types=1);
4
5/**
6 * @author Tahaa Karim <tahaalibra@gmail.com>
7 *
8 * Mail
9 *
10 * This code is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License, version 3,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License, version 3,
20 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21 *
22 */
23
24namespace OCA\Mail\Controller;
25
26use OCA\Mail\Exception\NotImplemented;
27use OCA\Mail\Service\AliasesService;
28use OCP\AppFramework\Controller;
29use OCP\AppFramework\Db\DoesNotExistException;
30use OCP\AppFramework\Http;
31use OCP\AppFramework\Http\JSONResponse;
32use OCP\IRequest;
33
34class AliasesController extends Controller {
35
36	/** @var AliasesService */
37	private $aliasService;
38
39	/** @var string */
40	private $currentUserId;
41
42	public function __construct(string $appName,
43								IRequest $request,
44								AliasesService $aliasesService,
45								string $UserId) {
46		parent::__construct($appName, $request);
47		$this->aliasService = $aliasesService;
48		$this->currentUserId = $UserId;
49	}
50
51	/**
52	 * @NoAdminRequired
53	 * @TrapError
54	 *
55	 * @param int $accountId
56	 *
57	 * @return JSONResponse
58	 */
59	public function index(int $accountId): JSONResponse {
60		return new JSONResponse($this->aliasService->findAll($accountId, $this->currentUserId));
61	}
62
63	/**
64	 * @NoAdminRequired
65	 * @TrapError
66	 */
67	public function show() {
68		throw new NotImplemented();
69	}
70
71	/**
72	 * @NoAdminRequired
73	 * @TrapError
74	 */
75	public function update(int $id, string $alias, string $aliasName): JSONResponse {
76		return new JSONResponse($this->aliasService->update($this->currentUserId, $id, $alias, $aliasName));
77	}
78
79	/**
80	 * @NoAdminRequired
81	 * @TrapError
82	 *
83	 * @param int $id
84	 * @return JSONResponse
85	 */
86	public function destroy(int $id): JSONResponse {
87		return new JSONResponse($this->aliasService->delete($this->currentUserId, $id));
88	}
89
90	/**
91	 * @NoAdminRequired
92	 * @TrapError
93	 *
94	 * @param int $accountId
95	 * @param string $alias
96	 * @param string $aliasName
97	 *
98	 * @return JSONResponse
99	 * @throws DoesNotExistException
100	 */
101	public function create(int $accountId, string $alias, string $aliasName): JSONResponse {
102		return new JSONResponse(
103			$this->aliasService->create($this->currentUserId, $accountId, $alias, $aliasName),
104			Http::STATUS_CREATED
105		);
106	}
107
108	/**
109	 * @NoAdminRequired
110	 * @TrapError
111	 *
112	 * @param int $id
113	 * @param string|null $signature
114	 *
115	 * @return JSONResponse
116	 * @throws DoesNotExistException
117	 */
118	public function updateSignature(int $id, string $signature = null): JSONResponse {
119		return new JSONResponse($this->aliasService->updateSignature($this->currentUserId, $id, $signature));
120	}
121}
122