From 84bd7f66d9dc64999bd372d674767852cf2f3c56 Mon Sep 17 00:00:00 2001 From: wjsjwr Date: Sun, 29 Dec 2024 23:34:31 +0800 Subject: [PATCH] Implement add transaction --- .idea/workspace.xml | 29 +++---- bookkeeper/src/main.rs | 21 +++-- bookkeeper/templates/index.html.j2 | 98 +++++++++++++++++++--- bookkeeper/templates/tx.html.j2 | 129 ++++++++++++++++------------- 4 files changed, 189 insertions(+), 88 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a60b665..47ffc98 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -10,19 +10,11 @@ - - - - - + - - - - - + - { "customColor": "", @@ -157,7 +149,7 @@ - + - @@ -213,7 +213,8 @@ - diff --git a/bookkeeper/src/main.rs b/bookkeeper/src/main.rs index b529d78..86567b9 100644 --- a/bookkeeper/src/main.rs +++ b/bookkeeper/src/main.rs @@ -69,7 +69,7 @@ async fn index(conn: Connection<'_, Db>) -> Template { }) } -#[derive(FromForm)] +#[derive(FromForm, Debug)] struct TransModel<'r> { code: usize, name: &'r str, @@ -78,10 +78,10 @@ struct TransModel<'r> { volume: i32, net_buy: &'r str, date: &'r str, - split_pieces: Vec>, + split_pieces: Option>>, } -#[derive(FromForm, Serialize)] +#[derive(FromForm, Serialize, Debug)] struct SplitPieceForm<'r> { date: &'r str, sell: &'r str, @@ -98,8 +98,17 @@ struct SplitPiece{ volume: i32, } +#[get("/add")] +async fn get_add() -> Template { + Template::render("tx", context! { + r: (), + target: "/add", + }) +} + #[post("/add", data = "")] async fn add(trans: Form>, conn: Connection<'_, Db>) -> Flash { + dbg!(&trans); let record = translate(trans); record.insert(conn.into_inner()).await.unwrap(); @@ -120,9 +129,9 @@ fn translate(trans: Form) -> transaction::ActiveModel { ..Default::default() }; - if trans.split_pieces.len() > 0 { + if let Some(split_pieces) = &trans.split_pieces { let mut sp: Vec = Vec::new(); - for split_piece in trans.split_pieces.iter() { + for split_piece in split_pieces.iter() { sp.push(SplitPiece{ date: NaiveDate::parse_from_str(split_piece.date, "%Y-%m-%d").unwrap(), sell: Decimal::from_str(split_piece.sell).unwrap(), @@ -165,5 +174,5 @@ async fn rocket() -> _ { .attach(Db::init()) .attach(AdHoc::try_on_ignite("Migrations", run_migrations)) .attach(Template::fairing()) - .mount("/", routes![index, add]) + .mount("/", routes![index, add, get_add]) } diff --git a/bookkeeper/templates/index.html.j2 b/bookkeeper/templates/index.html.j2 index 6245405..2f5690b 100644 --- a/bookkeeper/templates/index.html.j2 +++ b/bookkeeper/templates/index.html.j2 @@ -2,18 +2,18 @@ {% block title %}交易{% endblock title %} {% block container %}
- +
- - + + - + - + @@ -24,11 +24,11 @@ {% for r in rows %} {% if r.split_pieces %} {% for p in r.split_pieces %} - + {% if loop.index == 1 %} - + @@ -36,8 +36,8 @@ - - + + @@ -50,10 +50,10 @@ {% endfor %} {% else %} - + - + @@ -74,4 +74,80 @@
代码名称代码名称 买入日期 买入价成交量买量 实际买入 卖出日期 卖出价成交量卖量 实际卖出 收益 实际收益
{{ r.code }} {{ r.name }}{{ r.date }}{{ r.date }} {{ r.buy }} {{ r.volume }} {{ r.net_buy }}{{ p.sell or "" }} {{ p.volume or "" }} {{ p.net_sell or "" }}{{ (r.gain or 0)|float|round(4) }}%{{ (r.net_gain or 0)|float|round(4) }}%{{ (r.gain or 0)|float|round(4) }}%{{ (r.net_gain or 0)|float|round(4) }}% 修改
{{ r.code }} {{ r.name }}{{ r.date }}{{ r.date }} {{ r.buy }} {{ r.volume }} {{ r.net_buy }}
+ + + + {% endblock container %} diff --git a/bookkeeper/templates/tx.html.j2 b/bookkeeper/templates/tx.html.j2 index af87e20..12dd47e 100644 --- a/bookkeeper/templates/tx.html.j2 +++ b/bookkeeper/templates/tx.html.j2 @@ -1,61 +1,76 @@ -
- - - - - - - - - - - - - - - - - - - - - {% for r in rows %} - - - - - - - - {% if r.split_pieces %} - - - - - {% else %} - - - - - {% endif %} - - - + + + + +
代码名称买入日期买入价成交量实际买入价卖出日期卖出价成交量实际卖出价收益实际收益O操作
{{ r.code }}{{ r.name }}{{ r.date }}{{ r.buy }}{{ r.volume }}{{ r.net_buy }}}{{ r.sell_date or "" }}{{ r.sell or "" }}{{ r.sold_volume or "" }}{{ r.net_sell or "" }}{{ r.gain or "" }}{{ r.net_gain or "" }} - {% if r.sold_volume == r.volume %} - ✅ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+ + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + {% if r.split_pieces %} + {% for split in r.split_pieces %} + + + + + + {% endfor %} {% else %} - ☐ + + + + + {% endif %} - - - - {% endfor %} - - - - -
操作
删除删除 - 修改 -
添加
+
添加
+
+
+ +
+
+ +
+
+