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

Migration to prisma #9

Merged
merged 7 commits into from
Feb 7, 2024
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
33 changes: 13 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
This is Blog-g-ers - Micro blogging platform

## Getting Started
## Tech Stack

First, run the development server:
- Nextjs
- MongoDB
- Prisma
- NextUI
- shadcnUI

## Run

Run the development server:

```bash
npm run dev
Expand All @@ -14,21 +22,6 @@ pnpm dev

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
## Live

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
You can check out [the live website here.](https://blog-g-gers.vercel.app/).
169 changes: 169 additions & 0 deletions actions/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
"use server";

import type { blogs } from "@prisma/client";
import { PrismaClient } from "@prisma/client";

import { authOptions } from "@/config/authoptions";
import { getServerSession } from "next-auth/next";
const prisma = new PrismaClient();

export async function FetchAll() {
try {
const resp = await prisma.blogs.findMany();
return resp;
} catch (err) {
console.log(err);
}
}
export async function FetchBlog(props: string | string[]) {
let id;
if (Array.isArray(props)) {
id = props[0];
} else {
id = props;
}
try {
const resp = await prisma.blogs.findFirst({
where: {
id: id,
},
});
return resp;
} catch (err) {
console.log(err);
}
}

export async function FetchBlogsByUser() {
const session = await getServerSession(authOptions);
const usr = await prisma.users.findFirst({
where: {
email: session?.user?.email!,
},
});
try {
const resp = await prisma.blogs.findMany({
where: {
userId: usr?.id,
},
});
return resp;
} catch (err) {
console.log(err);
}
}

export async function LikeBlog(tempBlog: blogs) {
const session = await getServerSession(authOptions);

try {
const resp = await prisma.blogs.update({
where: {
id: tempBlog.id,
},
data: {
likes: {
push: session?.user?.email || "",
},
},
});
return resp;
} catch (err) {
console.log(err);
}
}

export async function CommentBlog(tempBlog: blogs) {
const session = await getServerSession(authOptions);
console.log(tempBlog);

try {
const resp = await prisma.blogs.update({
where: {
id: tempBlog.id,
},
data: {
comments: {
push: tempBlog.comments,
},
},
});
return resp;
} catch (err) {
console.log(err);
}
}

interface FormDataProps {
title: string;
hashtags: string;
content: string;
likes: string[];
comments: string[];
}

export async function PostBlog(formData: FormDataProps) {
const session = await getServerSession(authOptions);
const usr = await prisma.users.findFirst({
where: {
email: session?.user?.email!,
},
});
await prisma.blogs.create({
data: {
title: formData.title,
content: formData.content,
hashtags: formData.hashtags,
likes: formData.likes,
comments: formData.comments,
userId: usr?.id!,
username: session?.user?.name!,
},
});
}

interface UpdateBlogProps {
id: string;
title: string;
hashtags: string;
content: string;
likes: string[];
username: string;
}

export async function UpdateBlog(tempBlog: UpdateBlogProps) {
try {
const resp = await prisma.blogs.update({
where: {
id: tempBlog.id,
},
data: {
title: tempBlog.title,
hashtags: tempBlog.hashtags,
content: tempBlog.content,
},
});
return resp;
} catch (err) {
console.log(err);
}
}

export async function DeleteBlog(props: string | string[]) {
let id;
if (Array.isArray(props)) {
id = props[0];
} else {
id = props;
}
try {
const resp = await prisma.blogs.delete({
where: {
id: id,
},
});
return resp;
} catch (err) {
console.log(err);
}
}
25 changes: 0 additions & 25 deletions actions/getBlogById.ts

This file was deleted.

19 changes: 0 additions & 19 deletions actions/getBlogs.ts

This file was deleted.

41 changes: 1 addition & 40 deletions app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,6 @@
import NextAuth, { NextAuthOptions } from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import GitHubProvider from "next-auth/providers/github";
import { MongoDBAdapter } from "@next-auth/mongodb-adapter";
import NextAuth from "next-auth";

import clientPromise from "@/utils/mongoClient";
import { authOptions } from "@/config/authoptions";

interface AuthOptionProps extends NextAuthOptions {}

// export const authOptions: AuthOptionProps = {
// adapter: MongoDBAdapter(clientPromise),
// secret: process.env.MY_SECRET,
// providers: [
// GoogleProvider({
// clientId: process.env.GOOGLE_CLIENT_ID || "",
// clientSecret: process.env.GOOGLE_CLIENT_SECRET || "",
// }),
// GitHubProvider({
// clientId: process.env.GITHUB_CLIENT_ID || "",
// clientSecret: process.env.GITHUB_CLIENT_SECRET || "",
// }),
// ],
// pages: {
// signIn: "/signin",
// },
// callbacks: {
// async jwt({ token, trigger, session }) {
// if (trigger === "update" && session?.name) {
// token.name = session.name;
// }
// return token;
// },
// async session({ session, token, user }) {
// return session; // The return type will match the one returned in `useSession()`
// },
// },

// session: {
// strategy: "jwt",
// },
// };

const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
8 changes: 0 additions & 8 deletions app/api/blogs/route.ts

This file was deleted.

55 changes: 0 additions & 55 deletions app/api/user/[id]/route.ts

This file was deleted.

Loading
Loading