mirror of
https://github.com/Floriansylvain/CineAllo.git
synced 2026-08-19 19:53:16 +02:00
🎨 - Improved struct / errors managment
This commit is contained in:
+17
-14
@@ -1,39 +1,42 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { PrismaClient } from '@prisma/client'
|
import { Prisma, PrismaClient } from '@prisma/client'
|
||||||
|
import createHttpError from 'http-errors'
|
||||||
|
|
||||||
const prisma = new PrismaClient()
|
const prisma = new PrismaClient()
|
||||||
export const showsRouter = express.Router()
|
export const showsRouter = express.Router()
|
||||||
|
|
||||||
showsRouter.get('/', async function(req, res, next) {
|
showsRouter.get('/', async function(req, res, next) {
|
||||||
const articles = await prisma.serie.findMany()
|
const shows = await prisma.serie.findMany()
|
||||||
|
|
||||||
res.send({articles})
|
res.send({shows})
|
||||||
})
|
})
|
||||||
|
|
||||||
showsRouter.get('/:id', async function(req, res, next) {
|
showsRouter.get('/:id', function(req, res, next) {
|
||||||
const articles = await prisma.serie.findMany({
|
prisma.serie.findUniqueOrThrow({
|
||||||
where: {
|
where: {
|
||||||
title: {
|
title: {
|
||||||
equals: req.params.id
|
equals: req.params.id
|
||||||
}
|
}
|
||||||
|
}})
|
||||||
|
.then(show => res.send(show))
|
||||||
|
.catch(error => {
|
||||||
|
if (error instanceof Prisma.NotFoundError) {
|
||||||
|
next(createHttpError(404, 'Show unknown.'))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
res.send({articles})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
showsRouter.post('/', async function (req, res, next) {
|
showsRouter.post('/', async function (req, res, next) {
|
||||||
try {
|
prisma.serie.create({
|
||||||
res.send(await prisma.serie.create({
|
|
||||||
data: {
|
data: {
|
||||||
title: req.body.title,
|
title: req.body.title,
|
||||||
date: req.body.date,
|
date: req.body.date,
|
||||||
description: req.body.description,
|
description: req.body.description,
|
||||||
thumbmailURL: req.body.thumbmailURL
|
thumbmailURL: req.body.thumbmailURL
|
||||||
}
|
}})
|
||||||
}))
|
.then(() => res.send({message: 'Show posted!'}))
|
||||||
} catch (error) {
|
.catch(() => {
|
||||||
res.send({error, message: "You might want to check your Show data format / content."})
|
next(createHttpError(400, 'Check your show model/content.'))
|
||||||
}
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user