From 30b3abe7d8ad0c8d8c7b6a1071e0803ed6309ba5 Mon Sep 17 00:00:00 2001 From: yenicelik Date: Sun, 5 Feb 2023 14:26:46 +0100 Subject: [PATCH] added entities for credits, referrals and rpc requests --- entities/src/credits.rs | 33 ++++++++ entities/src/mod.rs | 3 + entities/src/prelude.rs | 3 + entities/src/referral.rs | 43 +++++++++++ entities/src/rpc_request.rs | 33 ++++++++ migration/src/lib.rs | 6 ++ .../src/m20230205_130035_create_credits.rs | 66 ++++++++++++++++ .../src/m20230205_133204_create_requests.rs | 70 +++++++++++++++++ .../src/m20230205_133755_create_referrals.rs | 77 +++++++++++++++++++ 9 files changed, 334 insertions(+) create mode 100644 entities/src/credits.rs create mode 100644 entities/src/referral.rs create mode 100644 entities/src/rpc_request.rs create mode 100644 migration/src/m20230205_130035_create_credits.rs create mode 100644 migration/src/m20230205_133204_create_requests.rs create mode 100644 migration/src/m20230205_133755_create_referrals.rs diff --git a/entities/src/credits.rs b/entities/src/credits.rs new file mode 100644 index 00000000..0cd1c772 --- /dev/null +++ b/entities/src/credits.rs @@ -0,0 +1,33 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "credits")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub credits: u64, + #[sea_orm(unique)] + pub user_id: u64, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::user::Entity", + from = "Column::UserId", + to = "super::user::Column::Id", + on_update = "NoAction", + on_delete = "NoAction" + )] + User, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::User.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/entities/src/mod.rs b/entities/src/mod.rs index 5325e16d..4b97c952 100644 --- a/entities/src/mod.rs +++ b/entities/src/mod.rs @@ -2,11 +2,14 @@ pub mod prelude; +pub mod credits; pub mod login; pub mod pending_login; +pub mod referral; pub mod revert_log; pub mod rpc_accounting; pub mod rpc_key; +pub mod rpc_request; pub mod sea_orm_active_enums; pub mod secondary_user; pub mod serialization; diff --git a/entities/src/prelude.rs b/entities/src/prelude.rs index 218a3d47..38337598 100644 --- a/entities/src/prelude.rs +++ b/entities/src/prelude.rs @@ -8,3 +8,6 @@ pub use super::rpc_key::Entity as RpcKey; pub use super::secondary_user::Entity as SecondaryUser; pub use super::user::Entity as User; pub use super::user_tier::Entity as UserTier; +pub use super::referral::Entity as Referral; +pub use super::credits::Entity as Credits; +pub use super::rpc_request::Entity as RpcRequest; diff --git a/entities/src/referral.rs b/entities/src/referral.rs new file mode 100644 index 00000000..0a8b5059 --- /dev/null +++ b/entities/src/referral.rs @@ -0,0 +1,43 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "referral")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + #[sea_orm(unique)] + pub referral_code: String, + pub used_referral_code: Option, + #[sea_orm(unique)] + pub user_id: u64, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "Entity", + from = "Column::UsedReferralCode", + to = "Column::ReferralCode", + on_update = "NoAction", + on_delete = "NoAction" + )] + SelfRef, + #[sea_orm( + belongs_to = "super::user::Entity", + from = "Column::UserId", + to = "super::user::Column::Id", + on_update = "NoAction", + on_delete = "NoAction" + )] + User, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::User.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/entities/src/rpc_request.rs b/entities/src/rpc_request.rs new file mode 100644 index 00000000..186ec589 --- /dev/null +++ b/entities/src/rpc_request.rs @@ -0,0 +1,33 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "rpc_request")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub tx_hash: Option, + pub chain: String, + pub user_id: u64, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::user::Entity", + from = "Column::UserId", + to = "super::user::Column::Id", + on_update = "NoAction", + on_delete = "NoAction" + )] + User, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::User.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/migration/src/lib.rs b/migration/src/lib.rs index ca074f18..833d1a2c 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -13,6 +13,9 @@ mod m20221108_200345_save_anon_stats; mod m20221211_124002_request_method_privacy; mod m20221213_134158_move_login_into_database; mod m20230119_204135_better_free_tier; +mod m20230205_130035_create_credits; +mod m20230205_133204_create_requests; +mod m20230205_133755_create_referrals; pub struct Migrator; @@ -33,6 +36,9 @@ impl MigratorTrait for Migrator { Box::new(m20221211_124002_request_method_privacy::Migration), Box::new(m20221213_134158_move_login_into_database::Migration), Box::new(m20230119_204135_better_free_tier::Migration), + Box::new(m20230205_130035_create_credits::Migration), + Box::new(m20230205_133204_create_requests::Migration), + Box::new(m20230205_133755_create_referrals::Migration), ] } } diff --git a/migration/src/m20230205_130035_create_credits.rs b/migration/src/m20230205_130035_create_credits.rs new file mode 100644 index 00000000..01499910 --- /dev/null +++ b/migration/src/m20230205_130035_create_credits.rs @@ -0,0 +1,66 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(Credits::Table) + .if_not_exists() + .col( + ColumnDef::new(Credits::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col( + ColumnDef::new(Credits::Credits) + .big_unsigned() + .not_null() + .default(0) + ) + .col( + ColumnDef::new(Credits::UserId) + .big_unsigned() + .unique_key() + .not_null() + ) + .foreign_key( + sea_query::ForeignKey::create() + .from(Credits::Table, Credits::UserId) + .to(User::Table, User::Id), + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(Credits::Table).to_owned()) + .await + } +} + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum Credits { + Table, + Id, + UserId, + Credits, +} + +#[derive(Iden)] +enum User { + Table, + Id, + Address, + Description, + Email, +} \ No newline at end of file diff --git a/migration/src/m20230205_133204_create_requests.rs b/migration/src/m20230205_133204_create_requests.rs new file mode 100644 index 00000000..061584fb --- /dev/null +++ b/migration/src/m20230205_133204_create_requests.rs @@ -0,0 +1,70 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(RpcRequest::Table) + .if_not_exists() + .col( + ColumnDef::new(RpcRequest::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col( + ColumnDef::new(RpcRequest::TxHash) + .string() + ) + // TODO: Should eventually be an enum ... + .col( + ColumnDef::new(RpcRequest::Chain) + .string() + .not_null() + ) + .col( + ColumnDef::new(RpcRequest::UserId) + .big_unsigned() + .not_null() + ) + .foreign_key( + sea_query::ForeignKey::create() + .from(RpcRequest::Table, RpcRequest::UserId) + .to(User::Table, User::Id), + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(RpcRequest::Table).to_owned()) + .await + } +} + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum RpcRequest { + Table, + Id, + UserId, + TxHash, + Chain +} + +#[derive(Iden)] +enum User { + Table, + Id, + Address, + Description, + Email, +} \ No newline at end of file diff --git a/migration/src/m20230205_133755_create_referrals.rs b/migration/src/m20230205_133755_create_referrals.rs new file mode 100644 index 00000000..a74416dc --- /dev/null +++ b/migration/src/m20230205_133755_create_referrals.rs @@ -0,0 +1,77 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(Referral::Table) + .if_not_exists() + .col( + ColumnDef::new(Referral::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col( + ColumnDef::new(Referral::ReferralCode) + .string() + .unique_key() + .not_null() + ) + .col( + ColumnDef::new(Referral::UsedReferralCode) + .string() + ) + // Basically, this links to who invited the user ... + .foreign_key( + sea_query::ForeignKey::create() + .from(Referral::Table, Referral::UsedReferralCode) + .to(Referral::Table, Referral::ReferralCode), + ) + .col( + ColumnDef::new(Referral::UserId) + .big_unsigned() + .unique_key() + .not_null() + ) + .foreign_key( + sea_query::ForeignKey::create() + .from(Referral::Table, Referral::UserId) + .to(User::Table, User::Id), + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(Referral::Table).to_owned()) + .await + } +} + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum Referral { + Table, + Id, + UserId, + ReferralCode, + UsedReferralCode +} + +#[derive(Iden)] +enum User { + Table, + Id, + Address, + Description, + Email, +}