Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add API to change the category #87

Merged
merged 3 commits into from
Jun 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions controller/notesapicontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,15 @@ public function get($id, $exclude='') {
* @NoCSRFRequired
*
* @param string $content
* @param string $category
* @param int $modified
* @param boolean $favorite
* @return DataResponse
*/
public function create($content, $modified=0, $favorite=null) {
return $this->respond(function () use ($content, $modified, $favorite) {
public function create($content, $category=null, $modified=0, $favorite=null) {
return $this->respond(function () use ($content, $category, $modified, $favorite) {
$note = $this->service->create($this->userId);
return $this->updateData($note->getId(), $content, $modified, $favorite);
return $this->updateData($note->getId(), $content, $category, $modified, $favorite);
});
}

Expand All @@ -127,13 +128,14 @@ public function create($content, $modified=0, $favorite=null) {
*
* @param int $id
* @param string $content
* @param string $category
* @param int $modified
* @param boolean $favorite
* @return DataResponse
*/
public function update($id, $content=null, $modified=0, $favorite=null) {
return $this->respond(function () use ($id, $content, $modified, $favorite) {
return $this->updateData($id, $content, $modified, $favorite);
public function update($id, $content=null, $category=null, $modified=0, $favorite=null) {
return $this->respond(function () use ($id, $content, $category, $modified, $favorite) {
return $this->updateData($id, $content, $category, $modified, $favorite);
});
}

Expand All @@ -145,14 +147,14 @@ public function update($id, $content=null, $modified=0, $favorite=null) {
* @param boolean $favorite
* @return Note
*/
private function updateData($id, $content, $modified, $favorite) {
private function updateData($id, $content, $category, $modified, $favorite) {
if($favorite!==null) {
$this->service->favorite($id, $favorite, $this->userId);
}
if($content===null) {
return $this->service->get($id, $this->userId);
} else {
return $this->service->update($id, $content, $this->userId, $modified);
return $this->service->update($id, $content, $this->userId, $category, $modified);
}
}

Expand Down
57 changes: 45 additions & 12 deletions service/notesservice.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCP\IL10N;
use OCP\Files\IRootFolder;
use OCP\Files\Folder;
use OCP\ILogger;

use OCA\Notes\Db\Note;

Expand All @@ -27,14 +28,20 @@ class NotesService {

private $l10n;
private $root;
private $logger;
private $appName;

/**
* @param IRootFolder $root
* @param IL10N $l10n
* @param ILogger $logger
* @param String $appName
*/
public function __construct (IRootFolder $root, IL10N $l10n) {
public function __construct (IRootFolder $root, IL10N $l10n, ILogger $logger, $appName) {
$this->root = $root;
$this->l10n = $l10n;
$this->logger = $logger;
$this->appName = $appName;
}


Expand Down Expand Up @@ -117,7 +124,7 @@ public function create ($userId) {
* @throws NoteDoesNotExistException if note does not exist
* @return \OCA\Notes\Db\Note the updated note
*/
public function update ($id, $content, $userId, $mtime=0) {
public function update ($id, $content, $userId, $category=null, $mtime=0) {
$notesFolder = $this->getFolderForUser($userId);
$file = $this->getFileById($notesFolder, $id);
$folder = $file->getParent();
Expand All @@ -138,15 +145,32 @@ public function update ($id, $content, $userId, $mtime=0) {
// using a maximum of 100 chars should be enough
$title = mb_substr($title, 0, 100, "UTF-8");

// generate filename if there were collisions
$currentFilePath = $file->getPath();
$basePath = pathinfo($file->getPath(), PATHINFO_DIRNAME);
$fileExtension = pathinfo($file->getName(), PATHINFO_EXTENSION);
$newFilePath = $basePath . '/' . $this->generateFileName($folder, $title, $fileExtension, $id);

// if the current path is not the new path, the file has to be renamed
if($currentFilePath !== $newFilePath) {
$file->move($newFilePath);
// rename/move file with respect to title/category
// this can fail if access rights are not sufficient or category name is illegal
try {
$currentFilePath = $file->getPath();
$fileExtension = pathinfo($file->getName(), PATHINFO_EXTENSION);

// detect (new) folder path based on category name
if($category===null) {
$basePath = pathinfo($file->getPath(), PATHINFO_DIRNAME);
} else {
$basePath = $notesFolder->getPath();
if(!empty($category))
$basePath .= '/'.$category;
$this->getOrCreateFolder($basePath);
}

// assemble new file path
$newFilePath = $basePath . '/' . $this->generateFileName($folder, $title, $fileExtension, $id);

// if the current path is not the new path, the file has to be renamed
if($currentFilePath !== $newFilePath) {
$file->move($newFilePath);
}
} catch(\OCP\Files\NotPermittedException $e) {
$this->logger->error('Moving this note to the desired target is not allowed. Please check the note\'s target category.', array('app' => $this->appName));
}

$file->putContent($content);
Expand Down Expand Up @@ -179,7 +203,7 @@ public function favorite ($id, $favorite, $userId){
$tagger->removeFromFavorites($id);

$tags = $tagger->getTagsForObjects([$id]);
return in_array(\OC\Tags::TAG_FAVORITE, $tags[$id]);
return array_key_exists($id, $tags) && in_array(\OC\Tags::TAG_FAVORITE, $tags[$id]);
}


Expand Down Expand Up @@ -219,6 +243,16 @@ private function getFileById ($folder, $id) {
*/
private function getFolderForUser ($userId) {
$path = '/' . $userId . '/files/Notes';
return $this->getOrCreateFolder($path);
}


/**
* Finds a folder and creates it if non-existent
* @param string $path path to the folder
* @return Folder
*/
private function getOrCreateFolder($path) {
if ($this->root->nodeExists($path)) {
$folder = $this->root->get($path);
} else {
Expand Down Expand Up @@ -262,7 +296,6 @@ private function generateFileName (Folder $folder, $title, $extension, $id) {
}
}


/**
* gather note files in given directory and all subdirectories
* @param Folder $folder
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/controller/NotesApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testUpdate() {
$this->assertEquals($note->getId(), $note2->getId());
$this->assertNotEquals($t, $note2->getModified());

$note3 = $this->controller->update($note->getId(), 'test3', $t)->getData();
$note3 = $this->controller->update($note->getId(), 'test3', null, $t)->getData();
$this->assertEquals('test3', $note3->getContent());
$this->assertEquals($note->getId(), $note3->getId());
$this->assertEquals($t, $note3->getModified());
Expand Down