Skip to content

Commit

Permalink
added backend schema, server actions and testing API for faq forum
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivanshPlays committed Nov 9, 2024
1 parent 49a3364 commit 2780452
Show file tree
Hide file tree
Showing 8 changed files with 418 additions and 0 deletions.
79 changes: 79 additions & 0 deletions alimento-nextjs/actions/forum/Answer.tsx
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",
};
}
}
141 changes: 141 additions & 0 deletions alimento-nextjs/actions/forum/Question.tsx
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',
};
}
}
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 });
}
}
37 changes: 37 additions & 0 deletions alimento-nextjs/app/api/testing/faq/[customerId]/route.ts
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 });
}
}
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 });
}
}
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 });
}
}
Loading

0 comments on commit 2780452

Please sign in to comment.