forked from adrianhajdin/project_next_14_ai_prompt_sharing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d7b6c6
commit 44be0ff
Showing
18 changed files
with
464 additions
and
379 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,54 @@ | ||
import { connectToDB } from '@utils/database'; | ||
import Prompt from '@models/prompt'; | ||
import Prompt from "@models/prompt"; | ||
import { connectToDB } from "@utils/database"; | ||
|
||
// GET (read) | ||
export const GET = async (request, { params }) => { | ||
try { | ||
await connectToDB(); | ||
try { | ||
await connectToDB() | ||
|
||
const prompt = await Prompt.findById(params.id).populate('creator'); | ||
if(!prompt) return new Response("Prompt not found", { status: 404 }) | ||
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 }) | ||
} | ||
return new Response(JSON.stringify(prompt), { status: 200 }) | ||
|
||
} catch (error) { | ||
return new Response("Internal Server Error", { status: 500 }); | ||
} | ||
} | ||
|
||
// PATCH (update) | ||
export const PATCH = async (request, { params }) => { | ||
const { prompt, tag } = await request.json(); | ||
const { prompt, tag } = await request.json(); | ||
|
||
try { | ||
await connectToDB(); | ||
try { | ||
await connectToDB(); | ||
|
||
const existingPrompt = await Prompt.findById(params.id); | ||
// Find the existing prompt by ID | ||
const existingPrompt = await Prompt.findById(params.id); | ||
|
||
if(!existingPrompt) return new Response("Prompt not found", { status: 404 }) | ||
if (!existingPrompt) { | ||
return new Response("Prompt not found", { status: 404 }); | ||
} | ||
|
||
existingPrompt.prompt = prompt; | ||
existingPrompt.tag = tag; | ||
// Update the prompt with new data | ||
existingPrompt.prompt = prompt; | ||
existingPrompt.tag = tag; | ||
|
||
await existingPrompt.save(); | ||
await existingPrompt.save(); | ||
|
||
return new Response(JSON.stringify(existingPrompt), { status: 200 }) | ||
} catch (error) { | ||
return new Response("Failed to update prompt", { status: 500 }) | ||
} | ||
} | ||
return new Response("Successfully updated the Prompts", { status: 200 }); | ||
} catch (error) { | ||
return new Response("Error Updating Prompt", { status: 500 }); | ||
} | ||
}; | ||
|
||
// DELETE (delete) | ||
export const DELETE = async (request, { params }) => { | ||
try { | ||
await connectToDB(); | ||
try { | ||
await connectToDB(); | ||
|
||
await Prompt.findByIdAndRemove(params.id); | ||
// Find the prompt by ID and remove it | ||
await Prompt.findByIdAndRemove(params.id); | ||
|
||
return new Response("Prompt deleted successfully", { status: 200 }) | ||
} catch (error) { | ||
return new Response("Failed to delete prompt", { status: 500 }) | ||
} | ||
} | ||
return new Response("Prompt deleted successfully", { status: 200 }); | ||
} catch (error) { | ||
return new Response("Error deleting prompt", { status: 500 }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,16 @@ | ||
import { connectToDB } from '@utils/database'; | ||
import Prompt from '@models/prompt'; | ||
import Prompt from "@models/prompt"; | ||
import { connectToDB } from "@utils/database"; | ||
|
||
export const POST = async (req) => { | ||
const { userId, prompt, tag } = await req.json(); | ||
export const POST = async (request) => { | ||
const { userId, prompt, tag } = await request.json(); | ||
|
||
try { | ||
await connectToDB(); | ||
const newPrompt = new Prompt({ | ||
creator: userId, | ||
prompt, | ||
tag | ||
}) | ||
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 }) | ||
} | ||
} | ||
await newPrompt.save(); | ||
return new Response(JSON.stringify(newPrompt), { status: 201 }) | ||
} catch (error) { | ||
return new Response("Failed to create a new prompt", { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { connectToDB } from '@utils/database'; | ||
import Prompt from '@models/prompt'; | ||
import Prompt from "@models/prompt"; | ||
import { connectToDB } from "@utils/database"; | ||
|
||
export const GET = async (request) => { | ||
try { | ||
await connectToDB(); | ||
try { | ||
await connectToDB() | ||
|
||
const prompts = await Prompt.find({}).populate('creator'); | ||
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 }) | ||
} | ||
} | ||
return new Response(JSON.stringify(prompts), { status: 200 }) | ||
} catch (error) { | ||
return new Response("Failed to fetch all prompts", { status: 500 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,14 @@ | ||
import { connectToDB } from '@utils/database'; | ||
import Prompt from '@models/prompt'; | ||
import Prompt from "@models/prompt"; | ||
import { connectToDB } from "@utils/database"; | ||
|
||
export const GET = async (request, { params }) => { | ||
try { | ||
await connectToDB(); | ||
try { | ||
await connectToDB() | ||
|
||
const prompts = await Prompt.find({ | ||
creator: params.id | ||
}).populate('creator'); | ||
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 }) | ||
} | ||
} | ||
return new Response(JSON.stringify(prompts), { status: 200 }) | ||
} catch (error) { | ||
return new Response("Failed to fetch prompts created by user", { status: 500 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,51 @@ | ||
'use client'; | ||
"use client"; | ||
|
||
import { useState } from 'react'; | ||
import { useSession } from 'next-auth/react'; | ||
import { useRouter } from 'next/navigation'; | ||
import { useState } from "react"; | ||
import { useSession } from "next-auth/react"; | ||
import { useRouter } from "next/navigation"; | ||
|
||
import Form from '@components/Form'; | ||
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 [submitting, setIsSubmitting] = useState(false); | ||
const [post, setPost] = useState({ prompt: "", tag: "" }); | ||
|
||
const createPrompt = async (e) => { | ||
e.preventDefault(); | ||
setSubmitting(true); | ||
setIsSubmitting(true); | ||
|
||
try { | ||
const response = await fetch('/api/prompt/new', { | ||
method: 'POST', | ||
const response = await fetch("/api/prompt/new", { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
prompt: post.prompt, | ||
userId: session?.user.id, | ||
tag: post.tag | ||
}) | ||
}) | ||
tag: post.tag, | ||
}), | ||
}); | ||
|
||
if(response.ok) { | ||
router.push('/'); | ||
if (response.ok) { | ||
router.push("/"); | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
} finally { | ||
setSubmitting(false); | ||
setIsSubmitting(false); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<Form | ||
type="Create" | ||
type='Create' | ||
post={post} | ||
setPost={setPost} | ||
submitting={submitting} | ||
handleSubmit={createPrompt} | ||
/> | ||
) | ||
} | ||
); | ||
}; | ||
|
||
export default CreatePrompt | ||
export default CreatePrompt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,28 @@ | ||
import '@styles/globals.css'; | ||
import "@styles/globals.css"; | ||
|
||
import Nav from '@components/Nav'; | ||
import Provider from '@components/Provider'; | ||
import Nav from "@components/Nav"; | ||
import Provider from "@components/Provider"; | ||
|
||
export const metadata = { | ||
title: "Promptopia", | ||
description: 'Discover & Share AI Prompts' | ||
} | ||
description: "Discover & Share AI Prompts", | ||
}; | ||
|
||
const RootLayout = ({ children }) => { | ||
return ( | ||
<html lang="en"> | ||
<body> | ||
<Provider> | ||
<div className="main"> | ||
<div className="gradient" /> | ||
</div> | ||
const RootLayout = ({ children }) => ( | ||
<html lang='en'> | ||
<body> | ||
<Provider> | ||
<div className='main'> | ||
<div className='gradient' /> | ||
</div> | ||
|
||
<main className="app"> | ||
<Nav /> | ||
{children} | ||
</main> | ||
</Provider> | ||
</body> | ||
</html> | ||
) | ||
} | ||
<main className='app'> | ||
<Nav /> | ||
{children} | ||
</main> | ||
</Provider> | ||
</body> | ||
</html> | ||
); | ||
|
||
export default RootLayout; | ||
export default RootLayout; |
Oops, something went wrong.