mirror of
https://github.com/JanGross/quicknotes.git
synced 2025-12-01 07:37:18 +01:00
Add and real APi, now Cnp of NoteController...
This commit is contained in:
@@ -1,18 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
return [
|
return ['resources' =>
|
||||||
'resources' => [
|
[
|
||||||
'note' => ['url' => '/notes'],
|
'note' => ['url' => '/notes'],
|
||||||
'tag' => ['url' => '/tags'],
|
'noteApi' => ['url' => '/api/v1/notes']
|
||||||
'note_api' => ['url' => '/api/0.1/notes']
|
],
|
||||||
],
|
'routes' => [
|
||||||
'routes' => [
|
['name' => 'page#index', 'url' => '/', 'verb' => 'GET']
|
||||||
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
|
]
|
||||||
['name' => 'note_api#preflighted_cors', 'url' => '/api/0.1/{path}',
|
|
||||||
'verb' => 'OPTIONS', 'requirements' => ['path' => '.+']],
|
|
||||||
['name' => 'note#get_user_groups_and_users_with_share', 'url' => '/api/0.1/getusergroups', 'verb' => 'POST'],
|
|
||||||
['name' => 'note#add_group_share', 'url' => '/api/0.1/groups/addshare', 'verb' => 'POST'],
|
|
||||||
['name' => 'note#remove_group_share', 'url' => '/api/0.1/groups/removeshare', 'verb' => 'POST'],
|
|
||||||
['name' => 'note#add_user_share', 'url' => '/api/0.1/users/addshare', 'verb' => 'POST'],
|
|
||||||
['name' => 'note#remove_user_share', 'url' => '/api/0.1/users/removeshare', 'verb' => 'POST']
|
|
||||||
]
|
|
||||||
];
|
];
|
||||||
|
|||||||
150
lib/Controller/NoteApiController.php
Normal file
150
lib/Controller/NoteApiController.php
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* @copyright 2016-2020 Matias De lellis <mati86dl@gmail.com>
|
||||||
|
*
|
||||||
|
* @author 2016 Matias De lellis <mati86dl@gmail.com>
|
||||||
|
*
|
||||||
|
* @license GNU AGPL version 3 or any later version
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace OCA\QuickNotes\Controller;
|
||||||
|
|
||||||
|
use OCP\AppFramework\ApiController;
|
||||||
|
use OCP\AppFramework\Http;
|
||||||
|
use OCP\AppFramework\Http\JSONResponse;
|
||||||
|
|
||||||
|
use OCP\IRequest;
|
||||||
|
|
||||||
|
use OCA\QuickNotes\Service\NoteService;
|
||||||
|
|
||||||
|
|
||||||
|
class NoteApiController extends ApiController {
|
||||||
|
|
||||||
|
private $noteService;
|
||||||
|
private $userId;
|
||||||
|
|
||||||
|
public function __construct($AppName,
|
||||||
|
IRequest $request,
|
||||||
|
NoteService $noteService,
|
||||||
|
$userId)
|
||||||
|
{
|
||||||
|
parent::__construct($AppName, $request);
|
||||||
|
|
||||||
|
$this->noteService = $noteService;
|
||||||
|
$this->userId = $userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @NoAdminRequired
|
||||||
|
* @CORS
|
||||||
|
* @NoCSRFRequired
|
||||||
|
*/
|
||||||
|
public function index(): JSONResponse {
|
||||||
|
$notes = $this->noteService->getAll($this->userId);
|
||||||
|
$etag = md5(json_encode($notes));
|
||||||
|
|
||||||
|
$lastModified = new \DateTime(null, new \DateTimeZone('GMT'));
|
||||||
|
$timestamp = max(array_map(function($note) { return $note->getTimestamp(); }, $notes));
|
||||||
|
$lastModified->setTimestamp($timestamp);
|
||||||
|
|
||||||
|
$response = new JSONResponse($notes);
|
||||||
|
$response->setETag($etag);
|
||||||
|
$response->setLastModified($lastModified);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @NoAdminRequired
|
||||||
|
* @CORS
|
||||||
|
* @NoCSRFRequired
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
*/
|
||||||
|
public function show($id): JSONResponse {
|
||||||
|
$note = $this->noteService->get($this->userId, $id);
|
||||||
|
if (is_null($note)) {
|
||||||
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
$etag = md5(json_encode($note));
|
||||||
|
|
||||||
|
$response = new JSONResponse($note);
|
||||||
|
$response->setETag($etag);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @NoAdminRequired
|
||||||
|
* @CORS
|
||||||
|
* @NoCSRFRequired
|
||||||
|
*
|
||||||
|
* @param string $title
|
||||||
|
* @param string $content
|
||||||
|
* @param string $color
|
||||||
|
*/
|
||||||
|
public function create($title, $content, $color = "#F7EB96") {
|
||||||
|
$note = $this->noteService->create($this->userId, $title, $content, $color);
|
||||||
|
|
||||||
|
$etag = md5(json_encode($note));
|
||||||
|
|
||||||
|
$response = new JSONResponse($note);
|
||||||
|
$response->setETag($etag);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @NoAdminRequired
|
||||||
|
* @CORS
|
||||||
|
* @NoCSRFRequired
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
* @param string $title
|
||||||
|
* @param string $content
|
||||||
|
* @param array $attachts
|
||||||
|
* @param bool $pinned
|
||||||
|
* @param array $tags
|
||||||
|
* @param string $color
|
||||||
|
*/
|
||||||
|
public function update(int $id, string $title, string $content, array $attachts, bool $pinned, array $tags, string $color = "#F7EB96"): JSONResponse {
|
||||||
|
$note = $this->noteService->update($this->userId, $id, $title, $content, $attachts, $pinned, $tags, $color);
|
||||||
|
if (is_null($note)) {
|
||||||
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
$etag = md5(json_encode($note));
|
||||||
|
|
||||||
|
$response = new JSONResponse($note);
|
||||||
|
$response->setETag($etag);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @NoAdminRequired
|
||||||
|
* @CORS
|
||||||
|
* @NoCSRFRequired
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
*/
|
||||||
|
public function destroy(int $id): JSONResponse {
|
||||||
|
$this->noteService->destroy($this->userId, $id);
|
||||||
|
return new JSONResponse([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Nextcloud - quicknotes
|
|
||||||
*
|
|
||||||
* This file is licensed under the Affero General Public License version 3 or
|
|
||||||
* later. See the COPYING file.
|
|
||||||
*
|
|
||||||
* @author Matias De lellis <mati86dl@gmail.com>
|
|
||||||
* @copyright Matias De lellis 2019
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace OCA\QuickNotes\Controller;
|
|
||||||
|
|
||||||
use OCP\IRequest;
|
|
||||||
use OCP\AppFramework\Http;
|
|
||||||
use OCP\AppFramework\Http\DataResponse;
|
|
||||||
use OCP\AppFramework\Http\JSONResponse;
|
|
||||||
use OCP\AppFramework\Controller;
|
|
||||||
|
|
||||||
use OCA\QuickNotes\Db\Tag;
|
|
||||||
use OCA\QuickNotes\Db\TagMapper;
|
|
||||||
|
|
||||||
class TagController extends Controller {
|
|
||||||
|
|
||||||
private $tagmapper;
|
|
||||||
private $userId;
|
|
||||||
|
|
||||||
public function __construct($AppName,
|
|
||||||
IRequest $request,
|
|
||||||
TagMapper $tagmapper,
|
|
||||||
$UserId)
|
|
||||||
{
|
|
||||||
parent::__construct($AppName, $request);
|
|
||||||
$this->tagmapper = $tagmapper;
|
|
||||||
$this->userId = $UserId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @NoAdminRequired
|
|
||||||
*/
|
|
||||||
public function index() {
|
|
||||||
$notes = $this->tagmapper->findAll($this->userId);
|
|
||||||
return new DataResponse($notes);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user