mirror of
https://github.com/Floriansylvain/SnippetsManager.git
synced 2026-08-19 11:43:15 +02:00
Added DELETE category route and userId middleware
This commit is contained in:
@@ -149,6 +149,34 @@ describe('GET /v1/category', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('DELETE /v1/category', () => {
|
||||||
|
it('returns status code 200 and a success message', async () => {
|
||||||
|
const res = await request(app)
|
||||||
|
.delete('/v1/category')
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.set('Cookie', jwtCookie as string)
|
||||||
|
.send(JSON.stringify({
|
||||||
|
name: 'VueJS Composition API'
|
||||||
|
}))
|
||||||
|
|
||||||
|
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('PUT /v1/user', () => {
|
describe('PUT /v1/user', () => {
|
||||||
it('returns status code 200 and success message', async () => {
|
it('returns status code 200 and success message', async () => {
|
||||||
const res = await request(app)
|
const res = await request(app)
|
||||||
|
|||||||
+26
-18
@@ -1,7 +1,7 @@
|
|||||||
import { Category, PrismaClient } from "@prisma/client"
|
import { Category, Prisma, PrismaClient } from "@prisma/client"
|
||||||
import express, { RequestHandler } from "express"
|
import express, { RequestHandler } from "express"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { parseJwtUserId } from "./user.js"
|
import { userIdMiddleware } from "./user.js"
|
||||||
|
|
||||||
const categoryRouter = express.Router()
|
const categoryRouter = express.Router()
|
||||||
const prisma = new PrismaClient()
|
const prisma = new PrismaClient()
|
||||||
@@ -11,16 +11,10 @@ const categoryData = z.object({
|
|||||||
}).required()
|
}).required()
|
||||||
|
|
||||||
const categoryGet: RequestHandler = async (req, res) => {
|
const categoryGet: RequestHandler = async (req, res) => {
|
||||||
const userId = parseJwtUserId(req.cookies.jwt)
|
|
||||||
if (userId === undefined) {
|
|
||||||
res.status(400).json({ message: 'Incorrect JWT payload.' })
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let categories: Category[] | null = null
|
let categories: Category[] | null = null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
categories = await prisma.category.findMany({ where: { user_id: userId } })
|
categories = await prisma.category.findMany({ where: { user_id: req.body.userId } })
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
res.status(400).json({ message: (error.issues ?? error) })
|
res.status(400).json({ message: (error.issues ?? error) })
|
||||||
return;
|
return;
|
||||||
@@ -30,18 +24,12 @@ const categoryGet: RequestHandler = async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const categoryPost: RequestHandler = async (req, res) => {
|
const categoryPost: RequestHandler = async (req, res) => {
|
||||||
const userId = parseJwtUserId(req.cookies.jwt)
|
|
||||||
if (userId === undefined) {
|
|
||||||
res.status(400).json({ message: 'Incorrect JWT payload.' })
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const newCategory = categoryData.parse(req.body)
|
const newCategory = categoryData.parse(req.body)
|
||||||
await prisma.category.create({
|
await prisma.category.create({
|
||||||
data: {
|
data: {
|
||||||
name: newCategory.name,
|
name: newCategory.name,
|
||||||
user_id: userId
|
user_id: req.body.userId
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
@@ -52,7 +40,27 @@ const categoryPost: RequestHandler = async (req, res) => {
|
|||||||
res.json({ message: 'Category successfully added.' })
|
res.json({ message: 'Category successfully added.' })
|
||||||
}
|
}
|
||||||
|
|
||||||
categoryRouter.get('/', categoryGet)
|
const categoryDelete: RequestHandler = async (req, res) => {
|
||||||
categoryRouter.post('/', categoryPost)
|
let deleted: Prisma.BatchPayload
|
||||||
|
|
||||||
|
try {
|
||||||
|
const categoryToDel = categoryData.parse(req.body)
|
||||||
|
deleted = await prisma.category.deleteMany({
|
||||||
|
where: {
|
||||||
|
name: categoryToDel.name,
|
||||||
|
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.` })
|
||||||
|
}
|
||||||
|
|
||||||
|
categoryRouter.get('/', userIdMiddleware, categoryGet)
|
||||||
|
categoryRouter.post('/', userIdMiddleware, categoryPost)
|
||||||
|
categoryRouter.delete('/', userIdMiddleware, categoryDelete)
|
||||||
|
|
||||||
export default categoryRouter
|
export default categoryRouter
|
||||||
|
|||||||
@@ -21,6 +21,16 @@ export function parseJwtUserId(jwtoken: string): number | undefined {
|
|||||||
return payload.userId
|
return payload.userId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const userIdMiddleware: RequestHandler = (req, res, next) => {
|
||||||
|
const userId = parseJwtUserId(req.cookies.jwt)
|
||||||
|
if (userId === undefined) {
|
||||||
|
res.status(400).json({ message: 'Incorrect JWT payload.' })
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
req.body.userId = userId
|
||||||
|
next()
|
||||||
|
}
|
||||||
|
|
||||||
const userRouterPut: RequestHandler = async (req, res) => {
|
const userRouterPut: RequestHandler = async (req, res) => {
|
||||||
const userId = parseJwtUserId(req.cookies.jwt)
|
const userId = parseJwtUserId(req.cookies.jwt)
|
||||||
if (userId === undefined) {
|
if (userId === undefined) {
|
||||||
|
|||||||
Reference in New Issue
Block a user