mirror of
https://github.com/Floriansylvain/SnippetsManager.git
synced 2026-08-19 11:43:15 +02:00
Fixed category get route conception
This commit is contained in:
@@ -130,12 +130,9 @@ describe('GET /v1/category', () => {
|
|||||||
.get('/v1/category')
|
.get('/v1/category')
|
||||||
.set('Content-Type', 'application/json')
|
.set('Content-Type', 'application/json')
|
||||||
.set('Cookie', jwtCookie as string)
|
.set('Cookie', jwtCookie as string)
|
||||||
.send(JSON.stringify({
|
|
||||||
name: 'VueJS Composition API'
|
|
||||||
}))
|
|
||||||
|
|
||||||
expect(res.status).toEqual(200)
|
expect(res.status).toEqual(200)
|
||||||
expect(res.body).toHaveProperty('category')
|
expect(res.body).toHaveProperty('categories')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns status code 400 and error message', async () => {
|
it('returns status code 400 and error message', async () => {
|
||||||
|
|||||||
@@ -10,19 +10,23 @@ const categoryData = z.object({
|
|||||||
name: z.string().max(50)
|
name: z.string().max(50)
|
||||||
}).required()
|
}).required()
|
||||||
|
|
||||||
// TODO Mettre le where sur le user id plutot que sur un nom de category
|
|
||||||
const categoryGet: RequestHandler = async (req, res) => {
|
const categoryGet: RequestHandler = async (req, res) => {
|
||||||
let category: Category | null = null
|
const userId = parseJwtUserId(req.cookies.jwt)
|
||||||
|
if (userId === undefined) {
|
||||||
|
res.status(400).json({ message: 'Incorrect JWT payload.' })
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let categories: Category[] | null = null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const categoryGet = categoryData.parse(req.body)
|
categories = await prisma.category.findMany({ where: { user_id: userId } })
|
||||||
category = await prisma.category.findFirst({ where: { name: categoryGet.name } })
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
res.status(400).json({ message: (error.issues ?? error) })
|
res.status(400).json({ message: (error.issues ?? error) })
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json({ category })
|
res.json({ categories })
|
||||||
}
|
}
|
||||||
|
|
||||||
const categoryPost: RequestHandler = async (req, res) => {
|
const categoryPost: RequestHandler = async (req, res) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user