Skip to content

Commit

Permalink
backend: refactor to use dev API response generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ericswpark committed Feb 24, 2024
1 parent 72931e0 commit dc65a2c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 97 deletions.
27 changes: 6 additions & 21 deletions backend/src/app/api/v0/authors/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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,
});
}
51 changes: 12 additions & 39 deletions backend/src/app/api/v0/books/fetch-update/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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,
});
}
41 changes: 13 additions & 28 deletions backend/src/app/api/v0/books/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -177,18 +162,18 @@ 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),
]);
}
}
}
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,
});
}
6 changes: 1 addition & 5 deletions backend/src/app/api/v0/bookstatus/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}

Expand Down Expand Up @@ -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,
});
}
8 changes: 4 additions & 4 deletions backend/src/app/api/v0/dev_api_response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit dc65a2c

Please sign in to comment.