Files
link-free/src/pages/api/data.ts

47 lines
1.0 KiB
TypeScript
Raw Normal View History

2021-12-15 08:11:58 -03:00
import type { NextApiRequest, NextApiResponse } from 'next'
2021-12-13 15:34:37 -03:00
import nc from 'next-connect'
import { prisma } from '../../services/prisma'
2021-12-15 08:11:58 -03:00
const handler = nc<NextApiRequest, NextApiResponse>({
onNoMatch: (_req, res) => {
res.status(404).json({ error: 'Not found' })
},
onError: (err, _req, res) => {
res.status(500).json({ error: err.message })
}
})
2021-12-13 15:34:37 -03:00
.get(async (_req, res) => {
2021-12-15 08:11:58 -03:00
const response = await prisma.data.findUnique({
where: { id: 1 }
})
if (!response) {
throw new Error('No data found')
2021-12-13 15:34:37 -03:00
}
2021-12-15 08:11:58 -03:00
res.status(200).json(response?.data)
2021-12-13 15:34:37 -03:00
})
.put(async (req, res) => {
2021-12-15 08:11:58 -03:00
const { data } = req.body
if (!data) {
throw new Error('No data provided')
}
2021-12-13 15:34:37 -03:00
try {
const response = await prisma.data.update({
2021-12-15 08:11:58 -03:00
where: { id: 1 },
2021-12-13 15:34:37 -03:00
data: { data: JSON.stringify(req.body.data) }
})
res.status(200).json(response)
2021-12-15 08:11:58 -03:00
} catch (err: any) {
console.error(err)
res.status(500).json({ error: err.message })
2021-12-13 15:34:37 -03:00
}
})
export default handler