diff --git a/src/__tests__/app.test.ts b/src/__tests__/app.test.ts index 348c5ae..27b34cf 100644 --- a/src/__tests__/app.test.ts +++ b/src/__tests__/app.test.ts @@ -13,6 +13,7 @@ const testUserCredentials = { } let category_id: number +let snippet_id: number // Mettre a jour les routes Category en stockant les IDs sur GET pour reutiliser sur DELETE @@ -129,7 +130,7 @@ describe('POST /v1/category', () => { }) describe('GET /v1/category', () => { - it('returns status code 200 and the category created', async () => { + it('returns status code 200 and the categories', async () => { const res = await request(app) .get('/v1/category') .set('Content-Type', 'application/json') @@ -200,6 +201,59 @@ describe('DELETE /v1/category', () => { }) }) +describe('POST /v1/snippet', () => { + it('returns status code 200 and the snippets', async () => { + const res = await request(app) + .post('/v1/snippet') + .set('Content-Type', 'application/json') + .set('Cookie', jwtCookie as string) + .send(JSON.stringify({ + title: 'Vue3 CompAPI TS script-template-style', + code: ` + + + + `, + language: 'vue' + })) + + expect(res.status).toEqual(200) + expect(res.body).toHaveProperty('message') + }) +}) + +describe('GET /v1/snippet', () => { + it('returns status code 200 and success message', async () => { + const res = await request(app) + .get('/v1/snippet') + .set('Content-Type', 'application/json') + .set('Cookie', jwtCookie as string) + + snippet_id = res.body.snippets[0].id + + expect(res.status).toEqual(200) + expect(res.body.snippets[0].title).toEqual('Vue3 CompAPI TS script-template-style') + }) +}) + +describe('DELETE /v1/snippet', () => { + it('returns status code 200 and success message', async () => { + const res = await request(app) + .delete('/v1/snippet') + .set('Content-Type', 'application/json') + .set('Cookie', jwtCookie as string) + .send(JSON.stringify({ + id: snippet_id + })) + + expect(res.status).toEqual(200) + expect(res.body).toHaveProperty('message') + }) +}) + describe('PUT /v1/user', () => { it('returns status code 200 and success message', async () => { const res = await request(app) diff --git a/src/app.ts b/src/app.ts index 82ee662..c9db9ba 100644 --- a/src/app.ts +++ b/src/app.ts @@ -7,6 +7,7 @@ import cookieParser from 'cookie-parser' import { getJwtSecret } from './utils/jwt.js' import sessionRouter from './routers/session.js' import categoryRouter from './routers/category.js' +import snippetRouter from './routers/snippet' const authMiddleware: RequestHandler = (req, res, next) => { const token = req.cookies.jwt @@ -59,6 +60,7 @@ export function initServer(): express.Express { appRouter.use('/session/', sessionRouter) appRouter.use('/user/', authMiddleware, userRouter) appRouter.use('/category/', authMiddleware, categoryRouter) + appRouter.use('/snippet/', authMiddleware, snippetRouter) app.use('/v1/', appRouter) diff --git a/src/routers/snippet.ts b/src/routers/snippet.ts index f1a4615..810f85a 100644 --- a/src/routers/snippet.ts +++ b/src/routers/snippet.ts @@ -6,11 +6,17 @@ import { userIdMiddleware } from "./user.js" const snippetRouter = express.Router() const prisma = new PrismaClient() -const snippetData = z.object({ +const snippetPostParser = z.object({ code: z.string(), - title: z.string().max(50) + title: z.string().max(50), + language: z.string().max(32) }).required() +const snippetDeleteParser = z.object({ + id: z.number(), +}).required() + + // TODO Ajouter paramètres sur la requête pour rechercher des snippets // TODO Ajouter pagination const snippetGet: RequestHandler = async (req, res) => { @@ -23,50 +29,58 @@ const snippetGet: RequestHandler = async (req, res) => { return; } - res.json({ categories: snippets }) + res.json({ 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; -// } +const snippetPost: RequestHandler = async (req, res) => { + try { + const newSnippet = snippetPostParser.parse(req.body) + await prisma.snippet.create({ + data: { + title: newSnippet.title, + code: newSnippet.code, + user: { + connect: { id: req.body.userId } + }, + language: { + connectOrCreate: { + where: { name: newSnippet.language }, + create: { name: newSnippet.language } + } + } + } + }) + } catch (error: any) { + res.status(400).json({ message: (error.issues ?? error) }) + return; + } -// res.json({ message: 'Snippet successfully added.' }) -// } + res.json({ message: 'Snippet successfully added.' }) +} -// const snippetDelete: RequestHandler = async (req, res) => { -// let deleted: Prisma.BatchPayload +const snippetDelete: RequestHandler = async (req, res) => { + let deleted: Prisma.BatchPayload -// try { -// const snippetToDel = snippetData.parse(req.body) -// deleted = await prisma.snippet.deleteMany({ -// where: { + try { + const snippetToDel = snippetDeleteParser.parse(req.body) + deleted = await prisma.category.deleteMany({ + where: { + id: snippetToDel.id, + user_id: req.body.userId + } + }) + } catch (error: any) { + res.status(400).json({ message: (error.issues ?? error) }) + return; + } -// 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.` }) -// } + res.json({ message: `${deleted.count} snippet(s) successfully deleted.` }) +} // TODO update snippet snippetRouter.get('/', userIdMiddleware, snippetGet) -// snippetRouter.post('/', userIdMiddleware, snippetPost) -// snippetRouter.delete('/', userIdMiddleware, snippetDelete) +snippetRouter.post('/', userIdMiddleware, snippetPost) +snippetRouter.delete('/', userIdMiddleware, snippetDelete) export default snippetRouter