Skip to content

Commit

Permalink
chore: use workspace to split the code. write the tests and fix bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Reknij committed May 22, 2024
1 parent fb2ad4a commit d708c32
Show file tree
Hide file tree
Showing 113 changed files with 3,753 additions and 2,929 deletions.
546 changes: 417 additions & 129 deletions server/Cargo.lock

Large diffs are not rendered by default.

54 changes: 50 additions & 4 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,30 @@ version = "0.1.0"
edition = "2021"

[profile.release]
strip = true # Automatically strip symbols from the binary.
strip = true # Automatically strip symbols from the binary.
lto = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
[workspace]
members = ["crates/*"]

[workspace.dependencies]
public_system = { version = "*", path = "crates/public_system" }
user_system = { version = "*", path = "crates/user_system" }
elerp_service = { version = "*", path = "crates/elerp_service" }
elerp_common = { version = "*", path = "crates/elerp_common" }
area_module = { version = "*", path = "crates/area_module" }
person_module = { version = "*", path = "crates/person_module" }
warehouse_module = { version = "*", path = "crates/warehouse_module" }
sku_module = { version = "*", path = "crates/sku_module" }
sku_category_module = { version = "*", path = "crates/sku_category_module" }
order_module = { version = "*", path = "crates/order_module" }
order_category_module = { version = "*", path = "crates/order_category_module" }
order_payment_module = { version = "*", path = "crates/order_payment_module" }
guest_order_module = { version = "*", path = "crates/guest_order_module" }
statistical_module = { version = "*", path = "crates/statistical_module" }
inventory_module = { version = "*", path = "crates/inventory_module" }
serde = "1"
serde_json = "1.0"

Expand All @@ -32,7 +50,8 @@ sqlx = { version = "0.7", features = ["runtime-tokio", "tls-rustls", "sqlite"] }

anyhow = "1.0"
clap = { version = "4.4.7", features = ["derive"] }
futures = "0.3.29"
futures = "0.3.30"
futures-util = "0.3.30"

utoipa = { version = "4", features = ["axum_extras"] }
utoipa-rapidoc = { version = "2", features = ["axum"] }
Expand All @@ -42,7 +61,34 @@ strum = { version = "0.25", features = ["derive"] }
rcgen = "0.11.3"
rust_xlsxwriter = "0.58.0"
tokio-util = "0.7.10"
chrono = { version = "0.4", default-features = false, features = ["std", "clock"] }
chrono = { version = "0.4", default-features = false, features = [
"std",
"clock",
] }
ahash = "0.8.8"
regex = "1.10.3"
toml = "0.8"

[dependencies]
tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
elerp_common = { workspace = true }
elerp_service = { workspace = true }

[dev-dependencies]
public_system = { version = "*", workspace = true }
user_system = { version = "*", workspace = true }
elerp_service = { version = "*", workspace = true }
elerp_common = { version = "*", workspace = true }
area_module = { version = "*", workspace = true }
person_module = { version = "*", workspace = true }
warehouse_module = { version = "*", workspace = true }
sku_module = { version = "*", workspace = true }
sku_category_module = { version = "*", workspace = true }
order_module = { version = "*", workspace = true }
order_category_module = { version = "*", workspace = true }
order_payment_module = { version = "*", workspace = true }
guest_order_module = { version = "*", workspace = true }
statistical_module = { version = "*", workspace = true }
inventory_module = { version = "*", workspace = true }
15 changes: 15 additions & 0 deletions server/crates/area_module/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "area_module"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = { workspace = true }
sqlx = { workspace = true }
elerp_common = { workspace = true }
public_system = { workspace = true }
tracing = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
utoipa = { workspace = true }
futures = { workspace = true }
123 changes: 41 additions & 82 deletions server/src/erp/area_module.rs → server/crates/area_module/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use anyhow::bail;
use futures::TryStreamExt;
use sqlx::{Row, SqliteConnection};

use self::model::{Area, GetAreasQuery};
use crate::public_system::{
use anyhow::Result;
use elerp_common::sql;
use elerp_common::sql::get_row_from_table;
use elerp_common::sql::is_exists_in_table;
use elerp_common::sql::remove_row_from_table;
use elerp_common::sql::row_is_duplicate_col_in_table;
use elerp_common::sql::rows_to_objects;
use elerp_common::{
area_module::model::area::{Area, GetAreasQuery},
model::{Pagination, WebSocketFlags},
PublicSystem,
};

use super::Result;

pub mod model;
use futures::TryStreamExt;
use public_system::PublicSystem;
use sqlx::{Row, SqliteConnection};

#[derive(Debug, Clone)]
pub struct AreaModule {
Expand Down Expand Up @@ -45,67 +47,47 @@ impl AreaModule {
}

pub async fn is_limit_reached(&self, tx: &mut SqliteConnection) -> Result<bool> {
let count: i64 = sqlx::query("SELECT COUNT(*) as count FROM areas;")
.fetch_one(&mut *tx)
.await?
.get("count");
let count: i64 = sqlx::query("SELECT COUNT(*) as count FROM areas;").fetch_one(&mut *tx).await?.get("count");
Ok(count >= self.ps.get_config().limit.areas)
}

pub async fn is_exists_name(
&self,
name: &str,
prev: Option<i64>,
tx: &mut SqliteConnection,
) -> Result<bool> {
self.ps
.row_is_duplicate_col_in_table(name, prev, "areas", "name", tx)
.await
pub async fn is_exists_name(&self, name: &str, prev: Option<i64>, tx: &mut SqliteConnection) -> Result<bool> {
row_is_duplicate_col_in_table(name, prev, "areas", "name", tx).await
}

pub async fn is_exists(&self, id: i64, tx: &mut SqliteConnection) -> Result<bool> {
self.ps.is_exists_in_table("areas", "id", id, tx).await
is_exists_in_table("areas", "id", id, tx).await
}

pub async fn add(&self, mut v: Area, tx: &mut SqliteConnection) -> Result<Area> {
let r = sqlx::query(
"INSERT INTO areas (name, description, color, text_color) VALUES(?, ?, ?, ?)",
)
.bind(&v.name)
.bind(&v.description)
.bind(&v.color)
.bind(&v.text_color)
.execute(&mut *tx)
.await?;
let r = sqlx::query("INSERT INTO areas (name, description, color, text_color) VALUES(?, ?, ?, ?)")
.bind(&v.name)
.bind(&v.description)
.bind(&v.color)
.bind(&v.text_color)
.execute(&mut *tx)
.await?;
if r.rows_affected() != 1 {
bail!("Can't add area");
}
v.id = self
.ps
.try_set_standard_id(r.last_insert_rowid(), "areas", tx)
.await?;
v.id = sql::try_set_standard_id(r.last_insert_rowid(), "areas", tx).await?;
self.ps.notice(WebSocketFlags::AddArea(v.id)).await?;
Ok(v)
}

pub async fn remove(&self, id: i64, notice: bool, tx: &mut SqliteConnection) -> Result<bool> {
let r = self.ps.remove_row_from_table(id, "areas", tx).await?;
let r = remove_row_from_table(id, "areas", tx).await?;
if notice {
self.ps.notice(WebSocketFlags::RemoveArea(id)).await?;
}
Ok(r)
}

pub async fn get(&self, id: i64, tx: &mut SqliteConnection) -> Result<Option<Area>> {
self.ps.get_row_from_table("areas", "id", id, tx).await
get_row_from_table("areas", "id", id, tx).await
}

pub async fn get_multiple(
&self,
pagination: &Pagination,
query: &GetAreasQuery,
tx: &mut SqliteConnection,
) -> Result<Vec<Area>> {
pub async fn get_multiple(&self, pagination: &Pagination, query: &GetAreasQuery, tx: &mut SqliteConnection) -> Result<Vec<Area>> {
let qw = query.get_where_condition();
let ob = query.get_order_condition();
let rows = sqlx::query(&format!(
Expand All @@ -123,15 +105,10 @@ impl AreaModule {
.bind(pagination.offset())
.fetch_all(&mut *tx)
.await?;
self.ps.rows_to_objects(rows)
rows_to_objects(rows)
}

pub async fn get_multiple_ids(
&self,
pagination: &Pagination,
query: &GetAreasQuery,
tx: &mut SqliteConnection,
) -> Result<Vec<i64>> {
pub async fn get_multiple_ids(&self, pagination: &Pagination, query: &GetAreasQuery, tx: &mut SqliteConnection) -> Result<Vec<i64>> {
let qw = query.get_where_condition();
let rows = sqlx::query(&format!(
"SELECT
Expand All @@ -148,27 +125,19 @@ impl AreaModule {

pub async fn get_count(&self, query: &GetAreasQuery, tx: &mut SqliteConnection) -> Result<i64> {
let qw = query.get_where_condition();
let row = sqlx::query(&format!("SELECT count(*) as count FROM areas {qw}"))
.fetch_one(&mut *tx)
.await?;
let row = sqlx::query(&format!("SELECT count(*) as count FROM areas {qw}")).fetch_one(&mut *tx).await?;
Ok(row.get("count"))
}

pub async fn update(
&self,
id: i64,
mut v: Area,
tx: &mut SqliteConnection,
) -> Result<Option<Area>> {
let r =
sqlx::query("UPDATE areas SET name=?, description=?, color=?, text_color=? WHERE id=?")
.bind(&v.name)
.bind(&v.description)
.bind(&v.color)
.bind(&v.text_color)
.bind(id)
.execute(&mut *tx)
.await?;
pub async fn update(&self, id: i64, mut v: Area, tx: &mut SqliteConnection) -> Result<Option<Area>> {
let r = sqlx::query("UPDATE areas SET name=?, description=?, color=?, text_color=? WHERE id=?")
.bind(&v.name)
.bind(&v.description)
.bind(&v.color)
.bind(&v.text_color)
.bind(id)
.execute(&mut *tx)
.await?;
Ok(if r.rows_affected() == 1 {
v.id = id;
self.ps.notice(WebSocketFlags::UpdateArea(id)).await?;
Expand All @@ -179,17 +148,7 @@ impl AreaModule {
}

pub async fn is_depend_by_another(&self, id: i64, tx: &mut SqliteConnection) -> Result<bool> {
Ok(sqlx::query("SELECT id FROM persons WHERE area_id=?")
.bind(id)
.fetch(&mut *tx)
.try_next()
.await?
.is_some()
|| sqlx::query("SELECT id FROM warehouses WHERE area_id=?")
.bind(id)
.fetch(&mut *tx)
.try_next()
.await?
.is_some())
Ok(sqlx::query("SELECT id FROM persons WHERE area_id=?").bind(id).fetch(&mut *tx).try_next().await?.is_some()
|| sqlx::query("SELECT id FROM warehouses WHERE area_id=?").bind(id).fetch(&mut *tx).try_next().await?.is_some())
}
}
21 changes: 21 additions & 0 deletions server/crates/elerp_common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "elerp_common"
version = "0.1.0"
edition = "2021"

[dependencies]
tempfile = "3"
sqlx = { workspace = true }
toml = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
utoipa = { workspace = true }
strum = { workspace = true }
ahash = { workspace = true }
tracing = { workspace = true }
tokio = { workspace = true }
clap = { workspace = true }
anyhow = { workspace = true }
regex = { workspace = true }
futures = { workspace = true }
futures-util = { workspace = true }
1 change: 1 addition & 0 deletions server/crates/elerp_common/src/area_module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod model;
1 change: 1 addition & 0 deletions server/crates/elerp_common/src/area_module/model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod area;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use sqlx::prelude::FromRow;
use utoipa::{IntoParams, ToSchema};

use crate::erp::util::{get_search_where_condition, get_sort_col_str, get_sorter_str};
use crate::sql::{get_search_where_condition, get_sort_col_str, get_sorter_str};

#[derive(Debug, Serialize, Deserialize, ToSchema, FromRow)]
pub struct Area {
Expand All @@ -17,7 +17,7 @@ pub struct Area {
pub text_color: Option<String>,
}

#[derive(Debug, Deserialize, ToSchema, IntoParams)]
#[derive(Debug, Default, Deserialize, ToSchema, IntoParams)]
pub struct GetAreasQuery {
pub id: Option<i64>,
pub name: Option<String>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::path::PathBuf;

use crate::i64_safe_max;
use serde::Deserialize;
use tokio::fs;
use tracing::warn;

use crate::{meta::MetaInfo, myhelper::i64_safe_max};
use crate::meta::MetaInfo;

#[derive(Debug, Deserialize)]
struct AppConfigInternal {
Expand Down
1 change: 1 addition & 0 deletions server/crates/elerp_common/src/guest_order_module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod model;
1 change: 1 addition & 0 deletions server/crates/elerp_common/src/guest_order_module/model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod guest_order;
Loading

0 comments on commit d708c32

Please sign in to comment.