-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added backend schema, server actions and testing API for faq forum
- Loading branch information
1 parent
49a3364
commit 2780452
Showing
8 changed files
with
418 additions
and
0 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,79 @@ | ||
"use server" | ||
|
||
import prismadb from "@/lib/prismadb"; | ||
import { Answer } from "@prisma/client"; | ||
import { UUID } from "crypto"; | ||
|
||
type CreateAnswerResponse = { | ||
success: boolean; | ||
data?: Answer | ||
error?: string; | ||
}; | ||
|
||
export async function createAnswer( | ||
questionId: string, | ||
content: string | ||
): Promise<CreateAnswerResponse> { | ||
try { | ||
|
||
const newAnswer = await prismadb.answer.create({ | ||
data: { | ||
content, | ||
questionId, | ||
}, | ||
}); | ||
|
||
await prismadb.question.update({ | ||
where: { id: questionId }, | ||
data: { answered: true }, | ||
}); | ||
|
||
return { | ||
success: true, | ||
data: newAnswer | ||
}; | ||
} catch (error) { | ||
console.error("Error creating answer:", error); | ||
return { | ||
success: false, | ||
error: "Failed to create answer", | ||
}; | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
type DeleteAnswerResponse = { | ||
success: boolean; | ||
data?: Answer; | ||
error?: string; | ||
}; | ||
|
||
export async function deleteSpecificAnswer(answerId: string): Promise<DeleteAnswerResponse> { | ||
try { | ||
const deletedAnswer = await prismadb.answer.delete({ | ||
where: { | ||
id: answerId, | ||
}, | ||
}); | ||
|
||
if (!deletedAnswer) { | ||
return { | ||
success: false, | ||
error: "Error in deleting the answer", | ||
}; | ||
} | ||
|
||
return { | ||
success: true, | ||
data: deletedAnswer, | ||
}; | ||
} catch (error) { | ||
console.error("Error deleting specific answer:", error); | ||
return { | ||
success: false, | ||
error: "Failed to delete specific answer", | ||
}; | ||
} | ||
} |
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,141 @@ | ||
'use server'; | ||
|
||
import prismadb from '@/lib/prismadb'; | ||
import { Answer, Question } from '@prisma/client'; | ||
import { UUID } from 'crypto'; | ||
|
||
interface QuestionWithAnswer extends Question{ | ||
answers?:Answer[] | ||
} | ||
|
||
type QuestionResponse = { | ||
success: boolean; | ||
data?: Question; | ||
error?: string; | ||
}; | ||
|
||
type getQuestionsResponse = { | ||
success: boolean; | ||
data?: QuestionWithAnswer[]; | ||
error?: string; | ||
}; | ||
|
||
|
||
export async function createQuestion( | ||
content: string | ||
): Promise<QuestionResponse> { | ||
try { | ||
const newQuestion = await prismadb.question.create({ | ||
data: { | ||
content, | ||
}, | ||
}); | ||
|
||
return { | ||
success: true, | ||
data: newQuestion, | ||
}; | ||
} catch (error) { | ||
console.error('Error creating question:', error); | ||
return { | ||
success: false, | ||
error: 'Failed to create question', | ||
}; | ||
} | ||
} | ||
|
||
export async function getUnansweredQuestions( | ||
): Promise<getQuestionsResponse> { | ||
try { | ||
const unAnsweredQuestions = await prismadb.question.findMany({ | ||
where: { | ||
answered: false, | ||
}, | ||
}); | ||
|
||
if (!unAnsweredQuestions.length) { | ||
return { | ||
success: false, | ||
error: 'No unanswered questions!', | ||
}; | ||
} | ||
|
||
return { | ||
success: true, | ||
data: unAnsweredQuestions, | ||
}; | ||
} catch (error) { | ||
console.error('Error fetching unanswered questions', error); | ||
return { | ||
success: false, | ||
error: 'Failed to fetch unanswered questions', | ||
}; | ||
} | ||
} | ||
|
||
|
||
export async function getAnsweredQuestions( | ||
): Promise<getQuestionsResponse> { | ||
try { | ||
const AnsweredQuestions = await prismadb.question.findMany({ | ||
where: { | ||
answered: true, | ||
}, | ||
include:{ | ||
answers:true | ||
} | ||
}); | ||
|
||
if (!AnsweredQuestions.length) { | ||
return { | ||
success: false, | ||
error: 'No unanswered questions!', | ||
}; | ||
} | ||
|
||
return { | ||
success: true, | ||
data: AnsweredQuestions, | ||
}; | ||
} catch (error) { | ||
console.error('Error fetching answered questions', error); | ||
return { | ||
success: false, | ||
error: 'Failed to fetch answered questions', | ||
}; | ||
} | ||
} | ||
|
||
// This will be only accessible by the admin | ||
|
||
// deletes a question and all its related answers if present | ||
|
||
export async function deleteQuestion( | ||
questionId: string, | ||
): Promise<QuestionResponse> { | ||
try { | ||
const deletedQuestion = await prismadb.question.delete({ | ||
where: { | ||
id:questionId | ||
}, | ||
}); | ||
|
||
if (!deletedQuestion) { | ||
return { | ||
success: false, | ||
error: 'error in deleting the question', | ||
}; | ||
} | ||
|
||
return { | ||
success: true, | ||
data: deletedQuestion, | ||
}; | ||
} catch (error) { | ||
console.error('Error deleting specific question', error); | ||
return { | ||
success: false, | ||
error: 'Failed to delete specific question', | ||
}; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
alimento-nextjs/app/api/testing/faq/[customerId]/[questionId]/route.ts
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,39 @@ | ||
|
||
import { createAnswer } from "@/actions/forum/Answer"; | ||
import { createQuestion, getUnansweredQuestions } from "@/actions/forum/Question"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export type Params = Promise<{ | ||
customerId: string; | ||
questionId : string | ||
}>; | ||
|
||
export async function POST( | ||
request: NextRequest, | ||
{ params }: { params: Params } | ||
) { | ||
const { customerId , questionId } = await params; | ||
|
||
if (!customerId) { | ||
return new NextResponse('customer not authenticated', { status: 401 }); | ||
} | ||
|
||
const { content } = await request.json(); | ||
|
||
if (!content || !questionId) { | ||
return new NextResponse('Content and questionId is required', { status: 400 }); | ||
} | ||
|
||
try { | ||
const resp = await createAnswer(questionId,content); | ||
|
||
if (resp.success) { | ||
return NextResponse.json(resp.data); | ||
} else { | ||
return NextResponse.json({ err: resp.error }, { status: 400 }); | ||
} | ||
} catch (err) { | ||
console.log('[CREATE_ANSWER]', err); | ||
return new NextResponse('Internal Server Error', { 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,37 @@ | ||
|
||
import { createQuestion, getUnansweredQuestions } from "@/actions/forum/Question"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export type Params = Promise<{ | ||
customerId: string; | ||
}>; | ||
|
||
export async function POST( | ||
request: NextRequest, | ||
{ params }: { params: Params } | ||
) { | ||
const { customerId } = await params; | ||
|
||
if (!customerId) { | ||
return new NextResponse('customer not authenticated', { status: 401 }); | ||
} | ||
|
||
const { content } = await request.json(); | ||
|
||
if (!content) { | ||
return new NextResponse('Content is required', { status: 400 }); | ||
} | ||
|
||
try { | ||
const resp = await createQuestion(content); | ||
|
||
if (resp.success) { | ||
return NextResponse.json(resp.data); | ||
} else { | ||
return NextResponse.json({ err: resp.error }, { status: 400 }); | ||
} | ||
} catch (err) { | ||
console.log('[CREATE_QUESTION]', err); | ||
return new NextResponse('Internal Server Error', { status: 500 }); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
alimento-nextjs/app/api/testing/faq/admin/[adminId]/answer/[answerId]/route.ts
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,36 @@ | ||
|
||
import { deleteSpecificAnswer } from "@/actions/forum/Answer"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export type Params = Promise<{ | ||
userId: string; | ||
answerId: string; | ||
}>; | ||
|
||
export async function DELETE( | ||
request: NextRequest, | ||
{ params }: { params: Params } | ||
) { | ||
const { userId, answerId } = await params; | ||
|
||
if (!userId) { | ||
return new NextResponse('User not authenticated', { status: 401 }); | ||
} | ||
|
||
if (!answerId) { | ||
return new NextResponse('Answer ID is required', { status: 400 }); | ||
} | ||
|
||
try { | ||
const resp = await deleteSpecificAnswer(answerId); | ||
|
||
if (resp.success) { | ||
return NextResponse.json(resp.data); | ||
} else { | ||
return NextResponse.json({ err: resp.error }, { status: 400 }); | ||
} | ||
} catch (err) { | ||
console.log('[DELETE_ANSWER]', err); | ||
return new NextResponse('Internal Server Error', { status: 500 }); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
alimento-nextjs/app/api/testing/faq/admin/[adminId]/question/[questionId]/route.ts
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,36 @@ | ||
|
||
import { deleteQuestion } from "@/actions/forum/Question"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export type Params = Promise<{ | ||
adminId: string; | ||
questionId: string; | ||
}>; | ||
|
||
export async function DELETE( | ||
request: NextRequest, | ||
{ params }: { params: Params } | ||
) { | ||
const { adminId, questionId } = await params; | ||
|
||
if (!adminId) { | ||
return new NextResponse('Admin not authenticated', { status: 401 }); | ||
} | ||
|
||
if (!questionId) { | ||
return new NextResponse('Question ID is required', { status: 400 }); | ||
} | ||
|
||
try { | ||
const resp = await deleteQuestion(questionId); | ||
|
||
if (resp.success) { | ||
return NextResponse.json(resp.data); | ||
} else { | ||
return NextResponse.json({ err: resp.error }, { status: 400 }); | ||
} | ||
} catch (err) { | ||
console.log('[DELETE_QUESTION]', err); | ||
return new NextResponse('Internal Server Error', { status: 500 }); | ||
} | ||
} |
Oops, something went wrong.