This commit is contained in:
2024-11-13 13:41:35 +01:00
32 changed files with 549 additions and 34 deletions

View File

@@ -1,3 +1,5 @@
import { hasPermissions } from "#shared/auth.util";
export default defineEventHandler(async (e) => {
const session = await getUserSession(e);

View File

@@ -1,6 +1,6 @@
import useDatabase from '~/composables/useDatabase';
import { explorerContentTable } from '~/db/schema';
import type { Navigation } from '~/types/api';
import type { Navigation, NavigationItem } from '~/schemas/navigation';
export default defineEventHandler(async (e) => {
const { user } = await getUserSession(e);
@@ -11,11 +11,19 @@ export default defineEventHandler(async (e) => {
}*/
const db = useDatabase();
const content = db.select({ path: explorerContentTable.path, title: explorerContentTable.title, type: explorerContentTable.type, private: explorerContentTable.private, navigable: explorerContentTable.navigable, owner: explorerContentTable.owner }).from(explorerContentTable).prepare().all() as (Navigation & { owner: number, navigable: boolean })[];
const content = db.select({
path: explorerContentTable.path,
type: explorerContentTable.type,
owner: explorerContentTable.owner,
title: explorerContentTable.title,
navigable: explorerContentTable.navigable,
private: explorerContentTable.private,
order: explorerContentTable.order,
}).from(explorerContentTable).prepare().all();
if(content.length > 0)
{
const navigation: Navigation[] = [];
const navigation: Navigation = [];
for(const idx in content)
{
@@ -45,7 +53,7 @@ export default defineEventHandler(async (e) => {
setResponseStatus(e, 404);
});
function addChild(arr: Navigation[], e: Navigation): void
function addChild(arr: Navigation, e: NavigationItem): void
{
const parent = arr.find(f => e.path.startsWith(f.path));
@@ -58,6 +66,11 @@ function addChild(arr: Navigation[], e: Navigation): void
}
else
{
arr.push({ title: e.title, path: e.path, type: e.type, private: e.private });
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,49 @@
import { hasPermissions } from "#shared/auth.util";
import useDatabase from '~/composables/useDatabase';
import { explorerContentTable } from '~/db/schema';
import { schema } from '~/schemas/navigation';
export default defineEventHandler(async (e) => {
const { user } = await getUserSession(e);
if(!user || !hasPermissions(user.permissions, ['admin', 'editor']))
{
throw createError({ statusCode: 401, statusText: 'Unauthorized' });
}
const body = await readValidatedBody(e, schema.safeParse);
if(!body.success)
{
throw body.error;
}
const db = useDatabase();
db.transaction((tx) => {
for(let i = 0; i < body.data.length; i++)
{
tx.insert(explorerContentTable).values({
path: body.data[i].path,
owner: body.data[i].owner,
title: body.data[i].title,
type: body.data[i].type,
navigable: body.data[i].navigable,
private: body.data[i].private,
order: body.data[i].order,
content: Buffer.from('', 'utf-8'),
}).onConflictDoUpdate({
set: {
owner: body.data[i].owner,
title: body.data[i].title,
type: body.data[i].type,
navigable: body.data[i].navigable,
private: body.data[i].private,
order: body.data[i].order,
},
target: explorerContentTable.path,
})
}
})
setResponseStatus(e, 404);
});