You've already forked obsidian-visualiser
Starting to put back the server part. Currently the registration and login are almost ready.
This commit is contained in:
105
server/api/auth/login.post.ts
Normal file
105
server/api/auth/login.post.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import useDatabase from '~/composables/useDatabase';
|
||||
import { schema } from '~/schemas/login';
|
||||
import type { User, UserExtendedData, UserRawData, UserSession, UserSessionRequired } from '~/types/auth';
|
||||
import { ZodError } from 'zod';
|
||||
import { checkSession, logSession } from '~/server/utils/user';
|
||||
import { usersTable } from '~/db/schema';
|
||||
import { eq, or, sql } from 'drizzle-orm';
|
||||
import type { BunSQLiteDatabase } from 'drizzle-orm/bun-sqlite';
|
||||
|
||||
interface SuccessHandler
|
||||
{
|
||||
success: true;
|
||||
session: UserSession;
|
||||
}
|
||||
interface ErrorHandler
|
||||
{
|
||||
success: false;
|
||||
error: Error | ZodError<{
|
||||
usernameOrEmail: string;
|
||||
password: string;
|
||||
}>;
|
||||
}
|
||||
type Return = SuccessHandler | ErrorHandler;
|
||||
|
||||
export default defineEventHandler(async (e): Promise<Return> => {
|
||||
try
|
||||
{
|
||||
const session = await getUserSession(e);
|
||||
const db = useDatabase();
|
||||
|
||||
const checkedSession = await checkSession(e, session);
|
||||
|
||||
if(checkedSession !== undefined)
|
||||
return checkedSession;
|
||||
|
||||
const body = await readValidatedBody(e, schema.safeParse);
|
||||
|
||||
if (!body.success)
|
||||
{
|
||||
await clearUserSession(e);
|
||||
|
||||
setResponseStatus(e, 406);
|
||||
return { success: false, error: body.error };
|
||||
}
|
||||
|
||||
const hash = await Bun.password.hash(body.data.password);
|
||||
const id = db.select({ id: usersTable.id, hash: usersTable.hash }).from(usersTable).where(or(eq(usersTable.username, sql.placeholder('username')), eq(usersTable.email, sql.placeholder('username')))).prepare().get({ username: body.data.usernameOrEmail });
|
||||
|
||||
if(!id || !id.id || !id.hash)
|
||||
{
|
||||
await clearUserSession(e);
|
||||
|
||||
setResponseStatus(e, 401);
|
||||
return { success: false, error: new ZodError([{ code: 'custom', path: ['username'], message: 'Identifiant inconnu' }]) };
|
||||
}
|
||||
|
||||
const valid = await Bun.password.verify(body.data.password, id.hash);
|
||||
|
||||
if(!valid)
|
||||
{
|
||||
await clearUserSession(e);
|
||||
|
||||
setResponseStatus(e, 401);
|
||||
return { success: false, error: new ZodError([{ code: 'custom', path: ['password'], message: 'Mot de passe incorrect' }]) };
|
||||
}
|
||||
|
||||
const user = db.query.usersTable.findFirst({
|
||||
columns: {
|
||||
id: true,
|
||||
email: true,
|
||||
username: true,
|
||||
state: true,
|
||||
},
|
||||
with: {
|
||||
data: true,
|
||||
},
|
||||
where: (table) => eq(table.id, sql.placeholder('id'))
|
||||
}).prepare().get({ id: id.id });
|
||||
|
||||
if(!user)
|
||||
{
|
||||
setResponseStatus(e, 401);
|
||||
return { success: false, error: new Error('Données utilisateur introuvable') };
|
||||
}
|
||||
|
||||
const data = await logSession(e, await setUserSession(e, {
|
||||
user: {
|
||||
...user.data,
|
||||
email: user.email,
|
||||
username: user.username,
|
||||
state: user.state,
|
||||
}
|
||||
}) as UserSessionRequired);
|
||||
|
||||
setResponseStatus(e, 201);
|
||||
return { success: true, session: data };
|
||||
}
|
||||
catch(err: any)
|
||||
{
|
||||
console.error(err);
|
||||
|
||||
await clearUserSession(e);
|
||||
return { success: false, error: err as Error };
|
||||
}
|
||||
});
|
||||
87
server/api/auth/register.post.ts
Normal file
87
server/api/auth/register.post.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { count, eq, sql } from 'drizzle-orm';
|
||||
import { ZodError, type ZodIssue } from 'zod';
|
||||
import useDatabase from '~/composables/useDatabase';
|
||||
import { usersDataTable, usersTable } from '~/db/schema';
|
||||
import { schema } from '~/schemas/registration';
|
||||
import { checkSession, logSession } from '~/server/utils/user';
|
||||
import type { UserSession, UserSessionRequired } from '~/types/auth';
|
||||
|
||||
interface SuccessHandler
|
||||
{
|
||||
success: true;
|
||||
session: UserSession;
|
||||
}
|
||||
interface ErrorHandler
|
||||
{
|
||||
success: false;
|
||||
error: Error | ZodError<{
|
||||
username: string;
|
||||
email: string;
|
||||
password: string;
|
||||
}>;
|
||||
}
|
||||
type Return = SuccessHandler | ErrorHandler;
|
||||
|
||||
export default defineEventHandler(async (e): Promise<Return> => {
|
||||
try
|
||||
{
|
||||
const session = await getUserSession(e);
|
||||
const db = useDatabase();
|
||||
|
||||
const checkedSession = await checkSession(e, session);
|
||||
|
||||
if(checkedSession !== undefined)
|
||||
return checkedSession;
|
||||
|
||||
const body = await readValidatedBody(e, schema.safeParse);
|
||||
|
||||
if (!body.success)
|
||||
{
|
||||
await clearUserSession(e);
|
||||
|
||||
setResponseStatus(e, 406);
|
||||
return { success: false, error: body.error };
|
||||
}
|
||||
|
||||
const checkUsername = db.select({ count: count() }).from(usersTable).where(eq(usersTable.username, sql.placeholder('username'))).prepare().get({ username: body.data.username });
|
||||
const checkEmail = db.select({ count: count() }).from(usersTable).where(eq(usersTable.email, sql.placeholder('email'))).prepare().get({ email: body.data.email });
|
||||
|
||||
const errors: ZodIssue[] = [];
|
||||
if(!checkUsername || checkUsername.count !== 0)
|
||||
errors.push({ code: 'custom', path: ['username'], message: "Ce nom d'utilisateur est déjà utilisé" });
|
||||
if(!checkEmail || checkEmail.count !== 0)
|
||||
errors.push({ code: 'custom', path: ['email'], message: "Cette adresse mail est déjà utilisée" });
|
||||
|
||||
if(errors.length > 0)
|
||||
{
|
||||
setResponseStatus(e, 406);
|
||||
return { success: false, error: new ZodError(errors) };
|
||||
}
|
||||
else
|
||||
{
|
||||
const hash = await Bun.password.hash(body.data.password);
|
||||
db.insert(usersTable).values({ username: sql.placeholder('username'), email: sql.placeholder('email'), hash: sql.placeholder('hash'), state: sql.placeholder('state') }).prepare().run({ username: body.data.username, email: body.data.email, hash, state: 0 });
|
||||
const id = db.select({ id: usersTable.id }).from(usersTable).where(eq(usersTable.username, sql.placeholder('username'))).prepare().get({ username: body.data.username });
|
||||
|
||||
if(!id || !id.id)
|
||||
{
|
||||
setResponseStatus(e, 406);
|
||||
return { success: false, error: new Error('Erreur de création de compte') };
|
||||
}
|
||||
|
||||
db.insert(usersDataTable).values({ id: sql.placeholder('id') }).prepare().run({ id: id.id });
|
||||
|
||||
logSession(e, await setUserSession(e, { user: { id: id.id, username: body.data.username, email: body.data.email, state: 0, signin: new Date() } }) as UserSessionRequired);
|
||||
|
||||
setResponseStatus(e, 201);
|
||||
return { success: true, session };
|
||||
}
|
||||
}
|
||||
catch(err: any)
|
||||
{
|
||||
console.error(err);
|
||||
|
||||
await clearUserSession(e);
|
||||
return { success: false, error: err as Error };
|
||||
}
|
||||
});
|
||||
8
server/api/auth/session.delete.ts
Normal file
8
server/api/auth/session.delete.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { eventHandler } from 'h3';
|
||||
import { clearUserSession } from '~/server/utils/session';
|
||||
|
||||
export default eventHandler(async (event) => {
|
||||
await clearUserSession(event);
|
||||
|
||||
return { loggedOut: true };
|
||||
})
|
||||
13
server/api/auth/session.get.ts
Normal file
13
server/api/auth/session.get.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { eventHandler } from 'h3'
|
||||
import { getUserSession, sessionHooks } from '~/server/utils/session'
|
||||
import type { UserSessionRequired } from '~/types/auth'
|
||||
|
||||
export default eventHandler(async (event) => {
|
||||
const session = await getUserSession(event)
|
||||
|
||||
if (session.user) {
|
||||
await sessionHooks.callHookParallel('fetch', session as UserSessionRequired, event)
|
||||
}
|
||||
|
||||
return session
|
||||
})
|
||||
31
server/api/project.get.ts
Normal file
31
server/api/project.get.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import useDatabase from '~/composables/useDatabase';
|
||||
import { ProjectSearch } from '~/types/api';
|
||||
|
||||
export default defineEventHandler(async (e) => {
|
||||
const query = getQuery(e);
|
||||
|
||||
const where = ["f.type != $type"];
|
||||
const criteria: Record<string, any> = { $type: "Folder" };
|
||||
|
||||
if(query && query.owner !== undefined)
|
||||
{
|
||||
where.push("owner = $owner");
|
||||
criteria["$owner"] = query.owner;
|
||||
}
|
||||
if(query && query.name !== undefined)
|
||||
{
|
||||
where.push("name = $name");
|
||||
criteria["$name"] = query.name;
|
||||
}
|
||||
|
||||
const db = useDatabase();
|
||||
|
||||
const content = db.query(`SELECT p.*, u.username, COUNT(f.path) as pages FROM explorer_projects p LEFT JOIN users u ON p.owner = u.id LEFT JOIN explorer_files f ON f.project = p.id WHERE ${where.join(" AND ")} GROUP BY p.id`).all(criteria) as ProjectSearch[];
|
||||
|
||||
if(content.length > 0)
|
||||
{
|
||||
return content;
|
||||
}
|
||||
|
||||
setResponseStatus(e, 404);
|
||||
});
|
||||
20
server/api/project/[projectId].get.ts
Normal file
20
server/api/project/[projectId].get.ts
Normal 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);
|
||||
});
|
||||
20
server/api/project/[projectId].patch.ts
Normal file
20
server/api/project/[projectId].patch.ts
Normal 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);
|
||||
});
|
||||
20
server/api/project/[projectId].post.ts
Normal file
20
server/api/project/[projectId].post.ts
Normal 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);
|
||||
});
|
||||
0
server/api/project/[projectId]/access.post.ts
Normal file
0
server/api/project/[projectId]/access.post.ts
Normal file
0
server/api/project/[projectId]/comment.post.ts
Normal file
0
server/api/project/[projectId]/comment.post.ts
Normal file
54
server/api/project/[projectId]/file.get.ts
Normal file
54
server/api/project/[projectId]/file.get.ts
Normal 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))}`
|
||||
});
|
||||
0
server/api/project/[projectId]/file.post.ts
Normal file
0
server/api/project/[projectId]/file.post.ts
Normal file
41
server/api/project/[projectId]/file/[path].get.ts
Normal file
41
server/api/project/[projectId]/file/[path].get.ts
Normal 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")}`
|
||||
});
|
||||
72
server/api/project/[projectId]/navigation.get.ts
Normal file
72
server/api/project/[projectId]/navigation.get.ts
Normal 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
42
server/api/project/[projectId]/tags/[tag].get.ts
Normal file
42
server/api/project/[projectId]/tags/[tag].get.ts
Normal 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")}`
|
||||
});
|
||||
21
server/api/search.get.ts
Normal file
21
server/api/search.get.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import useDatabase from '~/composables/useDatabase';
|
||||
|
||||
export default defineEventHandler(async (e) => {
|
||||
const query = getQuery(e);
|
||||
|
||||
if (query.search) {
|
||||
const db = useDatabase();
|
||||
|
||||
const projects = db.query(`SELECT p.*, u.username, COUNT(f.path) as pages FROM explorer_projects p LEFT JOIN users u ON p.owner = u.id LEFT JOIN explorer_files f ON f.project = p.id WHERE name LIKE ?1 AND f.type != "Folder" GROUP BY p.id`).all(query.search) as ProjectSearch[];
|
||||
const files = db.query(`SELECT f.*, u.username, count(c.path) as comments FROM explorer_files f LEFT JOIN users u ON f.owner = u.id LEFT JOIN explorer_comments c ON c.project = f.project AND c.path = f.path WHERE title LIKE ?1 AND private = 0 AND type != "Folder" GROUP BY f.project, f.path`).all(query.search) as FileSearch[];
|
||||
const users = db.query(`SELECT id, username FROM users WHERE username LIKE ?1`).all(query.search) as UserSearch[];
|
||||
|
||||
return {
|
||||
projects,
|
||||
files,
|
||||
users
|
||||
} as Search;
|
||||
}
|
||||
|
||||
setResponseStatus(e, 404);
|
||||
});
|
||||
16
server/api/users/[id].get.ts
Normal file
16
server/api/users/[id].get.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import useDatabase from "~/composables/useDatabase";
|
||||
import type { User } from "~/types/auth";
|
||||
|
||||
export default defineEventHandler((e) => {
|
||||
const id = getRouterParam(e, 'id');
|
||||
|
||||
if(!id)
|
||||
{
|
||||
setResponseStatus(e, 400);
|
||||
return;
|
||||
}
|
||||
|
||||
const db = useDatabase();
|
||||
|
||||
return db.query(`SELECT id, username, email, state, d.* FROM users u LEFT JOIN users_data d ON u.id = d.user_id WHERE u.id = ?1`).get(id) as User;
|
||||
});
|
||||
16
server/api/users/[id]/comments.get.ts
Normal file
16
server/api/users/[id]/comments.get.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import useDatabase from "~/composables/useDatabase";
|
||||
import type { CommentSearch } from "~/types/api";
|
||||
|
||||
export default defineEventHandler((e) => {
|
||||
const id = getRouterParam(e, 'id');
|
||||
|
||||
if(!id)
|
||||
{
|
||||
setResponseStatus(e, 400);
|
||||
return;
|
||||
}
|
||||
|
||||
const db = useDatabase();
|
||||
|
||||
return db.query(`SELECT * FROM explorer_comments WHERE user_id = ?1`).all(id) as CommentSearch[];
|
||||
});
|
||||
16
server/api/users/[id]/projects.get.ts
Normal file
16
server/api/users/[id]/projects.get.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import useDatabase from "~/composables/useDatabase";
|
||||
import type { ProjectSearch } from "~/types/api";
|
||||
|
||||
export default defineEventHandler((e) => {
|
||||
const id = getRouterParam(e, 'id');
|
||||
|
||||
if(!id)
|
||||
{
|
||||
setResponseStatus(e, 400);
|
||||
return;
|
||||
}
|
||||
|
||||
const db = useDatabase();
|
||||
|
||||
return db.query(`SELECT p.*, count(f.path) as pages FROM explorer_projects p LEFT JOIN explorer_files f ON p.id = f.project WHERE p.owner = ?1`).all(id) as Omit<ProjectSearch, 'username'>[];
|
||||
});
|
||||
Reference in New Issue
Block a user