Description
Create a POST FormQuestions api endpoint that creates multiple form questions in the database
Technical details
-
Create a function in app/api/forms/[formId]/formQuestions/route.ts that looks like export async function POST (request: NextRequest, { params }: { params: { formId: string } }) { ... }
-
Get the formId using params.formId
-
Connect to the database by calling await dbConnect()
-
Get the body of the POST request by calling await request.json()
-
make sure that the request body is in the correct format by calling z.array(zCreateFormQuestionRequest).safeParse([//put body here]) and checking the return value
-
make sure that the id is in the correct format by calling zObjectId.safeParse([//id]) and checking the return value
-
call the createFormQuestions function
-
Return an empty array for now by calling return NextResponse.json({}, { status: 201 })
-
The API should be able to return multiple status codes:
- on success: 201
- on form not found (
createFormQuestions returns null): 404
- on invalid body: 400
- we will worry about other response codes later
Teachables
- With the function
export async function POST (request: NextRequest) { ... }, here is a breakdown of each part:
export = "make it so other things in this project have access to this function"
async (asynchronous) = "This function will take some time to execute, and I am willing to wait for it to finish before moving on"
function = used to specify that we are creating a function
POST = the API Method, and the name of the function. If I wanted to make a GET api endpoint, I would call the function GET.
NextRequest = a data type (like a Java class) that contains the request body, headers, query parameters, and some extra information
await dbConnect() establishes a connection to the database. Andrew can explain what this code does if you'd like, but for now it's enough to know that you must first connect to the database before inserting something into it
Testing steps
- Test by making a POST api call using postman with the url
localhost:3000/api/forms/[formId]/formQuestions and create a request body with an array of valid form question objects
- Check the database to make sure that the form questions were inserted properly, both into the forms collection and the formQuestions collection
- Search by _id in the form collection using this query:
{_id: ObjectId([id])}
- Search for the form question in the form collection using this query:
{questions: ObjectId("[id]")}
Dependencies
None
Description
Create a POST FormQuestions api endpoint that creates multiple form questions in the database
Technical details
Create a function in
app/api/forms/[formId]/formQuestions/route.tsthat looks likeexport async function POST (request: NextRequest, { params }: { params: { formId: string } }) { ... }Get the formId using
params.formIdConnect to the database by calling
await dbConnect()Get the body of the POST request by calling
await request.json()make sure that the request body is in the correct format by calling
z.array(zCreateFormQuestionRequest).safeParse([//put body here])and checking the return valuemake sure that the id is in the correct format by calling zObjectId.safeParse([//id]) and checking the return value
call the
createFormQuestionsfunctionReturn an empty array for now by calling
return NextResponse.json({}, { status: 201 })The API should be able to return multiple status codes:
createFormQuestionsreturns null): 404Teachables
export async function POST (request: NextRequest) { ... }, here is a breakdown of each part:export= "make it so other things in this project have access to this function"async(asynchronous) = "This function will take some time to execute, and I am willing to wait for it to finish before moving on"function= used to specify that we are creating a functionPOST= the API Method, and the name of the function. If I wanted to make a GET api endpoint, I would call the function GET.NextRequest= a data type (like a Java class) that contains the request body, headers, query parameters, and some extra informationawait dbConnect()establishes a connection to the database. Andrew can explain what this code does if you'd like, but for now it's enough to know that you must first connect to the database before inserting something into itTesting steps
localhost:3000/api/forms/[formId]/formQuestionsand create a request body with an array of valid form question objects{_id: ObjectId([id])}{questions: ObjectId("[id]")}Dependencies
None