Skip to content

Commit

Permalink
Merge pull request #87 from nextcloud/update-category
Browse files Browse the repository at this point in the history
add API to change the category
  • Loading branch information
Henni authored Jun 6, 2017
2 parents 8c8aaeb + 8f2ba89 commit da4f9cc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
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
55 changes: 44 additions & 11 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,21 +124,38 @@ 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();
$title = $this->getSafeTitleFromContent($content);

// 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 @@ -229,6 +253,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 @@ -272,7 +306,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

0 comments on commit da4f9cc

Please sign in to comment.