diff options
-rw-r--r-- | lib/Controller/ItemController.php | 8 | ||||
-rw-r--r-- | lib/Db/ItemMapper.php | 19 | ||||
-rw-r--r-- | lib/Service/ItemService.php | 13 |
3 files changed, 37 insertions, 3 deletions
diff --git a/lib/Controller/ItemController.php b/lib/Controller/ItemController.php index b72311416..9c83b4dfc 100644 --- a/lib/Controller/ItemController.php +++ b/lib/Controller/ItemController.php @@ -334,12 +334,14 @@ class ItemController extends Controller public function share($itemId, $shareWithId) { try { - $this->itemService->shareItem($itemId, $shareWithId, $this->userId); + $exists = $this->itemService->checkSharing($itemId, $shareWithId, $this->userId); + + if($exists==0){ + $this->itemService->shareItem($itemId, $shareWithId, $this->userId); + } } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } - - return []; } } diff --git a/lib/Db/ItemMapper.php b/lib/Db/ItemMapper.php index 765c67698..d5ffc41a5 100644 --- a/lib/Db/ItemMapper.php +++ b/lib/Db/ItemMapper.php @@ -582,4 +582,23 @@ class ItemMapper extends NewsMapper // persist new item $this->insert($newItem); } + + /** + * Check if the article is already shared between the users + */ + public function checkSharing($itemId, $shareWithId, $userId) + { + $item = $this->find($userId, $itemId); + + $sql = 'SELECT COUNT(*) AS size FROM `*PREFIX*news_items` `items` ' . + 'WHERE `items`.`shared_by` = ? '. + 'AND `items`.`shared_with` = ?'. + 'AND `items`.`guid_hash` = ?'; + + $params = [$userId ,$shareWithId, $item->getGuidHash()]; + + $result = $this->execute($sql, $params)->fetch(); + + return (int)$result['size']; + } } diff --git a/lib/Service/ItemService.php b/lib/Service/ItemService.php index 6bb09e64c..7e0743324 100644 --- a/lib/Service/ItemService.php +++ b/lib/Service/ItemService.php @@ -372,4 +372,17 @@ class ItemService extends Service { return $this->itemMapper->sharedCount($userId); } + + /** + * Check if the article is already shared between the users + * + * @param string $itemId the id article + * @param string $userId the name of the user + * @param string $shareWithId the name of the second user + * @return int 0 if not exists or > 0 + */ + public function checkSharing($itemId, $shareWithId, $userId) + { + return $this->itemMapper->checkSharing($itemId, $shareWithId, $userId); + } } |