diff --git a/backend/api/src/lib.rs b/backend/api/src/lib.rs index 9b15187..2036f17 100644 --- a/backend/api/src/lib.rs +++ b/backend/api/src/lib.rs @@ -12,7 +12,7 @@ use std::collections::HashMap; use std::net::{IpAddr, Ipv6Addr, SocketAddr}; use std::str::FromStr; use tower::ServiceBuilder; -use tower_http::cors::CorsLayer; +use tower_http::cors::{Any,CorsLayer}; use tower_http::trace::TraceLayer; use booksman_orm::{ sea_orm::{Database, DatabaseConnection}, @@ -144,8 +144,9 @@ pub async fn main() { // it is required to add ".allow_headers([http::header::CONTENT_TYPE])" // or see this issue https://github.com/tokio-rs/axum/issues/849 CorsLayer::new() - .allow_origin("http://localhost:8080".parse::().unwrap()) - .allow_methods([Method::GET, Method::POST]), + .allow_methods([Method::GET, Method::POST]) + .allow_origin("http://localhost:8080".parse::().unwrap()) + .allow_headers(Any), ); let sock_addr = SocketAddr::from(( @@ -283,6 +284,7 @@ async fn create_book( Extension(ref conn): Extension, doc_sent : Form, ) -> impl IntoResponse { +println!("Creating book"); let book: book::Model = book::Model{ open_library_key: doc_sent.open_library_key.to_owned(), title: (doc_sent.title.to_owned()), diff --git a/backend/orm/src/query.rs b/backend/orm/src/query.rs index d5312e0..a7fb6c0 100644 --- a/backend/orm/src/query.rs +++ b/backend/orm/src/query.rs @@ -1,4 +1,4 @@ -use ::entity::entities::{book::Entity as Book}; +use ::entity::entities::book::Entity as Book; //, book_author::Entity as Author, book_person::Entity as Person, book_place::Entity as Place, book_subject::Entity as Subject, book_time::Entity as Time, book_isbn::Entity as ISBN}; use ::entity::entities::{book,book_author,book_person,book_place,book_subject,book_time,book_isbn}; //use ::entity::entities::{prelude::*, *}; @@ -72,7 +72,7 @@ pub async fn find_books_plus_meta_in_page( Condition::any() .add(book::Column::Id.is_in(book_ids)) ).find_with_related(Author).all(db).await?;*/ - let paginator2 = Book::find() + let paginator2: Vec = Book::find() .filter(Condition::any() .add(book::Column::Id.is_in(book_ids))) .join(JoinType::LeftJoin, book_author::Relation::Book.def()) @@ -81,7 +81,7 @@ pub async fn find_books_plus_meta_in_page( .join(JoinType::LeftJoin, book_subject::Relation::Book.def()) .join(JoinType::LeftJoin, book_time::Relation::Book.def()) .join(JoinType::LeftJoin, book_isbn::Relation::Book.def()) - .column_as(book::Column::Id, "id") + // .column_as(book::Column::Id, "id") .into_model::() .all(db).await?; diff --git a/frontend/src/main.rs b/frontend/src/main.rs index a442c0b..b463f9c 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -65,16 +65,16 @@ async fn fetch_books(search: String) -> Result, reqwasm::Error> { Ok(body) } -async fn add_book(record: BookUI) -> Result { +async fn add_book(record: BookUI) -> Result { let url = format!("http://localhost:8081/api/create"); - let _resp = Request::post(&url).body(serde_wasm_bindgen::to_value(&record).unwrap()).send().await?; - Ok("OK".to_string()) + let resp = Request::post(&url).body(serde_wasm_bindgen::to_value(&record).unwrap()).header("content-type","application/x-www-form-urlencoded").send().await?; + Ok(resp) } -async fn delete_book(id: i32) -> Result { +async fn delete_book(id: i32) -> Result { let url = format!("http://localhost:8081/api/delete/{}", id); - let _resp = Request::post(&url).send().await?; - Ok("OK".to_string()) + let resp = Request::post(&url).send().await?; + Ok(resp) } async fn list_books() -> Result, reqwasm::Error> { @@ -175,7 +175,8 @@ pub fn BookDB(cx: Scope, bookitem: BookUIProp) -> View { let book = bookitem.bookitem.clone(); let handle_delete = move |_| { spawn_local(async move { - delete_book(book.id).await.unwrap(); + let temp = delete_book(book.id).await.unwrap(); + println!("{}",temp.status()); }); }; @@ -198,11 +199,13 @@ async fn ListOL(cx: Scope<'_>) -> View { let app_state = app_state.clone(); app_state.search.track(); spawn_local(async move { + if *app_state.search.get() != "" { app_state.books.set( fetch_books(app_state.search.get().to_string()) .await .unwrap(), ) + } }); }); @@ -271,6 +274,7 @@ let inp_time = create_signal(cx, String::new()); let inp_isbn = create_signal(cx, String::new()); let handle_add = |_| { +info!("Adding book"); let authors: Vec = (*inp_author.get()).clone().split(",").map(str::to_string).collect::>(); let persons: Vec = (*inp_person.get()).clone().split(",").map(str::to_string).collect::>(); let places: Vec = (*inp_place.get()).clone().split(",").map(|x| x.to_string()).collect::>(); @@ -281,15 +285,15 @@ let inp_isbn = create_signal(cx, String::new()); id: 1, title: (*inp_title.get()).clone(), open_library_key: Some((*inp_olkey.get()).clone()), - edition_count: Some(inp_editioncount.get().parse::().unwrap()), - first_publish_year: Some(inp_publishyear.get().parse::().unwrap()), - median_page_count: Some(inp_medianpage.get().parse::().unwrap()), + edition_count: Some(inp_editioncount.get().parse::().unwrap_or(0)), + first_publish_year: Some(inp_publishyear.get().parse::().unwrap_or(0)), + median_page_count: Some(inp_medianpage.get().parse::().unwrap_or(0)), goodread_id: Some((*inp_goodread.get()).clone()), description: Some((*inp_desc.get()).clone()), cover: Some((*inp_cover.get()).clone()), location: Some((*inp_location.get()).clone()), time_added: Some("NA".to_string()), - rating: Some(inp_rating.get().parse::().unwrap()), + rating: Some(inp_rating.get().parse::().unwrap_or(0)), comments: Some((*inp_comments.get()).clone()), author_name: Some(authors), person: Some(persons), @@ -298,37 +302,37 @@ let inp_isbn = create_signal(cx, String::new()); time: Some(times), isbn: Some(isbns), }; - spawn_local(async move { - add_book(record).await.unwrap(); - }); - app_state.adding.set(false); app_state.addingbook.set(BookUI::default()); + spawn_local(async move { + let temp = add_book(record).await.unwrap(); + println!("{}",temp.status()); + }); + }; view! {cx, p { (if *app_state.adding.get() == true { view!{ cx, - "Hello" - input(bind:value=inp_title, placeholder= (*app_state.addingbook.get()).clone().title) - input(bind:value=inp_olkey, placeholder= (*app_state.addingbook.get()).clone().open_library_key.unwrap_or("".to_string())) - input(bind:value=inp_editioncount, placeholder= (*app_state.addingbook.get()).clone().edition_count.unwrap_or(0).to_string()) - input(bind:value=inp_publishyear, placeholder= (*app_state.addingbook.get()).clone().first_publish_year.unwrap_or(0).to_string()) - input(bind:value=inp_medianpage, placeholder= (*app_state.addingbook.get()).clone().median_page_count.unwrap_or(0).to_string()) - input(bind:value=inp_goodread, placeholder= (*app_state.addingbook.get()).clone().goodread_id.unwrap_or("".to_string())) - input(bind:value=inp_desc, placeholder= (*app_state.addingbook.get()).clone().description.unwrap_or("".to_string())) - input(bind:value=inp_cover, placeholder= (*app_state.addingbook.get()).clone().cover.unwrap_or("".to_string())) - input(bind:value=inp_location, placeholder= (*app_state.addingbook.get()).clone().location.unwrap_or("".to_string())) - input(bind:value=inp_rating, placeholder= (*app_state.addingbook.get()).clone().rating.unwrap_or(0).to_string()) - input(bind:value=inp_comments, placeholder= (*app_state.addingbook.get()).clone().comments.unwrap_or("".to_string())) - input(bind:value=inp_author, placeholder=(*app_state.addingbook.get()).clone().author_name.unwrap_or(vec!["".to_string()]).join(", ")) - input(bind:value=inp_person, placeholder=(*app_state.addingbook.get()).clone().person.unwrap_or(vec!["".to_string()]).join(", ")) - input(bind:value=inp_place, placeholder=(*app_state.addingbook.get()).clone().place.unwrap_or(vec!["".to_string()]).join(", ")) - input(bind:value=inp_subject, placeholder=(*app_state.addingbook.get()).clone().subject.unwrap_or(vec!["".to_string()]).join(", ")) - input(bind:value=inp_time, placeholder=(*app_state.addingbook.get()).clone().time.unwrap_or(vec!["".to_string()]).join(", ")) - input(bind:value=inp_isbn, placeholder=(*app_state.addingbook.get()).clone().isbn.unwrap_or(vec!["".to_string()]).join(", ")) + input(bind:value=inp_title, placeholder="Title" ,value = (*app_state.addingbook.get()).clone().title) + input(bind:value=inp_olkey, placeholder="OpenLibrary Key" ,value= (*app_state.addingbook.get()).clone().open_library_key.unwrap_or("".to_string())) + input(bind:value=inp_editioncount, placeholder="Number of editions" ,value= (*app_state.addingbook.get()).clone().edition_count.unwrap_or(0).to_string()) + input(bind:value=inp_publishyear, placeholder="First publish year" ,value= (*app_state.addingbook.get()).clone().first_publish_year.unwrap_or(0).to_string()) + input(bind:value=inp_medianpage, placeholder="Page count" ,value= (*app_state.addingbook.get()).clone().median_page_count.unwrap_or(0).to_string()) + input(bind:value=inp_goodread, placeholder="GoodRead ID" ,value= (*app_state.addingbook.get()).clone().goodread_id.unwrap_or("".to_string())) + input(bind:value=inp_desc, placeholder="Description" ,value= (*app_state.addingbook.get()).clone().description.unwrap_or("".to_string())) + input(bind:value=inp_cover, placeholder="Cover URL" ,value= (*app_state.addingbook.get()).clone().cover.unwrap_or("".to_string())) + input(bind:value=inp_location, placeholder="Location" ,value= (*app_state.addingbook.get()).clone().location.unwrap_or("".to_string())) + input(bind:value=inp_rating, placeholder="Rating (/10)" ,value= (*app_state.addingbook.get()).clone().rating.unwrap_or(0).to_string()) + input(bind:value=inp_comments, placeholder="Comments" ,value= (*app_state.addingbook.get()).clone().comments.unwrap_or("".to_string())) + input(bind:value=inp_author, placeholder="Authors" ,value=(*app_state.addingbook.get()).clone().author_name.unwrap_or(vec!["".to_string()]).join(", ")) + input(bind:value=inp_person, placeholder="Persons" ,value=(*app_state.addingbook.get()).clone().person.unwrap_or(vec!["".to_string()]).join(", ")) + input(bind:value=inp_place, placeholder="Places" ,value=(*app_state.addingbook.get()).clone().place.unwrap_or(vec!["".to_string()]).join(", ")) + input(bind:value=inp_subject, placeholder="Subjects" ,value=(*app_state.addingbook.get()).clone().subject.unwrap_or(vec!["".to_string()]).join(", ")) + input(bind:value=inp_time, placeholder="Times" ,value=(*app_state.addingbook.get()).clone().time.unwrap_or(vec!["".to_string()]).join(", ")) + input(bind:value=inp_isbn, placeholder="ISBNs" ,value=(*app_state.addingbook.get()).clone().isbn.unwrap_or(vec!["".to_string()]).join(", ")) button(class="add", on:click=handle_add){ "Add book to DB" } } } else {