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:
+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 { z } from "zod"
|
||||
import { parseJwtUserId } from "./user.js"
|
||||
import { userIdMiddleware } from "./user.js"
|
||||
|
||||
const categoryRouter = express.Router()
|
||||
const prisma = new PrismaClient()
|
||||
@@ -11,16 +11,10 @@ const categoryData = z.object({
|
||||
}).required()
|
||||
|
||||
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
|
||||
|
||||
try {
|
||||
categories = await prisma.category.findMany({ where: { user_id: userId } })
|
||||
categories = await prisma.category.findMany({ where: { user_id: req.body.userId } })
|
||||
} catch (error: any) {
|
||||
res.status(400).json({ message: (error.issues ?? error) })
|
||||
return;
|
||||
@@ -30,18 +24,12 @@ const categoryGet: 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 {
|
||||
const newCategory = categoryData.parse(req.body)
|
||||
await prisma.category.create({
|
||||
data: {
|
||||
name: newCategory.name,
|
||||
user_id: userId
|
||||
user_id: req.body.userId
|
||||
}
|
||||
})
|
||||
} catch (error: any) {
|
||||
@@ -52,7 +40,27 @@ const categoryPost: RequestHandler = async (req, res) => {
|
||||
res.json({ message: 'Category successfully added.' })
|
||||
}
|
||||
|
||||
categoryRouter.get('/', categoryGet)
|
||||
categoryRouter.post('/', categoryPost)
|
||||
const categoryDelete: RequestHandler = async (req, res) => {
|
||||
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
|
||||
|
||||
@@ -21,6 +21,16 @@ export function parseJwtUserId(jwtoken: string): number | undefined {
|
||||
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 userId = parseJwtUserId(req.cookies.jwt)
|
||||
if (userId === undefined) {
|
||||
|
||||
Reference in New Issue
Block a user