more webshop
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
pub mod admin_seeder;
|
||||
pub mod shipping_seeder;
|
||||
pub mod view_engine;
|
||||
|
||||
48
src/initializers/shipping_seeder.rs
Normal file
48
src/initializers/shipping_seeder.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use async_trait::async_trait;
|
||||
use loco_rs::prelude::*;
|
||||
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter, Set};
|
||||
|
||||
use crate::models::_entities::shipping_methods;
|
||||
|
||||
/// (code, display name, price in cents, requires a pickup point)
|
||||
const CARRIERS: [(&str, &str, i64, bool); 3] = [
|
||||
("packeta", "Packeta", 300, true),
|
||||
("dpd", "DPD", 450, false),
|
||||
("dhl", "DHL", 500, false),
|
||||
];
|
||||
|
||||
pub struct ShippingSeeder;
|
||||
|
||||
#[async_trait]
|
||||
impl Initializer for ShippingSeeder {
|
||||
fn name(&self) -> String {
|
||||
"shipping-seeder".to_string()
|
||||
}
|
||||
|
||||
async fn before_run(&self, ctx: &AppContext) -> Result<()> {
|
||||
for (position, (code, name, price_cents, requires_pickup_point)) in
|
||||
CARRIERS.iter().enumerate()
|
||||
{
|
||||
let exists = shipping_methods::Entity::find()
|
||||
.filter(shipping_methods::Column::Code.eq(*code))
|
||||
.one(&ctx.db)
|
||||
.await?
|
||||
.is_some();
|
||||
if exists {
|
||||
continue;
|
||||
}
|
||||
shipping_methods::ActiveModel {
|
||||
code: Set((*code).to_string()),
|
||||
name: Set((*name).to_string()),
|
||||
price_cents: Set(*price_cents),
|
||||
requires_pickup_point: Set(*requires_pickup_point),
|
||||
enabled: Set(true),
|
||||
position: Set(position as i32),
|
||||
..Default::default()
|
||||
}
|
||||
.insert(&ctx.db)
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user