-
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.
Implemented create propmts and saving created prompts to MongoDB data…
…base. Implemented rendering created propmts on homepage with ability to copy prompts with a button
- Loading branch information
Showing
7 changed files
with
250 additions
and
8 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
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 }) | ||
} | ||
} |
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 |
---|---|---|
@@ -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 }) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use client'; | ||
|
||
import { useState } from 'react'; | ||
import { useSession } from 'next-auth/react'; | ||
import { useRouter } from 'next/navigation'; | ||
|
||
import Form from '@components/form'; | ||
import { set } from 'mongoose'; | ||
|
||
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 |
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
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Schema, model, models } from 'mongoose'; | ||
|
||
const PromptSchema = new Schema({ | ||
creator: { | ||
type: Schema.Types.ObjectId, | ||
ref: 'User', | ||
}, | ||
prompt: { | ||
type: String, | ||
required: [true, 'Prompt is required.'], | ||
}, | ||
tag: { | ||
type: String, | ||
required: [true, 'Tag is required.'], | ||
} | ||
}); | ||
|
||
const Prompt = models.Prompt || model('Prompt', PromptSchema); | ||
|
||
export default Prompt; |