From 58b7fecd25131d6d2280fa97c6bef795b0b7c95b Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Thu, 2 Feb 2023 12:21:25 +0100 Subject: [PATCH] Separated get/:id from get/ --- src/__tests__/app.test.ts | 12 ++++++++---- src/routers/category.ts | 30 +++++++++++++++++++++++------- src/routers/snippet.ts | 30 +++++++++++++++++++++++------- 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/src/__tests__/app.test.ts b/src/__tests__/app.test.ts index 00fe552..c685233 100644 --- a/src/__tests__/app.test.ts +++ b/src/__tests__/app.test.ts @@ -127,7 +127,7 @@ describe('POST /v1/category', () => { }) }) -describe('GET /v1/category/:id?', () => { +describe('GET /v1/category/', () => { it('returns status code 200 and the categories', async () => { const res = await request(app) .get('/v1/category') @@ -139,7 +139,9 @@ describe('GET /v1/category/:id?', () => { expect(res.status).toEqual(200) expect(res.body.categories[0].name).toEqual('VueJS Composition API') }) +}) +describe('GET /v1/category/:id', () => { it('returns status code 200 and the categories', async () => { const res = await request(app) .get(`/v1/category/${category_id}`) @@ -147,7 +149,7 @@ describe('GET /v1/category/:id?', () => { .set('Cookie', jwtCookie as string) expect(res.status).toEqual(200) - expect(res.body.categories[0].name).toEqual('VueJS Composition API') + expect(res.body.category.name).toEqual('VueJS Composition API') }) }) @@ -215,7 +217,7 @@ describe('POST /v1/snippet', () => { }) }) -describe('GET /v1/snippet/:id?', () => { +describe('GET /v1/snippet/', () => { it('returns status code 200 and all snippets', async () => { const res = await request(app) .get('/v1/snippet') @@ -227,7 +229,9 @@ describe('GET /v1/snippet/:id?', () => { expect(res.status).toEqual(200) expect(res.body.snippets[0].title).toEqual('Vue3 CompAPI TS script-template-style') }) +}) +describe('GET /v1/snippet/:id', () => { it('returns status code 200 and one snippet', async () => { const res = await request(app) .get(`/v1/snippet/${snippet_id}`) @@ -235,7 +239,7 @@ describe('GET /v1/snippet/:id?', () => { .set('Cookie', jwtCookie as string) expect(res.status).toEqual(200) - expect(res.body.snippets[0].title).toEqual('Vue3 CompAPI TS script-template-style') + expect(res.body.snippet.title).toEqual('Vue3 CompAPI TS script-template-style') }) }) diff --git a/src/routers/category.ts b/src/routers/category.ts index 9ab34bf..9d7e2c1 100644 --- a/src/routers/category.ts +++ b/src/routers/category.ts @@ -21,14 +21,11 @@ const paramsIdParser = z.object({ // TODO Ajouter pagination et queries de recherche const categoryGet: RequestHandler = async (req, res) => { let categories: Category[] | null = null - const whereClause: any = { user_id: req.body.userId } - - if (req.params.id != undefined) { - whereClause.id = paramsIdParser.parse(req.params).id - } try { - categories = await prisma.category.findMany({ where: whereClause }) + categories = await prisma.category.findMany({ + where: { user_id: req.body.userId } + }) } catch (error: any) { res.status(400).json({ message: (error.issues ?? error) }) return; @@ -37,6 +34,24 @@ const categoryGet: RequestHandler = async (req, res) => { res.json({ categories }) } +const categoryGetUnique: RequestHandler = async (req, res) => { + let category: Category | null = null + + try { + category = await prisma.category.findFirst({ + where: { + id: paramsIdParser.parse(req.params).id, + user_id: req.body.userId + } + }) + } catch (error: any) { + res.status(400).json({ message: (error.issues ?? error) }) + return; + } + + res.json({ category }) +} + const categoryPost: RequestHandler = async (req, res) => { try { const newCategory = categoryPostParser.parse(req.body) @@ -94,7 +109,8 @@ const categoryDelete: RequestHandler = async (req, res) => { res.json({ message: `${deleted.count} category / categories successfully deleted.` }) } -categoryRouter.get('/:id?', userIdMiddleware, categoryGet) +categoryRouter.get('/', userIdMiddleware, categoryGet) +categoryRouter.get('/:id', userIdMiddleware, categoryGetUnique) categoryRouter.post('/', userIdMiddleware, categoryPost) categoryRouter.put('/:id', userIdMiddleware, categoryUpdate) categoryRouter.delete('/:id', userIdMiddleware, categoryDelete) diff --git a/src/routers/snippet.ts b/src/routers/snippet.ts index 5ca1ae8..b4f4549 100644 --- a/src/routers/snippet.ts +++ b/src/routers/snippet.ts @@ -26,14 +26,11 @@ const paramsIdParser = z.object({ // TODO Ajouter pagination const snippetGet: RequestHandler = async (req, res) => { let snippets: Snippet[] | null = null - const whereClause: any = { user_id: req.body.userId } - - if (req.params.id != undefined) { - whereClause.id = paramsIdParser.parse(req.params).id - } try { - snippets = await prisma.snippet.findMany({ where: whereClause }) + snippets = await prisma.snippet.findMany({ + where: { user_id: req.body.userId } + }) } catch (error: any) { res.status(400).json({ message: (error.issues ?? error) }) return; @@ -42,6 +39,24 @@ const snippetGet: RequestHandler = async (req, res) => { res.json({ snippets }) } +const snippetGetUnique: RequestHandler = async (req, res) => { + let snippet: Snippet | null = null + + try { + snippet = await prisma.snippet.findFirst({ + where: { + id: paramsIdParser.parse(req.params).id, + user_id: req.body.userId + } + }) + } catch (error: any) { + res.status(400).json({ message: (error.issues ?? error) }) + return; + } + + res.json({ snippet }) +} + const snippetPost: RequestHandler = async (req, res) => { try { const newSnippet = snippetPostParser.parse(req.body) @@ -106,7 +121,8 @@ const snippetDelete: RequestHandler = async (req, res) => { res.json({ message: `${deleted.count} snippet(s) successfully deleted.` }) } -snippetRouter.get('/:id?', userIdMiddleware, snippetGet) +snippetRouter.get('/', userIdMiddleware, snippetGet) +snippetRouter.get('/:id', userIdMiddleware, snippetGetUnique) snippetRouter.post('/', userIdMiddleware, snippetPost) snippetRouter.put('/:id', userIdMiddleware, snippetUpdate) snippetRouter.delete('/:id', userIdMiddleware, snippetDelete)