Skip to content

Commit

Permalink
created getSurveyByID function and corresponding API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nkanchinadam committed Apr 15, 2024
1 parent 8188bd7 commit f0ae499
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/app/api/surveys/[surveyId]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { NextRequest, NextResponse } from "next/server";
import { getSurveyByID } from "@/lib/firebase/database/surveys";

export async function GET(
req: NextRequest,
{ params }: { params: { surveyId: string } }
) {
const { surveyId } = params;
try {
const survey = await getSurveyByID(surveyId);
return NextResponse.json({ data: survey }, { status: 200 });
} catch (err) {
return NextResponse.json(
{ error: "error getting survey by id" },
{ status: 500 }
);
}
}
12 changes: 11 additions & 1 deletion src/lib/firebase/database/surveys.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Survey } from '@/types/survey-types';
import { forms } from '../../googleAuthorization';
import { db } from "../firebaseConfig";
import { doc, setDoc } from 'firebase/firestore';
import { doc, getDoc, setDoc } from 'firebase/firestore';

export async function createSurvey(body: {title: string, documentTitle: string}) {
let form = null;
Expand Down Expand Up @@ -42,4 +43,13 @@ export async function updateSurvey(id: string) {
} catch (err) {
throw Error('unable to update survey in firestore');
}
}

export async function getSurveyByID(id: string) {
try {
const snapshot = await getDoc(doc(db, 'surveys', id));
return snapshot.exists() ? snapshot.data() as Survey : null
} catch (err) {
throw Error('survey with given id not found')
}
}

0 comments on commit f0ae499

Please sign in to comment.