1 | <?php |
||
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) { |
|
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) { |
|
143 | |||
144 | |||
145 | /** |
||
146 | * @NoAdminRequired |
||
147 | * @CORS |
||
148 | * @NoCSRFRequired |
||
149 | * |
||
150 | * @param int $id |
||
151 | * @return DataResponse |
||
152 | */ |
||
153 | public function destroy($id) { |
||
159 | |||
160 | |||
161 | } |
||
162 |