Last active: 10 months ago
implement model in rust
use crate::{
access_db,
api::{parse_booth_url, types::BoothExamples},
};
use anyhow::Result;
use sqlx::{Postgres, QueryBuilder};
pub struct BoothUrls(Vec<String>);
impl BoothUrls {
/// Record sucess post's url
pub async fn record_post(&self) -> Result<()> {
let pool = access_db().await;
let mut query_builder: QueryBuilder<Postgres> =
QueryBuilder::new("INSERT INTO post_record (url) ");
query_builder.push_values(&self.0, |mut b, url| {
b.push_bind(url);
});
let query = query_builder.build();
query.execute(pool).await?;
Ok(())
}
}
impl TryFrom<BoothExamples> for BoothUrls {
type Error = anyhow::Error;
fn try_from(value: BoothExamples) -> Result<Self, Self::Error> {
value
.data
.list
.data
.iter()
.map(parse_booth_url)
.collect::<anyhow::Result<Vec<_>>>()
.map(BoothUrls)
}
}