Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianhajdin committed May 4, 2023
1 parent 0c37f07 commit 4cafd7d
Show file tree
Hide file tree
Showing 42 changed files with 2,653 additions and 204 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/node_modules
/.pnp
.pnp.js
.env

# testing
/coverage
Expand Down
42 changes: 6 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
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).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
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.js`. The page auto-updates as you edit the file.

[http://localhost:3000/api/hello](http://localhost:3000/api/hello) is an endpoint that uses [Route Handlers](https://beta.nextjs.org/docs/routing/route-handlers). This endpoint can be edited in `app/api/hello/route.js`.

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.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
[] Implement Search
- Search by prompt
- Search by tag
- Search by username
[] Implement Click on tag
[] Implement View other profiles
51 changes: 51 additions & 0 deletions app/api/auth/[...nextauth]/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import NextAuth from 'next-auth';
import GoogleProvider from 'next-auth/providers/google';

import User from '@models/user';
import { connectToDB } from '@utils/database';

const handler = NextAuth({
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
})
],
callbacks: {
async session({ session }) {
const sessionUser = await User.findOne({
email: session.user.email
})

session.user.id = sessionUser._id.toString();

return session;
},
async signIn({ profile }) {
try {
await connectToDB();

// check if a user already exists
const userExists = await User.findOne({
email: profile.email
});

// if not, create a new user
if(!userExists) {
await User.create({
email: profile.email,
username: profile.name.replace(" ", "").toLowerCase(),
image: profile.picture
})
}

return true;
} catch (error) {
console.log(error);
return false;
}
}
}
})

export { handler as GET, handler as POST };
3 changes: 0 additions & 3 deletions app/api/hello/route.js

This file was deleted.

51 changes: 51 additions & 0 deletions app/api/prompt/[id]/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { connectToDB } from '@utils/database';
import Prompt from '@models/prompt';

// GET (read)
export const GET = async (request, { params }) => {
try {
await connectToDB();

const prompt = await Prompt.findById(params.id).populate('creator');
if(!prompt) return new Response("Prompt not found", { status: 404 })

return new Response(JSON.stringify(prompt), { status: 200 })
} catch (error) {
return new Response("Failed to fetch all prompts", { status: 500 })
}
}

// PATCH (update)
export const PATCH = async (request, { params }) => {
const { prompt, tag } = await request.json();

try {
await connectToDB();

const existingPrompt = await Prompt.findById(params.id);

if(!existingPrompt) return new Response("Prompt not found", { status: 404 })

existingPrompt.prompt = prompt;
existingPrompt.tag = tag;

await existingPrompt.save();

return new Response(JSON.stringify(existingPrompt), { status: 200 })
} catch (error) {
return new Response("Failed to update prompt", { status: 500 })
}
}

// DELETE (delete)
export const DELETE = async (request, { params }) => {
try {
await connectToDB();

await Prompt.findByIdAndRemove(params.id);

return new Response("Prompt deleted successfully", { status: 200 })
} catch (error) {
return new Response("Failed to delete prompt", { status: 500 })
}
}
21 changes: 21 additions & 0 deletions app/api/prompt/new/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { connectToDB } from '@utils/database';
import Prompt from '@models/prompt';

export const POST = async (req) => {
const { userId, prompt, tag } = await req.json();

try {
await connectToDB();
const newPrompt = new Prompt({
creator: userId,
prompt,
tag
})

await newPrompt.save();

return new Response(JSON.stringify(newPrompt), { status: 201 })
} catch (error) {
return new Response("Failed to create a new prompt", { status: 500 })
}
}
14 changes: 14 additions & 0 deletions app/api/prompt/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { connectToDB } from '@utils/database';
import Prompt from '@models/prompt';

export const GET = async (request) => {
try {
await connectToDB();

const prompts = await Prompt.find({}).populate('creator');

return new Response(JSON.stringify(prompts), { status: 200 })
} catch (error) {
return new Response("Failed to fetch all prompts", { status: 500 })
}
}
16 changes: 16 additions & 0 deletions app/api/users/[id]/posts/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { connectToDB } from '@utils/database';
import Prompt from '@models/prompt';

export const GET = async (request, { params }) => {
try {
await connectToDB();

const prompts = await Prompt.find({
creator: params.id
}).populate('creator');

return new Response(JSON.stringify(prompts), { status: 200 })
} catch (error) {
return new Response("Failed to fetch all prompts", { status: 500 })
}
}
54 changes: 54 additions & 0 deletions app/create-prompt/page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use client';

import { useState } from 'react';
import { useSession } from 'next-auth/react';
import { useRouter } from 'next/navigation';

import Form from '@components/Form';

const CreatePrompt = () => {
const router = useRouter();
const { data: session } = useSession();

const [submitting, setSubmitting] = useState(false);
const [post, setPost] = useState({
prompt: '',
tag: '',
});

const createPrompt = async (e) => {
e.preventDefault();
setSubmitting(true);

try {
const response = await fetch('/api/prompt/new', {
method: 'POST',
body: JSON.stringify({
prompt: post.prompt,
userId: session?.user.id,
tag: post.tag
})
})

if(response.ok) {
router.push('/');
}
} catch (error) {
console.log(error);
} finally {
setSubmitting(false);
}
}

return (
<Form
type="Create"
post={post}
setPost={setPost}
submitting={submitting}
handleSubmit={createPrompt}
/>
)
}

export default CreatePrompt
Binary file removed app/favicon.ico
Binary file not shown.
27 changes: 0 additions & 27 deletions app/globals.css

This file was deleted.

17 changes: 0 additions & 17 deletions app/layout.js

This file was deleted.

30 changes: 30 additions & 0 deletions app/layout.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import '@styles/globals.css';

import Nav from '@components/Nav';
import Provider from '@components/Provider';

export const metadata = {
title: "Promptopia",
description: 'Discover & Share AI Prompts'
}

const RootLayout = ({ children }) => {
return (
<html lang="en">
<body>
<Provider>
<div className="main">
<div className="gradient" />
</div>

<main className="app">
<Nav />
{children}
</main>
</Provider>
</body>
</html>
)
}

export default RootLayout;
Loading

0 comments on commit 4cafd7d

Please sign in to comment.