Skip to content

Commit

Permalink
backend: update author API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ericswpark committed Feb 24, 2024
1 parent 182254d commit e9e6ca4
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions backend/src/app/api/v0/authors/search/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const sdk = require('node-appwrite');
const sdk = require("node-appwrite");

import { NextRequest, NextResponse } from "next/server";
import { Query } from "appwrite";
Expand All @@ -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,
);
}

0 comments on commit e9e6ca4

Please sign in to comment.