Completed
Pull Request — master (#249)
by
unknown
03:38
created

NotesApiController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * ownCloud - Notes
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Bernhard Posselt <[email protected]>
9
 * @copyright Bernhard Posselt 2012, 2014
10
 */
11
12
namespace OCA\Notes\Controller;
13
14
use OCP\AppFramework\ApiController;
15
use OCP\AppFramework\Http\DataResponse;
16
use OCP\IRequest;
17
18
use OCA\Notes\Service\NotesService;
19
use OCA\Notes\Db\Note;
20
21
/**
22
 * Class NotesApiController
23
 *
24
 * @package OCA\Notes\Controller
25
 */
26
class NotesApiController extends ApiController {
27
28
    use Errors;
29
30
    /** @var NotesService */
31
    private $service;
32
    /** @var string */
33
    private $userId;
34
35
    /**
36
     * @param string $AppName
37
     * @param IRequest $request
38
     * @param NotesService $service
39
     * @param string $UserId
40
     */
41 10
    public function __construct($AppName, IRequest $request,
42
                                NotesService $service, $UserId){
43 10
        parent::__construct($AppName, $request);
44 10
        $this->service = $service;
45 10
        $this->userId = $UserId;
46 10
    }
47
48
49
    /**
50
     * @param Note $note
51
     * @param string[] $exclude the fields that should be removed from the
52
     * notes
53
     * @return Note
54
     */
55 4
    private function excludeFields(Note $note, array $exclude) {
56 4
        if(count($exclude) > 0) {
57 4
            foreach ($exclude as $field) {
58 4
                if(property_exists($note, $field)) {
59 2
                    unset($note->$field);
60 2
                }
61 4
            }
62 4
        }
63 4
        return $note;
64
    }
65
66
67
    /**
68
     * @NoAdminRequired
69
     * @CORS
70
     * @NoCSRFRequired
71
     *
72
     * @param string $exclude
73
     * @return DataResponse
74
     */
75 2
    public function index($exclude='') {
76 2
        $exclude = explode(',', $exclude);
77 2
        $notes = $this->service->getAll($this->userId);
78 2
        foreach ($notes as $note) {
79 2
            $note = $this->excludeFields($note, $exclude);
80 2
        }
81 2
        return new DataResponse($notes);
82
    }
83
84
85
    /**
86
     * @NoAdminRequired
87
     * @CORS
88
     * @NoCSRFRequired
89
     *
90
     * @param int $id
91
     * @param string $exclude
92
     * @return DataResponse
93
     */
94 3
    public function get($id, $exclude='') {
95 3
        $exclude = explode(',', $exclude);
96
97
        return $this->respond(function () use ($id, $exclude) {
98 3
            $note = $this->service->get($id, $this->userId);
99 2
            $note = $this->excludeFields($note, $exclude);
100 2
            return $note;
101 3
        });
102
    }
103
104
105
    /**
106
     * @NoAdminRequired
107
     * @CORS
108
     * @NoCSRFRequired
109
     *
110
     * @param string $content
111
     * @return DataResponse
112
     */
113 1
    public function create($content) {
114
        return $this->respond(function () use ($content) {
115 1
            $note = $this->service->create($this->userId);
116 1
            return $this->service->update($note->getId(), $content, $this->userId);
117 1
        });
118
    }
119
120
121
    /**
122
     * @NoAdminRequired
123
     * @CORS
124
     * @NoCSRFRequired
125
     *
126
     * @param int $id
127
     * @param string $content
128
     * @param boolean $favorite
129
     * @return DataResponse
130
     */
131 2
    public function update($id, $content=null, $favorite=null) {
132 2
        if($favorite!==null) {
133
            $this->service->favorite($id, $favorite, $this->userId);
134
        }
135
        return $this->respond(function () use ($id, $content) {
136 2
            if($content===null) {
137
                return $this->service->get($id, $this->userId);
138
            } else {
139 2
                return $this->service->update($id, $content, $this->userId);
140
            }
141 2
        });
142
    }
143
144
145
    /**
146
     * @NoAdminRequired
147
     * @CORS
148
     * @NoCSRFRequired
149
     *
150
     * @param int $id
151
     * @return DataResponse
152
     */
153
    public function destroy($id) {
154 2
        return $this->respond(function () use ($id) {
155 2
            $this->service->delete($id, $this->userId);
156 1
            return [];
157 2
        });
158
    }
159
160
161
}
162