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

38 lines
866 B
TypeScript
Raw Normal View History

2021-12-19 20:03:59 -03:00
import { prisma } from 'services/prisma'
import { ExceptionError } from 'utils/error'
import { nc } from 'utils/nc'
2021-12-19 22:40:57 -03:00
import { authMiddleware } from 'utils/authMiddleware'
2021-12-13 15:34:37 -03:00
2021-12-19 20:03:59 -03:00
const handler = nc
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) {
2021-12-15 14:05:27 -03:00
throw new ExceptionError('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
})
2021-12-19 22:40:57 -03:00
.use(authMiddleware)
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) {
2021-12-15 14:05:27 -03:00
throw new ExceptionError('No data provided')
2021-12-15 08:11:58 -03:00
}
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) {
2021-12-15 14:05:27 -03:00
throw new ExceptionError(err)
2021-12-13 15:34:37 -03:00
}
})
export default handler