Skip to content

Instantly share code, notes, and snippets.

@ernestofreyreg
Created August 20, 2021 11:55
Show Gist options
  • Save ernestofreyreg/ee0138ea7251249fe0b96cb91f8fd863 to your computer and use it in GitHub Desktop.
Save ernestofreyreg/ee0138ea7251249fe0b96cb91f8fd863 to your computer and use it in GitHub Desktop.
import { isValidEmail } from 'lib/email'
import { withMongo } from 'lib/mongodb'
import { Db } from 'mongodb'
import { nanoid } from 'nanoid'
import { NextApiRequest, NextApiResponse } from 'next'
import nc from 'next-connect'
interface User {
id: string
email: string
created: string
updated: string
}
export default nc<NextApiRequest, NextApiResponse>()
.get(async (req, res) => {
const registrations = await withMongo(async (db: Db) => {
const collection = db.collection<User>('registrations')
return await collection.find().toArray()
})
return res.json(registrations)
})
.post(async (req, res) => {
if (!isValidEmail(req.body.email)) {
return res.status(400).json({ error: 'invalid-email' })
}
await withMongo(async (db: Db) => {
const collection = db.collection<User>('registrations')
await collection.insertOne({
id: nanoid(),
email: req.body.email,
created: new Date().toISOString(),
updated: new Date().toISOString()
})
})
return res.status(204).end()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment