Skip to content

Commit

Permalink
shortened import statements, used id for creating response, and modif…
Browse files Browse the repository at this point in the history
…ied error messages
  • Loading branch information
nkanchinadam committed Apr 10, 2024
1 parent 030f25a commit 65a8537
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/app/api/responses/[responseId]/route.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
import { getResponseByID } from '../../../../lib/firebase/database/response';
import { Response } from '../../../../types/survey-types';
import { getResponseByID } from '@/lib/firebase/database/response';
import { Response } from '@/types/survey-types';

export async function GET(req: NextRequest, { params }: { params: { responseId: string } }) {
const { responseId } = params;
Expand Down
8 changes: 4 additions & 4 deletions src/app/api/responses/route.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// pages/route.tsx

import { NextRequest, NextResponse } from 'next/server';
import { getAllResponses, createResponse } from '../../../lib/firebase/database/response';
import { Response } from '../../../types/survey-types';
import { getAllResponses, createResponse } from '@/lib/firebase/database/response';
import { Response } from '@/types/survey-types';

export async function GET(req: NextRequest) {
try {
const allResponses = await getAllResponses();
return NextResponse.json(allResponses, { status: 200 });
} catch (error) {
console.error('Error getting responses:', error);
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
}
}

Expand All @@ -21,6 +21,6 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ message: 'Response created successfully' }, { status: 201 });
} catch (error) {
console.error('Error creating response:', error);
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
}
}
12 changes: 6 additions & 6 deletions src/lib/firebase/database/response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { db } from "../firebaseConfig";
import { collection, getDocs, addDoc, getDoc, doc } from 'firebase/firestore';
import { Response } from '../../../types/survey-types';
import { collection, getDocs, getDoc, doc, setDoc } from 'firebase/firestore';
import { Response } from '@/types/survey-types';

// GET all responses
export async function getAllResponses() {
Expand All @@ -13,16 +13,16 @@ export async function getAllResponses() {
return allResponses;
} catch (error) {
console.error('Error getting responses:', error);
throw new Error('Internal Server Error');
throw new Error('unable to get responses');
}
}

export async function createResponse(newResponse: Response) {
try {
await addDoc(collection(db, 'responses'), newResponse);
await setDoc(doc(db, 'responses', newResponse.responseId), newResponse);
} catch (error) {
console.error('Error creating response:', error);
throw new Error('Internal Server Error');
throw new Error('unable to create response');
}
}
export async function getResponseByID(responseId: string): Promise<Response | null> {
Expand All @@ -35,6 +35,6 @@ export async function getResponseByID(responseId: string): Promise<Response | nu
}
} catch (error) {
console.error('Error getting response by ID:', error);
throw new Error('Internal Server Error');
throw new Error('unable to get response by id');
}
}

0 comments on commit 65a8537

Please sign in to comment.