v0.10-frontend-compiles2

This commit is contained in:
2022-10-31 00:24:15 +05:30
parent fe40782c0e
commit bc58e586f7
3 changed files with 45 additions and 39 deletions

View File

@@ -12,7 +12,7 @@ use std::collections::HashMap;
use std::net::{IpAddr, Ipv6Addr, SocketAddr}; use std::net::{IpAddr, Ipv6Addr, SocketAddr};
use std::str::FromStr; use std::str::FromStr;
use tower::ServiceBuilder; use tower::ServiceBuilder;
use tower_http::cors::CorsLayer; use tower_http::cors::{Any,CorsLayer};
use tower_http::trace::TraceLayer; use tower_http::trace::TraceLayer;
use booksman_orm::{ use booksman_orm::{
sea_orm::{Database, DatabaseConnection}, sea_orm::{Database, DatabaseConnection},
@@ -144,8 +144,9 @@ pub async fn main() {
// it is required to add ".allow_headers([http::header::CONTENT_TYPE])" // it is required to add ".allow_headers([http::header::CONTENT_TYPE])"
// or see this issue https://github.com/tokio-rs/axum/issues/849 // or see this issue https://github.com/tokio-rs/axum/issues/849
CorsLayer::new() CorsLayer::new()
.allow_origin("http://localhost:8080".parse::<HeaderValue>().unwrap()) .allow_methods([Method::GET, Method::POST])
.allow_methods([Method::GET, Method::POST]), .allow_origin("http://localhost:8080".parse::<HeaderValue>().unwrap())
.allow_headers(Any),
); );
let sock_addr = SocketAddr::from(( let sock_addr = SocketAddr::from((
@@ -283,6 +284,7 @@ async fn create_book(
Extension(ref conn): Extension<DatabaseConnection>, Extension(ref conn): Extension<DatabaseConnection>,
doc_sent : Form<BookUI>, doc_sent : Form<BookUI>,
) -> impl IntoResponse { ) -> impl IntoResponse {
println!("Creating book");
let book: book::Model = book::Model{ let book: book::Model = book::Model{
open_library_key: doc_sent.open_library_key.to_owned(), open_library_key: doc_sent.open_library_key.to_owned(),
title: (doc_sent.title.to_owned()), title: (doc_sent.title.to_owned()),

View File

@@ -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}; //, 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::{book,book_author,book_person,book_place,book_subject,book_time,book_isbn};
//use ::entity::entities::{prelude::*, *}; //use ::entity::entities::{prelude::*, *};
@@ -72,7 +72,7 @@ pub async fn find_books_plus_meta_in_page(
Condition::any() Condition::any()
.add(book::Column::Id.is_in(book_ids)) .add(book::Column::Id.is_in(book_ids))
).find_with_related(Author).all(db).await?;*/ ).find_with_related(Author).all(db).await?;*/
let paginator2 = Book::find() let paginator2: Vec<BookAndMeta> = Book::find()
.filter(Condition::any() .filter(Condition::any()
.add(book::Column::Id.is_in(book_ids))) .add(book::Column::Id.is_in(book_ids)))
.join(JoinType::LeftJoin, book_author::Relation::Book.def()) .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_subject::Relation::Book.def())
.join(JoinType::LeftJoin, book_time::Relation::Book.def()) .join(JoinType::LeftJoin, book_time::Relation::Book.def())
.join(JoinType::LeftJoin, book_isbn::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::<BookAndMeta>() .into_model::<BookAndMeta>()
.all(db).await?; .all(db).await?;

View File

@@ -65,16 +65,16 @@ async fn fetch_books(search: String) -> Result<Vec<BookUI>, reqwasm::Error> {
Ok(body) Ok(body)
} }
async fn add_book(record: BookUI) -> Result<String, reqwasm::Error> { async fn add_book(record: BookUI) -> Result<reqwasm::http::Response, reqwasm::Error> {
let url = format!("http://localhost:8081/api/create"); let url = format!("http://localhost:8081/api/create");
let _resp = Request::post(&url).body(serde_wasm_bindgen::to_value(&record).unwrap()).send().await?; let resp = Request::post(&url).body(serde_wasm_bindgen::to_value(&record).unwrap()).header("content-type","application/x-www-form-urlencoded").send().await?;
Ok("OK".to_string()) Ok(resp)
} }
async fn delete_book(id: i32) -> Result<String, reqwasm::Error> { async fn delete_book(id: i32) -> Result<reqwasm::http::Response, reqwasm::Error> {
let url = format!("http://localhost:8081/api/delete/{}", id); let url = format!("http://localhost:8081/api/delete/{}", id);
let _resp = Request::post(&url).send().await?; let resp = Request::post(&url).send().await?;
Ok("OK".to_string()) Ok(resp)
} }
async fn list_books() -> Result<Vec<BookUI>, reqwasm::Error> { async fn list_books() -> Result<Vec<BookUI>, reqwasm::Error> {
@@ -175,7 +175,8 @@ pub fn BookDB<G: Html>(cx: Scope, bookitem: BookUIProp) -> View<G> {
let book = bookitem.bookitem.clone(); let book = bookitem.bookitem.clone();
let handle_delete = move |_| { let handle_delete = move |_| {
spawn_local(async 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<G: Html>(cx: Scope<'_>) -> View<G> {
let app_state = app_state.clone(); let app_state = app_state.clone();
app_state.search.track(); app_state.search.track();
spawn_local(async move { spawn_local(async move {
if *app_state.search.get() != "" {
app_state.books.set( app_state.books.set(
fetch_books(app_state.search.get().to_string()) fetch_books(app_state.search.get().to_string())
.await .await
.unwrap(), .unwrap(),
) )
}
}); });
}); });
@@ -271,6 +274,7 @@ let inp_time = create_signal(cx, String::new());
let inp_isbn = create_signal(cx, String::new()); let inp_isbn = create_signal(cx, String::new());
let handle_add = |_| { let handle_add = |_| {
info!("Adding book");
let authors: Vec<String> = (*inp_author.get()).clone().split(",").map(str::to_string).collect::<Vec<String>>(); let authors: Vec<String> = (*inp_author.get()).clone().split(",").map(str::to_string).collect::<Vec<String>>();
let persons: Vec<String> = (*inp_person.get()).clone().split(",").map(str::to_string).collect::<Vec<String>>(); let persons: Vec<String> = (*inp_person.get()).clone().split(",").map(str::to_string).collect::<Vec<String>>();
let places: Vec<String> = (*inp_place.get()).clone().split(",").map(|x| x.to_string()).collect::<Vec<String>>(); let places: Vec<String> = (*inp_place.get()).clone().split(",").map(|x| x.to_string()).collect::<Vec<String>>();
@@ -281,15 +285,15 @@ let inp_isbn = create_signal(cx, String::new());
id: 1, id: 1,
title: (*inp_title.get()).clone(), title: (*inp_title.get()).clone(),
open_library_key: Some((*inp_olkey.get()).clone()), open_library_key: Some((*inp_olkey.get()).clone()),
edition_count: Some(inp_editioncount.get().parse::<i32>().unwrap()), edition_count: Some(inp_editioncount.get().parse::<i32>().unwrap_or(0)),
first_publish_year: Some(inp_publishyear.get().parse::<i32>().unwrap()), first_publish_year: Some(inp_publishyear.get().parse::<i32>().unwrap_or(0)),
median_page_count: Some(inp_medianpage.get().parse::<i32>().unwrap()), median_page_count: Some(inp_medianpage.get().parse::<i32>().unwrap_or(0)),
goodread_id: Some((*inp_goodread.get()).clone()), goodread_id: Some((*inp_goodread.get()).clone()),
description: Some((*inp_desc.get()).clone()), description: Some((*inp_desc.get()).clone()),
cover: Some((*inp_cover.get()).clone()), cover: Some((*inp_cover.get()).clone()),
location: Some((*inp_location.get()).clone()), location: Some((*inp_location.get()).clone()),
time_added: Some("NA".to_string()), time_added: Some("NA".to_string()),
rating: Some(inp_rating.get().parse::<i32>().unwrap()), rating: Some(inp_rating.get().parse::<i32>().unwrap_or(0)),
comments: Some((*inp_comments.get()).clone()), comments: Some((*inp_comments.get()).clone()),
author_name: Some(authors), author_name: Some(authors),
person: Some(persons), person: Some(persons),
@@ -298,37 +302,37 @@ let inp_isbn = create_signal(cx, String::new());
time: Some(times), time: Some(times),
isbn: Some(isbns), isbn: Some(isbns),
}; };
spawn_local(async move {
add_book(record).await.unwrap();
});
app_state.adding.set(false); app_state.adding.set(false);
app_state.addingbook.set(BookUI::default()); app_state.addingbook.set(BookUI::default());
spawn_local(async move {
let temp = add_book(record).await.unwrap();
println!("{}",temp.status());
});
}; };
view! {cx, view! {cx,
p { p {
(if *app_state.adding.get() == true { (if *app_state.adding.get() == true {
view!{ cx, view!{ cx,
"Hello" input(bind:value=inp_title, placeholder="Title" ,value = (*app_state.addingbook.get()).clone().title)
input(bind:value=inp_title, placeholder= (*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_olkey, placeholder= (*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_editioncount, placeholder= (*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_publishyear, placeholder= (*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_medianpage, placeholder= (*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_goodread, placeholder= (*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_desc, placeholder= (*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_cover, placeholder= (*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_location, placeholder= (*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_rating, placeholder= (*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_comments, placeholder= (*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_author, placeholder=(*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_person, placeholder=(*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_place, placeholder=(*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_subject, placeholder=(*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_time, placeholder=(*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(", "))
input(bind:value=inp_isbn, placeholder=(*app_state.addingbook.get()).clone().isbn.unwrap_or(vec!["".to_string()]).join(", "))
button(class="add", on:click=handle_add){ "Add book to DB" } button(class="add", on:click=handle_add){ "Add book to DB" }
} }
} else { } else {