Starting to put back the server part. Currently the registration and login are almost ready.

This commit is contained in:
2024-11-05 18:09:42 +01:00
parent e8b521f122
commit 83ddaf19d4
52 changed files with 2022 additions and 118 deletions

View File

@@ -0,0 +1,20 @@
import useDatabase from '~/composables/useDatabase';
export default defineEventHandler(async (e) => {
const project = getRouterParam(e, "projectId");
const where = ["id = $id"];
const criteria: Record<string, any> = { $id: project };
if (!!project) {
const db = useDatabase();
const content = db.query(`SELECT * FROM explorer_projects WHERE ${where.join(" and ")}`).get(criteria) as Project;
if (content) {
return content;
}
}
setResponseStatus(e, 404);
});

View File

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

View File

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

View File

@@ -0,0 +1,54 @@
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))}`
});

View File

@@ -0,0 +1,41 @@
import useDatabase from '~/composables/useDatabase';
import type { CommentedFile, CommentSearch, File } from '~/types/api';
export default defineCachedEventHandler(async (e) => {
const project = getRouterParam(e, "projectId");
const path = decodeURIComponent(getRouterParam(e, "path") ?? '');
if(!project)
{
setResponseStatus(e, 404);
return;
}
if(!path)
{
setResponseStatus(e, 404);
return;
}
const where = ["project = $project", "path = $path"];
const criteria: Record<string, any> = { $project: project, $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, "projectId")}-${getRouterParam(e, "path")}`
});

View File

@@ -0,0 +1,72 @@
import useDatabase from '~/composables/useDatabase';
import { Navigation } from '~/types/api';
type NavigatioNExtension = Navigation & { owner: number, navigable: boolean };
export default defineEventHandler(async (e) => {
const project = getRouterParam(e, "projectId");
const { user } = await getUserSession(e);
if(!project)
{
setResponseStatus(e, 404);
return;
}
const db = useDatabase();
const content = db.query(`SELECT "path", "title", "type", "order", "private", "navigable", "owner" FROM explorer_files WHERE project = ?1`).all(project!).sort((a: any, b: any) => a.path.length - b.path.length) as NavigatioNExtension[];
if(content.length > 0)
{
const navigation: Navigation[] = [];
for(const idx in content)
{
const item = content[idx];
if(!!item.private && (user?.id ?? -1) !== item.owner || !item.navigable)
{
delete content[idx];
continue;
}
const parent = item.path.includes('/') ? item.path.substring(0, item.path.lastIndexOf('/')) : undefined;
if(parent && !content.find(e => e && e.path === parent))
{
delete content[idx];
continue;
}
}
for(const item of content.filter(e => !!e))
{
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, private: e.private });
arr.sort((a, b) => {
if(a.order && b.order)
return a.order - b.order;
return a.title.localeCompare(b.title);
});
}
}

View File

@@ -0,0 +1,42 @@
import useDatabase from '~/composables/useDatabase';
import type { Tag } from '~/types/api';
export default defineCachedEventHandler(async (e) => {
try
{
const project = getRouterParam(e, "projectId");
const tag = decodeURIComponent(getRouterParam(e, "tag") ?? '');
if(!project)
{
setResponseStatus(e, 404);
return;
}
if(!tag)
{
setResponseStatus(e, 404);
return;
}
const where = ["project = $project", "tag = $tag"];
const criteria: Record<string, any> = { $project: project, $tag: tag };
const db = useDatabase();
const content = db.query(`SELECT * FROM explorer_tags WHERE ${where.join(" and ")}`).get(criteria) as Tag;
if(content !== undefined)
{
return content;
}
setResponseStatus(e, 404);
}
catch(err)
{
console.error(err);
setResponseStatus(e, 500);
}
}, {
maxAge: 60*60*24,
getKey: (e) => `tag-${getRouterParam(e, "projectId")}-${getRouterParam(e, "tag")}`
});