added entities for credits, referrals and rpc requests

This commit is contained in:
yenicelik 2023-02-05 14:26:46 +01:00
parent 5628068888
commit 30b3abe7d8
9 changed files with 334 additions and 0 deletions

33
entities/src/credits.rs Normal file

@ -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<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

@ -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;

@ -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;

43
entities/src/referral.rs Normal file

@ -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<String>,
#[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<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

@ -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<String>,
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<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

@ -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),
]
}
}

@ -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,
}

@ -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,
}

@ -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,
}