35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
import useDatabase from '~/composables/useDatabase';
|
|
import type { CommentedFile, CommentSearch, File } from '~/types/api';
|
|
|
|
export default defineCachedEventHandler(async (e) => {
|
|
const path = decodeURIComponent(getRouterParam(e, "path") ?? '');
|
|
|
|
if(!path)
|
|
{
|
|
setResponseStatus(e, 404);
|
|
return;
|
|
}
|
|
|
|
const where = ["path = $path"];
|
|
const criteria: Record<string, any> = { $path: path };
|
|
|
|
if(where.length > 1)
|
|
{
|
|
const db = useDatabase();
|
|
|
|
const content = db.query(`SELECT * FROM explorer_files WHERE ${where.join(" and ")}`).get(criteria) as File;
|
|
|
|
if(content !== undefined)
|
|
{
|
|
const comments = db.query(`SELECT comment.*, user.username FROM explorer_comments comment LEFT JOIN users user ON comment.user_id = user.id WHERE ${where.join(" and ")}`).all(criteria) as CommentSearch[];
|
|
|
|
return { ...content, comments } as CommentedFile;
|
|
}
|
|
}
|
|
|
|
setResponseStatus(e, 404);
|
|
return;
|
|
}, {
|
|
maxAge: 60*60*24,
|
|
getKey: (e) => `file-${getRouterParam(e, "path")}`
|
|
}); |