v0.11-updatingP1
This commit is contained in:
@@ -176,6 +176,7 @@ pub async fn main() {
|
||||
.route("/api/delete/:id", get(delete_book))
|
||||
.route("/api/list", get(list_book))
|
||||
.route("/api/create", post(create_book))
|
||||
.route("/api/update", post(update_book))
|
||||
.nest("/images", get_service(ServeDir::new("../images")).handle_error(handle_error))
|
||||
.merge(SpaRouter::new("/assets", opt.static_dir))
|
||||
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()))
|
||||
@@ -441,3 +442,133 @@ let book: book::Model = book::Model{
|
||||
|
||||
"success"
|
||||
}
|
||||
|
||||
|
||||
|
||||
async fn update_book(
|
||||
Extension(ref conn): Extension<DatabaseConnection>,
|
||||
Json(doc_sent): Json<BookUI>,
|
||||
) -> impl IntoResponse {
|
||||
println!("Updating book");
|
||||
let mut cover = doc_sent.cover.clone();
|
||||
if !doc_sent.cover.is_none() {
|
||||
let img_bytes = reqwest::get(cover.unwrap()).await.unwrap().bytes().await.unwrap();
|
||||
//.expect("Could not fetch image");
|
||||
//let img_bytes = img_resp.unwrap().bytes();
|
||||
let image = image::load_from_memory(&img_bytes).unwrap();
|
||||
let temp_cover = doc_sent.cover.clone().unwrap();
|
||||
let img_id = temp_cover.split("/").last().unwrap();
|
||||
image.save(format!("../images/{}",img_id)).expect("Failed to save image");
|
||||
cover = Some(img_id.to_string());
|
||||
}
|
||||
let book: book::Model = book::Model{
|
||||
open_library_key: doc_sent.open_library_key.to_owned(),
|
||||
title: (doc_sent.title.to_owned()),
|
||||
edition_count: doc_sent.edition_count.to_owned(),
|
||||
first_publish_year: (doc_sent.first_publish_year.to_owned()),
|
||||
median_page_count: (doc_sent.median_page_count.to_owned()),
|
||||
goodread_id: doc_sent.goodread_id.to_owned(),
|
||||
description: doc_sent.description.to_owned(),
|
||||
comments: doc_sent.comments.to_owned(),
|
||||
cover: cover.to_owned(),
|
||||
rating: doc_sent.rating.to_owned(),
|
||||
time_added: doc_sent.time_added.to_owned(),
|
||||
id: doc_sent.id.to_owned(),
|
||||
location: doc_sent.location.to_owned(),
|
||||
};
|
||||
let created_book = MutationCore::update_book_by_id(conn, book.id, book)
|
||||
.await
|
||||
.expect("could not update book");
|
||||
|
||||
MutationCore::delete_book_author(conn, doc_sent.id)
|
||||
.await
|
||||
.expect("could not delete book authors while updating");
|
||||
MutationCore::delete_book_person(conn, doc_sent.id)
|
||||
.await
|
||||
.expect("could not delete book persons while updating");
|
||||
|
||||
MutationCore::delete_book_place(conn, doc_sent.id)
|
||||
.await
|
||||
.expect("could not delete book places while updating");
|
||||
|
||||
MutationCore::delete_book_subject(conn, doc_sent.id)
|
||||
.await
|
||||
.expect("could not delete book subjects while updating");
|
||||
|
||||
MutationCore::delete_book_time(conn, doc_sent.id)
|
||||
.await
|
||||
.expect("could not delete book times while updating");
|
||||
|
||||
MutationCore::delete_book_isbn(conn, doc_sent.id)
|
||||
.await
|
||||
.expect("could not delete book isbns while updating");
|
||||
|
||||
for author_name in doc_sent.author_name.as_ref().unwrap().iter() {
|
||||
let record : book_author::Model = book_author::Model{
|
||||
id: 1,
|
||||
book_id: doc_sent.id,
|
||||
author_name: author_name.to_owned(),
|
||||
};
|
||||
MutationCore::create_book_author(conn, record)
|
||||
.await
|
||||
.expect("could not create book");
|
||||
}
|
||||
for person in doc_sent.person.as_ref().unwrap().iter() {
|
||||
let record : book_person::Model = book_person::Model{
|
||||
id: 1,
|
||||
book_id: doc_sent.id,
|
||||
person: person.to_owned(),
|
||||
};
|
||||
MutationCore::create_book_person(conn, record)
|
||||
.await
|
||||
.expect("could not create book");
|
||||
|
||||
}
|
||||
for place in doc_sent.place.as_ref().unwrap().iter() {
|
||||
let record : book_place::Model = book_place::Model{
|
||||
id: 1,
|
||||
book_id: doc_sent.id,
|
||||
place: place.to_owned(),
|
||||
};
|
||||
MutationCore::create_book_place(conn, record)
|
||||
.await
|
||||
.expect("could not create book");
|
||||
|
||||
}
|
||||
for subject in doc_sent.subject.as_ref().unwrap().iter() {
|
||||
let record : book_subject::Model = book_subject::Model{
|
||||
id: 1,
|
||||
book_id: doc_sent.id,
|
||||
subject: subject.to_owned(),
|
||||
};
|
||||
MutationCore::create_book_subject(conn, record)
|
||||
.await
|
||||
.expect("could not create book");
|
||||
|
||||
}
|
||||
for time in doc_sent.time.as_ref().unwrap().iter() {
|
||||
let record : book_time::Model = book_time::Model{
|
||||
id: 1,
|
||||
book_id: doc_sent.id,
|
||||
time: time.to_owned(),
|
||||
};
|
||||
MutationCore::create_book_time(conn, record)
|
||||
.await
|
||||
.expect("could not create book");
|
||||
|
||||
}
|
||||
for isbn in doc_sent.isbn.as_ref().unwrap().iter() {
|
||||
let record : book_isbn::Model = book_isbn::Model{
|
||||
id: 1,
|
||||
book_id: doc_sent.id,
|
||||
isbn: isbn.to_owned(),
|
||||
};
|
||||
MutationCore::create_book_isbn(conn, record)
|
||||
.await
|
||||
.expect("could not create book");
|
||||
|
||||
}
|
||||
|
||||
|
||||
"success"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user