From 0f273c0a8ead390346571c4be2296fa6e384eff6 Mon Sep 17 00:00:00 2001 From: "David T. Sadler" Date: Thu, 7 Oct 2021 21:31:17 +0100 Subject: Add bookmarks --- public/add/index.php | 49 +++++++++++++++++++----------------------- public/index.php | 1 - src/DTS/Bookmark.php | 9 +++++--- src/DTS/BookmarkRepository.php | 25 +++++++++++++++++++-- src/DTS/Functions.php | 2 +- src/templates/add.php | 16 ++++++++++++++ src/templates/index.php | 1 - 7 files changed, 68 insertions(+), 35 deletions(-) create mode 100644 src/templates/add.php diff --git a/public/add/index.php b/public/add/index.php index 12119b6..11ce9bb 100644 --- a/public/add/index.php +++ b/public/add/index.php @@ -1,38 +1,33 @@ - $config['max_url_length']) { - respondAndExit(400, 'Bad Request'); + $bookmarks->add($bookmark); } -if (!file_put_contents($config['path_to_file'], "$url\n", FILE_APPEND)) { - respondAndExit(500, 'Internal Server Error'); -} +$html = $template->render('add'); -respondAndExit(201, 'Created'); +respondAndExit(200, 'OK', $html); diff --git a/public/index.php b/public/index.php index 9b4ac6e..2a9ccb7 100644 --- a/public/index.php +++ b/public/index.php @@ -18,7 +18,6 @@ $sort = $_GET['sort'] ?? null; $tag = $_GET['tag'] ?? null; - $bookmarks->load(); if ($tag !== null) { diff --git a/src/DTS/Bookmark.php b/src/DTS/Bookmark.php index 93c7a29..560fad4 100644 --- a/src/DTS/Bookmark.php +++ b/src/DTS/Bookmark.php @@ -10,18 +10,21 @@ class Bookmark public string $url; + public string $title; + public string $tag; public string $addedAt; - public bool $read; + public bool $unread; - function __construct(string $id, string $url, string $tag, string $addedAt, bool $read) + function __construct(string $id, string $url, string $title, string $tag, string $addedAt, bool $unread) { $this->id = $id; $this->url = $url; + $this->title = $title; $this->tag = $tag; $this->addedAt = $addedAt; - $this->read = $read; + $this->unread = $unread; } } diff --git a/src/DTS/BookmarkRepository.php b/src/DTS/BookmarkRepository.php index deb188c..ab54594 100644 --- a/src/DTS/BookmarkRepository.php +++ b/src/DTS/BookmarkRepository.php @@ -31,8 +31,9 @@ class BookmarkRepository implements \Iterator $this->repository[] = new Bookmark( $data[0], // Id. $data[1], // Url. - $data[2], // Tag. - $data[3], // Added At. + $data[2], // Title. + $data[3], // Tag. + $data[4], // Added At. (bool)$data[4], // Read. ); } @@ -60,6 +61,26 @@ class BookmarkRepository implements \Iterator return $this; } + public function add(Bookmark $bookmark): bool + { + if (($fp = fopen($this->pathToRepository, 'a')) === FALSE) { + throw new \Exception("Unable to open repository {$this->pathToRepository}"); + } + + $saved = fputcsv($fp, [ + $bookmark->id, + $bookmark->url, + $bookmark->title, + $bookmark->tag, + $bookmark->addedAt, + (int)$bookmark->unread, + ]); + + fclose($fp); + + return $saved !== false; + } + public function current(): mixed { return $this->repository[$this->position]; diff --git a/src/DTS/Functions.php b/src/DTS/Functions.php index b7f9cb3..d7ca835 100644 --- a/src/DTS/Functions.php +++ b/src/DTS/Functions.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace DTS\Functions; -function respondAndExit(int $responseCode, string $header, string $body, array $headers = []): void +function respondAndExit(int $responseCode, string $header, string $body = '', array $headers = []): void { header($header, false, $responseCode); diff --git a/src/templates/add.php b/src/templates/add.php new file mode 100644 index 0000000..a44c4d3 --- /dev/null +++ b/src/templates/add.php @@ -0,0 +1,16 @@ + + + + + + Bookmarks + + +
+
+
+
+ +
+ + diff --git a/src/templates/index.php b/src/templates/index.php index 034f09c..9656551 100644 --- a/src/templates/index.php +++ b/src/templates/index.php @@ -6,7 +6,6 @@ Bookmarks -

Hello