diff --git a/appinfo/database.xml b/appinfo/database.xml
index 6ba1905..3206a6c 100644
--- a/appinfo/database.xml
+++ b/appinfo/database.xml
@@ -136,4 +136,62 @@
+
+ *dbprefix*nextnote_shares
+
+
+ id
+ integer
+ 0
+ true
+ 1
+ 32
+
+
+ guid
+ text
+ true
+ 255
+
+
+ share_from
+ text
+ true
+ 255
+
+
+ share_to
+ text
+ true
+ 255
+
+
+ share_type
+ integer
+ 0
+ false
+ 32
+
+
+ share_target
+ text
+ true
+ 255
+
+
+ permissions
+ integer
+ true
+ 0
+ 1
+
+
+ expire_time
+ datetime
+ false
+ 0
+ 1
+
+
+
diff --git a/appinfo/info.xml b/appinfo/info.xml
index aaf908e..4d906f1 100644
--- a/appinfo/info.xml
+++ b/appinfo/info.xml
@@ -5,7 +5,7 @@
NextNote
NextNote
NextNote
- 1.2.5
+ 1.2.7
agpl
Ben Curtis
Sander Brand
diff --git a/lib/Db/Share.php b/lib/Db/Share.php
new file mode 100644
index 0000000..10bef4b
--- /dev/null
+++ b/lib/Db/Share.php
@@ -0,0 +1,82 @@
+.
+ *
+ */
+
+namespace OCA\NextNote\Db;
+use \OCP\AppFramework\Db\Entity;
+
+/**
+ * @method integer getId()
+ * @method void setId(int $value)
+ * @method void setGuid(string $value)
+ * @method string getGuid()
+ * @method void setShareFrom(string $value)
+ * @method string getShareFrom()
+ * @method void setShareTo(string $value)
+ * @method string getShareTo()
+ * @method void setShareType(string $value)
+ * @method string getShareType()
+ * @method void setShareTarget(string $value)
+ * @method string getShareTarget()
+ * @method void setPermissions(string $value)
+ * @method string getPermissions()
+ * @method void setExpireTime(string $value)
+ * @method string getExpireTime()
+
+ */
+
+
+class Share extends Entity implements \JsonSerializable{
+
+ use EntityJSONSerializer;
+
+ protected $guid;
+ protected $shareFrom; //User from
+ protected $shareTo; // Share to User / group
+ protected $shareType; // Note (1) or Notebook(2)
+ protected $shareTarget; //id of the entity
+ protected $permissions; // int permissions
+ protected $expireTime; // Expire time of share. 0 to disable
+
+
+ public function __construct() {
+ // add types in constructor
+ $this->addType('permissions', 'integer');
+ $this->addType('shareTarget', 'integer');
+ $this->addType('shareType', 'integer');
+ }
+ /**
+ * Turns entity attributes into an array
+ */
+ public function jsonSerialize() {
+ return [
+ 'id' => $this->getId(),
+ 'guid' => $this->getGuid(),
+ 'share_from' => $this->getShareFrom(),
+ 'share_to' => $this->getShareTo(),
+ 'share_type' => $this->getShareType(),
+ 'share_target' => $this->getShareTarget(),
+ 'permissions' => $this->getPermissions(),
+ 'expire_time' => $this->getExpireTime(),
+ ];
+ }
+}
diff --git a/lib/Db/ShareMapper.php b/lib/Db/ShareMapper.php
new file mode 100644
index 0000000..315a443
--- /dev/null
+++ b/lib/Db/ShareMapper.php
@@ -0,0 +1,146 @@
+.
+ *
+ */
+
+namespace OCA\NextNote\Db;
+
+use OCA\NextNote\Service\NotebookService;
+use \OCA\NextNote\Utility\Utils;
+use OCP\AppFramework\Db\Entity;
+use OCP\IDBConnection;
+use OCP\AppFramework\Db\Mapper;
+
+class ShareMapper extends Mapper {
+ private $utils;
+
+ public function __construct(IDBConnection $db, Utils $utils) {
+ parent::__construct($db, 'nextnote_shares');
+ $this->utils = $utils;
+ }
+
+
+ /**
+ * @param $share_id
+ * @param null $user_id
+ * @return Share|Share[]
+ */
+ public function find($share_id, $user_id = null) {
+ $qb = $this->db->getQueryBuilder();
+ $qb->select('*')
+ ->from('nextnote_shares')
+ ->where($qb->expr()->eq('id', $qb->createNamedParameter($share_id)));
+
+ if ($user_id) {
+ $qb->andWhere($qb->expr()->eq('share_from', $qb->createNamedParameter($user_id)));
+ }
+
+ $results = [];
+ $result = $qb->execute();
+ while ($item = $result->fetch()) {
+ /**
+ * @var $share Share
+ */
+ $share = $this->makeEntityFromDBResult($item);
+ $results[] = $share;
+ }
+ $result->closeCursor();
+ if (count($results) === 1) {
+ return reset($results);
+ }
+ return $results;
+
+ }
+
+
+ /**
+ * @param $userId
+ * @return Share[] if not found
+ */
+ public function findSharesFromUser($userId) {
+ $qb = $this->db->getQueryBuilder();
+ $qb->select('*')
+ ->from('nextnote_shares')
+ ->where($qb->expr()->eq('share_from', $qb->createNamedParameter($userId)));
+
+
+ $results = [];
+ $result = $qb->execute();
+ while ($item = $result->fetch()) {
+ /**
+ * @var $share Share
+ */
+ $share = $this->makeEntityFromDBResult($item);
+
+ $results[] = $share;
+ }
+ $result->closeCursor();
+ return $results;
+ }
+
+
+ /**
+ * Creates a note
+ *
+ * @param Share|Entity $share
+ * @return Share|Entity
+ * @internal param $userId
+ */
+ public function insert(Entity $share) {
+ return parent::insert($share);
+ }
+
+ /**
+ * Update note
+ *
+ * @param Share|Entity $share
+ * @return Share|Entity
+ */
+ public function updateNote(Entity $share) {
+ parent::update($share);
+ return $this->find($share->getId());
+ }
+
+ /**
+ * Update note
+ *
+ * @param Share|Entity $share
+ * @return Share|Entity
+ */
+ public function delete(Entity $share) {
+ parent::delete($share);
+ return $this->find($share->getId());
+ }
+
+ public function makeEntityFromDBResult($arr) {
+ $share = new Share();
+ $share->setId($arr['id']);
+ $share->setGuid($arr['guid']);
+ $share->setShareFrom($arr['share_from']);
+ $share->setShareTo($arr['share_to']);
+ $share->setShareType($arr['share_type']);
+ $share->setShareTarget($arr['share_target']);
+ $share->setPermissions($arr['permissions']);
+ $share->setExpireTime($arr['expire_time']);
+
+ return $share;
+ }
+}
diff --git a/lib/Service/ShareService.php b/lib/Service/ShareService.php
new file mode 100644
index 0000000..4b8809f
--- /dev/null
+++ b/lib/Service/ShareService.php
@@ -0,0 +1,65 @@
+.
+ *
+ */
+
+namespace OCA\NextNote\Service;
+
+use OCA\NextNote\Db\Notebook;
+use OCA\NextNote\Db\Note;
+use OCA\NextNote\Db\ShareMapper;
+use OCA\NextNote\Fixtures\ExampleNote;
+use OCA\NextNote\Utility\Utils;
+use OCA\NextNote\Db\NoteMapper;
+
+
+class ShareService {
+
+ const PERMISSION_CREATE = 4;
+ const PERMISSION_READ = 1;
+ const PERMISSION_UPDATE = 2;
+ const PERMISSION_DELETE = 8;
+ const PERMISSION_SHARE = 16;
+ const PERMISSION_ALL = 31;
+
+
+ private $shareMapper;
+ private $utils;
+ private $notebookService;
+
+ public function __construct(ShareMapper $shareMapper, Utils $utils, NotebookService $notebookService) {
+ $this->shareMapper = $shareMapper;
+ $this->utils = $utils;
+ $this->notebookService = $notebookService;
+ }
+
+ /**
+ * Get shared notes from a user.
+ *
+ * @param $userId
+ * @param int|bool $deleted
+ * @return Note[]
+ */
+ public function getSharedNotesFromUser($userId, $deleted = false) {
+ // Get shares
+
+ }
+}