From e9e6ca4050648f0c7aff3638ddd1fc332e149bf9 Mon Sep 17 00:00:00 2001 From: Eric Park Date: Sat, 24 Feb 2024 11:04:20 -0500 Subject: [PATCH] backend: update author API endpoint --- .../src/app/api/v0/authors/search/route.ts | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/backend/src/app/api/v0/authors/search/route.ts b/backend/src/app/api/v0/authors/search/route.ts index f9333051..05e418ff 100644 --- a/backend/src/app/api/v0/authors/search/route.ts +++ b/backend/src/app/api/v0/authors/search/route.ts @@ -1,4 +1,4 @@ -const sdk = require('node-appwrite'); +const sdk = require("node-appwrite"); import { NextRequest, NextResponse } from "next/server"; import { Query } from "appwrite"; @@ -10,25 +10,41 @@ const databases = new sdk.Databases(client); const MAIN_DB_ID = process.env.mainDBID; const AUTHOR_COLLECTION_ID = process.env.authorCollectionID; +function construct_development_api_response( + message: string, + response_name: string, + response_data: any, +) { + return NextResponse.json( + { + message, + warning: + "You are calling a development API! The schema may change without warning.", + [response_name]: response_data, + }, + { status: 200 }, + ); +} + export async function GET(request: NextRequest) { - const name = request.nextUrl.searchParams.get("name"); - - if (!name) { - return NextResponse.json( - { message: `Parameters not supplied.` }, - { status: 400 } - ); - } - - let db_query = await databases.listDocuments( - MAIN_DB_ID, AUTHOR_COLLECTION_ID, - [ - Query.equal('name', name as string) - ] - ); + const name = request.nextUrl.searchParams.get("name"); + if (!name) { return NextResponse.json( - { message: `DB search results for: ${name}`, db_query }, - { status: 200 } + { message: `Parameters not supplied.` }, + { status: 400 }, ); + } + + let db_query = await databases.listDocuments( + MAIN_DB_ID, + AUTHOR_COLLECTION_ID, + [Query.equal("name", name as string)], + ); + + return construct_development_api_response( + `DB search results for: ${name}`, + "results", + db_query, + ); }