From 2ab5661583d74b03c86bb4f437616bb634d9c4fc Mon Sep 17 00:00:00 2001 From: "David T. Sadler" Date: Tue, 12 Oct 2021 20:41:49 +0100 Subject: Add delete --- public/delete/index.php | 27 ++++++++++++++++ public/edit/index.php | 2 -- public/index.php | 2 -- src/DTS/BookmarkRepository.php | 70 +++++++++++++++++++++++++----------------- src/templates/delete.php | 15 +++++++++ src/templates/index.php | 2 +- 6 files changed, 85 insertions(+), 33 deletions(-) create mode 100644 public/delete/index.php create mode 100644 src/templates/delete.php diff --git a/public/delete/index.php b/public/delete/index.php new file mode 100644 index 0000000..1b2484d --- /dev/null +++ b/public/delete/index.php @@ -0,0 +1,27 @@ +find($id); + +if ('POST' === filter_input(INPUT_SERVER, 'REQUEST_METHOD')) { + $bookmarks->delete($bookmark); +} + +$html = $template->render('delete', compact('bookmark')); + +respondAndExit(200, 'OK', $html); diff --git a/public/edit/index.php b/public/edit/index.php index 0cc1c27..65b3fa5 100644 --- a/public/edit/index.php +++ b/public/edit/index.php @@ -16,8 +16,6 @@ $template = new Template($config['path_to_templates']); $id = filter_input(INPUT_GET, 'id') ?? filter_input(INPUT_POST, 'id') ?? null; -$bookmarks->load(); - $bookmark = $bookmarks->find($id); if ('POST' === filter_input(INPUT_SERVER, 'REQUEST_METHOD')) { diff --git a/public/index.php b/public/index.php index 2a9ccb7..7453506 100644 --- a/public/index.php +++ b/public/index.php @@ -18,8 +18,6 @@ $sort = $_GET['sort'] ?? null; $tag = $_GET['tag'] ?? null; -$bookmarks->load(); - if ($tag !== null) { $bookmarks->filter($tag); } diff --git a/src/DTS/BookmarkRepository.php b/src/DTS/BookmarkRepository.php index d673338..5d22100 100644 --- a/src/DTS/BookmarkRepository.php +++ b/src/DTS/BookmarkRepository.php @@ -17,30 +17,8 @@ class BookmarkRepository implements \Iterator function __construct(string $pathToRepository) { $this->pathToRepository = $pathToRepository; - } - - public function load(): void - { - if (!is_file($this->pathToRepository) || !is_readable($this->pathToRepository)) { - throw new \Exception("Unable to locate repository {$this->pathToRepository}"); - } - - if (($fp = fopen($this->pathToRepository, 'r')) === FALSE) { - throw new \Exception("Unable to read from repository {$this->pathToRepository}"); - } - - while (($data = fgetcsv($fp)) !== FALSE) { - $this->repository[] = new Bookmark( - $data[0], // Id. - $data[1], // Url. - $data[2], // Title. - $data[3], // Tag. - $data[4], // Added At. - (bool)$data[4], // Read. - ); - } - fclose($fp); + $this->load(); } public function sort(bool $asc = true): BookmarkRepository @@ -67,14 +45,14 @@ class BookmarkRepository implements \Iterator { $index = $this->findBookmarkIndex($id); - return $this->repository[$index] ?? null; + return $this->repository[$index] ?? null; } public function add(Bookmark $bookmark): bool { $this->repository[] = $bookmark; - return $this->store(); + return $this->save(); } public function update(Bookmark $bookmark): bool @@ -83,9 +61,21 @@ class BookmarkRepository implements \Iterator if ($index !== null) { $this->repository[$index] = $bookmark; - print_r($bookmark); - return $this->store(); + return $this->save(); + } + + return false; + } + + public function delete(Bookmark $bookmark): bool + { + $index = $this->findBookmarkIndex($bookmark->id); + + if ($index !== null) { + unset($this->repository[$index]); + + return $this->save(); } return false; @@ -127,7 +117,31 @@ class BookmarkRepository implements \Iterator return null; } - public function store(): bool + private function load(): void + { + if (!is_file($this->pathToRepository) || !is_readable($this->pathToRepository)) { + throw new \Exception("Unable to locate repository {$this->pathToRepository}"); + } + + if (($fp = fopen($this->pathToRepository, 'r')) === FALSE) { + throw new \Exception("Unable to read from repository {$this->pathToRepository}"); + } + + while (($data = fgetcsv($fp)) !== FALSE) { + $this->repository[] = new Bookmark( + $data[0], // Id. + $data[1], // Url. + $data[2], // Title. + $data[3], // Tag. + $data[4], // Added At. + (bool)$data[4], // Read. + ); + } + + fclose($fp); + } + + private function save(): bool { if (($fp = fopen($this->pathToRepository, 'w')) === FALSE) { throw new \Exception("Unable to open repository {$this->pathToRepository}"); diff --git a/src/templates/delete.php b/src/templates/delete.php new file mode 100644 index 0000000..1be762b --- /dev/null +++ b/src/templates/delete.php @@ -0,0 +1,15 @@ + + + + + + Bookmarks + + +
+ + url.' '.$bookmark->title.' '.$bookmark->tag; ?> + +
+ + diff --git a/src/templates/index.php b/src/templates/index.php index 2703851..1298985 100644 --- a/src/templates/index.php +++ b/src/templates/index.php @@ -8,7 +8,7 @@ -- cgit v1.2.3-13-gbd6f