diff options
| author | David T. Sadler <davidtsadler@googlemail.com> | 2021-10-12 20:41:49 +0100 |
|---|---|---|
| committer | David T. Sadler <davidtsadler@googlemail.com> | 2021-10-12 20:41:49 +0100 |
| commit | 2ab5661583d74b03c86bb4f437616bb634d9c4fc (patch) | |
| tree | 2e09a8386148e8994def741f5fffdd733d297e38 /src | |
| parent | 523a84dd4b7cb611955e949fd6debc4a3c037b91 (diff) | |
Add delete
Diffstat (limited to 'src')
| -rw-r--r-- | src/DTS/BookmarkRepository.php | 70 | ||||
| -rw-r--r-- | src/templates/delete.php | 15 | ||||
| -rw-r--r-- | src/templates/index.php | 2 |
3 files changed, 58 insertions, 29 deletions
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 @@ +<!doctype html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>Bookmarks</title> + </head> + <body> + <form action="/delete" method="POST"> + <input type="hidden" name="id" value="<?php echo $bookmark->id; ?>"/> + <?php echo $bookmark->url.' '.$bookmark->title.' '.$bookmark->tag; ?> + <button type="submit">Delete</button> + </form> + </body> +</html> 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 @@ <body> <ul> <?php foreach ($bookmarks as $bookmark) { ?> - <li><a href="/edit?id=<?php echo $bookmark->id; ?>"><?php echo $bookmark->title; ?></a></li> + <li><a href="/edit?id=<?php echo $bookmark->id; ?>"><?php echo $bookmark->title; ?></a> | <a href="/delete?id=<?php echo $bookmark->id; ?>">Delete</a></li> <?php } ?> </ul> </body> |
