feat: allow filtering with null (no) rating

This commit is contained in:
2023-08-20 20:13:47 +02:00
parent aeb067aadb
commit 93f359e684
12 changed files with 67 additions and 36 deletions

View File

@@ -1,6 +1,6 @@
import { BadRequestError } from "@lib/errors.ts";
import { resources } from "@lib/resources.ts";
import { ResourceStatus, SearchResult } from "@lib/types.ts";
import { SearchResult } from "@lib/types.ts";
import { getTypeSenseClient } from "@lib/typesense.ts";
import { extractHashTags } from "@lib/string.ts";
@@ -10,7 +10,7 @@ type SearchParams = {
q: string;
type?: ResourceType;
tags?: string[];
status?: ResourceStatus;
rating?: string;
author?: string;
query_by?: string;
};
@@ -18,7 +18,7 @@ type SearchParams = {
export function parseResourceUrl(_url: string): SearchParams | undefined {
try {
const url = new URL(_url);
let query = url.searchParams.get("q");
let query = url.searchParams.get("q") || "*";
if (!query) {
return undefined;
}
@@ -34,7 +34,7 @@ export function parseResourceUrl(_url: string): SearchParams | undefined {
q: query,
type: url.searchParams.get("type") as ResourceType || undefined,
tags: hashTags,
status: url.searchParams.get("status") as ResourceStatus || undefined,
rating: url.searchParams.get("rating") || undefined,
query_by: url.searchParams.get("query_by") || undefined,
};
} catch (_err) {
@@ -43,7 +43,7 @@ export function parseResourceUrl(_url: string): SearchParams | undefined {
}
export async function searchResource(
{ q, query_by = "name,description,author,tags", tags = [], type, status }:
{ q, query_by = "name,description,author,tags", tags = [], type, rating }:
SearchParams,
): Promise<SearchResult> {
const typesenseClient = await getTypeSenseClient();
@@ -57,10 +57,6 @@ export async function searchResource(
filter_by.push(`type:=${type}`);
}
if (status) {
filter_by.push(`status:=${status}`);
}
if (tags?.length) {
filter_by.push(`tags:[${tags.map((t) => `\`${t}\``).join(",")}]`);
for (const tag of tags) {
@@ -71,6 +67,14 @@ export async function searchResource(
}
}
if (typeof rating !== "undefined") {
if (rating === "null") {
filter_by.push(`rating: null`);
} else {
filter_by.push(`rating: ${rating}`);
}
}
return await typesenseClient.collections("resources")
.documents().search({
q,