Untitled

mail@pastecode.io avatar
unknown
typescript
2 years ago
1.0 kB
3
Indexable
import { PrismaClient, Prisma } from '@prisma/client'
import express, { Router } from 'express'

const { user} = new PrismaClient()
const app = express()
const router = express.Router()

router.get('/', async (req, res) => {
    const users = await user.findMany({
        select: {
            name: true,
            posts: true
        }
    });
    res.json(users);
});

router.post('/', async (req, res) => {
    const { name, email, title } = req.body;

    const userExists = await user.findUnique({
        where: {
            email: email
        },
        select: {
            email: true
        },
    });
    
    if (userExists) {
        return res.status(400).json({
            msg: "User already exists"
        });
    };

    const newUser = await user.create({
        data: {
            name: name,
            email: email,
            // posts: {
            //     create: { title: "test title", },
            //   },
          },         
    });
    res.json(newUser);
})

export default router;