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

@ -23,5 +23,3 @@ export const TYPESENSE_API_KEY = Deno.env.get("TYPESENSE_API_KEY");
export const LOG_LEVEL: string = Deno.env.get("LOG_LEVEL") ||
"warn";
console.log({ LOG_LEVEL });

View File

@ -23,9 +23,8 @@ const logFuncs = {
} as const;
let longestScope = 0;
let logLevel = (_LOG_LEVEL && _LOG_LEVEL in logMap && logMap[_LOG_LEVEL]) ||
let logLevel = (_LOG_LEVEL && _LOG_LEVEL in logMap && logMap[_LOG_LEVEL]) ??
LOG_LEVEL.WARN;
console.log({ logLevel, logMap });
const ee = new EventEmitter<{
log: { level: LOG_LEVEL; scope: string; args: unknown[] };

View File

@ -13,7 +13,7 @@ export type Article = {
name: string;
tags: string[];
meta: {
status: "finished" | "not-finished";
done?: boolean;
date: Date;
link: string;
thumbnail?: string;

View File

@ -17,7 +17,6 @@ export type Movie = {
average?: string;
author: string;
rating: number;
status: "not-seen" | "watch-again" | "finished";
};
};

View File

@ -18,7 +18,7 @@ export type Series = {
rating: number;
average?: string;
thumbnail?: string;
status: "not-seen" | "watch-again" | "finished";
done?: boolean;
};
};

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,

View File

@ -41,6 +41,7 @@ export type GenericResource = {
meta?: {
image?: string;
author?: string;
rating?: number;
average?: string;
thumbnail?: string;
};
@ -66,9 +67,4 @@ export type TypesenseDocument = {
image?: string;
};
export enum ResourceStatus {
COMPLETED = "completed",
NOT_COMPLETED = "not_completed",
}
export type SearchResult = SearchResponse<TypesenseDocument>;

View File

@ -77,7 +77,6 @@ async function initializeTypesense() {
{ name: "type", type: "string", facet: true },
{ name: "date", type: "string", optional: true },
{ name: "author", type: "string", facet: true, optional: true },
{ name: "status", type: "string", facet: true, optional: true },
{ name: "rating", type: "int32", facet: true },
{ name: "tags", type: "string[]", facet: true },
{ name: "description", type: "string", optional: true },