From 6fee53233eb9f65af827f528031902176e00618e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 14 Jan 2014 15:22:38 +0100 Subject: [PATCH] [add] remove feature --- app/controllers/api.php | 8 ++++++ app/controllers/front.php | 10 +++++++ app/views/_entry.twig | 2 +- app/views/view.twig | 4 +-- src/Poche/Api/EntryApi.php | 4 +++ src/Poche/Repository/EntryRepository.php | 12 +++++++-- tests/functionals/ApiTest.php | 33 ++++++++++++++++++++++-- 7 files changed, 66 insertions(+), 7 deletions(-) diff --git a/app/controllers/api.php b/app/controllers/api.php index a16ddea..f51a280 100644 --- a/app/controllers/api.php +++ b/app/controllers/api.php @@ -70,4 +70,12 @@ $api->get('/unstar', function (Request $request) use ($app) { return $app->json($entry, 201); }); + +$api->get('/remove', function (Request $request) use ($app) { + $id = $request->request->get('id'); + + $entry = $app['entry_api']->remove($id); + + return $app->json($entry, 201); +}); return $api; diff --git a/app/controllers/front.php b/app/controllers/front.php index 8849582..4958735 100644 --- a/app/controllers/front.php +++ b/app/controllers/front.php @@ -59,6 +59,16 @@ $front->get('/unstar/{id}', function (Request $request, $id) use ($app) { }) ->bind('unstar_entry'); +$front->get('/remove/{id}', function (Request $request, $id) use ($app) { + + $entry = $app['entry_api']->remove($id); + + $referer = $request->headers->get('referer'); + + return $app->redirect($referer); +}) +->bind('remove_entry'); + $front->match('/add', function (Request $request) use ($app) { $data = array('url'); diff --git a/app/views/_entry.twig b/app/views/_entry.twig index 11cdc83..27db457 100644 --- a/app/views/_entry.twig +++ b/app/views/_entry.twig @@ -4,7 +4,7 @@ \ No newline at end of file diff --git a/app/views/view.twig b/app/views/view.twig index 86b07a2..841c14c 100644 --- a/app/views/view.twig +++ b/app/views/view.twig @@ -9,8 +9,8 @@

{{ entry.title }}

diff --git a/src/Poche/Api/EntryApi.php b/src/Poche/Api/EntryApi.php index 04d69b0..ab98092 100644 --- a/src/Poche/Api/EntryApi.php +++ b/src/Poche/Api/EntryApi.php @@ -39,6 +39,10 @@ class EntryApi return $this->entryRepository->unstar($id); } + public function remove($id) { + return $this->entryRepository->remove($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 0afceb3..e6e5362 100644 --- a/src/Poche/Repository/EntryRepository.php +++ b/src/Poche/Repository/EntryRepository.php @@ -20,7 +20,7 @@ class EntryRepository //TODO don't hardcode the user ;) public function getBookmarks($userId = 1) { - $sql = "SELECT * FROM entries where user_id = ? AND bookmark = 1 ORDER BY id DESC"; + $sql = "SELECT * FROM entries where user_id = ? AND bookmark = 1 and status <> 'removed' ORDER BY id DESC"; $entries = $this->db->fetchAll($sql, array($userId)); return $entries ? $entries : array(); @@ -34,7 +34,7 @@ class EntryRepository //TODO don't hardcode the user ;) public function getEntryById($id, $userId = 1) { - $sql = "SELECT * FROM entries where id = ? AND user_id = ?"; + $sql = "SELECT * FROM entries where id = ? AND user_id = ? AND status <> 'removed'"; $entry = $this->db->fetchAll($sql, array($id, $userId)); return $entry ? $entry : array(); @@ -72,5 +72,13 @@ class EntryRepository return $count; } + //TODO don't hardcode the user ;) + public function remove($id, $userId = 1) { + $sql = "UPDATE entries SET status = 'removed' where id = ? AND user_id = ?"; + $count = $this->db->executeUpdate($sql, array($id, $userId)); + + return $count; + } + } diff --git a/tests/functionals/ApiTest.php b/tests/functionals/ApiTest.php index 050d449..3ba0263 100644 --- a/tests/functionals/ApiTest.php +++ b/tests/functionals/ApiTest.php @@ -195,6 +195,37 @@ class ApiTest extends PocheWebTestCase $this->assertEquals('1', $client->getResponse()->getContent()); } + + public function testRemove() + { + + //Load some entries + Fixtures::loadEntries($this->app['db']); + + $client = $this->createClient(); + $crawler = $client->request( + 'GET', + '/api/remove', + array(), + array(), + array('CONTENT_TYPE' => 'application/json'), + '{"id":"1"}' + ); + + $this->assertEquals($client->getResponse()->getStatusCode(), 201); + + // Assert that the "Content-Type" header is "application/json" + $this->assertTrue( + $client->getResponse()->headers->contains( + 'Content-Type', + 'application/json' + ) + ); + + $this->assertEquals('1', $client->getResponse()->getContent()); + + } + public function testPostEntries() { @@ -222,7 +253,5 @@ class ApiTest extends PocheWebTestCase ); $this->assertEquals($client->getResponse()->getContent(),'{"url":"http:\/\/perdu.com","title":"Vous Etes Perdu ?","content":"[unable to retrieve full-text content]"}'); - - } }