From 7187d5ea8c5963d856181696547cb6965bb716a6 Mon Sep 17 00:00:00 2001 From: Josh Hamilton Date: Wed, 4 Aug 2021 21:31:38 -0500 Subject: [PATCH] add posts --- pages/api/posts.js | 40 ++++++++++++++++++ pages/index.js | 41 ++++++++++++++++++- .../migration.sql | 14 +++++++ prisma/schema.prisma | 11 +++++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 pages/api/posts.js create mode 100644 prisma/migrations/20210726025242_create_posts_table/migration.sql diff --git a/pages/api/posts.js b/pages/api/posts.js new file mode 100644 index 0000000..62d590a --- /dev/null +++ b/pages/api/posts.js @@ -0,0 +1,40 @@ +import { getSession } from 'next-auth/client'; +import { PrismaClient } from '@prisma/client'; + +const prisma = new PrismaClient(); + +async function createPost(req, res) { + const session = await getSession({ req }); + + if (!session) { + return res.status(401).json({ unauthorized: true }); + } + + const user = await prisma.user.findUnique({ + where: { email: session.user.email }, + }); + + if (!req.body.title || !req.body.body) { + return res.status(500).json({ error: 'validation error' }); + } + + const post = await prisma.post.create({ + data: { + userId: user.id, + title: req.body.title, + body: req.body.body, + }, + }); + + if (post.id) { + res.status(200).json(post); + } else { + res.status(500).json({ error: 'something went wrong' }); + } +} + +export default async function handler(req, res) { + if (req.method === 'POST') { + return createPost(req, res); + } +} diff --git a/pages/index.js b/pages/index.js index 8c1e174..7fb6fa2 100644 --- a/pages/index.js +++ b/pages/index.js @@ -1,6 +1,10 @@ import { signIn, signOut, getSession } from 'next-auth/client'; +import { useState } from 'react'; export default function Page({ session }) { + const [title, setTitle] = useState(''); + const [body, setBody] = useState(''); + return ( <> {!session && ( @@ -12,7 +16,42 @@ export default function Page({ session }) { {session && ( <> Signed in as {session.user.email}
- + +
{ + e.preventDefault(); + + fetch('http://github.com/api/posts', { + method: 'POST', + credentials: 'include', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ title, body }), + }); + }}> +
+ +

+ setTitle(e.target.value)} + /> +
+
+ +

+ +
+ +
)} diff --git a/prisma/migrations/20210726025242_create_posts_table/migration.sql b/prisma/migrations/20210726025242_create_posts_table/migration.sql new file mode 100644 index 0000000..239ebb4 --- /dev/null +++ b/prisma/migrations/20210726025242_create_posts_table/migration.sql @@ -0,0 +1,14 @@ +-- CreateTable +CREATE TABLE "Post" ( + "id" TEXT NOT NULL, + "userId" TEXT NOT NULL, + "title" TEXT NOT NULL, + "body" TEXT NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + PRIMARY KEY ("id") +); + +-- AddForeignKey +ALTER TABLE "Post" ADD FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 35fd473..339ccd7 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -47,6 +47,7 @@ model User { updatedAt DateTime @updatedAt accounts Account[] sessions Session[] + Post Post[] } model VerificationRequest { @@ -59,3 +60,13 @@ model VerificationRequest { @@unique([identifier, token]) } + +model Post { + id String @id @default(cuid()) + userId String + title String + body String + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + user User @relation(fields: [userId], references: [id]) +}