Skip to content

Commit

Permalink
feat(add): Adds support for adding books to the catalogue
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh committed Jul 30, 2024
1 parent 79afdd0 commit db3c2ec
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
16 changes: 15 additions & 1 deletion api/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,21 @@ fn proxy_request(_req: Request) -> Result<Response<Body>, Error> {
.header("Content-Type", "application/json")
.body(response_body.into())?);
}
// TODO: Add books
"isbn" => {
let response = client
.get(format!(
"https://openlibrary.org/search.json?title={query}&fields=key,title,isbn,cover_i,editions,editions.isbn",
query = query_params.query.replace(" ", "+")
))
.send()?;

let response_body = response.text().unwrap();

return Ok(Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "application/json")
.body(response_body.into())?);
}
_ => {
return Ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
Expand Down
22 changes: 21 additions & 1 deletion api/add/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,28 @@ async function getDataForType(type, query) {

break;
}
case "book":
case "book": {
// Return open library search
const response = await fetch(`${window.location.href}?type=${type}&query=${query}`, {
headers: { Accept: "application/json", "x-proxy-source": "isbn" },
});

const responseData = await response.json();

suggestions = responseData.docs.map(
(
/** @type {{ title: string; key: string; cover_i: number; isbn: string[], editions: {docs: { isbn: string[]}[]}[]; }} */ result,
) => {
return {
name: result.title,
id: result.editions[0]?.docs[0]?.isbn[0] ?? result.isbn?.[0] ?? "UNKNOWN",
poster_path: `http://covers.openlibrary.org/b/id/${result.cover_i}-L.jpg`,
};
},
);

break;
}
}

updateSuggestionList();
Expand Down

0 comments on commit db3c2ec

Please sign in to comment.