This commit is contained in:
2022-10-24 21:27:51 +05:30
parent 2143923ee8
commit 5bd0b4975a
4 changed files with 229 additions and 35 deletions

View File

@@ -1,8 +1,9 @@
use axum::{
http::{HeaderValue, Method},
response::IntoResponse,
routing::get,
routing::{get,post},
Json, Router,
extract::{Extension, Form, Path, Query},
};
use axum_extra::routing::SpaRouter;
use clap::Parser;
@@ -13,6 +14,11 @@ use std::str::FromStr;
use tower::ServiceBuilder;
use tower_http::cors::CorsLayer;
use tower_http::trace::TraceLayer;
use booksman_orm::{
sea_orm::{Database, DatabaseConnection},
Mutation as MutationCore, Query as QueryCore,
};
use ::entity::entities::{book,book_author,book_person,book_place,book_subject,book_time,book_isbn};
#[derive(Deserialize, Serialize, Debug)]
struct Docs {
@@ -29,21 +35,7 @@ struct Docs {
subject: Option<Vec<String>>,
time: Option<Vec<String>>,
}
/*
impl Docs {
fn set_cover_url(&mut self) {
match self.cover_i {
Some(cover_i) => {
self.cover_url = Some(format!(
"https://covers.openlibrary.org/b/id/{}-L.jpg",
(cover_i.unwrap())
))
}
None => (),
}
}
}
*/
#[derive(Deserialize, Serialize, Debug)]
struct Books {
num_found: u32,
@@ -99,7 +91,10 @@ pub async fn main() {
tracing_subscriber::fmt::init();
let app = Router::new()
.route("/api/hello", get(hello))
.route("/api/search_openlibrary", get(search_openlibrary))
.route("/api/delete/:id", post(delete_book))
.route("/api/list", post(list_book))
.route("/api/create", post(create_book))
.merge(SpaRouter::new("/assets", opt.static_dir))
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()))
.layer(
@@ -111,7 +106,7 @@ pub async fn main() {
// or see this issue https://github.com/tokio-rs/axum/issues/849
CorsLayer::new()
.allow_origin("http://localhost:8080".parse::<HeaderValue>().unwrap())
.allow_methods([Method::GET]),
.allow_methods([Method::GET, Method::POST]),
);
let sock_addr = SocketAddr::from((
@@ -127,7 +122,7 @@ pub async fn main() {
.expect("Unable to start server");
}
async fn hello(
async fn search_openlibrary(
axum::extract::Query(params): axum::extract::Query<HashMap<String, String>>,
) -> impl IntoResponse {
print!("Get items with query params: {:?}\n", params);
@@ -140,3 +135,49 @@ async fn hello(
print!("Search token {:?}\n", search);
return Json(resjson);
}
async fn delete_book(
Extension(ref conn): Extension<DatabaseConnection>,
Path(id): Path<i32>,
) -> impl IntoResponse {
MutationCore::delete_book(conn, id)
.await
.expect("could not delete book");
"success"
}
async fn list_book(
Extension(ref conn): Extension<DatabaseConnection>,
) -> impl IntoResponse {
let books = QueryCore::find_books_in_page(conn,1,5)
.await
.expect("could not list books");
"success"
}
async fn create_book(
Extension(ref conn): Extension<DatabaseConnection>,
doc_sent : Form<Docs>,
) -> impl IntoResponse {
let book: book::Model = book::Model{
open_library_key: "NONE".to_string(),
title: (doc_sent.title.to_owned()),
edition_count: 1,
first_publish_year: 1,//(doc_sent.first_publish_year.unwrap().to_owned()),
median_page_count: 1, //(doc_sent.number_of_pages_median.unwrap().to_owned()),
goodread_id: "NONE".to_string(),
description: "NONE".to_string(),
comments: "NONE".to_string(),
cover: "NONE".to_string(),
rating: 1,
time_added: "NONE".to_string(),
id: 1,
location: "NONE".to_string(),
};
let created_book = MutationCore::create_book(conn, book)
.await
.expect("could not create book");
"success"
}