Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore : added data handling #7

Merged
merged 2 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion components/forms/providerButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 0 additions & 3 deletions components/headComponent.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
27 changes: 22 additions & 5 deletions pages/confessions.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
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 (
<Layout>
<ProtectedComponent>
<div className="h-auto min-h-screen bg-zinc-900">
<ConfessionsHeader />

<main>
<h1 className="mt-10 text-center text-3xl text-zinc-300">
Still not done
</h1>
{confessionData.map((confession: any) => (
<ConfessionCard confession={confession} />
))}
</main>
</div>
</ProtectedComponent>
Expand All @@ -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),
},
};
}
68 changes: 32 additions & 36 deletions pages/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Layout>
<ProtectedComponent>
Expand All @@ -28,46 +31,34 @@ const Home: NextPage<{ session: Session }> = ({ session }) => {
<PlusIcon className="h-4 w-4" />
<p>Write confession</p>
</button>
<p className="text-zinc-400">You've made 0 confessions</p>
<p className="text-zinc-400">You've made {userData.confessions.length} confessions</p>
</div>

<div>
{/* --- No confession --- */}
{/* <h1 className="text-zinc-400 text-2xl text-center mt-10">
You haven't wrote a confession yet
</h1>
{userData.confessions.length > 0 ? (
<div>
<p className="mb-8 text-lg font-medium text-zinc-500">
Your confessions
</p>
<div className="grid grid-cols-3 gap-10">

{userData.confessions.map((confession: any) => (
<ConfessionCard confession={confession} />
))}

<button className="block mt-8 mx-auto border border-zinc-800 px-4 py-2.5 rounded-md text-sky-500 font-medium">
Write one
</button> */}
</div>
</div>
) : (
<div>
<h1 className="text-zinc-400 text-2xl text-center mt-10">
You haven't wrote a confession yet
</h1>

{/* --- Has confessions --- */}
<p className="mb-8 text-lg font-medium text-zinc-500">
Your confessions
</p>
<div className="grid grid-cols-3 gap-10">
<ConfessionCard
confession={{
title: "Confession Title",
content:
"Lorem, ipsum dolor sit amet consectetur adipisicing elit. Amet in nesciunt asperiores eius sed sint facere soluta atque perspiciatis incidunt.",
}}
/>
<ConfessionCard
confession={{
title: "Meow",
content:
"Lorem, ipsum dolor sit amet consectetur adipisicing elit. Amet in nesciunt asperiores eius sed sint facere soluta atque perspiciatis incidunt. dsfdsfdsfdsfdsfds",
}}
/>
<ConfessionCard
confession={{
title: "HIhihihihi",
content:
"Lorem, ipsum dolor sit amet consectetur adipisicing elit. Amet in nesciunt asperiores eius sed sint facere soluta atque perspiciatis incidunt. dsfdsfdsfdsfdsfds",
}}
/>
</div>
<button className="block mt-8 mx-auto border border-zinc-800 px-4 py-2.5 rounded-md text-sky-500 font-medium">
Write one
</button>
</div>
)}
</div>
</main>
</div>
Expand All @@ -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: {
Expand All @@ -98,6 +93,7 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
return {
props: {
session,
user: JSON.stringify(user),
},
};
};
80 changes: 80 additions & 0 deletions prisma/handler.ts
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions prisma/prisma.ts
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ model User {
confessions Confession[]
replies Reply[]
reacts Reactions[]
@@unique([id, name])
}

model VerificationToken {
Expand All @@ -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[]
}

Expand Down
18 changes: 18 additions & 0 deletions server.ts
Original file line number Diff line number Diff line change
@@ -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;