54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import useDatabase from '~/composables/useDatabase';
|
|
import type { File } from '~/types/api';
|
|
|
|
export default defineCachedEventHandler(async (e) => {
|
|
const project = getRouterParam(e, "projectId");
|
|
const query = getQuery(e);
|
|
|
|
if(!project)
|
|
{
|
|
setResponseStatus(e, 404);
|
|
return;
|
|
}
|
|
|
|
const where = ["project = $project"];
|
|
const criteria: Record<string, any> = { $project: project };
|
|
|
|
if(query && query.path !== undefined)
|
|
{
|
|
where.push("path = $path");
|
|
criteria["$path"] = query.path;
|
|
}
|
|
if(query && query.title !== undefined)
|
|
{
|
|
where.push("title = $title");
|
|
criteria["$title"] = query.title;
|
|
}
|
|
if(query && query.type !== undefined)
|
|
{
|
|
where.push("type = $type");
|
|
criteria["$type"] = query.type;
|
|
}
|
|
if (query && query.search !== undefined)
|
|
{
|
|
where.push("path LIKE $search");
|
|
criteria["$search"] = query.search;
|
|
}
|
|
|
|
if(where.length > 1)
|
|
{
|
|
const db = useDatabase();
|
|
|
|
const content = db.query(`SELECT * FROM explorer_files WHERE ${where.join(" and ")}`).all(criteria) as File[];
|
|
|
|
if(content.length > 0)
|
|
{
|
|
return content;
|
|
}
|
|
}
|
|
|
|
setResponseStatus(e, 404);
|
|
}, {
|
|
maxAge: 60*60*24,
|
|
getKey: (e) => `${getRouterParam(e, "projectId")}-${JSON.stringify(getQuery(e))}`
|
|
}); |