diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2015-12-24 15:19:50 +0100 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-01-02 23:27:41 +0100 |
commit | 5a4bbcc9a76fcdf54a6af25fcf7b26c9053a0ba3 (patch) | |
tree | ae181b75a7a9305b9d12774e4e451059acce5bce /src | |
parent | 27a8708b673bb0a28a520131d94ce17c7d3b9f96 (diff) | |
download | wallabag-5a4bbcc9a76fcdf54a6af25fcf7b26c9053a0ba3.tar.gz wallabag-5a4bbcc9a76fcdf54a6af25fcf7b26c9053a0ba3.tar.zst wallabag-5a4bbcc9a76fcdf54a6af25fcf7b26c9053a0ba3.zip |
Change the way to check for an existing entry
The repository method return the entry found or false if nothing exists.
Diffstat (limited to 'src')
4 files changed, 20 insertions, 12 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index 9097810c..37f7ab60 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php | |||
@@ -41,7 +41,6 @@ class EntryController extends Controller | |||
41 | */ | 41 | */ |
42 | public function addEntryFormAction(Request $request) | 42 | public function addEntryFormAction(Request $request) |
43 | { | 43 | { |
44 | $em = $this->getDoctrine()->getManager(); | ||
45 | $entry = new Entry($this->getUser()); | 44 | $entry = new Entry($this->getUser()); |
46 | 45 | ||
47 | $form = $this->createForm(new NewEntryType(), $entry); | 46 | $form = $this->createForm(new NewEntryType(), $entry); |
@@ -49,17 +48,17 @@ class EntryController extends Controller | |||
49 | $form->handleRequest($request); | 48 | $form->handleRequest($request); |
50 | 49 | ||
51 | if ($form->isValid()) { | 50 | if ($form->isValid()) { |
52 | $existingEntry = $em | 51 | // check for existing entry, if it exists, redirect to it with a message |
53 | ->getRepository('WallabagCoreBundle:Entry') | 52 | $existingEntry = $this->get('wallabag_core.entry_repository') |
54 | ->findOneByUrlAndUserId($entry->getUrl(), $this->getUser()->getId()); | 53 | ->existByUrlAndUserId($entry->getUrl(), $this->getUser()->getId()); |
55 | 54 | ||
56 | if (count($existingEntry) > 0) { | 55 | if (false !== $existingEntry) { |
57 | $this->get('session')->getFlashBag()->add( | 56 | $this->get('session')->getFlashBag()->add( |
58 | 'notice', | 57 | 'notice', |
59 | 'Entry already saved on '.$existingEntry[0]->getCreatedAt()->format('d-m-Y') | 58 | 'Entry already saved on '.$existingEntry['createdAt']->format('d-m-Y') |
60 | ); | 59 | ); |
61 | 60 | ||
62 | return $this->redirect($this->generateUrl('view', array('id' => $existingEntry[0]->getId()))); | 61 | return $this->redirect($this->generateUrl('view', array('id' => $existingEntry['id']))); |
63 | } | 62 | } |
64 | 63 | ||
65 | $this->updateEntry($entry); | 64 | $this->updateEntry($entry); |
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 502e9da0..b8e22eb8 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -226,18 +226,26 @@ class EntryRepository extends EntityRepository | |||
226 | 226 | ||
227 | /** | 227 | /** |
228 | * Find an entry by its url and its owner. | 228 | * Find an entry by its url and its owner. |
229 | * If it exists, return the entry otherwise return false. | ||
229 | * | 230 | * |
230 | * @param $url | 231 | * @param $url |
231 | * @param $userId | 232 | * @param $userId |
232 | * | 233 | * |
233 | * @return array | 234 | * @return array|bool |
234 | */ | 235 | */ |
235 | public function findOneByUrlAndUserId($url, $userId) | 236 | public function existByUrlAndUserId($url, $userId) |
236 | { | 237 | { |
237 | return $this->createQueryBuilder('e') | 238 | $res = $this->createQueryBuilder('e') |
239 | ->select('e.id, e.createdAt') | ||
238 | ->where('e.url = :url')->setParameter('url', $url) | 240 | ->where('e.url = :url')->setParameter('url', $url) |
239 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) | 241 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) |
240 | ->getQuery() | 242 | ->getQuery() |
241 | ->getResult(); | 243 | ->getResult(); |
244 | |||
245 | if (count($res) > 1) { | ||
246 | return next($res); | ||
247 | } | ||
248 | |||
249 | return false; | ||
242 | } | 250 | } |
243 | } | 251 | } |
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index c92b4eb3..96b1c931 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml | |||
@@ -63,6 +63,7 @@ services: | |||
63 | - @wallabag_core.tag_repository | 63 | - @wallabag_core.tag_repository |
64 | - @wallabag_core.entry_repository | 64 | - @wallabag_core.entry_repository |
65 | 65 | ||
66 | # repository as a service | ||
66 | wallabag_core.entry_repository: | 67 | wallabag_core.entry_repository: |
67 | class: Wallabag\CoreBundle\Repository\EntryRepository | 68 | class: Wallabag\CoreBundle\Repository\EntryRepository |
68 | factory: [ @doctrine.orm.default_entity_manager, getRepository ] | 69 | factory: [ @doctrine.orm.default_entity_manager, getRepository ] |
diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index dd1c34ab..ef8f9eb5 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php | |||
@@ -117,9 +117,9 @@ class PocketImport implements ImportInterface | |||
117 | 117 | ||
118 | $existingEntry = $this->em | 118 | $existingEntry = $this->em |
119 | ->getRepository('WallabagCoreBundle:Entry') | 119 | ->getRepository('WallabagCoreBundle:Entry') |
120 | ->findOneByUrlAndUserId($url, $this->user->getId()); | 120 | ->existByUrlAndUserId($url, $this->user->getId()); |
121 | 121 | ||
122 | if (count($existingEntry) > 0) { | 122 | if (false !== $existingEntry) { |
123 | ++$this->skippedEntries; | 123 | ++$this->skippedEntries; |
124 | continue; | 124 | continue; |
125 | } | 125 | } |