summaryrefslogtreecommitdiffstats
path: root/lib/Controller/FolderApiV2Controller.php
blob: 8855ba2373323a2428b85cd064a8061f830be702 (plain)
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
<?php
/**
 * Nextcloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author    Paul Tirk <paultirk@paultirk.com>
 * @copyright 2020 Paul Tirk
 */

namespace OCA\News\Controller;

use \OCP\IRequest;
use \OCP\IUserSession;
use \OCP\AppFramework\Http;

use \OCA\News\Service\FolderServiceV2;
use \OCA\News\Service\ItemServiceV2;
use \OCA\News\Service\Exceptions\ServiceNotFoundException;

class FolderApiV2Controller extends ApiController
{
    use ApiPayloadTrait;
    use JSONHttpErrorTrait;

    /**
     * @var FolderServiceV2
     */
    private $folderService;

    /**
     * @var ItemServiceV2
     */
    private $itemService;

    public function __construct(
        IRequest $request,
        IUserSession $userSession,
        FolderServiceV2 $folderService,
        ItemServiceV2 $itemService
    ) {
        parent::__construct($request, $userSession);

        $this->folderService = $folderService;
        $this->itemService = $itemService;
    }

    /**
     * @NoAdminRequired
     * @NoCSRFRequired
     * @CORS
     *
     * @param string $name
     * @return array|mixed|\OCP\AppFramework\Http\JSONResponse
     */
    public function create(string $name)
    {
        if (trim($name) === '') {
            return $this->errorResponseV2('folder name is empty', 1, Http::STATUS_BAD_REQUEST);
        }

        $this->folderService->purgeDeleted($this->getUserId(), false);
        $responseData = $this->serializeEntityV2(
            $this->folderService->create($this->getUserId(), $name)
        );
        return $this->responseV2([
            'folder' => $responseData
        ]);
    }

    /**
     * @NoAdminRequired
     * @NoCSRFRequired
     * @CORS
     * @param int    $folderId
     * @param string $name
     * @return array|\OCP\AppFramework\Http\JSONResponse
     */
    public function update(int $folderId, string $name)
    {
        if (trim($name) === '') {
            return $this->errorResponseV2('folder name is empty', 1, Http::STATUS_BAD_REQUEST);
        }

        $response = null;
        try {
            $response = $this->folderService->rename($this->getUserId(), $folderId, $name);
        } catch (ServiceNotFoundException $ex) {
            return $this->errorResponseWithExceptionV2($ex, Http::STATUS_NOT_FOUND);
        }

        return $this->responseV2([
            'folder' => $response
        ]);
    }


    /**
     * @NoAdminRequired
     * @NoCSRFRequired
     * @CORS
     *
     * @param int $folderId
     * @return array|\OCP\AppFramework\Http\JSONResponse
     */
    public function delete(int $folderId)
    {
        try {
            $responseData = $this->serializeEntityV2(
                $this->folderService->delete($this->getUserId(), $folderId)
            );
            return $this->responseV2([
                'folder' => $responseData
            ]);
        } catch (ServiceNotFoundException $ex) {
            return $this->errorResponseWithExceptionV2($ex, Http::STATUS_NOT_FOUND);
        }
    }
}