Fix backend to shared notes, and initial fronted code.

This commit is contained in:
Matias De lellis
2020-06-15 19:25:48 -03:00
parent 5f327bc136
commit c09c3aacb4
8 changed files with 126 additions and 63 deletions

View File

@@ -13,7 +13,8 @@ class Note extends Entity implements JsonSerializable {
protected $timestamp;
protected $colorId;
protected $userId;
protected $sharedWith;
protected $sharedWith = [];
protected $sharedBy = [];
protected $isShared;
protected $tags;
protected $attachts;
@@ -40,8 +41,9 @@ class Note extends Entity implements JsonSerializable {
'colorid' => $this->colorId,
'color' => $this->color,
'userid' => $this->userId,
'sharedwith' => $this->sharedWith,
'isshared' => $this->isShared,
'shared_with' => $this->sharedWith,
'shared_by' => $this->sharedBy,
'is_shared' => $this->isShared,
'tags' => $this->tags,
'attachts' => $this->attachts
];

View File

@@ -31,6 +31,22 @@ class NoteMapper extends QBMapper {
return $this->findEntity($qb);
}
/**
* @param int $id
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
* @return Note
*/
public function findShared($id) {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->tableName)
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);
return $this->findEntity($qb);
}
public function findAll($userId) {
$qb = $this->db->getQueryBuilder();
$qb->select('*')

View File

@@ -8,15 +8,18 @@ use OCP\AppFramework\Db\Entity;
class NoteShare extends Entity implements JsonSerializable {
protected $noteId;
protected $userId;
protected $sharedUser;
protected $sharedGroup;
public function jsonSerialize() {
return [
'id' => $this->id,
'noteid' => $this->noteId,
'shareduser' => $this->sharedUser,
'sharedgroup' => $this->sharedGroup
'user_id' => $this->userId,
'note_id' => $this->noteId,
'shared_user' => $this->sharedUser,
'shared_group' => $this->sharedGroup
];
}
}

View File

@@ -75,50 +75,44 @@ class NoteService {
*/
public function getAll(string $userId): array {
$notes = $this->notemapper->findAll($userId);
// Set shares with others.
foreach($notes as $note) {
$note->setIsShared(false);
$sharedWith = $this->notesharemapper->getSharesForNote($note->getId());
if(count($sharedWith) > 0) {
$shareList = array();
foreach($sharedWith as $share) {
$shareList[] = $share->getSharedUser();
}
$note->setSharedWith(implode(", ", $shareList));
} else {
$note->setSharedWith(null);
}
$note->setTags($this->tagmapper->getTagsForNote($userId, $note->getId()));
$note->setSharedWith($this->notesharemapper->getSharesForNote($note->getId()));
}
$shareEntries = $this->notesharemapper->findForUser($userId);
$shares = array();
foreach($shareEntries as $entry) {
try {
//find is only to check if current user is owner
$this->notemapper->find($entry->getNoteId(), $userId);
//user is owner, nothing to do
} catch(\OCP\AppFramework\Db\DoesNotExistException $e) {
$share = $this->notemapper->findById($entry->getNoteId());
$share->setIsShared(true);
$shares[] = $share;
}
// Get shares from others.
$shares = [];
$sharedEntries = $this->notesharemapper->findForUser($userId);
foreach($sharedEntries as $sharedEntry) {
$sharedNote = $this->notemapper->findShared($sharedEntry->getNoteId());
$sharedNote->setIsShared(true);
$sharedEntry->setUserId($sharedNote->getUserId());
$sharedNote->setSharedBy([$sharedEntry]);
$shares[] = $sharedNote;
}
// Attahch shared notes from others to same response
$notes = array_merge($notes, $shares);
foreach ($notes as $note) {
$note->setTitle(strip_tags($note->getTitle()));
// Set tags to response.
foreach($notes as $note) {
$note->setTags($this->tagmapper->getTagsForNote($userId, $note->getId()));
}
// Insert true color to response
// Insert color to response
foreach ($notes as $note) {
$note->setColor($this->colormapper->find($note->getColorId())->getColor());
}
// Insert true color to response
// Insert pin to response
foreach ($notes as $note) {
$note->setIsPinned($note->getPinned() ? true : false);
}
// Insert true attachts to response
// Insert attachts to response.
foreach ($notes as $note) {
$attachts = $this->attachMapper->findFromNote($userId, $note->getId());
foreach ($attachts as $attach) {