🎨 - Improved prisma funcs usage

This commit is contained in:
Florian Sylvain
2022-11-10 19:01:48 +01:00
parent c9a75fa700
commit 2304495136
+16 -16
View File
@@ -5,38 +5,38 @@ 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 shows = await prisma.serie.findMany() const shows = await prisma.serie.findMany()
res.send({shows}) res.send({ shows })
}) })
showsRouter.get('/:id', function(req, res, next) { showsRouter.get('/:id', function (req, res, next) {
prisma.serie.findUniqueOrThrow({ prisma.serie.findUniqueOrThrow({
where: { where: {
title: { title: req.params.id
equals: req.params.id
}
}})
.then(show => res.send(show))
.catch(error => {
if (error instanceof Prisma.NotFoundError) {
next(createHttpError(404, 'Show unknown.'))
} }
}) })
.then(show => res.send({ show }))
.catch(error => {
if (error instanceof Prisma.NotFoundError) {
next(createHttpError(404, 'Show not found.'))
}
})
}) })
showsRouter.post('/', async function (req, res, next) { showsRouter.post('/', function (req, res, next) {
prisma.serie.create({ 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(() => {
next(createHttpError(400, 'Check your show model/content.'))
}) })
.then(() => res.send({ message: 'Show posted!' }))
.catch(() => {
next(createHttpError(400, 'Check your show model/content.'))
})
}) })