basket logic working
This commit is contained in:
48
src/models/_entities/account_cart_items.rs
Normal file
48
src/models/_entities/account_cart_items.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.19
|
||||
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "account_cart_items")]
|
||||
pub struct Model {
|
||||
pub created_at: DateTimeWithTimeZone,
|
||||
pub updated_at: DateTimeWithTimeZone,
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub variant_id: i32,
|
||||
pub quantity: i32,
|
||||
pub user_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::product_variants::Entity",
|
||||
from = "Column::VariantId",
|
||||
to = "super::product_variants::Column::Id",
|
||||
on_update = "NoAction",
|
||||
on_delete = "Cascade"
|
||||
)]
|
||||
ProductVariants,
|
||||
#[sea_orm(
|
||||
belongs_to = "super::users::Entity",
|
||||
from = "Column::UserId",
|
||||
to = "super::users::Column::Id",
|
||||
on_update = "Cascade",
|
||||
on_delete = "Cascade"
|
||||
)]
|
||||
Users,
|
||||
}
|
||||
|
||||
impl Related<super::product_variants::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::ProductVariants.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::users::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Users.def()
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
pub mod prelude;
|
||||
|
||||
pub mod account_cart_items;
|
||||
pub mod account_discount_profiles;
|
||||
pub mod account_product_prices;
|
||||
pub mod account_product_resolutions;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.19
|
||||
|
||||
pub use super::account_cart_items::Entity as AccountCartItems;
|
||||
pub use super::account_discount_profiles::Entity as AccountDiscountProfiles;
|
||||
pub use super::account_product_prices::Entity as AccountProductPrices;
|
||||
pub use super::account_product_resolutions::Entity as AccountProductResolutions;
|
||||
|
||||
@@ -22,6 +22,8 @@ pub struct Model {
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(has_many = "super::account_cart_items::Entity")]
|
||||
AccountCartItems,
|
||||
#[sea_orm(has_many = "super::account_product_prices::Entity")]
|
||||
AccountProductPrices,
|
||||
#[sea_orm(has_many = "super::account_product_resolutions::Entity")]
|
||||
@@ -38,6 +40,12 @@ pub enum Relation {
|
||||
Products,
|
||||
}
|
||||
|
||||
impl Related<super::account_cart_items::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::AccountCartItems.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::account_product_prices::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::AccountProductPrices.def()
|
||||
|
||||
55
src/models/account_cart_items.rs
Normal file
55
src/models/account_cart_items.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
pub use crate::models::_entities::account_cart_items::{ActiveModel, Column, Entity, Model};
|
||||
use sea_orm::entity::prelude::*;
|
||||
use sea_orm::{ActiveValue, QueryFilter, QueryOrder};
|
||||
|
||||
pub type AccountCartItems = Entity;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl ActiveModelBehavior for ActiveModel {
|
||||
async fn before_save<C>(self, _db: &C, _insert: bool) -> std::result::Result<Self, DbErr>
|
||||
where
|
||||
C: ConnectionTrait,
|
||||
{
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub async fn find_for_user(
|
||||
db: &DatabaseConnection,
|
||||
user_id: i32,
|
||||
) -> Result<Vec<(i32, i32)>, DbErr> {
|
||||
Ok(Entity::find()
|
||||
.filter(Column::UserId.eq(user_id))
|
||||
.order_by_asc(Column::Id)
|
||||
.all(db)
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter_map(|item| (item.quantity > 0).then_some((item.variant_id, item.quantity)))
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub async fn replace_for_user(
|
||||
db: &DatabaseConnection,
|
||||
user_id: i32,
|
||||
items: &[(i32, i32)],
|
||||
) -> Result<(), DbErr> {
|
||||
Entity::delete_many()
|
||||
.filter(Column::UserId.eq(user_id))
|
||||
.exec(db)
|
||||
.await?;
|
||||
|
||||
for (variant_id, quantity) in items.iter().copied().filter(|(_, qty)| *qty > 0) {
|
||||
ActiveModel {
|
||||
user_id: ActiveValue::set(user_id),
|
||||
variant_id: ActiveValue::set(variant_id),
|
||||
quantity: ActiveValue::set(quantity),
|
||||
..Default::default()
|
||||
}
|
||||
.insert(db)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
pub mod _entities;
|
||||
|
||||
pub mod account_cart_items;
|
||||
pub mod account_discount_profiles;
|
||||
pub mod account_product_prices;
|
||||
pub mod account_product_resolutions;
|
||||
|
||||
Reference in New Issue
Block a user