diff --git a/backend/src/app/api/v0/authors/search/route.ts b/backend/src/app/api/v0/authors/search/route.ts index 05e418ff..b665b11b 100644 --- a/backend/src/app/api/v0/authors/search/route.ts +++ b/backend/src/app/api/v0/authors/search/route.ts @@ -4,28 +4,13 @@ import { NextRequest, NextResponse } from "next/server"; import { Query } from "appwrite"; import { client } from "@/app/appwrite"; +import { construct_development_api_response } from "../../dev_api_response"; 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"); @@ -42,9 +27,9 @@ export async function GET(request: NextRequest) { [Query.equal("name", name as string)], ); - return construct_development_api_response( - `DB search results for: ${name}`, - "results", - db_query, - ); + return construct_development_api_response({ + message: `DB search results for: ${name}`, + response_name: "results", + response_data: db_query, + }); } diff --git a/backend/src/app/api/v0/books/fetch-update/route.ts b/backend/src/app/api/v0/books/fetch-update/route.ts index 5b7e95b8..b22a30b0 100644 --- a/backend/src/app/api/v0/books/fetch-update/route.ts +++ b/backend/src/app/api/v0/books/fetch-update/route.ts @@ -4,6 +4,7 @@ import { NextRequest, NextResponse } from "next/server"; import { Query } from "appwrite"; import { client } from "@/app/appwrite"; +import { construct_development_api_response } from "../../dev_api_response"; const databases = new sdk.Databases(client); @@ -17,33 +18,6 @@ async function getBookFromGoogleBooksAPI(id: string) { return await gbooks_api_res.json(); } -function construct_development_api_response( - message: string, - response_name: string | null, - response_data: any | null, - status_code: number, -) { - if (response_name) { - return NextResponse.json( - { - message, - warning: - "You are calling a development API! The schema may change without warning.", - [response_name]: response_data, - }, - { status: status_code }, - ); - } - return NextResponse.json( - { - message, - warning: - "You are calling a development API! The schema may change without warning.", - }, - { status: status_code }, - ); -} - export async function POST(request: NextRequest) { const data = await request.json(); const gbooks_api_id = data.id; @@ -53,12 +27,12 @@ export async function POST(request: NextRequest) { ]); if (db_query.total == 0) { - return construct_development_api_response( - `A book with the specified ID ${gbooks_api_id} was not found.`, - null, - null, - 404, - ); + return construct_development_api_response({ + message: `A book with the specified ID ${gbooks_api_id} was not found.`, + response_name: null, + response_data: null, + status_code: 404, + }); } const target_book_id = db_query.documents[0].$id; @@ -72,10 +46,9 @@ export async function POST(request: NextRequest) { genre: gbooks_book_data.volumeInfo.categories[0], }); - return construct_development_api_response( - `The book ID ${target_book_id} (Google Books API ID ${gbooks_api_id}) was updated.`, - "gbooks_book_data", - gbooks_book_data, - 200, - ); + return construct_development_api_response({ + message: `The book ID ${target_book_id} (Google Books API ID ${gbooks_api_id}) was updated.`, + response_name: "gbooks_book_data", + response_data: gbooks_book_data, + }); } diff --git a/backend/src/app/api/v0/books/search/route.ts b/backend/src/app/api/v0/books/search/route.ts index a1df8d3d..b90a2e49 100644 --- a/backend/src/app/api/v0/books/search/route.ts +++ b/backend/src/app/api/v0/books/search/route.ts @@ -4,6 +4,7 @@ import { NextRequest, NextResponse } from "next/server"; import { ID, Query } from "appwrite"; import { client } from "@/app/appwrite"; +import { construct_development_api_response } from "../../dev_api_response"; const databases = new sdk.Databases(client); @@ -107,31 +108,15 @@ async function get_or_create_author_id(name: string) { } } -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 title = request.nextUrl.searchParams.get("title") as string; if (!title) { - return NextResponse.json( - { message: `Parameters not supplied.` }, - { status: 400 } - ); -} + return construct_development_api_response({ + message: "Parameter `title` not supplied.", + status_code: 400, + }); + } let db_query = await databases.listDocuments(MAIN_DB_ID, BOOK_COL_ID, [ Query.search("title", title), @@ -177,8 +162,8 @@ export async function GET(request: NextRequest) { authors: [author_id], editions: [edition_id], google_books_id: gbooks_target_book.id, - }) - + }); + // Fetch from DB to refresh db_query = await databases.listDocuments(MAIN_DB_ID, BOOK_COL_ID, [ Query.search("title", title), @@ -186,9 +171,9 @@ export async function GET(request: NextRequest) { } } } - return construct_development_api_response( - `DB search results for: ${title}`, - "results", - db_query, - ); + return construct_development_api_response({ + message: `DB search results for: ${title}`, + response_name: "results", + response_data: db_query, + }); } diff --git a/backend/src/app/api/v0/bookstatus/route.ts b/backend/src/app/api/v0/bookstatus/route.ts index 379b8a99..24e2e137 100644 --- a/backend/src/app/api/v0/bookstatus/route.ts +++ b/backend/src/app/api/v0/bookstatus/route.ts @@ -67,7 +67,6 @@ export async function GET(request: NextRequest) { message: `Book status results for: ${user_id}`, response_name: "results", response_data: db_query, - status_code: 200, }); } @@ -102,14 +101,11 @@ export async function POST(request: NextRequest) { { status: status, }, - bookStatusPermissions + bookStatusPermissions, ); } return construct_development_api_response({ message: `The book status item was updated.`, - response_name: null, - response_data: null, - status_code: 200, }); } diff --git a/backend/src/app/api/v0/dev_api_response.ts b/backend/src/app/api/v0/dev_api_response.ts index 4259b9e1..8c9b43cc 100644 --- a/backend/src/app/api/v0/dev_api_response.ts +++ b/backend/src/app/api/v0/dev_api_response.ts @@ -4,12 +4,12 @@ export function construct_development_api_response({ message, response_name, response_data, - status_code, + status_code = 200, }: { message: string; - response_name: string | null; - response_data: any | null; - status_code: number; + response_name?: string | null; + response_data?: any | null; + status_code?: number; }) { if (response_name) { return NextResponse.json(