Compare commits
12 Commits
952a37892d
...
dev/v1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39e656ef91 | ||
|
|
7a58035d0c | ||
|
|
3fa7649877 | ||
|
|
de38e20d3a | ||
|
|
ceb3edba39 | ||
|
|
2b2b895b7d | ||
|
|
dcc4c68abb | ||
|
|
56fc4be355 | ||
|
|
1ae32dd595 | ||
|
|
21a6b91139 | ||
|
|
e25c1b5ceb | ||
|
|
b3ee37fbe3 |
1
.env.template
Normal file
1
.env.template
Normal file
@@ -0,0 +1 @@
|
|||||||
|
DATABASE_URL=postgres://username:password@localhost/diesel_demo
|
||||||
263
Cargo.lock
generated
263
Cargo.lock
generated
@@ -1,6 +1,6 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
version = 4
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "addr2line"
|
name = "addr2line"
|
||||||
@@ -60,14 +60,14 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "axum"
|
name = "axum"
|
||||||
version = "0.7.5"
|
version = "0.8.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3a6c9af12842a67734c9a2e355436e5d03b22383ed60cf13cd0c18fbfe3dcbcf"
|
checksum = "021e862c184ae977658b36c4500f7feac3221ca5da43e3f25bd04ab6c79a29b5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
|
||||||
"axum-core",
|
"axum-core",
|
||||||
"axum-macros",
|
"axum-macros 0.5.0",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
"form_urlencoded",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"http",
|
"http",
|
||||||
"http-body",
|
"http-body",
|
||||||
@@ -85,9 +85,9 @@ dependencies = [
|
|||||||
"serde_json",
|
"serde_json",
|
||||||
"serde_path_to_error",
|
"serde_path_to_error",
|
||||||
"serde_urlencoded",
|
"serde_urlencoded",
|
||||||
"sync_wrapper 1.0.1",
|
"sync_wrapper",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tower",
|
"tower 0.5.2",
|
||||||
"tower-layer",
|
"tower-layer",
|
||||||
"tower-service",
|
"tower-service",
|
||||||
"tracing",
|
"tracing",
|
||||||
@@ -95,20 +95,19 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "axum-core"
|
name = "axum-core"
|
||||||
version = "0.4.3"
|
version = "0.5.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a15c63fd72d41492dc4f497196f5da1fb04fb7529e631d73630d1b491e47a2e3"
|
checksum = "68464cd0412f486726fb3373129ef5d2993f90c34bc2bc1c1e9943b2f4fc7ca6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
|
||||||
"bytes",
|
"bytes",
|
||||||
"futures-util",
|
"futures-core",
|
||||||
"http",
|
"http",
|
||||||
"http-body",
|
"http-body",
|
||||||
"http-body-util",
|
"http-body-util",
|
||||||
"mime",
|
"mime",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"rustversion",
|
"rustversion",
|
||||||
"sync_wrapper 0.1.2",
|
"sync_wrapper",
|
||||||
"tower-layer",
|
"tower-layer",
|
||||||
"tower-service",
|
"tower-service",
|
||||||
"tracing",
|
"tracing",
|
||||||
@@ -116,9 +115,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "axum-extra"
|
name = "axum-extra"
|
||||||
version = "0.9.3"
|
version = "0.10.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0be6ea09c9b96cb5076af0de2e383bd2bc0c18f827cf1967bdd353e0b910d733"
|
checksum = "45bf463831f5131b7d3c756525b305d40f1185b688565648a92e1392ca35713d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"axum-core",
|
"axum-core",
|
||||||
@@ -130,11 +129,11 @@ dependencies = [
|
|||||||
"http-body-util",
|
"http-body-util",
|
||||||
"mime",
|
"mime",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
|
"rustversion",
|
||||||
"serde",
|
"serde",
|
||||||
"tower",
|
"tower 0.5.2",
|
||||||
"tower-layer",
|
"tower-layer",
|
||||||
"tower-service",
|
"tower-service",
|
||||||
"tracing",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -149,6 +148,17 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "axum-macros"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "604fde5e028fea851ce1d8570bbdc034bec850d157f7569d10f347d06808c05c"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "backtrace"
|
name = "backtrace"
|
||||||
version = "0.3.73"
|
version = "0.3.73"
|
||||||
@@ -176,6 +186,12 @@ version = "0.22.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
|
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "base64ct"
|
||||||
|
version = "1.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "2.6.0"
|
version = "2.6.0"
|
||||||
@@ -233,7 +249,7 @@ dependencies = [
|
|||||||
"num-traits",
|
"num-traits",
|
||||||
"serde",
|
"serde",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
"windows-targets 0.52.6",
|
"windows-targets",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -389,6 +405,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"block-buffer",
|
"block-buffer",
|
||||||
"crypto-common",
|
"crypto-common",
|
||||||
|
"subtle",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -537,17 +554,20 @@ dependencies = [
|
|||||||
"async-trait",
|
"async-trait",
|
||||||
"axum",
|
"axum",
|
||||||
"axum-extra",
|
"axum-extra",
|
||||||
"axum-macros",
|
"axum-macros 0.4.1",
|
||||||
"chrono",
|
"chrono",
|
||||||
"deadpool-diesel",
|
"deadpool-diesel",
|
||||||
"diesel",
|
"diesel",
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
"jsonwebtoken",
|
"jsonwebtoken",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
|
"pbkdf2",
|
||||||
|
"rand_core",
|
||||||
|
"regex",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tower",
|
"tower 0.4.13",
|
||||||
"tower-http",
|
"tower-http",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
@@ -559,6 +579,15 @@ version = "0.3.9"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
|
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hmac"
|
||||||
|
version = "0.12.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
|
||||||
|
dependencies = [
|
||||||
|
"digest",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "http"
|
name = "http"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
@@ -637,6 +666,8 @@ dependencies = [
|
|||||||
"hyper",
|
"hyper",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"tower 0.4.13",
|
||||||
|
"tower-service",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -706,9 +737,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.155"
|
version = "0.2.172"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
|
checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lock_api"
|
name = "lock_api"
|
||||||
@@ -737,9 +768,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "matchit"
|
name = "matchit"
|
||||||
version = "0.7.3"
|
version = "0.8.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94"
|
checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
@@ -764,13 +795,13 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mio"
|
name = "mio"
|
||||||
version = "0.8.11"
|
version = "1.0.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
|
checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"wasi",
|
"wasi",
|
||||||
"windows-sys 0.48.0",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -868,7 +899,30 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
"redox_syscall",
|
"redox_syscall",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"windows-targets 0.52.6",
|
"windows-targets",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "password-hash"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "346f04948ba92c43e8469c1ee6736c7563d71012b17d40745260fe106aac2166"
|
||||||
|
dependencies = [
|
||||||
|
"base64ct",
|
||||||
|
"rand_core",
|
||||||
|
"subtle",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pbkdf2"
|
||||||
|
version = "0.12.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2"
|
||||||
|
dependencies = [
|
||||||
|
"digest",
|
||||||
|
"hmac",
|
||||||
|
"password-hash",
|
||||||
|
"sha2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -952,6 +1006,15 @@ dependencies = [
|
|||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.6.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.5.2"
|
version = "0.5.2"
|
||||||
@@ -1017,7 +1080,7 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
"spin",
|
"spin",
|
||||||
"untrusted",
|
"untrusted",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1108,6 +1171,17 @@ dependencies = [
|
|||||||
"digest",
|
"digest",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sha2"
|
||||||
|
version = "0.10.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"cpufeatures",
|
||||||
|
"digest",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sharded-slab"
|
name = "sharded-slab"
|
||||||
version = "0.1.7"
|
version = "0.1.7"
|
||||||
@@ -1151,7 +1225,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c"
|
checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1166,6 +1240,12 @@ version = "0.11.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "subtle"
|
||||||
|
version = "2.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.69"
|
version = "2.0.69"
|
||||||
@@ -1177,12 +1257,6 @@ dependencies = [
|
|||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "sync_wrapper"
|
|
||||||
version = "0.1.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sync_wrapper"
|
name = "sync_wrapper"
|
||||||
version = "1.0.1"
|
version = "1.0.1"
|
||||||
@@ -1252,28 +1326,27 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.38.0"
|
version = "1.45.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a"
|
checksum = "2513ca694ef9ede0fb23fe71a4ee4107cb102b9dc1930f6d0fd77aae068ae165"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"backtrace",
|
"backtrace",
|
||||||
"bytes",
|
"bytes",
|
||||||
"libc",
|
"libc",
|
||||||
"mio",
|
"mio",
|
||||||
"num_cpus",
|
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"signal-hook-registry",
|
"signal-hook-registry",
|
||||||
"socket2",
|
"socket2",
|
||||||
"tokio-macros",
|
"tokio-macros",
|
||||||
"windows-sys 0.48.0",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio-macros"
|
name = "tokio-macros"
|
||||||
version = "2.3.0"
|
version = "2.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a"
|
checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -1296,6 +1369,22 @@ dependencies = [
|
|||||||
"tracing",
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tower"
|
||||||
|
version = "0.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d039ad9159c98b70ecfd540b2573b97f7f52c3e8d9f8ad57a24b916a536975f9"
|
||||||
|
dependencies = [
|
||||||
|
"futures-core",
|
||||||
|
"futures-util",
|
||||||
|
"pin-project-lite",
|
||||||
|
"sync_wrapper",
|
||||||
|
"tokio",
|
||||||
|
"tower-layer",
|
||||||
|
"tower-service",
|
||||||
|
"tracing",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tower-http"
|
name = "tower-http"
|
||||||
version = "0.5.2"
|
version = "0.5.2"
|
||||||
@@ -1315,15 +1404,15 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tower-layer"
|
name = "tower-layer"
|
||||||
version = "0.3.2"
|
version = "0.3.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0"
|
checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tower-service"
|
name = "tower-service"
|
||||||
version = "0.3.2"
|
version = "0.3.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52"
|
checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tracing"
|
name = "tracing"
|
||||||
@@ -1511,16 +1600,7 @@ version = "0.52.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
|
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-targets 0.52.6",
|
"windows-targets",
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows-sys"
|
|
||||||
version = "0.48.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
|
|
||||||
dependencies = [
|
|
||||||
"windows-targets 0.48.5",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1529,22 +1609,7 @@ version = "0.52.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-targets 0.52.6",
|
"windows-targets",
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows-targets"
|
|
||||||
version = "0.48.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
|
|
||||||
dependencies = [
|
|
||||||
"windows_aarch64_gnullvm 0.48.5",
|
|
||||||
"windows_aarch64_msvc 0.48.5",
|
|
||||||
"windows_i686_gnu 0.48.5",
|
|
||||||
"windows_i686_msvc 0.48.5",
|
|
||||||
"windows_x86_64_gnu 0.48.5",
|
|
||||||
"windows_x86_64_gnullvm 0.48.5",
|
|
||||||
"windows_x86_64_msvc 0.48.5",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1553,46 +1618,28 @@ version = "0.52.6"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows_aarch64_gnullvm 0.52.6",
|
"windows_aarch64_gnullvm",
|
||||||
"windows_aarch64_msvc 0.52.6",
|
"windows_aarch64_msvc",
|
||||||
"windows_i686_gnu 0.52.6",
|
"windows_i686_gnu",
|
||||||
"windows_i686_gnullvm",
|
"windows_i686_gnullvm",
|
||||||
"windows_i686_msvc 0.52.6",
|
"windows_i686_msvc",
|
||||||
"windows_x86_64_gnu 0.52.6",
|
"windows_x86_64_gnu",
|
||||||
"windows_x86_64_gnullvm 0.52.6",
|
"windows_x86_64_gnullvm",
|
||||||
"windows_x86_64_msvc 0.52.6",
|
"windows_x86_64_msvc",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows_aarch64_gnullvm"
|
|
||||||
version = "0.48.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_aarch64_gnullvm"
|
name = "windows_aarch64_gnullvm"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows_aarch64_msvc"
|
|
||||||
version = "0.48.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_aarch64_msvc"
|
name = "windows_aarch64_msvc"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows_i686_gnu"
|
|
||||||
version = "0.48.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_gnu"
|
name = "windows_i686_gnu"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
@@ -1605,48 +1652,24 @@ version = "0.52.6"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows_i686_msvc"
|
|
||||||
version = "0.48.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_msvc"
|
name = "windows_i686_msvc"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows_x86_64_gnu"
|
|
||||||
version = "0.48.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnu"
|
name = "windows_x86_64_gnu"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows_x86_64_gnullvm"
|
|
||||||
version = "0.48.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnullvm"
|
name = "windows_x86_64_gnullvm"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "windows_x86_64_msvc"
|
|
||||||
version = "0.48.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_msvc"
|
name = "windows_x86_64_msvc"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
async-trait = "0.1.81"
|
async-trait = "0.1.81"
|
||||||
axum = {version = "0.7.5", features = ["macros"]}
|
axum = {version = "0.8", features = ["macros"]}
|
||||||
axum-extra = { version = "0.9.3", features = ["typed-header"] }
|
axum-extra = { version = "0.10", features = ["typed-header"] }
|
||||||
chrono = {version = "0.4", features = ["serde"]}
|
chrono = {version = "0.4", features = ["serde"]}
|
||||||
deadpool-diesel = {version ="0.6.1", features = ["postgres"]}
|
deadpool-diesel = {version ="0.6.1", features = ["postgres"]}
|
||||||
diesel = { version = "2", features = ["postgres", "chrono"] }
|
diesel = { version = "2", features = ["postgres", "chrono"] }
|
||||||
@@ -23,3 +23,6 @@ tracing = "0.1"
|
|||||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||||
once_cell = "1.19.0"
|
once_cell = "1.19.0"
|
||||||
axum-macros = "0.4.1"
|
axum-macros = "0.4.1"
|
||||||
|
pbkdf2 = { version = "0.12", features = ["simple"] }
|
||||||
|
rand_core ={version = "0.6", features = ["std"]}
|
||||||
|
regex = {version = "1.10"}
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
# see https://diesel.rs/guides/configuring-diesel-cli
|
# see https://diesel.rs/guides/configuring-diesel-cli
|
||||||
|
|
||||||
[print_schema]
|
[print_schema]
|
||||||
file = "src/schema.rs"
|
file = "src/model/schema.rs"
|
||||||
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]
|
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]
|
||||||
|
|
||||||
[migrations_directory]
|
[migrations_directory]
|
||||||
dir = "/data/codes/helios-server-rs/migrations"
|
dir = "./migrations"
|
||||||
|
|||||||
@@ -7,5 +7,6 @@ DROP TABLE IF EXISTS "transactions";
|
|||||||
DROP TABLE IF EXISTS "transaction_tag_rels";
|
DROP TABLE IF EXISTS "transaction_tag_rels";
|
||||||
DROP TABLE IF EXISTS "accounts";
|
DROP TABLE IF EXISTS "accounts";
|
||||||
DROP TABLE IF EXISTS "amounts";
|
DROP TABLE IF EXISTS "amounts";
|
||||||
|
|
||||||
DROP TABLE IF EXISTS "users";
|
DROP TABLE IF EXISTS "users";
|
||||||
|
DROP TABLE IF EXISTS "operations";
|
||||||
|
DROP TABLE IF EXISTS "operation_snapshots";
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
-- Your SQL goes here
|
-- Your SQL goes here
|
||||||
-- Your SQL goes here
|
|
||||||
CREATE TABLE "categories" (
|
CREATE TABLE "categories" (
|
||||||
"id" BIGSERIAL PRIMARY KEY,
|
"id" BIGSERIAL PRIMARY KEY,
|
||||||
"uid" BIGINT NOT NULL,
|
"uid" BIGINT NOT NULL,
|
||||||
|
"book_id" BIGINT NOT NULL,
|
||||||
"name" TEXT NOT NULL,
|
"name" TEXT NOT NULL,
|
||||||
|
"level" INT NOT NULL DEFAULT 0,
|
||||||
|
"parent_category_id" BIGINT NOT NULL DEFAULT 0,
|
||||||
|
"op_id" BIGINT NOT NULL DEFAULT 0,
|
||||||
"is_delete" BOOLEAN NOT NULL DEFAULT FALSE,
|
"is_delete" BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
||||||
"update_at" TIMESTAMP NOT NULL DEFAULT current_timestamp
|
"update_at" TIMESTAMP NOT NULL DEFAULT current_timestamp
|
||||||
@@ -12,7 +15,11 @@ CREATE TABLE "categories" (
|
|||||||
CREATE TABLE "tags" (
|
CREATE TABLE "tags" (
|
||||||
"id" BIGSERIAL PRIMARY KEY,
|
"id" BIGSERIAL PRIMARY KEY,
|
||||||
"uid" BIGINT NOT NULL,
|
"uid" BIGINT NOT NULL,
|
||||||
|
"book_id" BIGINT NOT NULL,
|
||||||
"name" TEXT NOT NULL,
|
"name" TEXT NOT NULL,
|
||||||
|
"level" INT NOT NULL DEFAULT 0,
|
||||||
|
"parent_tag_id" BIGINT NOT NULL DEFAULT 0,
|
||||||
|
"op_id" BIGINT NOT NULL DEFAULT 0,
|
||||||
"is_delete" BOOLEAN NOT NULL DEFAULT FALSE,
|
"is_delete" BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
||||||
"update_at" TIMESTAMP NOT NULL DEFAULT current_timestamp
|
"update_at" TIMESTAMP NOT NULL DEFAULT current_timestamp
|
||||||
@@ -22,6 +29,7 @@ CREATE TABLE "books" (
|
|||||||
"id" BIGSERIAL PRIMARY KEY,
|
"id" BIGSERIAL PRIMARY KEY,
|
||||||
"uid" BIGINT NOT NULL,
|
"uid" BIGINT NOT NULL,
|
||||||
"name" TEXT NOT NULL,
|
"name" TEXT NOT NULL,
|
||||||
|
"op_id" BIGINT NOT NULL DEFAULT 0,
|
||||||
"is_delete" BOOLEAN NOT NULL DEFAULT FALSE,
|
"is_delete" BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
||||||
"update_at" TIMESTAMP NOT NULL DEFAULT current_timestamp
|
"update_at" TIMESTAMP NOT NULL DEFAULT current_timestamp
|
||||||
@@ -33,6 +41,7 @@ CREATE TABLE "transactions" (
|
|||||||
"book_id" BIGINT NOT NULL,
|
"book_id" BIGINT NOT NULL,
|
||||||
"description" TEXT NOT NULL,
|
"description" TEXT NOT NULL,
|
||||||
"category_id" BIGINT NOT NULL,
|
"category_id" BIGINT NOT NULL,
|
||||||
|
"op_id" BIGINT NOT NULL DEFAULT 0,
|
||||||
"is_delete" BOOLEAN NOT NULL DEFAULT FALSE,
|
"is_delete" BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
"time" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT current_timestamp,
|
"time" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT current_timestamp,
|
||||||
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
||||||
@@ -44,6 +53,7 @@ CREATE TABLE "transaction_tag_rels" (
|
|||||||
"uid" BIGINT NOT NULL,
|
"uid" BIGINT NOT NULL,
|
||||||
"transaction_id" BIGINT NOT NULL,
|
"transaction_id" BIGINT NOT NULL,
|
||||||
"tag_id" BIGINT NOT NULL,
|
"tag_id" BIGINT NOT NULL,
|
||||||
|
"op_id" BIGINT NOT NULL DEFAULT 0,
|
||||||
"is_delete" BOOLEAN NOT NULL DEFAULT FALSE,
|
"is_delete" BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
||||||
"update_at" TIMESTAMP NOT NULL DEFAULT current_timestamp
|
"update_at" TIMESTAMP NOT NULL DEFAULT current_timestamp
|
||||||
@@ -53,7 +63,8 @@ CREATE TABLE "accounts" (
|
|||||||
"id" BIGSERIAL PRIMARY KEY,
|
"id" BIGSERIAL PRIMARY KEY,
|
||||||
"uid" BIGINT NOT NULL,
|
"uid" BIGINT NOT NULL,
|
||||||
"name" TEXT NOT NULL,
|
"name" TEXT NOT NULL,
|
||||||
"type" BIGINT NOT NULL DEFAULT 0,
|
"account_type" BIGINT NOT NULL DEFAULT 0,
|
||||||
|
"op_id" BIGINT NOT NULL DEFAULT 0,
|
||||||
"is_delete" BOOLEAN NOT NULL DEFAULT FALSE,
|
"is_delete" BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
||||||
"update_at" TIMESTAMP NOT NULL DEFAULT current_timestamp
|
"update_at" TIMESTAMP NOT NULL DEFAULT current_timestamp
|
||||||
@@ -62,9 +73,12 @@ CREATE TABLE "accounts" (
|
|||||||
CREATE TABLE "amounts" (
|
CREATE TABLE "amounts" (
|
||||||
"id" BIGSERIAL PRIMARY KEY,
|
"id" BIGSERIAL PRIMARY KEY,
|
||||||
"uid" BIGINT NOT NULL,
|
"uid" BIGINT NOT NULL,
|
||||||
|
"account_id" BIGINT NOT NULL,
|
||||||
"transaction_id" BIGINT NOT NULL,
|
"transaction_id" BIGINT NOT NULL,
|
||||||
"value" BIGINT NOT NULL DEFAULT 0,
|
"value" BIGINT NOT NULL DEFAULT 0,
|
||||||
"expo" BIGINT NOT NULL DEFAULT 5,
|
"expo" BIGINT NOT NULL DEFAULT 5,
|
||||||
|
"currency" TEXT NOT NULL DEFAULT '',
|
||||||
|
"op_id" BIGINT NOT NULL DEFAULT 0,
|
||||||
"is_delete" BOOLEAN NOT NULL DEFAULT FALSE,
|
"is_delete" BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
||||||
"update_at" TIMESTAMP NOT NULL DEFAULT current_timestamp
|
"update_at" TIMESTAMP NOT NULL DEFAULT current_timestamp
|
||||||
@@ -72,10 +86,27 @@ CREATE TABLE "amounts" (
|
|||||||
|
|
||||||
CREATE TABLE "users" (
|
CREATE TABLE "users" (
|
||||||
"id" BIGSERIAL PRIMARY KEY,
|
"id" BIGSERIAL PRIMARY KEY,
|
||||||
"username" TEXT NOT NULL,
|
"username" TEXT NOT NULL UNIQUE,
|
||||||
"password" TEXT NOT NULL,
|
"password" TEXT NOT NULL,
|
||||||
"mail" TEXT NOT NULL,
|
"mail" TEXT NOT NULL,
|
||||||
"is_delete" BOOLEAN NOT NULL DEFAULT FALSE,
|
"is_delete" BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
||||||
"update_at" TIMESTAMP NOT NULL DEFAULT current_timestamp
|
"update_at" TIMESTAMP NOT NULL DEFAULT current_timestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE "operations" (
|
||||||
|
"id" BIGSERIAL PRIMARY KEY,
|
||||||
|
"uid" BIGINT NOT NULL,
|
||||||
|
"entity_type" BIGINT NOT NULL,
|
||||||
|
"entity_id" BIGINT NOT NULL,
|
||||||
|
"action" BIGINT NOT NULL,
|
||||||
|
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp,
|
||||||
|
"update_at" TIMESTAMP NOT NULL DEFAULT current_timestamp
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE "operation_snapshots" (
|
||||||
|
"id" BIGSERIAL PRIMARY KEY,
|
||||||
|
"uid" BIGINT NOT NULL,
|
||||||
|
"max_op_id" BIGINT NOT NULL,
|
||||||
|
"create_at" TIMESTAMP NOT NULL DEFAULT current_timestamp
|
||||||
|
);
|
||||||
|
|||||||
@@ -13,69 +13,68 @@ use serde::{Deserialize, Serialize};
|
|||||||
use crate::model::db_model;
|
use crate::model::db_model;
|
||||||
use crate::model::schema;
|
use crate::model::schema;
|
||||||
use crate::util;
|
use crate::util;
|
||||||
// use crate::model::schema::categories::dsl::categories;
|
|
||||||
use crate::util::req::CommonResp;
|
use crate::util::req::CommonResp;
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
use crate::middleware::auth;
|
use crate::middleware::auth;
|
||||||
use crate::middleware::auth::Claims;
|
use crate::middleware::auth::Claims;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct CreateAccountRequest {
|
||||||
|
name: String,
|
||||||
|
account_type: i64,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct CreateCategoryResponse {
|
pub struct CreateAccountResponse {
|
||||||
id: i64,
|
id: i64,
|
||||||
name: String,
|
name: String,
|
||||||
|
account_type: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_nest_handlers() -> Router<crate::AppState> {
|
pub fn get_nest_handlers() -> Router<crate::AppState> {
|
||||||
Router::new()
|
Router::new()
|
||||||
.route("/", post(create_category).get(get_all_categories))
|
.route("/", post(create_account).get(get_all_accounts))
|
||||||
.route("/:id", post(update_category).get(get_category))
|
.route("/{id}", post(update_account).get(get_account))
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct CreateCategoryRequest {
|
|
||||||
name: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[debug_handler]
|
#[debug_handler]
|
||||||
pub async fn create_category(
|
pub async fn create_account(
|
||||||
State(app_state): State<crate::AppState>,
|
State(app_state): State<crate::AppState>,
|
||||||
claims: Claims,
|
claims: Claims,
|
||||||
Json(payload): Json<CreateCategoryRequest>,
|
Json(payload): Json<CreateAccountRequest>,
|
||||||
) -> Result<Json<db_model::Category>, (StatusCode, String)> {
|
) -> Result<Json<db_model::Account>, (StatusCode, String)> {
|
||||||
let uid: i64 = claims.uid.clone(); // TODO replace with actual user id.
|
let uid: i64 = claims.uid.clone();
|
||||||
// let ret = CreateCategoryResponse{id: 134132413541, name: "24532452".to_string()};
|
|
||||||
let conn = app_state
|
let conn = app_state
|
||||||
.db
|
.db
|
||||||
.get()
|
.get()
|
||||||
.await
|
.await
|
||||||
.map_err(util::req::internal_error)?;
|
.map_err(util::req::internal_error)?;
|
||||||
let new_category = db_model::CategoryForm {
|
let new_account = db_model::AccountForm {
|
||||||
name: payload.name,
|
name: payload.name,
|
||||||
uid,
|
account_type: payload.account_type,
|
||||||
|
uid: uid,
|
||||||
};
|
};
|
||||||
let res = conn
|
let res = conn
|
||||||
.interact(move |conn| {
|
.interact(move |conn| {
|
||||||
diesel::insert_into(schema::categories::table)
|
diesel::insert_into(schema::accounts::table)
|
||||||
.values(&new_category)
|
.values(&new_account)
|
||||||
.returning(db_model::Category::as_returning())
|
.returning(db_model::Account::as_returning())
|
||||||
.get_result(conn)
|
.get_result(conn)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.map_err(util::req::internal_error)?
|
.map_err(util::req::internal_error)?
|
||||||
.map_err(util::req::internal_error)?;
|
.map_err(util::req::internal_error)?;
|
||||||
// let ret = CreateCategoryResponse{id: res.id, name: res.name};
|
|
||||||
Ok(Json(res))
|
Ok(Json(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update_category(
|
pub async fn update_account(
|
||||||
Path(id): Path<i64>,
|
Path(id): Path<i64>,
|
||||||
State(app_state): State<crate::AppState>,
|
State(app_state): State<crate::AppState>,
|
||||||
claims: Claims,
|
claims: Claims,
|
||||||
Json(payload): Json<CreateCategoryRequest>,
|
Json(payload): Json<CreateAccountRequest>,
|
||||||
) -> Result<Json<CommonResp>, (StatusCode, String)> {
|
) -> Result<Json<CommonResp>, (StatusCode, String)> {
|
||||||
let uid: i64 = claims.uid.clone(); // TODO replace with actual user id.
|
let uid: i64 = claims.uid.clone();
|
||||||
// let ret = CreateCategoryResponse{id: 134132413541, name: "24532452".to_string()};
|
|
||||||
let conn = app_state
|
let conn = app_state
|
||||||
.db
|
.db
|
||||||
.get()
|
.get()
|
||||||
@@ -84,28 +83,28 @@ pub async fn update_category(
|
|||||||
let now = Utc::now().naive_utc();
|
let now = Utc::now().naive_utc();
|
||||||
let res = conn
|
let res = conn
|
||||||
.interact(move |conn| {
|
.interact(move |conn| {
|
||||||
diesel::update(schema::categories::table)
|
diesel::update(schema::accounts::table)
|
||||||
.filter(schema::categories::id.eq(id))
|
.filter(schema::accounts::id.eq(id))
|
||||||
.filter(schema::categories::uid.eq(uid))
|
.filter(schema::accounts::uid.eq(uid))
|
||||||
.set((
|
.set((
|
||||||
schema::categories::name.eq(payload.name),
|
schema::accounts::name.eq(payload.name),
|
||||||
schema::categories::update_at.eq(now),
|
schema::accounts::account_type.eq(payload.account_type),
|
||||||
|
schema::accounts::update_at.eq(now),
|
||||||
))
|
))
|
||||||
.execute(conn)
|
.execute(conn)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.map_err(util::req::internal_error)?
|
.map_err(util::req::internal_error)?
|
||||||
.map_err(util::req::internal_error)?;
|
.map_err(util::req::internal_error)?;
|
||||||
// let ret = CreateCategoryResponse{id: res.id, name: res.name};
|
|
||||||
let resp = util::req::CommonResp { code: 0 };
|
let resp = util::req::CommonResp { code: 0 };
|
||||||
Ok(Json(resp))
|
Ok(Json(resp))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_category(
|
pub async fn get_account(
|
||||||
Path(id): Path<i64>,
|
Path(id): Path<i64>,
|
||||||
State(app_state): State<crate::AppState>,
|
State(app_state): State<crate::AppState>,
|
||||||
claims: Claims,
|
claims: Claims,
|
||||||
) -> Result<Json<db_model::Category>, (StatusCode, String)> {
|
) -> Result<Json<db_model::Account>, (StatusCode, String)> {
|
||||||
let uid: i64 = claims.uid.clone();
|
let uid: i64 = claims.uid.clone();
|
||||||
let conn = app_state
|
let conn = app_state
|
||||||
.db
|
.db
|
||||||
@@ -114,10 +113,10 @@ pub async fn get_category(
|
|||||||
.map_err(util::req::internal_error)?;
|
.map_err(util::req::internal_error)?;
|
||||||
let res = conn
|
let res = conn
|
||||||
.interact(move |conn| {
|
.interact(move |conn| {
|
||||||
schema::categories::table
|
schema::accounts::table
|
||||||
.filter(schema::categories::id.eq(id))
|
.filter(schema::accounts::id.eq(id))
|
||||||
.filter(schema::categories::uid.eq(uid))
|
.filter(schema::accounts::uid.eq(uid))
|
||||||
.select(db_model::Category::as_select())
|
.select(db_model::Account::as_select())
|
||||||
.limit(1)
|
.limit(1)
|
||||||
.get_result(conn)
|
.get_result(conn)
|
||||||
})
|
})
|
||||||
@@ -127,10 +126,10 @@ pub async fn get_category(
|
|||||||
Ok(Json(res))
|
Ok(Json(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_all_categories(
|
pub async fn get_all_accounts(
|
||||||
State(app_state): State<crate::AppState>,
|
State(app_state): State<crate::AppState>,
|
||||||
claims: Claims,
|
claims: Claims,
|
||||||
) -> Result<Json<Vec<db_model::Category>>, (StatusCode, String)> {
|
) -> Result<Json<Vec<db_model::Account>>, (StatusCode, String)> {
|
||||||
let uid: i64 = claims.uid.clone();
|
let uid: i64 = claims.uid.clone();
|
||||||
let conn = app_state
|
let conn = app_state
|
||||||
.db
|
.db
|
||||||
@@ -139,9 +138,9 @@ pub async fn get_all_categories(
|
|||||||
.map_err(util::req::internal_error)?;
|
.map_err(util::req::internal_error)?;
|
||||||
let res = conn
|
let res = conn
|
||||||
.interact(move |conn| {
|
.interact(move |conn| {
|
||||||
schema::categories::table
|
schema::accounts::table
|
||||||
.filter(schema::categories::uid.eq(uid))
|
.filter(schema::accounts::uid.eq(uid))
|
||||||
.select(db_model::Category::as_select())
|
.select(db_model::Account::as_select())
|
||||||
.load(conn)
|
.load(conn)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
145
src/ledger/book.rs
Normal file
145
src/ledger/book.rs
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
use axum::routing::{get, post};
|
||||||
|
use axum::{
|
||||||
|
extract::{Path, State},
|
||||||
|
http::StatusCode,
|
||||||
|
Json, Router,
|
||||||
|
};
|
||||||
|
use axum_macros::debug_handler;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
// use diesel::update;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
// use serde_json::to_string;
|
||||||
|
use crate::model::db_model;
|
||||||
|
use crate::model::schema;
|
||||||
|
use crate::util;
|
||||||
|
use crate::util::req::CommonResp;
|
||||||
|
use chrono::prelude::*;
|
||||||
|
use tracing::info;
|
||||||
|
use crate::middleware::auth;
|
||||||
|
use crate::middleware::auth::Claims;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct CreateBookRequest {
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct CreateBookResponse {
|
||||||
|
id: i64,
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_nest_handlers() -> Router<crate::AppState> {
|
||||||
|
Router::new()
|
||||||
|
.route("/", post(create_book).get(get_all_books))
|
||||||
|
.route("/{id}", post(update_book).get(get_book))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[debug_handler]
|
||||||
|
pub async fn create_book(
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
Json(payload): Json<CreateBookRequest>,
|
||||||
|
) -> Result<Json<db_model::Book>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let new_book = db_model::BookForm {
|
||||||
|
name: payload.name,
|
||||||
|
uid,
|
||||||
|
};
|
||||||
|
let res = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
diesel::insert_into(schema::books::table)
|
||||||
|
.values(&new_book)
|
||||||
|
.returning(db_model::Book::as_returning())
|
||||||
|
.get_result(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
Ok(Json(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_book(
|
||||||
|
Path(id): Path<i64>,
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
Json(payload): Json<CreateBookRequest>,
|
||||||
|
) -> Result<Json<CommonResp>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let now = Utc::now().naive_utc();
|
||||||
|
let res = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
diesel::update(schema::books::table)
|
||||||
|
.filter(schema::books::id.eq(id))
|
||||||
|
.filter(schema::books::uid.eq(uid))
|
||||||
|
.set((
|
||||||
|
schema::books::name.eq(payload.name),
|
||||||
|
schema::books::update_at.eq(now),
|
||||||
|
))
|
||||||
|
.execute(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let resp = util::req::CommonResp { code: 0 };
|
||||||
|
Ok(Json(resp))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_book(
|
||||||
|
Path(id): Path<i64>,
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
) -> Result<Json<db_model::Book>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let res = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
schema::books::table
|
||||||
|
.filter(schema::books::id.eq(id))
|
||||||
|
.filter(schema::books::uid.eq(uid))
|
||||||
|
.select(db_model::Book::as_select())
|
||||||
|
.limit(1)
|
||||||
|
.get_result(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
Ok(Json(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_all_books(
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
) -> Result<Json<Vec<db_model::Book>>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let res = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
schema::books::table
|
||||||
|
.filter(schema::books::uid.eq(uid))
|
||||||
|
.select(db_model::Book::as_select())
|
||||||
|
.load(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
Ok(Json(res))
|
||||||
|
}
|
||||||
267
src/ledger/category.rs
Normal file
267
src/ledger/category.rs
Normal file
@@ -0,0 +1,267 @@
|
|||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use std::sync::Arc;
|
||||||
|
// use std::sync::Arc;
|
||||||
|
use axum::routing::{get, post};
|
||||||
|
use axum::{
|
||||||
|
extract::{Path, State},
|
||||||
|
http::StatusCode,
|
||||||
|
Json, Router,
|
||||||
|
};
|
||||||
|
use axum_macros::debug_handler;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
// use diesel::update;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
// use serde_json::to_string;
|
||||||
|
use crate::model::db_model;
|
||||||
|
use crate::model::schema;
|
||||||
|
use crate::model::schema::categories::parent_category_id;
|
||||||
|
use crate::util;
|
||||||
|
// use crate::model::schema::categories::dsl::categories;
|
||||||
|
use crate::util::req::CommonResp;
|
||||||
|
use chrono::prelude::*;
|
||||||
|
use tracing::info;
|
||||||
|
use crate::middleware::auth;
|
||||||
|
use crate::middleware::auth::Claims;
|
||||||
|
use crate::model::db_model::Category;
|
||||||
|
use crate::util::operation::{
|
||||||
|
EntityType, ENTITY_CATEGORY,
|
||||||
|
ActionType, ACTION_CREATE, ACTION_UPDATE, ACTION_DELETE,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn get_nest_handlers() -> Router<crate::AppState> {
|
||||||
|
Router::new()
|
||||||
|
.route("/", post(create_category).get(get_all_categories))
|
||||||
|
.route("/{id}", post(update_category).get(get_category))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct CreateCategoryRequest {
|
||||||
|
name: String,
|
||||||
|
level: String,
|
||||||
|
parent_category_id: String,
|
||||||
|
book_id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct CreateCategoryResponse {
|
||||||
|
id: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[debug_handler]
|
||||||
|
pub async fn create_category(
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
Json(payload): Json<CreateCategoryRequest>,
|
||||||
|
) -> Result<Json<CreateCategoryResponse>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let level: i32 = match payload.level.parse() {
|
||||||
|
Ok(level) => level,
|
||||||
|
Err(_) => {
|
||||||
|
return Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
"Invalid level".to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let parent_cid: i64 = match payload.parent_category_id.parse(){
|
||||||
|
Ok(id) => id,
|
||||||
|
Err(_) => {
|
||||||
|
return Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
"Invalid parent_category_id".to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let book_id: i64 = match payload.book_id.parse() {
|
||||||
|
Ok(id) => id,
|
||||||
|
Err(_) => {
|
||||||
|
return Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
"Invalid book_id".to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let new_category = db_model::CategoryForm {
|
||||||
|
name: payload.name,
|
||||||
|
uid: uid,
|
||||||
|
level: level,
|
||||||
|
parent_category_id: parent_cid,
|
||||||
|
book_id: book_id,
|
||||||
|
};
|
||||||
|
let new_operation = db_model::CreateOperation{
|
||||||
|
uid: uid,
|
||||||
|
entity_type: ENTITY_CATEGORY,
|
||||||
|
entity_id: 0,
|
||||||
|
action: ACTION_CREATE,
|
||||||
|
};
|
||||||
|
let mut create_response = CreateCategoryResponse{
|
||||||
|
id: 0,
|
||||||
|
};
|
||||||
|
// Check if book exists under current user
|
||||||
|
let book_exists = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
schema::books::table
|
||||||
|
.select(diesel::dsl::exists(db_model::Category.as_select().filter(schema::books::id.eq(book_id))))
|
||||||
|
// .filter(schema::books::uid.eq(uid))(schema::books::id.eq(book_id))))
|
||||||
|
.get_result::<bool>(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
|
||||||
|
if !book_exists {
|
||||||
|
return Err((StatusCode::NOT_FOUND, "Book not found for the user".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let cuid = uid;
|
||||||
|
let create_result = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
conn.transaction(|conn| {
|
||||||
|
let category = diesel::insert_into(schema::categories::table)
|
||||||
|
.values(&new_category)
|
||||||
|
.returning(db_model::Category::as_returning())
|
||||||
|
.get_result(conn)?;
|
||||||
|
let operation = diesel::insert_into(schema::operations::table)
|
||||||
|
.values(&new_operation)
|
||||||
|
.returning(db_model::Operation::as_returning())
|
||||||
|
.get_result(conn)?;
|
||||||
|
diesel::update(schema::categories::table)
|
||||||
|
.filter(schema::categories::id.eq(category.id))
|
||||||
|
.filter(schema::categories::uid.eq(cuid))
|
||||||
|
.set((schema::categories::op_id.eq(operation.id)))
|
||||||
|
.execute(conn)?;
|
||||||
|
diesel::update(schema::operations::table)
|
||||||
|
.filter(schema::operations::id.eq(operation.id))
|
||||||
|
.filter(schema::operations::uid.eq(cuid))
|
||||||
|
.set((schema::operations::entity_id.eq(category.id)))
|
||||||
|
.execute(conn)?;
|
||||||
|
diesel::result::QueryResult::Ok((category.id))
|
||||||
|
})
|
||||||
|
// diesel::insert_into(schema::categories::table)
|
||||||
|
// .values(&new_category)
|
||||||
|
// .returning(db_model::Category::as_returning())
|
||||||
|
// .get_result(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
create_response.id = create_result;
|
||||||
|
Ok(Json(create_response))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_category(
|
||||||
|
Path(id): Path<i64>,
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
Json(payload): Json<CreateCategoryRequest>,
|
||||||
|
) -> Result<Json<CommonResp>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let level: i32 = match payload.level.parse() {
|
||||||
|
Ok(level) => level,
|
||||||
|
Err(_) => {
|
||||||
|
return Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
"Invalid level".to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let parent_cid: i64 = match payload.parent_category_id.parse(){
|
||||||
|
Ok(id) => id,
|
||||||
|
Err(_) => {
|
||||||
|
return Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
"Invalid parent_category_id".to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let book_id: i64 = match payload.book_id.parse() {
|
||||||
|
Ok(id) => id,
|
||||||
|
Err(_) => {
|
||||||
|
return Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
"Invalid book_id".to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let now = Utc::now().naive_utc();
|
||||||
|
let res = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
diesel::update(schema::categories::table)
|
||||||
|
.filter(schema::categories::id.eq(id))
|
||||||
|
.filter(schema::categories::uid.eq(uid))
|
||||||
|
.set((
|
||||||
|
schema::categories::name.eq(payload.name),
|
||||||
|
schema::categories::level.eq(level),
|
||||||
|
schema::categories::parent_category_id.eq(parent_cid),
|
||||||
|
schema::categories::update_at.eq(now),
|
||||||
|
))
|
||||||
|
.execute(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
// let ret = CreateCategoryResponse{id: res.id, name: res.name};
|
||||||
|
let resp = util::req::CommonResp { code: 0 };
|
||||||
|
Ok(Json(resp))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_category(
|
||||||
|
Path(id): Path<i64>,
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
) -> Result<Json<db_model::Category>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let res = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
schema::categories::table
|
||||||
|
.filter(schema::categories::id.eq(id))
|
||||||
|
.filter(schema::categories::uid.eq(uid))
|
||||||
|
.select(db_model::Category::as_select())
|
||||||
|
.limit(1)
|
||||||
|
.get_result(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
Ok(Json(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_all_categories(
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
) -> Result<Json<Vec<db_model::Category>>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let res = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
schema::categories::table
|
||||||
|
.filter(schema::categories::uid.eq(uid))
|
||||||
|
.select(db_model::Category::as_select())
|
||||||
|
.load(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
Ok(Json(res))
|
||||||
|
}
|
||||||
6
src/ledger/mod.rs
Normal file
6
src/ledger/mod.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
pub mod category;
|
||||||
|
pub mod tag;
|
||||||
|
pub mod book;
|
||||||
|
pub mod account;
|
||||||
|
pub mod transaction;
|
||||||
|
pub mod operation;
|
||||||
81
src/ledger/operation.rs
Normal file
81
src/ledger/operation.rs
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
use diesel::prelude::*;
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
use axum::{extract::{Path, State, Query}, http::StatusCode, Json, Router};
|
||||||
|
use axum::routing::get;
|
||||||
|
use diesel::dsl::max;
|
||||||
|
use crate::model::{db_model, schema};
|
||||||
|
use crate::middleware::auth::Claims;
|
||||||
|
use crate::model::db_model::Operation;
|
||||||
|
use crate::util;
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct GetOperationsResponse {
|
||||||
|
start: i64,
|
||||||
|
end: i64,
|
||||||
|
total: i64,
|
||||||
|
operations: Vec<db_model::Operation>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct GetOperationsParam {
|
||||||
|
start: i64,
|
||||||
|
limit: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_nest_handlers() -> Router<crate::AppState> {
|
||||||
|
Router::new()
|
||||||
|
.route("/", get(get_operations))
|
||||||
|
}
|
||||||
|
|
||||||
|
// get_single_operation
|
||||||
|
|
||||||
|
pub async fn get_operations(
|
||||||
|
query_param: Query<GetOperationsParam>,
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
) -> Result<Json<GetOperationsResponse>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let start: i64 = match query_param.start {
|
||||||
|
..0 => 0,
|
||||||
|
_ => query_param.start
|
||||||
|
};
|
||||||
|
let limit: i32 = match query_param.limit {
|
||||||
|
..0 => 0,
|
||||||
|
crate::model::req::MAX_QUERY_LIMIT.. => crate::model::req::MAX_QUERY_LIMIT,
|
||||||
|
_ => query_param.limit
|
||||||
|
};
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let mut res = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
schema::operations::table
|
||||||
|
.filter(schema::operations::uid.eq(uid))
|
||||||
|
.filter(schema::operations::id.ge(start))
|
||||||
|
.limit(limit as i64)
|
||||||
|
.select(Operation::as_select())
|
||||||
|
.load(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
res.sort_by(|a,b| a.id.cmp(&b.id));
|
||||||
|
let res_start: i64 = match res.first() {
|
||||||
|
Some(r) => r.id,
|
||||||
|
None => 0,
|
||||||
|
};
|
||||||
|
let res_end = match res.last() {
|
||||||
|
Some(r) => r.id,
|
||||||
|
None => 0,
|
||||||
|
};
|
||||||
|
let resp = GetOperationsResponse{
|
||||||
|
start: res_start,
|
||||||
|
end: res_end,
|
||||||
|
total: res.len() as i64,
|
||||||
|
operations: res,
|
||||||
|
};
|
||||||
|
Ok(Json(resp))
|
||||||
|
// Ok(Json(res))
|
||||||
|
}
|
||||||
157
src/ledger/tag.rs
Normal file
157
src/ledger/tag.rs
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
// use std::sync::Arc;
|
||||||
|
use axum::routing::{get, post};
|
||||||
|
use axum::{
|
||||||
|
extract::{Path, State},
|
||||||
|
http::StatusCode,
|
||||||
|
Json, Router,
|
||||||
|
};
|
||||||
|
use axum_macros::debug_handler;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
// use diesel::update;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
// use serde_json::to_string;
|
||||||
|
use crate::model::db_model;
|
||||||
|
use crate::model::schema;
|
||||||
|
use crate::util;
|
||||||
|
use crate::util::req::CommonResp;
|
||||||
|
use chrono::prelude::*;
|
||||||
|
use tracing::info;
|
||||||
|
use crate::middleware::auth;
|
||||||
|
use crate::middleware::auth::Claims;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct CreateTagRequest {
|
||||||
|
book_id: i64,
|
||||||
|
name: String,
|
||||||
|
level: i32,
|
||||||
|
parent_tag_id: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct CreateTagResponse {
|
||||||
|
id: i64,
|
||||||
|
name: String,
|
||||||
|
book_id: i64,
|
||||||
|
level: i32,
|
||||||
|
parent_tag_id: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_nest_handlers() -> Router<crate::AppState> {
|
||||||
|
Router::new()
|
||||||
|
.route("/", post(create_tag).get(get_all_tags))
|
||||||
|
.route("/{id}", post(update_tag).get(get_tag))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[debug_handler]
|
||||||
|
pub async fn create_tag(
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
Json(payload): Json<CreateTagRequest>,
|
||||||
|
) -> Result<Json<db_model::Tag>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let new_tag = db_model::TagForm {
|
||||||
|
book_id:payload.book_id,
|
||||||
|
name: payload.name,
|
||||||
|
uid: uid,
|
||||||
|
level: payload.level,
|
||||||
|
parent_tag_id: payload.parent_tag_id,
|
||||||
|
};
|
||||||
|
let res = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
diesel::insert_into(schema::tags::table)
|
||||||
|
.values(&new_tag)
|
||||||
|
.returning(db_model::Tag::as_returning())
|
||||||
|
.get_result(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
Ok(Json(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_tag(
|
||||||
|
Path(id): Path<i64>,
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
Json(payload): Json<CreateTagRequest>,
|
||||||
|
) -> Result<Json<CommonResp>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let now = Utc::now().naive_utc();
|
||||||
|
let res = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
diesel::update(schema::tags::table)
|
||||||
|
.filter(schema::tags::id.eq(id))
|
||||||
|
.filter(schema::tags::uid.eq(uid))
|
||||||
|
.set((
|
||||||
|
schema::tags::name.eq(payload.name),
|
||||||
|
schema::tags::level.eq(payload.level),
|
||||||
|
schema::tags::parent_tag_id.eq(payload.parent_tag_id),
|
||||||
|
schema::tags::update_at.eq(now),
|
||||||
|
))
|
||||||
|
.execute(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let resp = util::req::CommonResp { code: 0 };
|
||||||
|
Ok(Json(resp))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_tag(
|
||||||
|
Path(id): Path<i64>,
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
) -> Result<Json<db_model::Tag>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let res = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
schema::tags::table
|
||||||
|
.filter(schema::tags::id.eq(id))
|
||||||
|
.filter(schema::tags::uid.eq(uid))
|
||||||
|
.select(db_model::Tag::as_select())
|
||||||
|
.limit(1)
|
||||||
|
.get_result(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
Ok(Json(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_all_tags(
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
) -> Result<Json<Vec<db_model::Tag>>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let res = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
schema::tags::table
|
||||||
|
.filter(schema::tags::uid.eq(uid))
|
||||||
|
.select(db_model::Tag::as_select())
|
||||||
|
.load(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
Ok(Json(res))
|
||||||
|
}
|
||||||
503
src/ledger/transaction.rs
Normal file
503
src/ledger/transaction.rs
Normal file
@@ -0,0 +1,503 @@
|
|||||||
|
use axum::extract::Query;
|
||||||
|
use axum::routing::{get, post};
|
||||||
|
use axum::{
|
||||||
|
extract::{Path, State},
|
||||||
|
http::StatusCode,
|
||||||
|
Json, Router,
|
||||||
|
};
|
||||||
|
use axum_macros::debug_handler;
|
||||||
|
use diesel::dsl::exists;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
use std::fmt;
|
||||||
|
use std::i64::MAX;
|
||||||
|
use chrono::ParseResult;
|
||||||
|
// use diesel::update;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
// use serde_json::to_string;
|
||||||
|
use crate::middleware::auth;
|
||||||
|
use crate::middleware::auth::Claims;
|
||||||
|
use crate::model::{db_model,schema,req};
|
||||||
|
use crate::util;
|
||||||
|
use crate::util::req::CommonResp;
|
||||||
|
use chrono::prelude::*;
|
||||||
|
use tracing::info;
|
||||||
|
use crate::model::req::{GetAmountByTransactionRangeParams, GetAmountParams, MAX_QUERY_LIMIT};
|
||||||
|
|
||||||
|
const PAYMENT_STORE_EXPO: i64 = 5;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct SubmitTransactionRequest {
|
||||||
|
description: String,
|
||||||
|
book_id: i64,
|
||||||
|
category_id: i64,
|
||||||
|
tag_ids: Vec<i64>,
|
||||||
|
time: String, // RFC 3339 "2020-04-12T22:10:57+02:00"
|
||||||
|
amounts: Vec<SubmitTransactionAmountRequest>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct SubmitTransactionAmountRequest {
|
||||||
|
account_id: i64,
|
||||||
|
payment: String,
|
||||||
|
expo: i32,
|
||||||
|
currency: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct CreateTransactionResponse {
|
||||||
|
pub id: i64,
|
||||||
|
pub book_id: i64,
|
||||||
|
pub description: String,
|
||||||
|
pub category_id: i64,
|
||||||
|
pub time: chrono::DateTime<Utc>,
|
||||||
|
pub tag_ids: Vec<i64>,
|
||||||
|
pub amount_ids: Vec<i64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct BatchGetTransactionRequest {
|
||||||
|
pub transaction_ids: Vec<i64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct BatchGetTransactionAmountRequest {
|
||||||
|
pub transaction_ids: Vec<i64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_nest_handlers() -> Router<crate::AppState> {
|
||||||
|
Router::new()
|
||||||
|
.route("/entry/batch_get", post(batch_get_transactions))
|
||||||
|
.route(
|
||||||
|
"/entry",
|
||||||
|
post(create_transaction) // create new transaction entry with amount
|
||||||
|
.get(get_all_transactions),// get all transactions with entry
|
||||||
|
)
|
||||||
|
.route("/entry/{id}", get(get_transaction)) // get transaction entry
|
||||||
|
.route("/amount/by_transaction_id", get(get_amounts_by_tid))
|
||||||
|
.route("/amount/batch_get_by_transaction_id", post(batch_get_amounts_by_tid))
|
||||||
|
.route("/amount", get(get_all_amounts_by_tid_range))
|
||||||
|
// .route("/entry/amount/:id", post(update_amount).get(get_amount)) // require query param tid=transaction_id
|
||||||
|
}
|
||||||
|
// implementation, or do something in between.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct TransactionError;
|
||||||
|
|
||||||
|
impl fmt::Display for TransactionError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "invalid transaction insert result")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[debug_handler]
|
||||||
|
pub async fn create_transaction(
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
Json(payload): Json<SubmitTransactionRequest>,
|
||||||
|
) -> Result<String, (StatusCode, String)> {
|
||||||
|
// ) -> Result<Json<db_model::Transaction>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
|
||||||
|
// 1. check related ids
|
||||||
|
// 1.1 check book id
|
||||||
|
if payload.book_id <= 0 {
|
||||||
|
return Err((StatusCode::BAD_REQUEST, "invalid book id".to_string()));
|
||||||
|
}
|
||||||
|
let check_book = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
diesel::select(exists(
|
||||||
|
schema::books::table
|
||||||
|
.filter(schema::books::uid.eq(uid))
|
||||||
|
.filter(schema::books::id.eq(payload.book_id)),
|
||||||
|
))
|
||||||
|
.get_result::<bool>(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
|
||||||
|
println!("book valid: {}", check_book);
|
||||||
|
if !check_book {
|
||||||
|
return Err((StatusCode::BAD_REQUEST, "invalid book id".to_string()));
|
||||||
|
}
|
||||||
|
// 1.2 check category id
|
||||||
|
if payload.category_id <= 0 {
|
||||||
|
return Err((StatusCode::BAD_REQUEST, "invalid category id".to_string()));
|
||||||
|
}
|
||||||
|
let check_category = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
diesel::select(exists(
|
||||||
|
schema::categories::table
|
||||||
|
.filter(schema::categories::uid.eq(uid))
|
||||||
|
.filter(schema::categories::id.eq(payload.category_id)),
|
||||||
|
))
|
||||||
|
.get_result::<bool>(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
|
||||||
|
println!("category valid: {}", check_category);
|
||||||
|
if !check_category {
|
||||||
|
return Err((StatusCode::BAD_REQUEST, "invalid category id".to_string()));
|
||||||
|
}
|
||||||
|
// 1.3 check tag ids
|
||||||
|
let payload_tag_size = payload.tag_ids.len() as i64;
|
||||||
|
let mut check_tag = payload_tag_size == 0;
|
||||||
|
if !check_tag {
|
||||||
|
let check_tag_count = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
schema::tags::table
|
||||||
|
.filter(schema::tags::uid.eq(uid))
|
||||||
|
.filter(schema::tags::id.eq_any(payload.tag_ids))
|
||||||
|
.select(diesel::dsl::count(schema::tags::id))
|
||||||
|
.first(conn)
|
||||||
|
.map(|x: i64| x as i64)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
println!("check tag: {}", check_tag_count);
|
||||||
|
check_tag = check_tag_count == payload_tag_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("tag valid: {}", check_tag);
|
||||||
|
|
||||||
|
if !check_tag {
|
||||||
|
return Err((StatusCode::BAD_REQUEST, "invalid tag ids".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1.4 check account
|
||||||
|
let mut check_amount = true;
|
||||||
|
let mut amounts: Vec<db_model::AmountForm> = Vec::new();
|
||||||
|
for amount_req in payload.amounts {
|
||||||
|
// Parse and check payment
|
||||||
|
let parse_payment_result =
|
||||||
|
util::math::parse_payment_to_value_expo(amount_req.payment.clone(), PAYMENT_STORE_EXPO);
|
||||||
|
let value: i64;
|
||||||
|
let expo: i64;
|
||||||
|
match parse_payment_result {
|
||||||
|
Ok((val, expon)) => {
|
||||||
|
value = val;
|
||||||
|
expo = expon;
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let amount = db_model::AmountForm {
|
||||||
|
uid: uid,
|
||||||
|
account_id: amount_req.account_id,
|
||||||
|
transaction_id: 0,
|
||||||
|
value: value,
|
||||||
|
expo: expo,
|
||||||
|
currency: amount_req.currency.clone(),
|
||||||
|
};
|
||||||
|
check_amount = check_amount && true;
|
||||||
|
amounts.push(amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
if !check_amount || amounts.len() == 0 {
|
||||||
|
return Err((StatusCode::BAD_REQUEST, "invalid amount".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. build and insert into db
|
||||||
|
let datetime_tz = chrono::DateTime::parse_from_rfc3339(payload.time.as_str());
|
||||||
|
let datetime = match datetime_tz {
|
||||||
|
Ok(dt) => dt,
|
||||||
|
Err(_) => {
|
||||||
|
return Err((StatusCode::BAD_REQUEST, "invalid datetime, must be RFC 3339".to_string()))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let datetime_utc = datetime.with_timezone(&Utc);
|
||||||
|
|
||||||
|
let mut transaction_resp: CreateTransactionResponse;
|
||||||
|
let mut amount_ids: Vec<i64> = Vec::new();
|
||||||
|
|
||||||
|
let transaction = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
conn.transaction(|conn| {
|
||||||
|
let new_transaction = db_model::TransactionForm {
|
||||||
|
id: None,
|
||||||
|
uid: uid,
|
||||||
|
book_id: payload.book_id,
|
||||||
|
description: payload.description,
|
||||||
|
category_id: payload.category_id,
|
||||||
|
// time: payload
|
||||||
|
time: datetime_utc,
|
||||||
|
};
|
||||||
|
let inserted_transactions = diesel::insert_into(schema::transactions::table)
|
||||||
|
.values(&new_transaction)
|
||||||
|
.returning(db_model::Transaction::as_returning())
|
||||||
|
.get_results(conn);
|
||||||
|
|
||||||
|
let mut new_tr_vec: Vec<db_model::Transaction>;
|
||||||
|
match inserted_transactions {
|
||||||
|
Ok(tr) => new_tr_vec = tr,
|
||||||
|
Err(e) => {
|
||||||
|
return diesel::result::QueryResult::Err(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut new_tid = 0 as i64;
|
||||||
|
let new_tr = new_tr_vec.get(0);
|
||||||
|
match new_tr {
|
||||||
|
Some(tr) =>new_tid = tr.id,
|
||||||
|
None => new_tid = 0,
|
||||||
|
}
|
||||||
|
if new_tid <= 0 {
|
||||||
|
return diesel::result::QueryResult::Err(diesel::result::Error::NotFound);
|
||||||
|
}
|
||||||
|
for amount in amounts.iter_mut() {
|
||||||
|
amount.transaction_id = new_tid;
|
||||||
|
}
|
||||||
|
let inserted_amounts = diesel::insert_into(schema::amounts::table)
|
||||||
|
.values(&amounts)
|
||||||
|
.returning(db_model::Amount::as_returning())
|
||||||
|
.get_results(conn);
|
||||||
|
let new_amounts: Vec<db_model::Amount> = match inserted_amounts {
|
||||||
|
Ok(ams) => ams,
|
||||||
|
Err(_) => Vec::new(),
|
||||||
|
};
|
||||||
|
for am in new_amounts {
|
||||||
|
amount_ids.push(am.id)
|
||||||
|
};
|
||||||
|
diesel::result::QueryResult::Ok(())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
|
||||||
|
// 3. build response data.
|
||||||
|
|
||||||
|
// Ok(Json(res))
|
||||||
|
Ok("finish".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_transaction(
|
||||||
|
Path(id): Path<i64>,
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
Json(payload): Json<SubmitTransactionRequest>,
|
||||||
|
) -> Result<Json<CommonResp>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let now = Utc::now().naive_utc();
|
||||||
|
let datetime_tz = chrono::DateTime::parse_from_rfc3339(payload.time.as_str());
|
||||||
|
let datetime = match datetime_tz {
|
||||||
|
Ok(dt) => dt,
|
||||||
|
Err(_) => {
|
||||||
|
return Err((StatusCode::BAD_REQUEST, "invalid datetime, must be RFC 3339".to_string()))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let datetime_utc = datetime.with_timezone(&Utc);
|
||||||
|
let res = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
diesel::update(schema::transactions::table)
|
||||||
|
.filter(schema::transactions::id.eq(id))
|
||||||
|
.filter(schema::transactions::uid.eq(uid))
|
||||||
|
.set((
|
||||||
|
schema::transactions::category_id.eq(payload.category_id),
|
||||||
|
schema::transactions::description.eq(payload.description),
|
||||||
|
schema::transactions::time.eq(datetime_utc),
|
||||||
|
schema::transactions::update_at.eq(now),
|
||||||
|
))
|
||||||
|
.execute(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let resp = util::req::CommonResp { code: 0 };
|
||||||
|
Ok(Json(resp))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_transaction(
|
||||||
|
Path(id): Path<i64>,
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
) -> Result<Json<db_model::Transaction>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let res = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
schema::transactions::table
|
||||||
|
.filter(schema::transactions::id.eq(id))
|
||||||
|
.filter(schema::transactions::uid.eq(uid))
|
||||||
|
.select(db_model::Transaction::as_select())
|
||||||
|
.limit(1)
|
||||||
|
.get_result(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
Ok(Json(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_all_transactions(
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
Query(queryParams): Query<req::GetTransactionsQueryParams>,
|
||||||
|
) -> Result<Json<Vec<db_model::Transaction>>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let offset = match queryParams.start {
|
||||||
|
None => {0}
|
||||||
|
Some(start) => if start > 0 {start-1} else {0}
|
||||||
|
};
|
||||||
|
let limit = match queryParams.limit {
|
||||||
|
None => {1 as i32}
|
||||||
|
Some(limit_num) => {
|
||||||
|
if(limit_num > req::MAX_QUERY_LIMIT) {
|
||||||
|
req::MAX_QUERY_LIMIT
|
||||||
|
} else if(limit_num < 1) {
|
||||||
|
1 as i32
|
||||||
|
} else {
|
||||||
|
limit_num
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let res = conn
|
||||||
|
.interact(move |conn| {
|
||||||
|
schema::transactions::table.filter(schema::transactions::uid.eq(uid))
|
||||||
|
.offset(offset)
|
||||||
|
.limit(limit as i64)
|
||||||
|
.select(db_model::Transaction::as_select())
|
||||||
|
.load(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
Ok(Json(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn batch_get_transactions(
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
Json(payload): Json<BatchGetTransactionRequest>,
|
||||||
|
) -> Result<Json<Vec<db_model::Transaction>>, (StatusCode, String)> {
|
||||||
|
let uid = claims.uid.clone();
|
||||||
|
if payload.transaction_ids.len() == 0 {
|
||||||
|
return Err((StatusCode::BAD_REQUEST, "no transaction_id list".to_string()));
|
||||||
|
}
|
||||||
|
let conn = app_state.db.get().await.map_err(util::req::internal_error)?;
|
||||||
|
let res = conn.interact(move |conn| {
|
||||||
|
schema::transactions::table
|
||||||
|
.filter(schema::transactions::uid.eq(uid))
|
||||||
|
.filter(schema::transactions::is_delete.eq(false))
|
||||||
|
.filter(schema::transactions::id.eq_any(payload.transaction_ids))
|
||||||
|
.select(db_model::Transaction::as_select())
|
||||||
|
.load(conn)
|
||||||
|
})
|
||||||
|
.await.map_err(util::req::internal_error)?.map_err(util::req::internal_error)?;
|
||||||
|
Ok(Json(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_amounts_by_tid(
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
Query(params): Query<GetAmountParams>,
|
||||||
|
) -> Result<Json<Vec<db_model::Amount>>, (StatusCode, String)> {
|
||||||
|
info!(params.transaction_id);
|
||||||
|
let tid = match params.transaction_id {
|
||||||
|
None => 0,
|
||||||
|
Some(idx) => idx,
|
||||||
|
};
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
let res = conn.interact(move |conn| {
|
||||||
|
schema::amounts::table
|
||||||
|
.filter(schema::amounts::uid.eq(uid))
|
||||||
|
.filter(schema::amounts::transaction_id.eq(tid))
|
||||||
|
.select(db_model::Amount::as_select())
|
||||||
|
.load(conn)
|
||||||
|
}).await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
Ok(Json(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn batch_get_amounts_by_tid(
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
Json(payload): Json<BatchGetTransactionAmountRequest>,
|
||||||
|
) -> Result<Json<Vec<db_model::Amount>>, (StatusCode, String)> {
|
||||||
|
let uid = claims.uid.clone();
|
||||||
|
if payload.transaction_ids.len() == 0 {
|
||||||
|
return Err((StatusCode::BAD_REQUEST, "no transaction_id list".to_string()));
|
||||||
|
}
|
||||||
|
let conn = app_state.db.get().await.map_err(util::req::internal_error)?;
|
||||||
|
let res = conn.interact(move |conn| {
|
||||||
|
schema::amounts::table
|
||||||
|
.filter(schema::amounts::uid.eq(uid))
|
||||||
|
.filter(schema::amounts::is_delete.eq(false))
|
||||||
|
.filter(schema::amounts::transaction_id.eq_any(payload.transaction_ids))
|
||||||
|
.select(db_model::Amount::as_select())
|
||||||
|
.load(conn)
|
||||||
|
})
|
||||||
|
.await.map_err(util::req::internal_error)?.map_err(util::req::internal_error)?;
|
||||||
|
Ok(Json(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_all_amounts_by_tid_range(
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
claims: Claims,
|
||||||
|
Query(params): Query<GetAmountByTransactionRangeParams>,
|
||||||
|
) -> Result<Json<Vec<db_model::Amount>>, (StatusCode, String)> {
|
||||||
|
let uid: i64 = claims.uid.clone();
|
||||||
|
let tid_from = match params.transaction_id_from {
|
||||||
|
None => {-1}
|
||||||
|
Some(id) => {id}
|
||||||
|
};
|
||||||
|
let tid_to = match params.transaction_id_to {
|
||||||
|
None => {-1}
|
||||||
|
Some(id) => {id}
|
||||||
|
};
|
||||||
|
if uid <= 0 || tid_from <= 0 || tid_to <= 0 || tid_from > tid_to {
|
||||||
|
return Err((StatusCode::BAD_REQUEST,"invalid values".to_string()));
|
||||||
|
}
|
||||||
|
let limit: i64 = match params.limit {
|
||||||
|
None => {MAX_QUERY_LIMIT as i64}
|
||||||
|
Some(i) => {
|
||||||
|
if i <= 0 {
|
||||||
|
MAX_QUERY_LIMIT as i64
|
||||||
|
} else {
|
||||||
|
i as i64
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let conn = app_state.db.get()
|
||||||
|
.await.map_err(util::req::internal_error)?;
|
||||||
|
let res = conn.interact(move |conn| {
|
||||||
|
schema::amounts::table
|
||||||
|
.filter(schema::amounts::uid.eq(uid))
|
||||||
|
.filter(schema::amounts::is_delete.eq(false))
|
||||||
|
.filter(schema::amounts::transaction_id.ge(tid_from))
|
||||||
|
.filter(schema::amounts::transaction_id.le(tid_to))
|
||||||
|
.limit(limit)
|
||||||
|
.select(db_model::Amount::as_select())
|
||||||
|
.load(conn)
|
||||||
|
}).await
|
||||||
|
.map_err(util::req::internal_error)?
|
||||||
|
.map_err(util::req::internal_error)?;
|
||||||
|
Ok(Json(res))
|
||||||
|
}
|
||||||
54
src/main.rs
54
src/main.rs
@@ -1,3 +1,4 @@
|
|||||||
|
use std::env;
|
||||||
use axum::{
|
use axum::{
|
||||||
// http::StatusCode,
|
// http::StatusCode,
|
||||||
// routing::{get, post},
|
// routing::{get, post},
|
||||||
@@ -5,18 +6,21 @@ use axum::{
|
|||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
use axum::http::Method;
|
use axum::http::Method;
|
||||||
use serde::{Deserialize, Serialize};
|
// use pbkdf2::password_hash::Error;
|
||||||
|
// use serde::{Deserialize, Serialize};
|
||||||
use tower::ServiceBuilder;
|
use tower::ServiceBuilder;
|
||||||
use tower_http::cors::{Any, CorsLayer};
|
use tower_http::cors::{Any, CorsLayer};
|
||||||
use tower_http::trace::TraceLayer;
|
use tower_http::trace::TraceLayer;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
|
use crate::util::pass::get_pbkdf2_from_psw;
|
||||||
|
|
||||||
// Project modules
|
// Project modules
|
||||||
mod category;
|
mod ledger;
|
||||||
mod middleware;
|
mod middleware;
|
||||||
mod model;
|
mod model;
|
||||||
mod util;
|
mod util;
|
||||||
|
mod user;
|
||||||
|
|
||||||
// Passed App State
|
// Passed App State
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -30,6 +34,12 @@ async fn main() {
|
|||||||
tracing_subscriber::registry()
|
tracing_subscriber::registry()
|
||||||
.with(tracing_subscriber::fmt::layer())
|
.with(tracing_subscriber::fmt::layer())
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
|
||||||
|
if args.len() <= 1 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
// initialize db connection
|
// initialize db connection
|
||||||
let db_url = std::env::var("DATABASE_URL").unwrap();
|
let db_url = std::env::var("DATABASE_URL").unwrap();
|
||||||
|
|
||||||
@@ -39,6 +49,37 @@ async fn main() {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let shared_state = AppState { db: pool };
|
let shared_state = AppState { db: pool };
|
||||||
|
let cmd = args[1].clone();
|
||||||
|
|
||||||
|
match cmd.as_str() {
|
||||||
|
"add_user" => {
|
||||||
|
println!("adding user");
|
||||||
|
if args.len() <= 4 {
|
||||||
|
println!("insufficient arg number");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let user = args[2].clone();
|
||||||
|
let psw = args[3].clone();
|
||||||
|
let mail = args[4].clone();
|
||||||
|
println!("adding user {}", user);
|
||||||
|
let hashed = get_pbkdf2_from_psw(psw);
|
||||||
|
let mut hash_psw = "".to_string();
|
||||||
|
match hashed {
|
||||||
|
Ok(val) => {
|
||||||
|
println!("get hash {}", val);
|
||||||
|
hash_psw=val;
|
||||||
|
}
|
||||||
|
Err(_) => {}
|
||||||
|
}
|
||||||
|
let res = user::dal::add_user(shared_state, user, hash_psw, mail)
|
||||||
|
.await;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
println!("unknown command {}", cmd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Register routers
|
// Register routers
|
||||||
let cors_layer = CorsLayer::new()
|
let cors_layer = CorsLayer::new()
|
||||||
@@ -50,8 +91,13 @@ async fn main() {
|
|||||||
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
// V1 apis
|
// V1 apis
|
||||||
.nest("/api/v1/category", category::handler::get_nest_handlers())
|
.nest("/api/v1/category", ledger::category::get_nest_handlers())
|
||||||
.nest("/api/v1/v2", category::handler::get_nest_handlers())
|
.nest("/api/v1/tag", ledger::tag::get_nest_handlers())
|
||||||
|
.nest("/api/v1/book", ledger::book::get_nest_handlers())
|
||||||
|
.nest("/api/v1/account", ledger::account::get_nest_handlers())
|
||||||
|
.nest("/api/v1/transaction", ledger::transaction::get_nest_handlers())
|
||||||
|
.nest("/api/v1/user", user::handler::get_nest_handlers())
|
||||||
|
.nest("/api/v1/operation", ledger::operation::get_nest_handlers())
|
||||||
.with_state(shared_state)
|
.with_state(shared_state)
|
||||||
.layer(global_layer);
|
.layer(global_layer);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
async_trait,
|
|
||||||
extract::FromRequestParts,
|
extract::FromRequestParts,
|
||||||
http::{
|
http::{
|
||||||
request::Parts,
|
request::Parts,
|
||||||
@@ -8,6 +7,7 @@ use axum::{
|
|||||||
Json, RequestPartsExt,
|
Json, RequestPartsExt,
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
};
|
};
|
||||||
|
use async_trait::async_trait;
|
||||||
use axum_extra::{
|
use axum_extra::{
|
||||||
headers::{authorization::Bearer, Authorization},
|
headers::{authorization::Bearer, Authorization},
|
||||||
TypedHeader,
|
TypedHeader,
|
||||||
@@ -22,7 +22,7 @@ use crate::util;
|
|||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct Claims {
|
pub struct Claims {
|
||||||
sub: String,
|
sub: String,
|
||||||
company: String,
|
// company: String,
|
||||||
exp: usize,
|
exp: usize,
|
||||||
pub uid: i64,
|
pub uid: i64,
|
||||||
}
|
}
|
||||||
@@ -68,7 +68,7 @@ impl Keys {
|
|||||||
|
|
||||||
impl Display for Claims {
|
impl Display for Claims {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(f, "Email: {}\nCompany: {}", self.sub, self.company)
|
write!(f, "Email: {}", self.sub)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,27 +81,25 @@ impl AuthBody {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl<S> FromRequestParts<S> for Claims
|
impl<S> FromRequestParts<S> for Claims
|
||||||
where
|
where
|
||||||
S: Send + Sync,
|
S: Send + Sync,
|
||||||
{
|
{
|
||||||
type Rejection = (StatusCode, String);
|
type Rejection = AuthError;
|
||||||
|
|
||||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||||
// Extract the token from the authorization header
|
// Extract the token from the authorization header
|
||||||
let TypedHeader(Authorization(bearer)) = parts
|
let TypedHeader(Authorization(bearer)) = parts
|
||||||
.extract::<TypedHeader<Authorization<Bearer>>>()
|
.extract::<TypedHeader<Authorization<Bearer>>>()
|
||||||
.await
|
.await
|
||||||
.map_err(util::req::internal_error)?;
|
.map_err(|_| AuthError::InvalidToken)?;
|
||||||
// Decode the user data
|
// Decode the user data
|
||||||
let token_data = decode::<Claims>(bearer.token(), &KEYS.decoding, &Validation::default())
|
let token_data = decode::<Claims>(bearer.token(), &KEYS.decoding, &Validation::default())
|
||||||
.map_err(util::req::internal_error)?;
|
.map_err(|_| AuthError::InvalidToken)?;
|
||||||
|
|
||||||
Ok(token_data.claims)
|
Ok(token_data.claims)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoResponse for AuthError {
|
impl IntoResponse for AuthError {
|
||||||
fn into_response(self) -> Response {
|
fn into_response(self) -> Response {
|
||||||
let (status, error_message) = match self {
|
let (status, error_message) = match self {
|
||||||
|
|||||||
@@ -1,13 +1,21 @@
|
|||||||
use crate::model::schema;
|
use crate::model::schema;
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use crate::model::schema::operations::entity_id;
|
||||||
|
|
||||||
#[derive(Queryable, Selectable, serde::Serialize, serde::Deserialize)]
|
#[derive(Queryable, Selectable, serde::Serialize, serde::Deserialize)]
|
||||||
#[diesel(table_name = schema::categories)]
|
#[diesel(table_name = schema::categories)]
|
||||||
#[diesel(check_for_backend(diesel::pg::Pg))]
|
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||||
pub struct Category {
|
pub struct Category {
|
||||||
id: i64,
|
#[serde(with = "string")]
|
||||||
|
pub id: i64,
|
||||||
uid: i64,
|
uid: i64,
|
||||||
name: String,
|
name: String,
|
||||||
|
level: i32,
|
||||||
|
parent_category_id: i64,
|
||||||
|
book_id: i64,
|
||||||
|
op_id: i64,
|
||||||
|
#[serde(skip_serializing)]
|
||||||
is_delete: bool,
|
is_delete: bool,
|
||||||
create_at: chrono::NaiveDateTime,
|
create_at: chrono::NaiveDateTime,
|
||||||
update_at: chrono::NaiveDateTime,
|
update_at: chrono::NaiveDateTime,
|
||||||
@@ -18,4 +26,200 @@ pub struct Category {
|
|||||||
pub struct CategoryForm {
|
pub struct CategoryForm {
|
||||||
pub uid: i64,
|
pub uid: i64,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub book_id: i64,
|
||||||
|
pub level: i32,
|
||||||
|
pub parent_category_id: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Queryable, Selectable, serde::Serialize, serde::Deserialize)]
|
||||||
|
#[diesel(table_name = schema::tags)]
|
||||||
|
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||||
|
pub struct Tag {
|
||||||
|
id: i64,
|
||||||
|
uid: i64,
|
||||||
|
book_id: i64,
|
||||||
|
name: String,
|
||||||
|
level: i32,
|
||||||
|
parent_tag_id: i64,
|
||||||
|
op_id: i64,
|
||||||
|
#[serde(skip_serializing)]
|
||||||
|
is_delete: bool,
|
||||||
|
create_at: chrono::NaiveDateTime,
|
||||||
|
update_at: chrono::NaiveDateTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize, Insertable)]
|
||||||
|
#[diesel(table_name = schema::tags)]
|
||||||
|
pub struct TagForm {
|
||||||
|
pub uid: i64,
|
||||||
|
pub book_id: i64,
|
||||||
|
pub name: String,
|
||||||
|
pub level: i32,
|
||||||
|
pub parent_tag_id: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Queryable, Selectable, serde::Serialize, serde::Deserialize)]
|
||||||
|
#[diesel(table_name = schema::books)]
|
||||||
|
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||||
|
pub struct Book {
|
||||||
|
#[serde(with = "string")]
|
||||||
|
id: i64,
|
||||||
|
uid: i64,
|
||||||
|
name: String,
|
||||||
|
op_id: i64,
|
||||||
|
#[serde(skip_serializing)]
|
||||||
|
is_delete: bool,
|
||||||
|
create_at: chrono::NaiveDateTime,
|
||||||
|
update_at: chrono::NaiveDateTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize, Insertable)]
|
||||||
|
#[diesel(table_name = schema::books)]
|
||||||
|
pub struct BookForm {
|
||||||
|
pub uid: i64,
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Queryable, Selectable, serde::Serialize, serde::Deserialize)]
|
||||||
|
#[diesel(table_name = schema::accounts)]
|
||||||
|
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||||
|
pub struct Account {
|
||||||
|
id: i64,
|
||||||
|
uid: i64,
|
||||||
|
name: String,
|
||||||
|
account_type: i64,
|
||||||
|
op_id: i64,
|
||||||
|
#[serde(skip_serializing)]
|
||||||
|
is_delete: bool,
|
||||||
|
create_at: chrono::NaiveDateTime,
|
||||||
|
update_at: chrono::NaiveDateTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize, Insertable)]
|
||||||
|
#[diesel(table_name = schema::accounts)]
|
||||||
|
pub struct AccountForm {
|
||||||
|
pub uid: i64,
|
||||||
|
pub name: String,
|
||||||
|
pub account_type: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Queryable, Selectable, serde::Serialize, serde::Deserialize)]
|
||||||
|
#[diesel(table_name = schema::transactions)]
|
||||||
|
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||||
|
pub struct Transaction {
|
||||||
|
#[serde(with = "string")]
|
||||||
|
pub id: i64,
|
||||||
|
uid: i64,
|
||||||
|
#[serde(with = "string")]
|
||||||
|
pub book_id: i64,
|
||||||
|
pub description: String,
|
||||||
|
#[serde(with = "string")]
|
||||||
|
pub category_id: i64,
|
||||||
|
pub time: chrono::DateTime<Utc>,
|
||||||
|
pub op_id: i64,
|
||||||
|
#[serde(skip_serializing)]
|
||||||
|
is_delete: bool,
|
||||||
|
create_at: chrono::NaiveDateTime,
|
||||||
|
update_at: chrono::NaiveDateTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize, Insertable)]
|
||||||
|
#[diesel(table_name = schema::transactions)]
|
||||||
|
pub struct TransactionForm {
|
||||||
|
pub id: Option<i64>,
|
||||||
|
pub uid: i64,
|
||||||
|
pub book_id: i64,
|
||||||
|
pub description: String,
|
||||||
|
pub category_id: i64,
|
||||||
|
pub time: chrono::DateTime<Utc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Queryable, Selectable, serde::Serialize, serde::Deserialize)]
|
||||||
|
#[diesel(table_name = schema::amounts)]
|
||||||
|
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||||
|
pub struct Amount {
|
||||||
|
pub id: i64,
|
||||||
|
uid: i64,
|
||||||
|
account_id: i64,
|
||||||
|
transaction_id: i64,
|
||||||
|
value: i64,
|
||||||
|
expo: i64,
|
||||||
|
currency: String,
|
||||||
|
pub op_id: i64,
|
||||||
|
#[serde(skip_serializing)]
|
||||||
|
is_delete: bool,
|
||||||
|
create_at: chrono::NaiveDateTime,
|
||||||
|
update_at: chrono::NaiveDateTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize, Insertable)]
|
||||||
|
#[diesel(table_name = schema::amounts)]
|
||||||
|
pub struct AmountForm {
|
||||||
|
pub uid: i64,
|
||||||
|
pub transaction_id: i64,
|
||||||
|
pub account_id: i64,
|
||||||
|
pub value: i64,
|
||||||
|
pub expo: i64,
|
||||||
|
pub currency: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Queryable, Selectable, serde::Serialize)]
|
||||||
|
#[diesel(table_name = schema::users)]
|
||||||
|
pub struct User {
|
||||||
|
pub id: i64,
|
||||||
|
pub username: String,
|
||||||
|
pub password: String,
|
||||||
|
pub mail: String,
|
||||||
|
#[serde(skip_serializing)]
|
||||||
|
pub is_delete: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Insertable)]
|
||||||
|
#[diesel(table_name = schema::users)]
|
||||||
|
pub struct UserForm {
|
||||||
|
pub username: String,
|
||||||
|
pub password: String,
|
||||||
|
pub mail: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Insertable,Queryable, Selectable, serde::Serialize)]
|
||||||
|
#[diesel(table_name = schema::operations)]
|
||||||
|
pub struct Operation {
|
||||||
|
pub id: i64,
|
||||||
|
pub uid: i64,
|
||||||
|
pub entity_type: i64,
|
||||||
|
pub entity_id: i64,
|
||||||
|
pub action: i64,
|
||||||
|
create_at: chrono::NaiveDateTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Insertable)]
|
||||||
|
#[diesel(table_name = schema::operations)]
|
||||||
|
pub struct CreateOperation {
|
||||||
|
pub uid: i64,
|
||||||
|
pub entity_type: i64,
|
||||||
|
pub entity_id: i64,
|
||||||
|
pub action: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
mod string {
|
||||||
|
use std::fmt::Display;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use serde::{de, Serializer, Deserialize, Deserializer};
|
||||||
|
|
||||||
|
pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where T: Display,
|
||||||
|
S: Serializer
|
||||||
|
{
|
||||||
|
serializer.collect_str(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
|
||||||
|
where T: FromStr,
|
||||||
|
T::Err: Display,
|
||||||
|
D: Deserializer<'de>
|
||||||
|
{
|
||||||
|
String::deserialize(deserializer)?.parse().map_err(de::Error::custom)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
pub mod db_model;
|
pub mod db_model;
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
|
pub mod req;
|
||||||
|
|||||||
42
src/model/req.rs
Normal file
42
src/model/req.rs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
use std::fmt;
|
||||||
|
use std::str::FromStr;
|
||||||
|
use serde::{de, Deserialize, Deserializer};
|
||||||
|
|
||||||
|
pub const QUERY_ORDER_INCREASE:i32 = 0;
|
||||||
|
pub const QUERY_ORDER_INVERT:i32 = 1;
|
||||||
|
|
||||||
|
pub const MAX_QUERY_LIMIT:i32 =1000;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct GetAmountParams {
|
||||||
|
#[serde(default, deserialize_with="empty_string_as_none")]
|
||||||
|
pub transaction_id: Option<i64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct GetAmountByTransactionRangeParams {
|
||||||
|
pub transaction_id_from: Option<i64>,
|
||||||
|
pub transaction_id_to: Option<i64>,
|
||||||
|
pub limit: Option<i64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serde deserialization decorator to map empty Strings to None,
|
||||||
|
fn empty_string_as_none<'de, D, T>(de: D) -> Result<Option<T>, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
T: FromStr,
|
||||||
|
T::Err: fmt::Display,
|
||||||
|
{
|
||||||
|
let opt = Option::<String>::deserialize(de)?;
|
||||||
|
match opt.as_deref() {
|
||||||
|
None | Some("") => Ok(None),
|
||||||
|
Some(s) => FromStr::from_str(s).map_err(de::Error::custom).map(Some),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct GetTransactionsQueryParams {
|
||||||
|
pub start: Option<i64>,
|
||||||
|
pub limit: Option<i32>,
|
||||||
|
}
|
||||||
@@ -5,8 +5,8 @@ diesel::table! {
|
|||||||
id -> Int8,
|
id -> Int8,
|
||||||
uid -> Int8,
|
uid -> Int8,
|
||||||
name -> Text,
|
name -> Text,
|
||||||
#[sql_name = "type"]
|
account_type -> Int8,
|
||||||
type_ -> Int8,
|
op_id -> Int8,
|
||||||
is_delete -> Bool,
|
is_delete -> Bool,
|
||||||
create_at -> Timestamp,
|
create_at -> Timestamp,
|
||||||
update_at -> Timestamp,
|
update_at -> Timestamp,
|
||||||
@@ -17,9 +17,12 @@ diesel::table! {
|
|||||||
amounts (id) {
|
amounts (id) {
|
||||||
id -> Int8,
|
id -> Int8,
|
||||||
uid -> Int8,
|
uid -> Int8,
|
||||||
|
account_id -> Int8,
|
||||||
transaction_id -> Int8,
|
transaction_id -> Int8,
|
||||||
value -> Int8,
|
value -> Int8,
|
||||||
expo -> Int8,
|
expo -> Int8,
|
||||||
|
currency -> Text,
|
||||||
|
op_id -> Int8,
|
||||||
is_delete -> Bool,
|
is_delete -> Bool,
|
||||||
create_at -> Timestamp,
|
create_at -> Timestamp,
|
||||||
update_at -> Timestamp,
|
update_at -> Timestamp,
|
||||||
@@ -31,6 +34,7 @@ diesel::table! {
|
|||||||
id -> Int8,
|
id -> Int8,
|
||||||
uid -> Int8,
|
uid -> Int8,
|
||||||
name -> Text,
|
name -> Text,
|
||||||
|
op_id -> Int8,
|
||||||
is_delete -> Bool,
|
is_delete -> Bool,
|
||||||
create_at -> Timestamp,
|
create_at -> Timestamp,
|
||||||
update_at -> Timestamp,
|
update_at -> Timestamp,
|
||||||
@@ -41,18 +45,47 @@ diesel::table! {
|
|||||||
categories (id) {
|
categories (id) {
|
||||||
id -> Int8,
|
id -> Int8,
|
||||||
uid -> Int8,
|
uid -> Int8,
|
||||||
|
book_id -> Int8,
|
||||||
name -> Text,
|
name -> Text,
|
||||||
|
level -> Int4,
|
||||||
|
parent_category_id -> Int8,
|
||||||
|
op_id -> Int8,
|
||||||
is_delete -> Bool,
|
is_delete -> Bool,
|
||||||
create_at -> Timestamp,
|
create_at -> Timestamp,
|
||||||
update_at -> Timestamp,
|
update_at -> Timestamp,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
operation_snapshots (id) {
|
||||||
|
id -> Int8,
|
||||||
|
uid -> Int8,
|
||||||
|
max_op_id -> Int8,
|
||||||
|
create_at -> Timestamp,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
operations (id) {
|
||||||
|
id -> Int8,
|
||||||
|
uid -> Int8,
|
||||||
|
entity_type -> Int8,
|
||||||
|
entity_id -> Int8,
|
||||||
|
action -> Int8,
|
||||||
|
create_at -> Timestamp,
|
||||||
|
update_at -> Timestamp,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
diesel::table! {
|
diesel::table! {
|
||||||
tags (id) {
|
tags (id) {
|
||||||
id -> Int8,
|
id -> Int8,
|
||||||
uid -> Int8,
|
uid -> Int8,
|
||||||
|
book_id -> Int8,
|
||||||
name -> Text,
|
name -> Text,
|
||||||
|
level -> Int4,
|
||||||
|
parent_tag_id -> Int8,
|
||||||
|
op_id -> Int8,
|
||||||
is_delete -> Bool,
|
is_delete -> Bool,
|
||||||
create_at -> Timestamp,
|
create_at -> Timestamp,
|
||||||
update_at -> Timestamp,
|
update_at -> Timestamp,
|
||||||
@@ -65,6 +98,7 @@ diesel::table! {
|
|||||||
uid -> Int8,
|
uid -> Int8,
|
||||||
transaction_id -> Int8,
|
transaction_id -> Int8,
|
||||||
tag_id -> Int8,
|
tag_id -> Int8,
|
||||||
|
op_id -> Int8,
|
||||||
is_delete -> Bool,
|
is_delete -> Bool,
|
||||||
create_at -> Timestamp,
|
create_at -> Timestamp,
|
||||||
update_at -> Timestamp,
|
update_at -> Timestamp,
|
||||||
@@ -78,6 +112,7 @@ diesel::table! {
|
|||||||
book_id -> Int8,
|
book_id -> Int8,
|
||||||
description -> Text,
|
description -> Text,
|
||||||
category_id -> Int8,
|
category_id -> Int8,
|
||||||
|
op_id -> Int8,
|
||||||
is_delete -> Bool,
|
is_delete -> Bool,
|
||||||
time -> Timestamptz,
|
time -> Timestamptz,
|
||||||
create_at -> Timestamp,
|
create_at -> Timestamp,
|
||||||
@@ -102,6 +137,8 @@ diesel::allow_tables_to_appear_in_same_query!(
|
|||||||
amounts,
|
amounts,
|
||||||
books,
|
books,
|
||||||
categories,
|
categories,
|
||||||
|
operation_snapshots,
|
||||||
|
operations,
|
||||||
tags,
|
tags,
|
||||||
transaction_tag_rels,
|
transaction_tag_rels,
|
||||||
transactions,
|
transactions,
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ diesel::table! {
|
|||||||
id -> Int8,
|
id -> Int8,
|
||||||
uid -> Int8,
|
uid -> Int8,
|
||||||
name -> Text,
|
name -> Text,
|
||||||
#[sql_name = "type"]
|
account_type -> Int8,
|
||||||
type_ -> Int8,
|
version_v1 -> Int8,
|
||||||
is_delete -> Bool,
|
is_delete -> Bool,
|
||||||
create_at -> Timestamp,
|
create_at -> Timestamp,
|
||||||
update_at -> Timestamp,
|
update_at -> Timestamp,
|
||||||
@@ -17,9 +17,12 @@ diesel::table! {
|
|||||||
amounts (id) {
|
amounts (id) {
|
||||||
id -> Int8,
|
id -> Int8,
|
||||||
uid -> Int8,
|
uid -> Int8,
|
||||||
|
account_id -> Int8,
|
||||||
transaction_id -> Int8,
|
transaction_id -> Int8,
|
||||||
value -> Int8,
|
value -> Int8,
|
||||||
expo -> Int8,
|
expo -> Int8,
|
||||||
|
currency -> Text,
|
||||||
|
version_v1 -> Int8,
|
||||||
is_delete -> Bool,
|
is_delete -> Bool,
|
||||||
create_at -> Timestamp,
|
create_at -> Timestamp,
|
||||||
update_at -> Timestamp,
|
update_at -> Timestamp,
|
||||||
@@ -31,6 +34,7 @@ diesel::table! {
|
|||||||
id -> Int8,
|
id -> Int8,
|
||||||
uid -> Int8,
|
uid -> Int8,
|
||||||
name -> Text,
|
name -> Text,
|
||||||
|
version_v1 -> Int8,
|
||||||
is_delete -> Bool,
|
is_delete -> Bool,
|
||||||
create_at -> Timestamp,
|
create_at -> Timestamp,
|
||||||
update_at -> Timestamp,
|
update_at -> Timestamp,
|
||||||
@@ -41,7 +45,11 @@ diesel::table! {
|
|||||||
categories (id) {
|
categories (id) {
|
||||||
id -> Int8,
|
id -> Int8,
|
||||||
uid -> Int8,
|
uid -> Int8,
|
||||||
|
book_id -> Int8,
|
||||||
name -> Text,
|
name -> Text,
|
||||||
|
level -> Int4,
|
||||||
|
parent_category_id -> Int8,
|
||||||
|
version_v1 -> Int8,
|
||||||
is_delete -> Bool,
|
is_delete -> Bool,
|
||||||
create_at -> Timestamp,
|
create_at -> Timestamp,
|
||||||
update_at -> Timestamp,
|
update_at -> Timestamp,
|
||||||
@@ -52,7 +60,11 @@ diesel::table! {
|
|||||||
tags (id) {
|
tags (id) {
|
||||||
id -> Int8,
|
id -> Int8,
|
||||||
uid -> Int8,
|
uid -> Int8,
|
||||||
|
book_id -> Int8,
|
||||||
name -> Text,
|
name -> Text,
|
||||||
|
level -> Int4,
|
||||||
|
parent_tag_id -> Int8,
|
||||||
|
version_v1 -> Int8,
|
||||||
is_delete -> Bool,
|
is_delete -> Bool,
|
||||||
create_at -> Timestamp,
|
create_at -> Timestamp,
|
||||||
update_at -> Timestamp,
|
update_at -> Timestamp,
|
||||||
@@ -65,6 +77,7 @@ diesel::table! {
|
|||||||
uid -> Int8,
|
uid -> Int8,
|
||||||
transaction_id -> Int8,
|
transaction_id -> Int8,
|
||||||
tag_id -> Int8,
|
tag_id -> Int8,
|
||||||
|
version_v1 -> Int8,
|
||||||
is_delete -> Bool,
|
is_delete -> Bool,
|
||||||
create_at -> Timestamp,
|
create_at -> Timestamp,
|
||||||
update_at -> Timestamp,
|
update_at -> Timestamp,
|
||||||
@@ -78,6 +91,7 @@ diesel::table! {
|
|||||||
book_id -> Int8,
|
book_id -> Int8,
|
||||||
description -> Text,
|
description -> Text,
|
||||||
category_id -> Int8,
|
category_id -> Int8,
|
||||||
|
version_v1 -> Int8,
|
||||||
is_delete -> Bool,
|
is_delete -> Bool,
|
||||||
time -> Timestamptz,
|
time -> Timestamptz,
|
||||||
create_at -> Timestamp,
|
create_at -> Timestamp,
|
||||||
@@ -97,6 +111,18 @@ diesel::table! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
versions_v1 (id) {
|
||||||
|
id -> Int8,
|
||||||
|
uid -> Int8,
|
||||||
|
entity_type -> Int8,
|
||||||
|
entity_id -> Int8,
|
||||||
|
action -> Int8,
|
||||||
|
create_at -> Timestamp,
|
||||||
|
update_at -> Timestamp,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
diesel::allow_tables_to_appear_in_same_query!(
|
diesel::allow_tables_to_appear_in_same_query!(
|
||||||
accounts,
|
accounts,
|
||||||
amounts,
|
amounts,
|
||||||
@@ -106,4 +132,5 @@ diesel::allow_tables_to_appear_in_same_query!(
|
|||||||
transaction_tag_rels,
|
transaction_tag_rels,
|
||||||
transactions,
|
transactions,
|
||||||
users,
|
users,
|
||||||
|
versions_v1,
|
||||||
);
|
);
|
||||||
|
|||||||
118
src/user/dal.rs
Normal file
118
src/user/dal.rs
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
use diesel::prelude::*;
|
||||||
|
use crate::model::{db_model, schema};
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fmt::Debug;
|
||||||
|
use pbkdf2::password_hash::{PasswordHash, PasswordVerifier};
|
||||||
|
use pbkdf2::Pbkdf2;
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
pub async fn add_user(app_state: crate::AppState, username: String, password: String, mail: String) -> Result<(), ()> {
|
||||||
|
let conn = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(|_| {
|
||||||
|
println!("fail to get db connection");
|
||||||
|
()
|
||||||
|
})?;
|
||||||
|
let target_username = username.clone();
|
||||||
|
// 1. check if current username exists.
|
||||||
|
let res = conn.interact(
|
||||||
|
move |conn| {
|
||||||
|
schema::users::table
|
||||||
|
.filter(schema::users::username.eq(target_username.clone()))
|
||||||
|
.count()
|
||||||
|
.get_result::<i64>(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(|_res| {
|
||||||
|
()
|
||||||
|
})?
|
||||||
|
.map_err(|_res| {
|
||||||
|
()
|
||||||
|
})?;
|
||||||
|
println!("ret {}", res);
|
||||||
|
if res > 0 {
|
||||||
|
println!("user already exists.");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let new_user_form = db_model::UserForm {
|
||||||
|
username: username.clone(),
|
||||||
|
password: password.clone(),
|
||||||
|
mail: mail.clone(),
|
||||||
|
};
|
||||||
|
// 2. adding user
|
||||||
|
let add_res = conn.interact(
|
||||||
|
move |conn| {
|
||||||
|
diesel::insert_into(schema::users::table)
|
||||||
|
.values(&new_user_form)
|
||||||
|
.returning(db_model::User::as_returning())
|
||||||
|
.get_result(conn)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.map_err(|_e| {
|
||||||
|
()
|
||||||
|
})?
|
||||||
|
.map_err(|_e| {
|
||||||
|
()
|
||||||
|
})?;
|
||||||
|
let out = json!(add_res);
|
||||||
|
println!("new user {}", out.to_string());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn check_user_psw(app_state: crate::AppState, username: String, password: String) -> bool {
|
||||||
|
let conn_res = app_state
|
||||||
|
.db
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(|_| {
|
||||||
|
println!("fail to get db connection");
|
||||||
|
()
|
||||||
|
});
|
||||||
|
let conn = match conn_res {
|
||||||
|
Ok(res) => res,
|
||||||
|
Err(_err) => { return false; }
|
||||||
|
};
|
||||||
|
// 1. get psw hash
|
||||||
|
let query_username = username.clone();
|
||||||
|
let user_rr = conn.interact(
|
||||||
|
|conn| {
|
||||||
|
schema::users::table
|
||||||
|
.filter(schema::users::username.eq(query_username))
|
||||||
|
.select(db_model::User::as_select())
|
||||||
|
.get_results(conn)
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let user_res = match user_rr {
|
||||||
|
Ok(res) => res,
|
||||||
|
Err(_) => return false,
|
||||||
|
};
|
||||||
|
println!("get user_res success");
|
||||||
|
let user = match user_res {
|
||||||
|
Ok(u) => u,
|
||||||
|
Err(_) => return false,
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("get user success");
|
||||||
|
|
||||||
|
if user.len() != 1 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
println!("get uniq user success");
|
||||||
|
let cur_user = user.get(0);
|
||||||
|
let psw = match cur_user {
|
||||||
|
Some(usr) => usr.password.clone(),
|
||||||
|
None => "".to_string(),
|
||||||
|
};
|
||||||
|
println!("comparing psw, get {}, stored {}.", password.clone(), psw.clone());
|
||||||
|
|
||||||
|
let hash_res = PasswordHash::new(psw.as_str());
|
||||||
|
let hash = match hash_res {
|
||||||
|
Ok(rs) => rs,
|
||||||
|
Err(_) => return false,
|
||||||
|
};
|
||||||
|
let check_res = Pbkdf2.verify_password(password.as_bytes(), &hash);
|
||||||
|
return check_res.is_ok();
|
||||||
|
}
|
||||||
29
src/user/handler.rs
Normal file
29
src/user/handler.rs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
use axum::{
|
||||||
|
extract::State, http::StatusCode, routing::post, Json, Router
|
||||||
|
};
|
||||||
|
use axum_macros::debug_handler;
|
||||||
|
use crate::middleware::auth::Claims;
|
||||||
|
use super::dal::check_user_psw;
|
||||||
|
|
||||||
|
pub fn get_nest_handlers() -> Router<crate::AppState> {
|
||||||
|
Router::new()
|
||||||
|
.route("/login", post(login))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
pub struct LoginCredentialRequest {
|
||||||
|
pub username: String,
|
||||||
|
pub password: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[debug_handler]
|
||||||
|
pub async fn login(
|
||||||
|
State(app_state): State<crate::AppState>,
|
||||||
|
Json(payload): Json<LoginCredentialRequest>,
|
||||||
|
) -> Result<(), (StatusCode, String)> {
|
||||||
|
let res = check_user_psw(app_state, payload.username.clone(), payload.password.clone()).await;
|
||||||
|
if !res {
|
||||||
|
return Err((StatusCode::UNAUTHORIZED, "invalid credentials".to_string()));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -1 +1,2 @@
|
|||||||
|
pub mod dal;
|
||||||
pub mod handler;
|
pub mod handler;
|
||||||
45
src/util/math.rs
Normal file
45
src/util/math.rs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
pub fn parse_payment_to_value_expo(payment_str: String, target_expo: i64) -> Result<(i64, i64), ()> {
|
||||||
|
// 1. check format
|
||||||
|
let re = Regex::new(r"[1-9]{0,9}[0-9]\.[0-9]{2,6}$").unwrap();
|
||||||
|
let res_format = re.is_match(payment_str.as_str());
|
||||||
|
if !res_format {
|
||||||
|
return Err(())
|
||||||
|
}
|
||||||
|
let mut value: i64 = 0;
|
||||||
|
let mut expo : i64 = 0;
|
||||||
|
|
||||||
|
let dot_index = payment_str.find('.');
|
||||||
|
let (int_part, decimal_part) = match dot_index {
|
||||||
|
Some(pos) => (&payment_str[..pos], &payment_str[pos+1..]),
|
||||||
|
None => (payment_str.as_str(), ""),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut dec_part_padding = format!("{:0<width$}", decimal_part, width=target_expo as usize);
|
||||||
|
if dec_part_padding.len() > target_expo as usize {
|
||||||
|
let pd = &dec_part_padding[..target_expo as usize];
|
||||||
|
dec_part_padding = pd.to_string();
|
||||||
|
}
|
||||||
|
let num_str = format!("{}{}", int_part, dec_part_padding);
|
||||||
|
println!("parsed num string \"{}\"", num_str);
|
||||||
|
let num = num_str.parse::<i64>().unwrap();
|
||||||
|
let value = num;
|
||||||
|
let expo = target_expo;
|
||||||
|
|
||||||
|
Ok((value, expo))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn test_parse_payment(){
|
||||||
|
let r1 = parse_payment_to_value_expo("1.345".to_string(), 6);
|
||||||
|
assert_eq!(r1, Ok((1345000, 6)));
|
||||||
|
let r2 = parse_payment_to_value_expo("0.01".to_string(), 6);
|
||||||
|
assert_eq!(r2, Ok((10000, 6)));
|
||||||
|
let r3 = parse_payment_to_value_expo("0.10000001".to_string(), 6);
|
||||||
|
assert_eq!(r3, Err(()));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1 +1,4 @@
|
|||||||
pub mod req;
|
pub mod req;
|
||||||
|
pub mod pass;
|
||||||
|
pub mod math;
|
||||||
|
pub mod operation;
|
||||||
|
|||||||
16
src/util/operation.rs
Normal file
16
src/util/operation.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
pub type EntityType = i64;
|
||||||
|
|
||||||
|
pub const ENTITY_CATEGORY: EntityType = 1;
|
||||||
|
pub const ENTITY_TAG: EntityType = 2;
|
||||||
|
pub const ENTITY_BOOK: EntityType = 3;
|
||||||
|
pub const ENTITY_ACCOUNT: EntityType = 4;
|
||||||
|
pub const ENTITY_TRANSACTION: EntityType = 5;
|
||||||
|
pub const ENTITY_AMOUNT: EntityType = 6;
|
||||||
|
|
||||||
|
|
||||||
|
pub type ActionType = i64;
|
||||||
|
|
||||||
|
pub const ACTION_CREATE: ActionType = 1;
|
||||||
|
pub const ACTION_UPDATE: ActionType = 2;
|
||||||
|
pub const ACTION_DELETE: ActionType = 3;
|
||||||
16
src/util/pass.rs
Normal file
16
src/util/pass.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
use std::error::Error;
|
||||||
|
use pbkdf2::{
|
||||||
|
password_hash::{
|
||||||
|
rand_core::OsRng,
|
||||||
|
PasswordHash,SaltString,
|
||||||
|
},
|
||||||
|
Pbkdf2,
|
||||||
|
};
|
||||||
|
use pbkdf2::password_hash::PasswordHasher;
|
||||||
|
|
||||||
|
pub fn get_pbkdf2_from_psw(password:String) -> Result<String, pbkdf2::password_hash::Error> {
|
||||||
|
let salt = SaltString::generate(&mut OsRng);
|
||||||
|
let password_hash = Pbkdf2.hash_password(password.as_bytes(), &salt)?.to_string();
|
||||||
|
println!("{}",password_hash);
|
||||||
|
return Ok(password_hash)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user