feat: new book price in database and book data endpoints
This commit is contained in:
parent
3320d1400c
commit
eb3181242b
6 changed files with 28 additions and 60 deletions
|
|
@ -1,7 +1,6 @@
|
|||
use reqwest::StatusCode;
|
||||
use sea_orm::ActiveValue::{NotSet, Set};
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::entities::book;
|
||||
|
||||
|
|
@ -10,13 +9,13 @@ pub struct Response {
|
|||
#[serde(rename = "numberOfRecords")]
|
||||
records_number: u32,
|
||||
#[serde(rename = "records")]
|
||||
records: Vec<RecordListElement>
|
||||
records: RecordListElement
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct RecordListElement {
|
||||
#[serde(rename = "record")]
|
||||
record: Record
|
||||
#[serde(default, rename = "record")]
|
||||
record: Vec<Record>
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
|
@ -33,7 +32,7 @@ pub struct RecordData {
|
|||
|
||||
#[derive(Deserialize)]
|
||||
pub struct RecordDataFields {
|
||||
#[serde(rename = "datafield")]
|
||||
#[serde(default, rename = "datafield")]
|
||||
datafields: Vec<DataField>
|
||||
}
|
||||
|
||||
|
|
@ -41,7 +40,7 @@ pub struct RecordDataFields {
|
|||
pub struct DataField {
|
||||
#[serde(rename = "@tag")]
|
||||
tag: String,
|
||||
#[serde(rename = "subfield")]
|
||||
#[serde(default, rename = "subfield")]
|
||||
subfields: Vec<SubField>
|
||||
}
|
||||
|
||||
|
|
@ -57,7 +56,8 @@ pub struct SubField {
|
|||
pub struct FetchedBook {
|
||||
pub ean: String,
|
||||
pub title: String,
|
||||
pub author: String
|
||||
pub author: String,
|
||||
pub price_new: Option<String>,
|
||||
}
|
||||
|
||||
impl FetchedBook {
|
||||
|
|
@ -66,7 +66,8 @@ impl FetchedBook {
|
|||
id: NotSet,
|
||||
ean: Set(self.ean.clone()),
|
||||
title: Set(self.title.clone()),
|
||||
author: Set(self.author.clone())
|
||||
author: Set(self.author.clone()),
|
||||
price_new: Set(self.price_new.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -88,11 +89,18 @@ pub async fn fetch_book_by_ean(web_client: &reqwest::Client, ean: &String) -> Op
|
|||
log::debug!(target: "api", "BNF returned 0 records for fetch");
|
||||
return None;
|
||||
}
|
||||
let data_dubfield = v.records.first().unwrap().record_data.record.record.datafields.iter().find(|d| d.tag == "200").unwrap().subfields.clone();
|
||||
let record = &v.records.record.first().unwrap();
|
||||
let data_dubfield = record.record_data.record.datafields.iter().find(|d| d.tag == "200").unwrap().subfields.clone();
|
||||
let price_new = match record.record_data.record.datafields.iter().find(|d| d.tag == "010") {
|
||||
Some(f) => f.subfields.iter().find(|p| p.code=="d").map_or(None, |v| Some(v.value.clone())),
|
||||
None => None
|
||||
};
|
||||
|
||||
Some(FetchedBook {
|
||||
ean: ean.to_string(),
|
||||
title: data_dubfield.iter().find(|p| p.code == "a").unwrap().value.clone(),
|
||||
author: data_dubfield.iter().find(|p| p.code == "f").unwrap().value.clone(),
|
||||
price_new,
|
||||
})
|
||||
},
|
||||
Err(e) => {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,5 @@ pub mod auth;
|
|||
pub mod bnf;
|
||||
pub mod cli;
|
||||
pub mod events;
|
||||
pub mod open_library;
|
||||
pub mod serde;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,43 +0,0 @@
|
|||
use reqwest::StatusCode;
|
||||
use sea_orm::ActiveValue::{NotSet, Set};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::entities::book;
|
||||
|
||||
pub struct FetchedBook {
|
||||
pub ean: String,
|
||||
pub title: String,
|
||||
pub author: String
|
||||
}
|
||||
|
||||
impl FetchedBook {
|
||||
pub fn to_active_model(&self) -> book::ActiveModel {
|
||||
book::ActiveModel {
|
||||
id: NotSet,
|
||||
ean: Set(self.ean.clone()),
|
||||
title: Set(self.title.clone()),
|
||||
author: Set(self.author.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn fetch_book_by_ean(web_client: &reqwest::Client, ean: &String) -> Option<FetchedBook> {
|
||||
let body = web_client.execute(
|
||||
web_client.get(format!("https://openlibrary.org/isbn/{ean}.json"))
|
||||
.build()
|
||||
.expect("get request creation failed")
|
||||
).await.unwrap();
|
||||
match body.status() {
|
||||
StatusCode::OK => {
|
||||
let res = body.text().await.unwrap();
|
||||
log::trace!(target: "api", "OpenLibrary book fetch result: {res:#?}");
|
||||
let v: Value = serde_json::from_str(&res).unwrap();
|
||||
Some(FetchedBook {
|
||||
ean: ean.to_string(),
|
||||
title: v.get("title").unwrap().to_string(),
|
||||
author: "temp".to_owned()
|
||||
})
|
||||
},
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
Reference in a new issue