49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
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);
|
|
}); |