From 0dd247408e68752995c46874852ccd6591c67a7e Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Sat, 1 Feb 2025 16:38:49 +0100 Subject: [PATCH] wip: 2025-02-01T13:48:23+0100 (1738414103) --- endpoints/search/GET.php | 48 +++++------ public/assets/css/pages/search.css | 110 +++++++++-------------- public/search.php | 120 +++++++++----------------- src/Database/Models/Search/Search.php | 42 +++++++++ src/Database/Models/Work/Tag.php | 2 +- src/Database/Tables/Search/Search.php | 24 ++++++ src/Database/Tables/Work/Tags.php | 10 +-- 7 files changed, 177 insertions(+), 179 deletions(-) create mode 100644 src/Database/Models/Search/Search.php create mode 100644 src/Database/Tables/Search/Search.php diff --git a/endpoints/search/GET.php b/endpoints/search/GET.php index 0954712..7eccbed 100644 --- a/endpoints/search/GET.php +++ b/endpoints/search/GET.php @@ -1,31 +1,26 @@ ruleset = new Ruleset(strict: true); $this->ruleset->GET([ - (new Rules(self::GET_QUERY)) + (new Rules(SearchTable::QUERY->value)) ->required() ->type(Type::STRING) - ->min(1) + ->min(2) ->max(parent::SIZE_VARCHAR) ]); @@ -34,22 +29,23 @@ parent::__construct(); } - private function search_work(): Response { - return (new Call(Endpoints::WORK->value))->params([ - WorkTable::TITLE->value => $_GET[self::GET_QUERY], - WorkTable::SUMMARY->value => $_GET[self::GET_QUERY] - ])->get(); - } - public function main(): Response { - $results = [ - Endpoints::WORK->value => $this->search_work()->output() - ]; - - // Calculate the total number of results from all searched endpoints - $num_results = array_sum(array_map(fn(array $result): int => count($result), array_values($results))); - - // Return 404 if no search results - return new Response($results, $num_results > 0 ? 200 : 404); + $result = $this->db + ->for(SearchTable::NAME) + ->where([ + SearchTable::QUERY->value => [ + Operators::LIKE->value => "%{$_GET[SearchTable::QUERY->value]}%" + ] + ]) + ->select([ + SearchTable::TITLE->value, + SearchTable::SUMMARY->value, + SearchTable::CATEGORY->value, + SearchTable::HREF->value + ]); + + return $result->num_rows > 0 + ? new Response($result->fetch_all(MYSQLI_ASSOC)) + : new Response([], 404); } } \ No newline at end of file diff --git a/public/assets/css/pages/search.css b/public/assets/css/pages/search.css index ac9f75c..640b15a 100644 --- a/public/assets/css/pages/search.css +++ b/public/assets/css/pages/search.css @@ -1,85 +1,57 @@ -/* # Overrides */ - -[vv-page="/search"]:not(body) { +vv-shell[vv-page="/search"] { display: flex; flex-direction: column; - gap: var(--padding); } -/* # Sections */ - -/* ## Search */ +/* # Search */ section.search { - width: 100%; - display: none; - flex-direction: column; - align-items: center; - gap: var(--padding); - background-color: rgba(255, 255, 255, .05); - padding: calc(var(--padding) * 1.5); - margin-bottom: calc(var(--padding) * 2); -} - -vv-shell[vv-page="/search"] > section.search { display: flex; + gap: var(--padding); + border-radius: 6px; + padding: var(--padding); + background-color: rgba(255, 255, 255, .1); } section.search form { display: contents; } -section.search search { - width: 100%; -} - section.search input { - width: 100%; - border: none; - color: black; - outline: none; - padding: var(--padding); - background-color: var(--color-accent); -} - -section.search button[type="submit"] { - width: 100%; - max-width: 350px; -} - -section.search > svg { - width: 100%; -} - -/* # Search results */ - -section.results .result { - display: flex; - flex-direction: column; - gap: calc(var(--padding) / 2); -} - -/* ## Titles */ - -section.title a h2 { - color: var(--color-accent); -} - -section.title a h2::before { - content: "// "; - color: white; -} - -/* ## Work */ - -section.results.work { - display: grid; - grid-template-columns: 1fr; - gap: calc(var(--padding) / 2); -} - -section.results.work .result { - padding: var(--padding); - background-color: rgba(255, 255, 255, .03); + flex: 1 1 auto; border-radius: 6px; + padding: 0 var(--padding); + border: solid 2px rgba(255, 255, 255, .1); + background-color: rgba(255, 255, 255, .1); +} + +section.search input:focus { + outline: none; + border-color: white; +} + +section.search select { + padding: 5px; + border: none; + background-color: transparent; +} + +section.search select :is(option, optgroup) { + color: black; +} + +/* # Start search */ + +section.start-search { + display: flex; + flex: 1 1 auto; + align-items: center; + flex-direction: column; + justify-content: center; + fill: var(--color-accent); + gap: calc(var(--padding) / 2); +} + +section.start-search svg { + width: 60px; } \ No newline at end of file diff --git a/public/search.php b/public/search.php index c3b27d2..52c81e9 100644 --- a/public/search.php +++ b/public/search.php @@ -1,95 +1,59 @@ value] ?? null; + } - require_once VV::root("api/src/Database/Tables/Work/Work.php"); - require_once VV::root("api/src/Database/Tables/Work/WorkActions.php"); + public static function get_category(): ?SearchCategoryEnum { + return SearchCategoryEnum::tryFromName($_GET[SearchTable::CATEGORY->value] ?? ""); + } - // Search endpoint query paramter - const SEARCH_PARAM = "q"; - - // Connect to VLW API - $api = new API(); - - // Get search results from endpoint - $response = $api->call(Endpoints::SEARCH->value)->params([SEARCH_PARAM => $_GET[SEARCH_PARAM]])->get(); + public function search(): array { + $results = parent::all([SearchTable::QUERY->value => self::get_query()]); + } + } ?> -ok): ?> - - json(); ?> - - - value]): ?> -
-

Work

-

value]) ?> search result(s) from my public work

-
-
- - - value] as $result): ?> -
-

value] ?>

-

value] ?>

-

value]) ?>

- - - call(Endpoints::WORK_ACTIONS->value)->params([ActionsTable::REF_WORK_ID->value => $result[WorkTable::ID->value]])->get(); ?> - - - ok): ?> -
- json() as $action): ?> - - - - - -
- - -
- -
- +value, $_GET)): ?> +
+ +
- - -
- -

No results for search term ""

-
- -
- -

Start typing to search

-
- - + - \ No newline at end of file diff --git a/src/Database/Models/Search/Search.php b/src/Database/Models/Search/Search.php new file mode 100644 index 0000000..484a7ef --- /dev/null +++ b/src/Database/Models/Search/Search.php @@ -0,0 +1,42 @@ +value => $this->id + ]); + } + + public static function all(array $params = []): array { + return array_map(fn(array $item): Search => new Search($item[SearchTable::ID->value]), parent::list(Endpoints::SEARCH, $params)); + } + + public function title(): ?string { + return $this->get(Search::TITLE->value); + } + + public function summary(): string { + return $this->get(Search::SUMMARY->value); + } + + public function category(): ?SearchCategoryEnum { + return SearchCategoryEnum::tryFromName($this->get(Search::CATEGORY->value)); + } + + public function href(): ?string { + return $this->get(Search::HREF->value); + } + } \ No newline at end of file diff --git a/src/Database/Models/Work/Tag.php b/src/Database/Models/Work/Tag.php index 83df2ed..fa6dc2f 100644 --- a/src/Database/Models/Work/Tag.php +++ b/src/Database/Models/Work/Tag.php @@ -30,6 +30,6 @@ } public function label(): TagsLabelEnum { - return TagsLabelEnum::from($this->get(TagsTable::LABEL->value)); + return TagsLabelEnum::fromName($this->get(TagsTable::LABEL->value)); } } \ No newline at end of file diff --git a/src/Database/Tables/Search/Search.php b/src/Database/Tables/Search/Search.php new file mode 100644 index 0000000..56a06b6 --- /dev/null +++ b/src/Database/Tables/Search/Search.php @@ -0,0 +1,24 @@ +