From c35799db3574c805c51a1d9d312db5994da79aa8 Mon Sep 17 00:00:00 2001 From: ishmael cascabel Date: Tue, 20 Sep 2022 21:32:15 +0800 Subject: [PATCH] chore : data handling --- .env | 2 +- .../confessions/confession-searchbar.tsx | 0 components/forms/providerButton.tsx | 2 +- components/headComponent.tsx | 3 - pages/confessions.tsx | 27 +++++-- pages/home.tsx | 68 ++++++++-------- prisma/handler.ts | 80 +++++++++++++++++++ prisma/prisma.ts | 15 ++++ prisma/schema.prisma | 4 +- server.ts | 18 +++++ 10 files changed, 172 insertions(+), 47 deletions(-) create mode 100644 components/confessions/confession-searchbar.tsx create mode 100644 prisma/handler.ts create mode 100644 prisma/prisma.ts create mode 100644 server.ts diff --git a/.env b/.env index 3d1525a..fc5c015 100644 --- a/.env +++ b/.env @@ -6,7 +6,7 @@ # See the documentation for all the connection string options: https://pris.ly/d/connection-strings -DATABASE_URL="mysql://johnaeron:johnaerondev@127.0.0.1:3306/incognitodb" +DATABASE_URL="mysql://root:admin@127.0.0.1:3306/incognito-confessions" NEXT_AUTH_URL=https://localhost:3000 SECRET="ede983d1b3598420b922f9b7cd3bb54e diff --git a/components/confessions/confession-searchbar.tsx b/components/confessions/confession-searchbar.tsx new file mode 100644 index 0000000..e69de29 diff --git a/components/forms/providerButton.tsx b/components/forms/providerButton.tsx index 02b9113..82c5375 100644 --- a/components/forms/providerButton.tsx +++ b/components/forms/providerButton.tsx @@ -10,7 +10,7 @@ interface ProviderButtonProps { export const ProviderButton = ({ provider, children }: ProviderButtonProps) => { const handleSignIn = () => { - signIn(provider, { callbackUrl: "/home", redirect: false }); + signIn(provider, { callbackUrl: "/home" }); }; // handleSignIn(); diff --git a/components/headComponent.tsx b/components/headComponent.tsx index 2f4867b..ef7d88d 100644 --- a/components/headComponent.tsx +++ b/components/headComponent.tsx @@ -1,7 +1,4 @@ -import Document from "next/document"; import Head from "next/head"; -import Link from "next/link"; -import Script from "next/script"; import React from "react"; interface HeadProps { diff --git a/pages/confessions.tsx b/pages/confessions.tsx index d83be70..a9ee2f4 100644 --- a/pages/confessions.tsx +++ b/pages/confessions.tsx @@ -1,9 +1,14 @@ -import { NextPage } from "next"; +import { GetServerSideProps, NextPage } from "next"; import { ConfessionsHeader } from "../components/confessions/confessions-header"; +import ConfessionCard from "../components/home/confession-card"; import { Layout } from "../components/layout"; import { ProtectedComponent } from "../components/protectedcomponent"; +import getHandler from "../prisma/handler"; + +const Confessions: NextPage<{confessions : any}> = ({ confessions }) => { + + const confessionData = JSON.parse(confessions); -const Confessions: NextPage = () => { return ( @@ -11,9 +16,9 @@ const Confessions: NextPage = () => {
-

- Still not done -

+ {confessionData.map((confession: any) => ( + + ))}
@@ -22,3 +27,15 @@ const Confessions: NextPage = () => { }; export default Confessions; + +export const getServerSideProps: GetServerSideProps = async (ctx) => { + + const { getConfessions } = getHandler(); + const confessions = await getConfessions(); + + return { + props: { + confessions: JSON.stringify(confessions), + }, + }; +} diff --git a/pages/home.tsx b/pages/home.tsx index c90f69a..447853d 100644 --- a/pages/home.tsx +++ b/pages/home.tsx @@ -14,8 +14,11 @@ import { PaperAirplaneIcon, } from "@heroicons/react/24/outline"; import ConfessionCard from "../components/home/confession-card"; +import getHandler from "../prisma/handler"; +import { create } from "domain"; -const Home: NextPage<{ session: Session }> = ({ session }) => { +const Home: NextPage<{ session: Session, user: any }> = ({ session, user }) => { + const userData = JSON.parse(user) return ( @@ -28,46 +31,34 @@ const Home: NextPage<{ session: Session }> = ({ session }) => {

Write confession

-

You've made 0 confessions

+

You've made {userData.confessions.length} confessions

- {/* --- No confession --- */} - {/*

- You haven't wrote a confession yet -

+ {userData.confessions.length > 0 ? ( +
+

+ Your confessions +

+
+ + {userData.confessions.map((confession: any) => ( + + ))} - */} +
+
+ ) : ( +
+

+ You haven't wrote a confession yet +

- {/* --- Has confessions --- */} -

- Your confessions -

-
- - - -
+ +
+ )}
@@ -86,6 +77,10 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => { authOptions ); + const { getUser, createConfession } = getHandler(); + const user = await getUser(session?.user?.email!); + + // await createConfession("hello", "lorem ipsumm", user?.id!); if (!session) { return { redirect: { @@ -98,6 +93,7 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => { return { props: { session, + user: JSON.stringify(user), }, }; }; diff --git a/prisma/handler.ts b/prisma/handler.ts new file mode 100644 index 0000000..2dfc9cd --- /dev/null +++ b/prisma/handler.ts @@ -0,0 +1,80 @@ +import { prisma } from "./prisma"; + +const getUser = async ( email : string ) => { + const user = await prisma.user.findUnique({ + where: { + email: email, + }, + include: { + confessions: true, + }, + }); + return user + +} + +const getConfessions = async () => { + const confessions = await prisma.confession.findMany(); + return confessions +} + +const getReactions = async ( confessionId : number ) => { + const reactions = await prisma.reactions.findMany({ + where: { + confessionId: confessionId, + }, + }); + return reactions +} + +const createConfession = async ( title : string, content: string , userId : string ) => { + const confession = await prisma.confession.create({ + data: { + title: title, + content: content, + reacts: 0, + author: { + connect: { + id: userId, + }, + } + }, + }); + return confession +} + +const deleteConfession = async ( confessionId : number ) => { + const confession = await prisma.confession.delete({ + where: { + id: confessionId, + }, + }); + return confession +} + +const editConfession = async ( confessionId : number, title : string, content: string ) => { + const confession = await prisma.confession.update({ + where: { + id: confessionId, + }, + data: { + title: title, + content: content, + }, + }); + return confession +} + + +const getHandler = () => { + return { + getUser, + getConfessions, + getReactions, + createConfession, + deleteConfession, + editConfession, + } +} + +export default getHandler \ No newline at end of file diff --git a/prisma/prisma.ts b/prisma/prisma.ts new file mode 100644 index 0000000..306fb95 --- /dev/null +++ b/prisma/prisma.ts @@ -0,0 +1,15 @@ +import { PrismaClient } from '@prisma/client' + +declare global { + // allow global `var` declarations + // eslint-disable-next-line no-var + var prisma: PrismaClient | undefined +} + +export const prisma = + global.prisma || + new PrismaClient({ + log: ['query'], + }) + +if (process.env.NODE_ENV !== 'production') global.prisma = prisma \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 1d6468a..df3f07c 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -50,6 +50,7 @@ model User { confessions Confession[] replies Reply[] reacts Reactions[] + @@unique([id, name]) } model VerificationToken { @@ -68,8 +69,9 @@ model Confession { content String reacts Int reacted_users Reactions[] - author User @relation(fields: [authorId], references: [id]) + author User @relation(fields: [authorId, authorName], references: [id, name]) authorId String + authorName String replies Reply[] } diff --git a/server.ts b/server.ts new file mode 100644 index 0000000..e12c0be --- /dev/null +++ b/server.ts @@ -0,0 +1,18 @@ +import { PrismaClient } from "@prisma/client"; + +const getUser = (email?: string) => { + const prisma = new PrismaClient(); + prisma.user.findUnique({ + where: { + email, + }, + include: { + confessions: true, + replies: true, + + }, + }); + +} + +export default getUser; \ No newline at end of file