WIP category routes standardization + snippets imp

This commit is contained in:
Florian Sylvain
2023-02-01 18:20:14 +01:00
parent 472be47056
commit c0ec0ac899
3 changed files with 82 additions and 4 deletions
+2
View File
@@ -12,6 +12,8 @@ const testUserCredentials = {
password: "aaaaaaaaaa" password: "aaaaaaaaaa"
} }
// Mettre a jour les routes Category en stockant les IDs sur GET pour reutiliser sur DELETE
beforeAll(async () => { beforeAll(async () => {
await prisma.user.create({ await prisma.user.create({
data: { data: {
+8 -4
View File
@@ -6,10 +6,14 @@ import { userIdMiddleware } from "./user.js"
const categoryRouter = express.Router() const categoryRouter = express.Router()
const prisma = new PrismaClient() const prisma = new PrismaClient()
const categoryData = z.object({ const categoryPostParser = z.object({
name: z.string().max(50) name: z.string().max(50)
}).required() }).required()
const categoryDeleteParser = z.object({
id: z.number()
}).required()
const categoryGet: RequestHandler = async (req, res) => { const categoryGet: RequestHandler = async (req, res) => {
let categories: Category[] | null = null let categories: Category[] | null = null
@@ -25,7 +29,7 @@ const categoryGet: RequestHandler = async (req, res) => {
const categoryPost: RequestHandler = async (req, res) => { const categoryPost: RequestHandler = async (req, res) => {
try { try {
const newCategory = categoryData.parse(req.body) const newCategory = categoryPostParser.parse(req.body)
await prisma.category.create({ await prisma.category.create({
data: { data: {
name: newCategory.name, name: newCategory.name,
@@ -44,10 +48,10 @@ const categoryDelete: RequestHandler = async (req, res) => {
let deleted: Prisma.BatchPayload let deleted: Prisma.BatchPayload
try { try {
const categoryToDel = categoryData.parse(req.body) const categoryToDel = categoryDeleteParser.parse(req.body)
deleted = await prisma.category.deleteMany({ deleted = await prisma.category.deleteMany({
where: { where: {
name: categoryToDel.name, id: categoryToDel.id,
user_id: req.body.userId user_id: req.body.userId
} }
}) })
+72
View File
@@ -0,0 +1,72 @@
import { Prisma, PrismaClient, Snippet } from "@prisma/client"
import express, { RequestHandler } from "express"
import { z } from "zod"
import { userIdMiddleware } from "./user.js"
const snippetRouter = express.Router()
const prisma = new PrismaClient()
const snippetData = z.object({
code: z.string(),
title: z.string().max(50)
}).required()
// TODO Ajouter paramètres sur la requête pour rechercher des snippets
// TODO Ajouter pagination
const snippetGet: RequestHandler = async (req, res) => {
let snippets: Snippet[] | null = null
try {
snippets = await prisma.snippet.findMany({ where: { user_id: req.body.userId } })
} catch (error: any) {
res.status(400).json({ message: (error.issues ?? error) })
return;
}
res.json({ categories: snippets })
}
// const snippetPost: RequestHandler = async (req, res) => {
// try {
// const newSnippet = snippetData.parse(req.body)
// await prisma.snippet.create({
// data: {
// code: newSnippet.code,
// title: newSnippet.code,
// user_id: req.body.userId
// }
// })
// } catch (error: any) {
// res.status(400).json({ message: (error.issues ?? error) })
// return;
// }
// res.json({ message: 'Snippet successfully added.' })
// }
// const snippetDelete: RequestHandler = async (req, res) => {
// let deleted: Prisma.BatchPayload
// try {
// const snippetToDel = snippetData.parse(req.body)
// deleted = await prisma.snippet.deleteMany({
// where: {
// 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.` })
// }
// TODO update snippet
snippetRouter.get('/', userIdMiddleware, snippetGet)
// snippetRouter.post('/', userIdMiddleware, snippetPost)
// snippetRouter.delete('/', userIdMiddleware, snippetDelete)
export default snippetRouter