feat: init book handler

This commit is contained in:
brian
2025-06-08 20:42:24 +08:00
parent 50913eecbb
commit 27c94f4276
26 changed files with 4307 additions and 3 deletions

22
migration/src/lib.rs Normal file
View File

@@ -0,0 +1,22 @@
pub use sea_orm_migration::prelude::*;
mod m20250525_000001_create_ledger_table_category;
mod m20250525_000002_create_ledger_table_book;
mod m20250525_000003_create_ledger_table_tag;
mod m20250525_000004_create_ledger_table_account;
mod m20250525_000005_create_ledger_table_transaction;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
Box::new(m20250525_000001_create_ledger_table_category::Migration),
Box::new(m20250525_000002_create_ledger_table_book::Migration),
Box::new(m20250525_000003_create_ledger_table_tag::Migration),
Box::new(m20250525_000004_create_ledger_table_account::Migration),
Box::new(m20250525_000005_create_ledger_table_transaction::Migration),
]
}
}

View File

@@ -0,0 +1,64 @@
use sea_orm_migration::prelude::*;
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20250525_000001_create_ledger_table_category" // Make sure this matches with the file name
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Category::Table)
.col(
ColumnDef::new(Category::Id)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Category::Name).string().not_null())
.col(ColumnDef::new(Category::Uid).big_integer().not_null())
.col(ColumnDef::new(Category::ParentId).big_integer().default(0i64).not_null())
.col(ColumnDef::new(Category::IsDeleted).boolean().default(false).not_null())
.col(
ColumnDef::new(Category::CreatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.col(
ColumnDef::new(Category::UpdatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.to_owned(),
)
.await
}
// Define how to rollback this migration: Drop the Bakery table.
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Category::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum Category {
Table,
Id,
Name,
Uid,
ParentId,
IsDeleted,
CreatedAt,
UpdatedAt,
}

View File

@@ -0,0 +1,63 @@
use sea_orm_migration::prelude::*;
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20250525_000002_create_ledger_table_book" // Make sure this matches with the file name
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
// Define how to apply this migration
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Book::Table)
.col(
ColumnDef::new(Book::Id)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Book::Name).string().not_null())
.col(ColumnDef::new(Book::Uid).big_integer().not_null())
.col(ColumnDef::new(Book::IsDeleted).boolean().default(false).not_null())
.col(
ColumnDef::new(Book::CreatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.col(
ColumnDef::new(Book::UpdatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.to_owned(),
)
.await
}
// Define how to rollback this migration: Drop the Bakery table.
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Book::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum Book {
Table,
Id,
Name,
Uid,
IsDeleted,
CreatedAt,
UpdatedAt,
}

View File

@@ -0,0 +1,62 @@
use sea_orm_migration::prelude::*;
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20250525_000001_create_ledger_table_tag" // Make sure this matches with the file name
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Tag::Table)
.col(
ColumnDef::new(Tag::Id)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Tag::Name).string().not_null())
.col(ColumnDef::new(Tag::Uid).big_integer().not_null())
.col(ColumnDef::new(Tag::IsDeleted).boolean().default(false).not_null())
.col(
ColumnDef::new(Tag::CreatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.col(
ColumnDef::new(Tag::UpdatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.to_owned(),
)
.await
}
// Define how to rollback this migration: Drop the Bakery table.
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Tag::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum Tag {
Table,
Id,
Name,
Uid,
IsDeleted,
CreatedAt,
UpdatedAt,
}

View File

@@ -0,0 +1,64 @@
use sea_orm_migration::prelude::*;
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20250525_000004_create_ledger_table_account" // Make sure this matches with the file name
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Account::Table)
.col(
ColumnDef::new(Account::Id)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Account::Name).string().not_null())
.col(ColumnDef::new(Account::Type).integer().not_null())
.col(ColumnDef::new(Account::Uid).big_integer().not_null())
.col(ColumnDef::new(Account::IsDeleted).boolean().default(false).not_null())
.col(
ColumnDef::new(Account::CreatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.col(
ColumnDef::new(Account::UpdatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.to_owned(),
)
.await
}
// Define how to rollback this migration: Drop the Bakery table.
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Account::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum Account {
Table,
Id,
Name,
Uid,
Type,
IsDeleted,
CreatedAt,
UpdatedAt,
}

View File

@@ -0,0 +1,84 @@
use sea_orm_migration::prelude::*;
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20250525_000005_create_ledger_table_transaction" // Make sure this matches with the file name
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Transaction::Table)
.col(
ColumnDef::new(Transaction::Id)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Transaction::Uid).big_integer().not_null())
.col(ColumnDef::new(Transaction::Type).integer().not_null())
.col(ColumnDef::new(Transaction::BookId).big_integer().not_null())
.col(
ColumnDef::new(Transaction::CategoryId)
.big_integer()
.not_null(),
)
.col(ColumnDef::new(Transaction::Description).string().not_null())
.col(
ColumnDef::new(Transaction::TransactionTime)
.timestamp_with_time_zone()
.default(Expr::current_timestamp())
.not_null(),
)
.col(
ColumnDef::new(Transaction::IsDeleted)
.boolean()
.default(false)
.not_null(),
)
.col(
ColumnDef::new(Transaction::CreatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.col(
ColumnDef::new(Transaction::UpdatedAt)
.date_time()
.default(Expr::current_timestamp())
.not_null(),
)
.to_owned(),
)
.await
}
// Define how to rollback this migration: Drop the Bakery table.
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Transaction::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum Transaction {
Table,
Id,
Uid,
Type,
BookId,
CategoryId,
Description,
TransactionTime,
IsDeleted,
CreatedAt,
UpdatedAt,
}

6
migration/src/main.rs Normal file
View File

@@ -0,0 +1,6 @@
use sea_orm_migration::prelude::*;
#[async_std::main]
async fn main() {
cli::run_cli(migration::Migrator).await;
}