Database and markdown refactoring

This commit is contained in:
2024-08-05 17:32:48 +02:00
parent fb58a24170
commit 310a4accc4
24 changed files with 1143 additions and 333 deletions

View File

@@ -0,0 +1,54 @@
import useDatabase from '~/composables/useDatabase';
export interface File
{
id: number;
path: string;
title: string;
type: 'Markdown' | 'Canvas' | 'File';
content: string;
owner: number;
}
export default defineEventHandler(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(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);
});

View File

@@ -0,0 +1,39 @@
import useDatabase from '~/composables/useDatabase';
export interface Comment
{
file: number;
user_id: number;
sequence: number;
position: number;
length: number;
content: string;
}
export default defineEventHandler(async (e) => {
const project = getRouterParam(e, "projectId");
const file = getRouterParam(e, "fileId");
const query = getQuery(e);
if(!project)
{
setResponseStatus(e, 404);
return;
}
const where = ["project = $project", "file = $file"];
const criteria: Record<string, any> = { $project: project, $file: file };
if(where.length > 1)
{
const db = useDatabase();
const content = db.query(`SELECT * FROM explorer_comments WHERE ${where.join(" and ")}`).all(criteria) as Comment[];
if(content.length > 0)
{
return content;
}
}
setResponseStatus(e, 404);
});

View File

@@ -0,0 +1,52 @@
import useDatabase from '~/composables/useDatabase';
export interface Navigation
{
title: string;
path: string;
type: string;
order: number
children?: Navigation[];
}
export default defineEventHandler(async (e) => {
const project = getRouterParam(e, "projectId");
if(!project)
{
setResponseStatus(e, 404);
return;
}
const db = useDatabase();
const content = db.query(`SELECT "path", "title", "type", "order" FROM explorer_files WHERE project = ?1 and navigable = 1`).all(project!).sort((a: any, b: any) => a.path.length - b.path.length) as any[];
if(content.length > 0)
{
const navigation: Navigation[] = [];
for(const item of content)
addChild(navigation, item);
return navigation;
}
setResponseStatus(e, 404);
});
function addChild(arr: Navigation[], e: Navigation): void
{
const parent = arr.find(f => e.path.startsWith(f.path));
if(parent)
{
if(!parent.children)
parent.children = [];
addChild(parent.children, e);
}
else
{
arr.push({ title: e.title, path: e.path, type: e.type, order: e.order });
}
}