admin panel have more control over payment now
This commit is contained in:
@@ -15,10 +15,12 @@ pub mod discount_profiles;
|
||||
pub mod o_auth2_sessions;
|
||||
pub mod order_items;
|
||||
pub mod orders;
|
||||
pub mod payment_methods;
|
||||
pub mod product_images;
|
||||
pub mod product_product_tags;
|
||||
pub mod product_tags;
|
||||
pub mod product_variants;
|
||||
pub mod products;
|
||||
pub mod shipping_methods;
|
||||
pub mod shop_settings;
|
||||
pub mod users;
|
||||
|
||||
21
src/models/_entities/payment_methods.rs
Normal file
21
src/models/_entities/payment_methods.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
//! `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 = "payment_methods")]
|
||||
pub struct Model {
|
||||
pub created_at: DateTimeWithTimeZone,
|
||||
pub updated_at: DateTimeWithTimeZone,
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
#[sea_orm(unique)]
|
||||
pub code: String,
|
||||
pub name: String,
|
||||
pub enabled: bool,
|
||||
pub position: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
@@ -13,10 +13,12 @@ pub use super::discount_profiles::Entity as DiscountProfiles;
|
||||
pub use super::o_auth2_sessions::Entity as OAuth2Sessions;
|
||||
pub use super::order_items::Entity as OrderItems;
|
||||
pub use super::orders::Entity as Orders;
|
||||
pub use super::payment_methods::Entity as PaymentMethods;
|
||||
pub use super::product_images::Entity as ProductImages;
|
||||
pub use super::product_product_tags::Entity as ProductProductTags;
|
||||
pub use super::product_tags::Entity as ProductTags;
|
||||
pub use super::product_variants::Entity as ProductVariants;
|
||||
pub use super::products::Entity as Products;
|
||||
pub use super::shipping_methods::Entity as ShippingMethods;
|
||||
pub use super::shop_settings::Entity as ShopSettings;
|
||||
pub use super::users::Entity as Users;
|
||||
|
||||
20
src/models/_entities/shop_settings.rs
Normal file
20
src/models/_entities/shop_settings.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
//! `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 = "shop_settings")]
|
||||
pub struct Model {
|
||||
pub created_at: DateTimeWithTimeZone,
|
||||
pub updated_at: DateTimeWithTimeZone,
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
#[sea_orm(unique)]
|
||||
pub key: String,
|
||||
#[sea_orm(column_type = "Text", nullable)]
|
||||
pub value: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
@@ -19,10 +19,12 @@ pub mod customer_profiles;
|
||||
pub mod o_auth2_sessions;
|
||||
pub mod order_items;
|
||||
pub mod orders;
|
||||
pub mod payment_methods;
|
||||
pub mod product_images;
|
||||
pub mod product_product_tags;
|
||||
pub mod product_tags;
|
||||
pub mod products;
|
||||
pub mod shipping_methods;
|
||||
pub mod shop_settings;
|
||||
pub mod users;
|
||||
pub mod product_variants;
|
||||
|
||||
54
src/models/payment_methods.rs
Normal file
54
src/models/payment_methods.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use sea_orm::{ActiveValue, ColumnTrait, EntityTrait, QueryFilter, QueryOrder};
|
||||
|
||||
pub use crate::models::_entities::payment_methods::{ActiveModel, Column, Entity, Model};
|
||||
pub type PaymentMethods = Entity;
|
||||
|
||||
pub const COD: &str = "cod";
|
||||
pub const BANK_TRANSFER: &str = "bank_transfer";
|
||||
|
||||
#[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,
|
||||
{
|
||||
if !insert && self.updated_at.is_unchanged() {
|
||||
let mut this = self;
|
||||
this.updated_at = ActiveValue::set(chrono::Utc::now().into());
|
||||
Ok(this)
|
||||
} else {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Entity {
|
||||
pub async fn enabled<C: ConnectionTrait>(db: &C) -> Result<Vec<Model>, DbErr> {
|
||||
Entity::find()
|
||||
.filter(Column::Enabled.eq(true))
|
||||
.order_by_asc(Column::Position)
|
||||
.all(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn find_enabled<C: ConnectionTrait>(db: &C, code: &str) -> Result<Option<Model>, DbErr> {
|
||||
Entity::find()
|
||||
.filter(Column::Code.eq(code))
|
||||
.filter(Column::Enabled.eq(true))
|
||||
.one(db)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub fn label_key(&self) -> &'static str {
|
||||
match self.code.as_str() {
|
||||
COD => "payment-cod",
|
||||
BANK_TRANSFER => "payment-bank",
|
||||
_ => "payment-custom",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModel {}
|
||||
47
src/models/shop_settings.rs
Normal file
47
src/models/shop_settings.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use sea_orm::{ActiveValue, ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter, TryIntoModel};
|
||||
|
||||
pub use crate::models::_entities::shop_settings::{ActiveModel, Column, Entity, Model};
|
||||
pub type ShopSettings = 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,
|
||||
{
|
||||
if !insert && self.updated_at.is_unchanged() {
|
||||
let mut this = self;
|
||||
this.updated_at = ActiveValue::set(chrono::Utc::now().into());
|
||||
Ok(this)
|
||||
} else {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Entity {
|
||||
pub async fn get<C: ConnectionTrait>(db: &C, key: &str) -> Result<Option<String>, DbErr> {
|
||||
Ok(Entity::find()
|
||||
.filter(Column::Key.eq(key))
|
||||
.one(db)
|
||||
.await?
|
||||
.and_then(|setting| setting.value))
|
||||
}
|
||||
|
||||
pub async fn set<C: ConnectionTrait>(db: &C, key: &str, value: Option<String>) -> Result<Model, DbErr> {
|
||||
let mut active = match Entity::find()
|
||||
.filter(Column::Key.eq(key))
|
||||
.one(db)
|
||||
.await?
|
||||
{
|
||||
Some(existing) => existing.into_active_model(),
|
||||
None => ActiveModel {
|
||||
key: ActiveValue::set(key.to_string()),
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
active.value = ActiveValue::set(value);
|
||||
active.save(db).await?.try_into_model()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user