diff --git a/src/__tests__/app.test.ts b/src/__tests__/app.test.ts index 31ab48f..e0b47c2 100644 --- a/src/__tests__/app.test.ts +++ b/src/__tests__/app.test.ts @@ -12,6 +12,8 @@ const testUserCredentials = { password: "aaaaaaaaaa" } +// Mettre a jour les routes Category en stockant les IDs sur GET pour reutiliser sur DELETE + beforeAll(async () => { await prisma.user.create({ data: { diff --git a/src/routers/category.ts b/src/routers/category.ts index 09fec23..ef60f4e 100644 --- a/src/routers/category.ts +++ b/src/routers/category.ts @@ -6,10 +6,14 @@ import { userIdMiddleware } from "./user.js" const categoryRouter = express.Router() const prisma = new PrismaClient() -const categoryData = z.object({ +const categoryPostParser = z.object({ name: z.string().max(50) }).required() +const categoryDeleteParser = z.object({ + id: z.number() +}).required() + const categoryGet: RequestHandler = async (req, res) => { let categories: Category[] | null = null @@ -25,7 +29,7 @@ const categoryGet: RequestHandler = async (req, res) => { const categoryPost: RequestHandler = async (req, res) => { try { - const newCategory = categoryData.parse(req.body) + const newCategory = categoryPostParser.parse(req.body) await prisma.category.create({ data: { name: newCategory.name, @@ -44,10 +48,10 @@ const categoryDelete: RequestHandler = async (req, res) => { let deleted: Prisma.BatchPayload try { - const categoryToDel = categoryData.parse(req.body) + const categoryToDel = categoryDeleteParser.parse(req.body) deleted = await prisma.category.deleteMany({ where: { - name: categoryToDel.name, + id: categoryToDel.id, user_id: req.body.userId } }) diff --git a/src/routers/snippet.ts b/src/routers/snippet.ts new file mode 100644 index 0000000..f1a4615 --- /dev/null +++ b/src/routers/snippet.ts @@ -0,0 +1,72 @@ +import { Prisma, PrismaClient, Snippet } from "@prisma/client" +import express, { RequestHandler } from "express" +import { z } from "zod" +import { userIdMiddleware } from "./user.js" + +const snippetRouter = express.Router() +const prisma = new PrismaClient() + +const snippetData = z.object({ + code: z.string(), + title: z.string().max(50) +}).required() + +// TODO Ajouter paramètres sur la requête pour rechercher des snippets +// TODO Ajouter pagination +const snippetGet: RequestHandler = async (req, res) => { + let snippets: Snippet[] | null = null + + try { + snippets = await prisma.snippet.findMany({ where: { user_id: req.body.userId } }) + } catch (error: any) { + res.status(400).json({ message: (error.issues ?? error) }) + return; + } + + res.json({ categories: snippets }) +} + +// const snippetPost: RequestHandler = async (req, res) => { +// try { +// const newSnippet = snippetData.parse(req.body) +// await prisma.snippet.create({ +// data: { +// code: newSnippet.code, +// title: newSnippet.code, +// user_id: req.body.userId +// } +// }) +// } catch (error: any) { +// res.status(400).json({ message: (error.issues ?? error) }) +// return; +// } + +// res.json({ message: 'Snippet successfully added.' }) +// } + +// const snippetDelete: RequestHandler = async (req, res) => { +// let deleted: Prisma.BatchPayload + +// try { +// const snippetToDel = snippetData.parse(req.body) +// deleted = await prisma.snippet.deleteMany({ +// where: { + +// user_id: req.body.userId +// } +// }) +// } catch (error: any) { +// res.status(400).json({ message: (error.issues ?? error) }) +// return; +// } + +// res.json({ message: `${deleted.count} category / categories successfully deleted.` }) +// } + +// TODO update snippet + +snippetRouter.get('/', userIdMiddleware, snippetGet) +// snippetRouter.post('/', userIdMiddleware, snippetPost) +// snippetRouter.delete('/', userIdMiddleware, snippetDelete) + +export default snippetRouter