bazzar/db-seed/src/product_photos.rs

158 lines
4.0 KiB
Rust

use actix::Addr;
use config::SharedAppConfig;
use database_manager::{query_db, Database};
use crate::{Result, SharedState};
async fn create_product_photo(
db: Addr<Database>,
seed: SharedState,
name: &'static str,
file_name: &'static str,
) -> crate::Result<()> {
let product_id = {
seed.lock()
.unwrap()
.products
.iter()
.find(|p| {
let _x = 1;
p.name.as_str() == name
})
.map(|p| p.id)
.ok_or_else(|| {
let _x = 1;
crate::Error::NoProduct(name.into())
})?
};
let photo_id = {
seed.lock()
.unwrap()
.photos
.iter()
.find(|p| p.file_name.as_str() == file_name)
.map(|p| p.id)
.ok_or_else(|| crate::Error::PhotoNotFound(file_name.into()))?
};
query_db!(
db,
database_manager::CreateProductPhoto {
product_id,
photo_id,
},
crate::Error::DbProductPhoto(product_id, photo_id)
);
Ok(())
}
pub(crate) async fn create_product_photos(
db: Addr<Database>,
seed: SharedState,
_config: SharedAppConfig,
) -> Result<()> {
let product_photos = query_db!(db, database_manager::AllProductPhotos, default vec![]);
if product_photos.len() >= 10 {
return Ok(());
}
let results = tokio::join!(
create_product_photo(
db.clone(),
seed.clone(),
"Nikon",
"pexels-alex-azabache-3907507.webp"
),
create_product_photo(
db.clone(),
seed.clone(),
"Bonoid CBD",
"pexels-binoid-cbd-3612182.webp"
),
create_product_photo(
db.clone(),
seed.clone(),
"Casio Speaker",
"pexels-caio-1279107.webp"
),
create_product_photo(
db.clone(),
seed.clone(),
"Eprism Studio",
"pexels-eprism-studio-335257.webp"
),
create_product_photo(
db.clone(),
seed.clone(),
"Best Phones 2022",
"pexels-gabriel-freytez-341523.webp"
),
create_product_photo(
db.clone(),
seed.clone(),
"Sweet cake",
"pexels-jess-bailey-designs-913135.webp"
),
create_product_photo(
db.clone(),
seed.clone(),
"Lexal 128G",
"pexels-luis-quintero-1738641.webp"
),
create_product_photo(
db.clone(),
seed.clone(),
"Fujifilm X-T10",
"pexels-math-90946.webp"
),
create_product_photo(
db.clone(),
seed.clone(),
"Sweet Tower",
"pexels-mike-380954.webp"
),
create_product_photo(
db.clone(),
seed.clone(),
"Nikon Lenses",
"pexels-pixabay-279906.webp"
),
create_product_photo(
db.clone(),
seed.clone(),
"Venus HD Professional",
"pexels-Venus-HD-Make-up-and-perfume-2587370.webp"
),
create_product_photo(
db.clone(),
seed.clone(),
"Fancy Plate",
"pexels-agnese-lunecka-10322857.webp"
),
create_product_photo(
db.clone(),
seed.clone(),
"Fancy Plate",
"pexels-agnese-lunecka-11179383.webp"
),
create_product_photo(
db.clone(),
seed.clone(),
"Fancy Plate",
"pexels-agnese-lunecka-11328773.webp"
),
);
results.0.unwrap();
results.1.unwrap();
results.2.unwrap();
results.3.unwrap();
results.4.unwrap();
results.5.unwrap();
results.6.unwrap();
results.7.unwrap();
results.8.unwrap();
results.9.unwrap();
results.10.unwrap();
Ok(())
}