diff --git a/app/controllers/api.php b/app/controllers/api.php index a7a35a3..a16ddea 100644 --- a/app/controllers/api.php +++ b/app/controllers/api.php @@ -55,4 +55,19 @@ $api->get('/mark-unread', function (Request $request) use ($app) { return $app->json($entry, 201); }); +$api->get('/star', function (Request $request) use ($app) { + $id = $request->request->get('id'); + + $entry = $app['entry_api']->star($id); + + return $app->json($entry, 201); +}); + +$api->get('/unstar', function (Request $request) use ($app) { + $id = $request->request->get('id'); + + $entry = $app['entry_api']->unstar($id); + + return $app->json($entry, 201); +}); return $api; diff --git a/app/controllers/front.php b/app/controllers/front.php index 18fbfc9..600b906 100644 --- a/app/controllers/front.php +++ b/app/controllers/front.php @@ -39,6 +39,26 @@ $front->get('/mark-unread/{id}', function (Request $request, $id) use ($app) { }) ->bind('mark_entry_unread'); +$front->get('/star/{id}', function (Request $request, $id) use ($app) { + + $entry = $app['entry_api']->star($id); + + $referer = $request->headers->get('referer'); + + return $app->redirect($referer); +}) +->bind('star_entry'); + +$front->get('/unstar/{id}', function (Request $request, $id) use ($app) { + + $entry = $app['entry_api']->unstar($id); + + $referer = $request->headers->get('referer'); + + return $app->redirect($referer); +}) +->bind('unstar_entry'); + $front->match('/add', function (Request $request) use ($app) { $data = array('url'); diff --git a/app/views/index.twig b/app/views/index.twig index 5797303..6dde9b3 100644 --- a/app/views/index.twig +++ b/app/views/index.twig @@ -24,7 +24,7 @@

{{ entry.content|striptags|slice(0, 300) }}…

diff --git a/src/Poche/Api/EntryApi.php b/src/Poche/Api/EntryApi.php index 00022c2..866d7a9 100644 --- a/src/Poche/Api/EntryApi.php +++ b/src/Poche/Api/EntryApi.php @@ -27,6 +27,14 @@ class EntryApi return $this->entryRepository->markAsUnread($id); } + public function star($id) { + return $this->entryRepository->star($id); + } + + public function unstar($id) { + return $this->entryRepository->unstar($id); + } + public function createEntryFromUrl($url) { //TODO: Fetch all what we need, fill the title, content … diff --git a/src/Poche/Repository/EntryRepository.php b/src/Poche/Repository/EntryRepository.php index 1c4977a..b198d3f 100644 --- a/src/Poche/Repository/EntryRepository.php +++ b/src/Poche/Repository/EntryRepository.php @@ -47,5 +47,22 @@ class EntryRepository return $count; } + + //TODO don't hardcode the user ;) + public function star($id, $userId = 1) { + $sql = "UPDATE entries SET bookmark = 1 where id = ? AND user_id = ?"; + $count = $this->db->executeUpdate($sql, array($id, $userId)); + + return $count; + } + + //TODO don't hardcode the user ;) + public function unstar($id, $userId = 1) { + $sql = "UPDATE entries SET bookmark = 0 where id = ? AND user_id = ?"; + $count = $this->db->executeUpdate($sql, array($id, $userId)); + + return $count; + } + }