mirror of
https://github.com/Floriansylvain/SnippetsManager.git
synced 2026-08-19 11:43:15 +02:00
Added IDs in Category routes params
This commit is contained in:
+16
-24
@@ -127,7 +127,7 @@ describe('POST /v1/category', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /v1/category', () => {
|
||||
describe('GET /v1/category/:id?', () => {
|
||||
it('returns status code 200 and the categories', async () => {
|
||||
const res = await request(app)
|
||||
.get('/v1/category')
|
||||
@@ -139,16 +139,25 @@ describe('GET /v1/category', () => {
|
||||
expect(res.status).toEqual(200)
|
||||
expect(res.body.categories[0].name).toEqual('VueJS Composition API')
|
||||
})
|
||||
|
||||
it('returns status code 200 and the categories', async () => {
|
||||
const res = await request(app)
|
||||
.get(`/v1/category/${category_id}`)
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Cookie', jwtCookie as string)
|
||||
|
||||
expect(res.status).toEqual(200)
|
||||
expect(res.body.categories[0].name).toEqual('VueJS Composition API')
|
||||
})
|
||||
})
|
||||
|
||||
describe('PUT /v1/category', () => {
|
||||
describe('PUT /v1/category/:id', () => {
|
||||
it('returns status code 200 and a success message', async () => {
|
||||
const res = await request(app)
|
||||
.put('/v1/category')
|
||||
.put(`/v1/category/${category_id}`)
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Cookie', jwtCookie as string)
|
||||
.send(JSON.stringify({
|
||||
id: category_id,
|
||||
name: 'VueJS v3 Composition API'
|
||||
}))
|
||||
|
||||
@@ -158,11 +167,10 @@ describe('PUT /v1/category', () => {
|
||||
|
||||
it('returns status code 400 and an error message', async () => {
|
||||
const res = await request(app)
|
||||
.put('/v1/category')
|
||||
.put(`/v1/category/${23591}`)
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Cookie', jwtCookie as string)
|
||||
.send(JSON.stringify({
|
||||
id: 2,
|
||||
pouet: 'salut'
|
||||
}))
|
||||
|
||||
@@ -171,32 +179,16 @@ describe('PUT /v1/category', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('DELETE /v1/category', () => {
|
||||
describe('DELETE /v1/category/:id', () => {
|
||||
it('returns status code 200 and a success message', async () => {
|
||||
const res = await request(app)
|
||||
.delete('/v1/category')
|
||||
.delete(`/v1/category/${category_id}`)
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Cookie', jwtCookie as string)
|
||||
.send(JSON.stringify({
|
||||
id: category_id
|
||||
}))
|
||||
|
||||
expect(res.status).toEqual(200)
|
||||
expect(res.body).toHaveProperty('message')
|
||||
})
|
||||
|
||||
it('returns status code 400 and an error message', async () => {
|
||||
const res = await request(app)
|
||||
.delete('/v1/category')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Cookie', jwtCookie as string)
|
||||
.send(JSON.stringify({
|
||||
blablabla: 'pouet'
|
||||
}))
|
||||
|
||||
expect(res.status).toEqual(400)
|
||||
expect(res.body).toHaveProperty('message')
|
||||
})
|
||||
})
|
||||
|
||||
describe('POST /v1/snippet', () => {
|
||||
|
||||
+15
-12
@@ -10,21 +10,25 @@ const categoryPostParser = z.object({
|
||||
name: z.string().max(50)
|
||||
}).required()
|
||||
|
||||
const categoryDeleteParser = z.object({
|
||||
id: z.number()
|
||||
const categoryUpdateParser = z.object({
|
||||
name: z.string().max(50)
|
||||
}).required()
|
||||
|
||||
const categoryUpdateParser = z.object({
|
||||
id: z.number(),
|
||||
name: z.string().max(50)
|
||||
const paramsIdParser = z.object({
|
||||
id: z.coerce.number(),
|
||||
}).required()
|
||||
|
||||
// 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: { user_id: req.body.userId } })
|
||||
categories = await prisma.category.findMany({ where: whereClause })
|
||||
} catch (error: any) {
|
||||
res.status(400).json({ message: (error.issues ?? error) })
|
||||
return;
|
||||
@@ -57,7 +61,7 @@ const categoryUpdate: RequestHandler = async (req, res) => {
|
||||
const categoryToUpdate = categoryUpdateParser.parse(req.body)
|
||||
updated = await prisma.category.updateMany({
|
||||
where: {
|
||||
id: categoryToUpdate.id,
|
||||
id: paramsIdParser.parse(req.params).id,
|
||||
user_id: req.body.userId
|
||||
},
|
||||
data: {
|
||||
@@ -76,10 +80,9 @@ const categoryDelete: RequestHandler = async (req, res) => {
|
||||
let deleted: Prisma.BatchPayload
|
||||
|
||||
try {
|
||||
const categoryToDel = categoryDeleteParser.parse(req.body)
|
||||
deleted = await prisma.category.deleteMany({
|
||||
where: {
|
||||
id: categoryToDel.id,
|
||||
id: paramsIdParser.parse(req.params).id,
|
||||
user_id: req.body.userId
|
||||
}
|
||||
})
|
||||
@@ -91,9 +94,9 @@ const categoryDelete: RequestHandler = async (req, res) => {
|
||||
res.json({ message: `${deleted.count} category / categories successfully deleted.` })
|
||||
}
|
||||
|
||||
categoryRouter.get('/', userIdMiddleware, categoryGet)
|
||||
categoryRouter.get('/:id?', userIdMiddleware, categoryGet)
|
||||
categoryRouter.post('/', userIdMiddleware, categoryPost)
|
||||
categoryRouter.put('/', userIdMiddleware, categoryUpdate)
|
||||
categoryRouter.delete('/', userIdMiddleware, categoryDelete)
|
||||
categoryRouter.put('/:id', userIdMiddleware, categoryUpdate)
|
||||
categoryRouter.delete('/:id', userIdMiddleware, categoryDelete)
|
||||
|
||||
export default categoryRouter
|
||||
|
||||
Reference in New Issue
Block a user