37 lines
881 B
TypeScript
37 lines
881 B
TypeScript
import { db } from "~~/server/lib/db";
|
|
import * as z from 'zod';
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
await protectRoute(event);
|
|
const userId = event.context.user!.id as string;
|
|
|
|
const result = await getValidatedQuery(event, z.object({
|
|
page: z.number().optional(),
|
|
limit: z.number().optional(),
|
|
}).safeParse)
|
|
|
|
if (!result.success) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: result.error.issues[0]!.message,
|
|
});
|
|
}
|
|
|
|
const { page, limit } = result.data;
|
|
|
|
const topics = await db.query.topics.findMany({
|
|
where: {
|
|
userId,
|
|
},
|
|
orderBy: {
|
|
createdAt: 'desc',
|
|
},
|
|
limit: page && limit ? limit : undefined,
|
|
offset: page && limit ? (page - 1) * limit : undefined,
|
|
});
|
|
|
|
return {
|
|
topics,
|
|
};
|
|
})
|