From ec907e4b5f9e352599398ff33ea811bed9214297 Mon Sep 17 00:00:00 2001 From: Vinod J M Date: Thu, 29 Dec 2022 22:22:04 +0530 Subject: [PATCH] user-auth orm and meili changes --- backend/Cargo.lock | 621 ++++++++++++++---- backend/api/Cargo.toml | 10 +- backend/api/src/lib.rs | 65 ++ backend/entity/Cargo.toml | 2 +- backend/entity/src/entities/book.rs | 27 +- backend/entity/src/entities/book_author.rs | 4 +- backend/entity/src/entities/book_isbn.rs | 4 +- backend/entity/src/entities/book_person.rs | 4 +- backend/entity/src/entities/book_place.rs | 4 +- backend/entity/src/entities/book_subject.rs | 4 +- backend/entity/src/entities/book_time.rs | 4 +- backend/entity/src/entities/mod.rs | 3 +- backend/entity/src/entities/prelude.rs | 3 +- backend/entity/src/entities/user.rs | 26 + backend/migration/Cargo.toml | 2 +- .../src/m20220101_000001_create_table.rs | 38 ++ backend/orm/Cargo.toml | 2 +- backend/orm/src/mutation.rs | 29 +- backend/orm/src/query.rs | 13 +- backend/search/src/lib.rs | 12 +- 20 files changed, 702 insertions(+), 175 deletions(-) create mode 100644 backend/entity/src/entities/user.rs diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 4091b14..8e662d8 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -34,15 +34,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "aho-corasick" -version = "0.7.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" -dependencies = [ - "memchr", -] - [[package]] name = "aliasable" version = "0.1.3" @@ -82,6 +73,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "anyhow" +version = "1.0.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61" + +[[package]] +name = "arrayref" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" + [[package]] name = "arrayvec" version = "0.5.2" @@ -188,6 +191,27 @@ dependencies = [ "event-listener", ] +[[package]] +name = "async-session" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07da4ce523b4e2ebaaf330746761df23a465b951a83d84bbce4233dabedae630" +dependencies = [ + "anyhow", + "async-lock", + "async-trait", + "base64 0.13.0", + "bincode", + "blake3", + "chrono", + "hmac 0.11.0", + "log", + "rand", + "serde", + "serde_json", + "sha2 0.9.9", +] + [[package]] name = "async-std" version = "1.12.0" @@ -292,7 +316,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9de18bc5f2e9df8f52da03856bf40e29b747de5a84e43aefff90e3dc4a21529b" dependencies = [ "async-trait", - "axum-core", + "axum-core 0.2.7", "bitflags", "bytes", "futures-util", @@ -300,13 +324,45 @@ dependencies = [ "http-body", "hyper", "itoa", - "matchit", + "matchit 0.5.0", "memchr", "mime", "percent-encoding", "pin-project-lite", "serde", + "sync_wrapper", + "tokio", + "tower", + "tower-http", + "tower-layer", + "tower-service", +] + +[[package]] +name = "axum" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08b108ad2665fa3f6e6a517c3d80ec3e77d224c47d605167aefaa5d7ef97fa48" +dependencies = [ + "async-trait", + "axum-core 0.3.0", + "bitflags", + "bytes", + "futures-util", + "headers", + "http", + "http-body", + "hyper", + "itoa", + "matchit 0.7.0", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", "serde_json", + "serde_path_to_error", "serde_urlencoded", "sync_wrapper", "tokio", @@ -330,13 +386,30 @@ dependencies = [ "mime", ] +[[package]] +name = "axum-core" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79b8558f5a0581152dc94dcd289132a1d377494bdeafcd41869b3258e3e2ad92" +dependencies = [ + "async-trait", + "bytes", + "futures-util", + "http", + "http-body", + "mime", + "rustversion", + "tower-layer", + "tower-service", +] + [[package]] name = "axum-extra" version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69034b3b0fd97923eee2ce8a47540edb21e07f48f87f67d44bb4271cec622bdb" dependencies = [ - "axum", + "axum 0.5.15", "bytes", "futures-util", "http", @@ -349,6 +422,64 @@ dependencies = [ "tower-service", ] +[[package]] +name = "axum-extra" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9a320103719de37b7b4da4c8eb629d4573f6bcfd3dfe80d3208806895ccf81d" +dependencies = [ + "axum 0.6.1", + "bytes", + "cookie", + "futures-util", + "http", + "mime", + "pin-project-lite", + "tokio", + "tower", + "tower-http", + "tower-layer", + "tower-service", +] + +[[package]] +name = "axum-login" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53267fed23ef097855844f578b4dfe15097186cb07f62491064fbc09bb4e09fd" +dependencies = [ + "async-trait", + "axum 0.6.1", + "axum-sessions", + "base64 0.13.0", + "dyn-clone", + "eyre", + "futures", + "ring", + "secrecy", + "serde_json", + "tokio", + "tower", + "tower-http", + "tracing", +] + +[[package]] +name = "axum-sessions" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b114309d293dd8a6fedebf09d5b8bbb0f7647b3d204ca0dd333b5f797aed5c8" +dependencies = [ + "async-session", + "axum 0.6.1", + "axum-extra 0.4.2", + "futures", + "http-body", + "tokio", + "tower", + "tracing", +] + [[package]] name = "backend" version = "0.1.0" @@ -364,7 +495,7 @@ checksum = "cab84319d616cfb654d03394f38ab7e6f0919e181b1b57e1fd15e7fb4077d9a7" dependencies = [ "addr2line", "cc", - "cfg-if", + "cfg-if 1.0.0", "libc", "miniz_oxide 0.5.3", "object", @@ -390,6 +521,21 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" +[[package]] +name = "base64" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ea22880d78093b0cbe17c89f64a7d457941e65759157ec6cb31a31d652b05e5" + +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + [[package]] name = "bit_field" version = "0.10.1" @@ -402,6 +548,30 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "blake3" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b64485778c4f16a6a5a9d335e80d449ac6c70cdd6a06d2af18a6f6f775a125b3" +dependencies = [ + "arrayref", + "arrayvec 0.5.2", + "cc", + "cfg-if 0.1.10", + "constant_time_eq", + "crypto-mac 0.8.0", + "digest 0.9.0", +] + +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array", +] + [[package]] name = "block-buffer" version = "0.10.2" @@ -429,8 +599,9 @@ dependencies = [ name = "booksman-api" version = "0.1.0" dependencies = [ - "axum", - "axum-extra", + "axum 0.6.1", + "axum-extra 0.3.7", + "axum-login", "booksman-orm", "booksman-search", "chrono", @@ -443,6 +614,7 @@ dependencies = [ "log", "meilisearch-sdk", "migration", + "rand", "reqwest", "sea-orm", "serde", @@ -536,6 +708,12 @@ version = "1.0.73" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + [[package]] name = "cfg-if" version = "1.0.0" @@ -612,6 +790,28 @@ dependencies = [ "cache-padded", ] +[[package]] +name = "constant_time_eq" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" + +[[package]] +name = "cookie" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e859cd57d0710d9e06c381b550c06e76992472a8c6d527aecd2fc673dcc231fb" +dependencies = [ + "base64 0.20.0", + "hmac 0.12.1", + "percent-encoding", + "rand", + "sha2 0.10.3", + "subtle", + "time 0.3.14", + "version_check", +] + [[package]] name = "core-foundation" version = "0.9.3" @@ -658,7 +858,7 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -667,7 +867,7 @@ version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "crossbeam-utils", ] @@ -677,7 +877,7 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "715e8152b692bba2d374b53d4875445368fdf21a94751410af607a5ac677d1fc" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "crossbeam-epoch", "crossbeam-utils", ] @@ -689,7 +889,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f916dfc5d356b0ed9dae65f1db9fc9770aa2851d2662b988ccf4fe3516e86348" dependencies = [ "autocfg", - "cfg-if", + "cfg-if 1.0.0", "crossbeam-utils", "memoffset", "scopeguard", @@ -701,7 +901,7 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1cd42583b04998a5363558e5f9291ee5a5ff6b49944332103f251e7479a82aa7" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "crossbeam-utils", ] @@ -711,7 +911,7 @@ version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51887d4adc7b564537b15adcfb307936f8075dfcd5f00dde9a9f1d29383682bc" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "once_cell", ] @@ -731,6 +931,26 @@ dependencies = [ "typenum", ] +[[package]] +name = "crypto-mac" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b584a330336237c1eecd3e94266efb216c56ed91225d634cb2991c5f3fd1aeab" +dependencies = [ + "generic-array", + "subtle", +] + +[[package]] +name = "crypto-mac" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1d1a86f49236c215f271d40892d5fc950490551400b02ef360692c29815c714" +dependencies = [ + "generic-array", + "subtle", +] + [[package]] name = "ctor" version = "0.1.23" @@ -772,13 +992,22 @@ dependencies = [ "winapi", ] +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + [[package]] name = "digest" version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506" dependencies = [ - "block-buffer", + "block-buffer 0.10.2", "crypto-common", "subtle", ] @@ -803,12 +1032,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "dotenv" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" - [[package]] name = "dotenvy" version = "0.15.3" @@ -818,6 +1041,12 @@ dependencies = [ "dirs", ] +[[package]] +name = "dyn-clone" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9b0705efd4599c15a38151f4721f7bc388306f61084d3bfd50bd07fbca5cb60" + [[package]] name = "either" version = "1.8.0" @@ -833,7 +1062,7 @@ version = "0.8.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9852635589dc9f9ea1b6fe9f05b50ef208c85c834a562f0c6abb1c475736ec2b" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -877,10 +1106,14 @@ dependencies = [ ] [[package]] -name = "fallible-iterator" -version = "0.2.0" +name = "eyre" +version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" +checksum = "4c2b6b5a29c02cdc822728b7d7b8ae1bab3e3b05d44522770ddd49722eeac7eb" +dependencies = [ + "indenter", + "once_cell", +] [[package]] name = "fastrand" @@ -1076,7 +1309,7 @@ version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "js-sys", "libc", "wasi 0.11.0+wasi-snapshot-preview1", @@ -1157,6 +1390,31 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "headers" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3e372db8e5c0d213e0cd0b9be18be2aca3d44cf2fe30a9d46a65581cd454584" +dependencies = [ + "base64 0.13.0", + "bitflags", + "bytes", + "headers-core", + "http", + "httpdate", + "mime", + "sha1", +] + +[[package]] +name = "headers-core" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" +dependencies = [ + "http", +] + [[package]] name = "heck" version = "0.3.3" @@ -1190,13 +1448,23 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hmac" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a2a2320eb7ec0ebe8da8f744d7812d9fc4cb4d09344ac01898dbcb6a20ae69b" +dependencies = [ + "crypto-mac 0.11.1", + "digest 0.9.0", +] + [[package]] name = "hmac" version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" dependencies = [ - "digest", + "digest 0.10.3", ] [[package]] @@ -1320,6 +1588,12 @@ dependencies = [ "tiff", ] +[[package]] +name = "indenter" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" + [[package]] name = "indexmap" version = "1.9.1" @@ -1336,7 +1610,7 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -1429,7 +1703,7 @@ version = "8.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aa4b4af834c6cfd35d8763d359661b90f2e45d8f750a0849156c7f4671af09c" dependencies = [ - "base64", + "base64 0.13.0", "ring", "serde", "serde_json", @@ -1464,7 +1738,7 @@ checksum = "6607c62aa161d23d17a9072cc5da0be67cdfc89d3afb1e8d9c842bebc2525ffe" dependencies = [ "arrayvec 0.5.2", "bitflags", - "cfg-if", + "cfg-if 1.0.0", "ryu", "static_assertions", ] @@ -1524,7 +1798,7 @@ version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "value-bag", ] @@ -1550,13 +1824,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73cbba799671b762df5a175adf59ce145165747bb891505c43d09aefbbf38beb" [[package]] -name = "md-5" -version = "0.10.2" +name = "matchit" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "274fd6bd98a3c75c9515d9393b063099f60f9b47f09ee20a34fd76287fd017f4" -dependencies = [ - "digest", -] +checksum = "b87248edafb776e59e6ee64a79086f65890d3510f2c656c000bf2a7e8a0aea40" [[package]] name = "meilisearch-sdk" @@ -1655,7 +1926,7 @@ dependencies = [ "libc", "log", "wasi 0.11.0+wasi-snapshot-preview1", - "windows-sys", + "windows-sys 0.36.1", ] [[package]] @@ -1781,6 +2052,12 @@ version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "074864da206b4973b84eb91683020dbefd6a8c3f0f38e054d93954e891935e4e" +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + [[package]] name = "openssl" version = "0.10.41" @@ -1788,7 +2065,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "618febf65336490dfcf20b73f885f5651a0c89c64c2d4a8c3662585a70bf5bd0" dependencies = [ "bitflags", - "cfg-if", + "cfg-if 1.0.0", "foreign-types", "libc", "once_cell", @@ -1888,7 +2165,7 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d76e8e1493bcac0d2766c42737f34458f1c8c50c0d23bcb24ea953affb273216" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "instant", "libc", "redox_syscall", @@ -1902,11 +2179,11 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "redox_syscall", "smallvec", - "windows-sys", + "windows-sys 0.36.1", ] [[package]] @@ -1978,47 +2255,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "899b00b9c8ab553c743b3e11e87c5c7d423b2a2de229ba95b24a756344748011" dependencies = [ "autocfg", - "cfg-if", + "cfg-if 1.0.0", "libc", "log", "wepoll-ffi", "winapi", ] -[[package]] -name = "postgres-protocol" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "878c6cbf956e03af9aa8204b407b9cbf47c072164800aa918c516cd4b056c50c" -dependencies = [ - "base64", - "byteorder", - "bytes", - "fallible-iterator", - "hmac", - "md-5", - "memchr", - "rand", - "sha2", - "stringprep", -] - -[[package]] -name = "postgres-types" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73d946ec7d256b04dfadc4e6a3292324e6f417124750fc5c0950f981b703a0f1" -dependencies = [ - "bytes", - "chrono", - "fallible-iterator", - "postgres-protocol", - "serde", - "serde_json", - "time 0.3.14", - "uuid", -] - [[package]] name = "ppv-lite86" version = "0.2.16" @@ -2147,8 +2390,6 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" dependencies = [ - "aho-corasick", - "memchr", "regex-syntax", ] @@ -2182,7 +2423,7 @@ version = "0.11.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b75aa69a3f06bbcc66ede33af2af253c6f7a86b1ca0033f60c580a27074fbf92" dependencies = [ - "base64", + "base64 0.13.0", "bytes", "encoding_rs", "futures-core", @@ -2264,7 +2505,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "88d6731146462ea25d9244b2ed5fd1d716d25c52e4d54aa4fb0f3c4e9854dbe2" dependencies = [ "lazy_static", - "windows-sys", + "windows-sys 0.36.1", ] [[package]] @@ -2281,9 +2522,9 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "sea-orm" -version = "0.9.2" +version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "089dcca5d2c8393f5f21c7f7b0d84062839c3264ba62dcc0043eb207862e13a1" +checksum = "bc2db217f2061ab2bbb1bd22323a533ace0617f97690919f3ed3894e1b3ba170" dependencies = [ "async-stream", "async-trait", @@ -2291,15 +2532,16 @@ dependencies = [ "futures", "futures-util", "log", - "once_cell", "ouroboros", "rust_decimal", "sea-orm-macros", "sea-query", + "sea-query-binder", "sea-strum", "serde", "serde_json", "sqlx", + "thiserror", "time 0.3.14", "tracing", "url", @@ -2308,14 +2550,13 @@ dependencies = [ [[package]] name = "sea-orm-cli" -version = "0.9.2" +version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cefd2d8878bd7e8b7313f036725fa3d08585d101fb1bf3adca7fc13f553f906" +checksum = "ebcce92f0f804acd10b4378a3c8b0e5fb28f3a9ae9337006bd651baa3a95632c" dependencies = [ - "async-std", "chrono", "clap", - "dotenv", + "dotenvy", "regex", "sea-schema", "tracing", @@ -2325,9 +2566,9 @@ dependencies = [ [[package]] name = "sea-orm-macros" -version = "0.9.2" +version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08d9708f945febe8625ccb0334654b97d1a1a0bffa0473d86e6108ad13f79bd5" +checksum = "38066057ef1fa17ddc6ce1458cf269862b8f1df919497d110ea127b549a90fbd" dependencies = [ "bae", "heck 0.3.3", @@ -2338,13 +2579,13 @@ dependencies = [ [[package]] name = "sea-orm-migration" -version = "0.9.2" +version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70a28587780fbae5c414a62bf0b32405f9da2e000d94f426abf214b2b2e68631" +checksum = "ada716f9825e4190a0a8ebaecbf7171ce0ed6f218ea2e70086bdc72ccfc1d03c" dependencies = [ "async-trait", "clap", - "dotenv", + "dotenvy", "sea-orm", "sea-orm-cli", "sea-schema", @@ -2354,20 +2595,33 @@ dependencies = [ [[package]] name = "sea-query" -version = "0.26.3" +version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6a1de9d0334895f7eb51bd1603c3c9f6737413f905a134001f1e198f43bfd70" +checksum = "a4f0fc4d8e44e1d51c739a68d336252a18bc59553778075d5e32649be6ec92ed" dependencies = [ "chrono", - "postgres-types", "rust_decimal", "sea-query-derive", - "sea-query-driver", "serde_json", "time 0.3.14", "uuid", ] +[[package]] +name = "sea-query-binder" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c2585b89c985cfacfe0ec9fc9e7bb055b776c1a2581c4e3c6185af2b8bf8865" +dependencies = [ + "chrono", + "rust_decimal", + "sea-query", + "serde_json", + "sqlx", + "time 0.3.14", + "uuid", +] + [[package]] name = "sea-query-derive" version = "0.2.0" @@ -2381,22 +2635,11 @@ dependencies = [ "thiserror", ] -[[package]] -name = "sea-query-driver" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbda46eb3484cae1efb7bc68bca50f553a5b42c076cf4cbfae05b27f707549d4" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "sea-schema" -version = "0.9.3" +version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94d070aba647637b533bd669a8e430bdc8f7d5249c9b53402da34347563bbfca" +checksum = "38d5fda574d980e9352b6c7abd6fc75697436fe0078cac2b548559b52643ad3b" dependencies = [ "futures", "sea-query", @@ -2437,6 +2680,15 @@ dependencies = [ "syn", ] +[[package]] +name = "secrecy" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bd1c54ea06cfd2f6b63219704de0b9b4f72dcc2b8fdef820be6cd799780e91e" +dependencies = [ + "zeroize", +] + [[package]] name = "security-framework" version = "2.7.0" @@ -2491,6 +2743,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_path_to_error" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26b04f22b563c91331a10074bda3dd5492e3cc39d56bd557e91c0af42b6c7341" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -2503,15 +2764,39 @@ dependencies = [ "serde", ] +[[package]] +name = "sha1" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "006769ba83e921b3085caa8334186b00cf92b4cb1a6cf4632fbccc8eff5c7549" +dependencies = [ + "cfg-if 1.0.0", + "cpufeatures", + "digest 0.10.3", +] + +[[package]] +name = "sha2" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" +dependencies = [ + "block-buffer 0.9.0", + "cfg-if 1.0.0", + "cpufeatures", + "digest 0.9.0", + "opaque-debug", +] + [[package]] name = "sha2" version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "899bf02746a2c92bf1053d9327dadb252b01af1f81f90cdb902411f518bc7215" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", - "digest", + "digest 0.10.3", ] [[package]] @@ -2642,7 +2927,7 @@ dependencies = [ "rust_decimal", "serde", "serde_json", - "sha2", + "sha2 0.10.3", "smallvec", "sqlformat", "sqlx-rt", @@ -2667,7 +2952,7 @@ dependencies = [ "proc-macro2", "quote", "serde_json", - "sha2", + "sha2 0.10.3", "sqlx-core", "sqlx-rt", "syn", @@ -2737,7 +3022,7 @@ version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "fastrand", "libc", "redox_syscall", @@ -2856,9 +3141,9 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.20.1" +version = "1.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a8325f63a7d4774dd041e363b2409ed1c5cbbd0f867795e661df066b2b0a581" +checksum = "eab6d665857cc6ca78d6e80303a02cea7a7851e85dfbd77cbdc09bd129f1ef46" dependencies = [ "autocfg", "bytes", @@ -2866,13 +3151,12 @@ dependencies = [ "memchr", "mio", "num_cpus", - "once_cell", "parking_lot 0.12.1", "pin-project-lite", "signal-hook-registry", "socket2", "tokio-macros", - "winapi", + "windows-sys 0.42.0", ] [[package]] @@ -2944,7 +3228,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c530c8675c1dbf98facee631536fa116b5fb6382d7dd6dc1b118d970eafe3ba" dependencies = [ "async-compression", - "base64", + "base64 0.13.0", "bitflags", "bytes", "futures-core", @@ -2969,9 +3253,9 @@ dependencies = [ [[package]] name = "tower-layer" -version = "0.3.1" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "343bc9466d3fe6b0f960ef45960509f84480bf4fd96f92901afe7ff3df9d3a62" +checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" [[package]] name = "tower-service" @@ -2985,7 +3269,7 @@ version = "0.1.36" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fce9567bd60a67d08a16488756721ba392f24f29006402881e43b19aac64307" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "log", "pin-project-lite", "tracing-attributes", @@ -3196,7 +3480,7 @@ version = "0.2.82" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc7652e3f6c4706c8d9cd54832c4a4ccb9b5336e2c3bd154d5cccfbf1c1f5f7d" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "wasm-bindgen-macro", ] @@ -3221,7 +3505,7 @@ version = "0.4.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fa76fb221a1f8acddf5b54ace85912606980ad661ac7a503b4570ffd3a624dad" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "js-sys", "wasm-bindgen", "web-sys", @@ -3318,43 +3602,100 @@ version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" dependencies = [ - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_msvc", + "windows_aarch64_msvc 0.36.1", + "windows_i686_gnu 0.36.1", + "windows_i686_msvc 0.36.1", + "windows_x86_64_gnu 0.36.1", + "windows_x86_64_msvc 0.36.1", ] +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc 0.42.0", + "windows_i686_gnu 0.42.0", + "windows_i686_msvc 0.42.0", + "windows_x86_64_gnu 0.42.0", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc 0.42.0", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" + [[package]] name = "windows_aarch64_msvc" version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" + [[package]] name = "windows_i686_gnu" version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" +[[package]] +name = "windows_i686_gnu" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" + [[package]] name = "windows_i686_msvc" version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" +[[package]] +name = "windows_i686_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" + [[package]] name = "windows_x86_64_gnu" version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" + [[package]] name = "windows_x86_64_msvc" version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" + [[package]] name = "winreg" version = "0.10.1" @@ -3373,3 +3714,9 @@ dependencies = [ "serde", "url", ] + +[[package]] +name = "zeroize" +version = "1.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c394b5bd0c6f669e7275d9c20aa90ae064cb22e75a1cad54e1b34088034b149f" diff --git a/backend/api/Cargo.toml b/backend/api/Cargo.toml index 44622cb..5c8d819 100644 --- a/backend/api/Cargo.toml +++ b/backend/api/Cargo.toml @@ -9,7 +9,7 @@ booksman-orm = { path = "../orm" } booksman-search = { path = "../search" } migration = { path = "../migration" } entity = { path = "../entity" } -axum = "^0.5" +axum = "^0.6" axum-extra = { version = "^0.3", features = ["spa"] } clap = { version = "^3", features = ["derive"] } dotenvy = "0.15.0" @@ -27,9 +27,15 @@ tracing = "^0.1" tracing-subscriber = "^0.3" itertools = "0.10" chrono = "0.4" +axum-login = "0.4" +#features = ["sqlite"] + +[dependencies.rand] +version = "0.8.5" +features = ["min_const_gen"] [dependencies.sea-orm] -version = "^0.9.2" # sea-orm version +version = "^0.10.6" # sea-orm version features = [ "debug-print", "runtime-tokio-native-tls", diff --git a/backend/api/src/lib.rs b/backend/api/src/lib.rs index cdf1c95..a67ca5b 100644 --- a/backend/api/src/lib.rs +++ b/backend/api/src/lib.rs @@ -31,6 +31,31 @@ use ::entity::entities::{book,book_author,book_person,book_place,book_subject,bo use std::env; use migration::{Migrator, MigratorTrait}; use chrono::Local; +use axum_login::{ + axum_sessions::{async_session::MemoryStore as SessionMemoryStore, SessionLayer}, + memory_store::MemoryStore as AuthMemoryStore, + secrecy::SecretVec, + AuthLayer, AuthUser, RequireAuthorizationLayer, +}; +use rand::Rng; +use std::{collections::HashMap, sync::Arc}; + +#[derive(Debug, Default, Clone)] +struct User { + id: i64, + password_hash: String, + name: String, +} + +impl AuthUser for User { + fn get_id(&self) -> String { + format!("{}", self.id) + } + + fn get_password_hash(&self) -> SecretVec { + SecretVec::new(self.password_hash.clone().into()) + } +} #[derive(Deserialize, Serialize, Debug, Clone)] struct BookUI { @@ -206,6 +231,8 @@ struct Opt { static_dir: String, } +type AuthContext = axum_login::extractors::AuthContext>; + #[tokio::main] pub async fn main() { let opt = Opt::parse(); @@ -225,6 +252,10 @@ pub async fn main() { let meili_url = env::var("MEILI_URL").expect("MEILI_URL is not set in .env file"); let meili_key = env::var("MEILI_KEY").expect("MEILI_KEY is not set in .env file"); + let secret = rand::thread_rng().gen::<[u8; 64]>(); + + let session_store = MemoryStore::new(); + let session_layer = SessionLayer::new(session_store, &secret).with_secure(false); let conn = Database::connect(db_url) .await @@ -232,8 +263,37 @@ pub async fn main() { // Apply all pending migrations Migrator::up(&conn, None).await.unwrap(); + + + let store = Arc::new(RwLock::new(HashMap::default())); + let users : Vec = get_users_seaorm(conn); + //let user = User::get_rusty_user(); + for user in users.iter() { + store.write().await.insert(user.get_id(), user); + } + + let user_store = AuthMemoryStore::new(&store); + let auth_layer = AuthLayer::new(user_store, &secret); + let meili_client = Client::new(meili_url, meili_key); + + async fn login_handler(mut auth: AuthContext, Json(user_sent): Json) { + auth.login(&user_sent).await.unwrap(); + } + + async fn register_handler(mut auth: AuthContext, Json(user_sent): Json) { + // add to db + store.write().await.insert(user_sent.get_id(), user_sent); + auth.login(&user_sent).await.unwrap(); + } + + async fn logout_handler(mut auth: AuthContext) { + dbg!("Logging out user: {}", &auth.current_user); + auth.logout().await; + } + + let app = Router::new() .route("/api/search_openlibrary", get(search_openlibrary)) .route("/api/create_by_isbn", get(create_by_isbn)) @@ -242,8 +302,13 @@ pub async fn main() { .route("/api/list_search", get(list_search_book)) .route("/api/create", post(create_book)) .route("/api/update", post(update_book)) + .route("/api/login", post(login_handler)) + .route("/api/register", post(register_handler)) + .route("/api/logout", post(logout_handler)) .nest("/images", get_service(ServeDir::new(images_dir)).handle_error(handle_error)) .merge(SpaRouter::new("/assets", opt.static_dir)) + .layer(auth_layer) + .layer(session_layer) .layer(ServiceBuilder::new().layer(TraceLayer::new_for_http())) .layer(Extension(conn)) .layer(Extension(meili_client)) diff --git a/backend/entity/Cargo.toml b/backend/entity/Cargo.toml index 981e6fd..fa03698 100644 --- a/backend/entity/Cargo.toml +++ b/backend/entity/Cargo.toml @@ -13,4 +13,4 @@ serde = { version = "1", features = ["derive"] } chrono = "0.4" [dependencies.sea-orm] -version = "^0.9.2" # sea-orm version +version = "^0.10.6" # sea-orm version diff --git a/backend/entity/src/entities/book.rs b/backend/entity/src/entities/book.rs index 8ae4d54..8833780 100644 --- a/backend/entity/src/entities/book.rs +++ b/backend/entity/src/entities/book.rs @@ -1,8 +1,8 @@ -//! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6 use sea_orm::entity::prelude::*; -#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[sea_orm(table_name = "book")] pub struct Model { #[sea_orm(primary_key)] @@ -19,12 +19,15 @@ pub struct Model { pub time_added: Option, pub rating: Option, pub comments: Option, + pub user_id: i32, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation { #[sea_orm(has_many = "super::book_author::Entity")] BookAuthor, + #[sea_orm(has_many = "super::book_isbn::Entity")] + BookIsbn, #[sea_orm(has_many = "super::book_person::Entity")] BookPerson, #[sea_orm(has_many = "super::book_place::Entity")] @@ -33,8 +36,14 @@ pub enum Relation { BookSubject, #[sea_orm(has_many = "super::book_time::Entity")] BookTime, - #[sea_orm(has_many = "super::book_isbn::Entity")] - BookIsbn, + #[sea_orm( + belongs_to = "super::user::Entity", + from = "Column::UserId", + to = "super::user::Column::Id", + on_update = "Cascade", + on_delete = "Cascade" + )] + User, } impl Related for Entity { @@ -43,6 +52,12 @@ impl Related for Entity { } } +impl Related for Entity { + fn to() -> RelationDef { + Relation::BookIsbn.def() + } +} + impl Related for Entity { fn to() -> RelationDef { Relation::BookPerson.def() @@ -67,9 +82,9 @@ impl Related for Entity { } } -impl Related for Entity { +impl Related for Entity { fn to() -> RelationDef { - Relation::BookIsbn.def() + Relation::User.def() } } diff --git a/backend/entity/src/entities/book_author.rs b/backend/entity/src/entities/book_author.rs index ce4e23e..9cdcb95 100644 --- a/backend/entity/src/entities/book_author.rs +++ b/backend/entity/src/entities/book_author.rs @@ -1,8 +1,8 @@ -//! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6 use sea_orm::entity::prelude::*; -#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[sea_orm(table_name = "book_author")] pub struct Model { #[sea_orm(primary_key)] diff --git a/backend/entity/src/entities/book_isbn.rs b/backend/entity/src/entities/book_isbn.rs index 86ccdb5..5e29119 100644 --- a/backend/entity/src/entities/book_isbn.rs +++ b/backend/entity/src/entities/book_isbn.rs @@ -1,8 +1,8 @@ -//! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6 use sea_orm::entity::prelude::*; -#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[sea_orm(table_name = "book_isbn")] pub struct Model { #[sea_orm(primary_key)] diff --git a/backend/entity/src/entities/book_person.rs b/backend/entity/src/entities/book_person.rs index 09a55eb..0f629cb 100644 --- a/backend/entity/src/entities/book_person.rs +++ b/backend/entity/src/entities/book_person.rs @@ -1,8 +1,8 @@ -//! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6 use sea_orm::entity::prelude::*; -#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[sea_orm(table_name = "book_person")] pub struct Model { #[sea_orm(primary_key)] diff --git a/backend/entity/src/entities/book_place.rs b/backend/entity/src/entities/book_place.rs index 5a254fb..57b88f8 100644 --- a/backend/entity/src/entities/book_place.rs +++ b/backend/entity/src/entities/book_place.rs @@ -1,8 +1,8 @@ -//! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6 use sea_orm::entity::prelude::*; -#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[sea_orm(table_name = "book_place")] pub struct Model { #[sea_orm(primary_key)] diff --git a/backend/entity/src/entities/book_subject.rs b/backend/entity/src/entities/book_subject.rs index 69e70e7..080bb2b 100644 --- a/backend/entity/src/entities/book_subject.rs +++ b/backend/entity/src/entities/book_subject.rs @@ -1,8 +1,8 @@ -//! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6 use sea_orm::entity::prelude::*; -#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[sea_orm(table_name = "book_subject")] pub struct Model { #[sea_orm(primary_key)] diff --git a/backend/entity/src/entities/book_time.rs b/backend/entity/src/entities/book_time.rs index a9d2038..c675f14 100644 --- a/backend/entity/src/entities/book_time.rs +++ b/backend/entity/src/entities/book_time.rs @@ -1,8 +1,8 @@ -//! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6 use sea_orm::entity::prelude::*; -#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[sea_orm(table_name = "book_time")] pub struct Model { #[sea_orm(primary_key)] diff --git a/backend/entity/src/entities/mod.rs b/backend/entity/src/entities/mod.rs index 78abcbd..ffbc5d4 100644 --- a/backend/entity/src/entities/mod.rs +++ b/backend/entity/src/entities/mod.rs @@ -1,4 +1,4 @@ -//! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6 pub mod prelude; @@ -9,3 +9,4 @@ pub mod book_person; pub mod book_place; pub mod book_subject; pub mod book_time; +pub mod user; diff --git a/backend/entity/src/entities/prelude.rs b/backend/entity/src/entities/prelude.rs index acb1b5f..97efd3c 100644 --- a/backend/entity/src/entities/prelude.rs +++ b/backend/entity/src/entities/prelude.rs @@ -1,4 +1,4 @@ -//! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6 pub use super::book::Entity as Book; pub use super::book_author::Entity as BookAuthor; @@ -7,3 +7,4 @@ pub use super::book_person::Entity as BookPerson; pub use super::book_place::Entity as BookPlace; pub use super::book_subject::Entity as BookSubject; pub use super::book_time::Entity as BookTime; +pub use super::user::Entity as User; diff --git a/backend/entity/src/entities/user.rs b/backend/entity/src/entities/user.rs new file mode 100644 index 0000000..4a6472b --- /dev/null +++ b/backend/entity/src/entities/user.rs @@ -0,0 +1,26 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "user")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub user_name: Option, + pub password_hash: String, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(has_many = "super::book::Entity")] + Book, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Book.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/backend/migration/Cargo.toml b/backend/migration/Cargo.toml index ec4f27e..2679b0e 100644 --- a/backend/migration/Cargo.toml +++ b/backend/migration/Cargo.toml @@ -13,7 +13,7 @@ async-std = { version = "^1", features = ["attributes", "tokio1"] } chrono = "0.4" [dependencies.sea-orm-migration] -version = "^0.9.2" +version = "^0.10.6" features = [ # Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI. # View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime. diff --git a/backend/migration/src/m20220101_000001_create_table.rs b/backend/migration/src/m20220101_000001_create_table.rs index a4f2af8..5037a25 100644 --- a/backend/migration/src/m20220101_000001_create_table.rs +++ b/backend/migration/src/m20220101_000001_create_table.rs @@ -10,6 +10,26 @@ impl MigrationTrait for Migration { // Replace the sample below with your own migration scripts //todo!(); + + manager + .create_table( + Table::create() + .table(User::Table) + .if_not_exists() + .col( + ColumnDef::new(User::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(User::UserName).string().unique_key().not_null()) + .col(ColumnDef::new(User::PasswordHash).string().not_null()) + .to_owned(), + ) + .await.expect("Migration failed"); + + manager .create_table( Table::create() @@ -34,6 +54,15 @@ impl MigrationTrait for Migration { .col(ColumnDef::new(Book::TimeAdded).time()) .col(ColumnDef::new(Book::Rating).integer()) .col(ColumnDef::new(Book::Comments).string()) + .col(ColumnDef::new(Book::UserId).integer().not_null()) + .foreign_key( + ForeignKey::create() + .name("FK_2e303c3a712662f1fc2a4d0aavc") + .from(Book::Table, Book::UserId) + .to(User::Table, User::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade), + ) .to_owned(), ) .await.expect("Migration failed"); @@ -246,6 +275,7 @@ enum Book { TimeAdded, Rating, Comments, + UserId } #[derive(Iden)] @@ -295,3 +325,11 @@ enum BookISBN { ISBN, BookId, } + +#[derive(Iden)] +enum User { + Table, + Id, + UserName, + PasswordHash, +} diff --git a/backend/orm/Cargo.toml b/backend/orm/Cargo.toml index 9fb2c1c..82e20ed 100644 --- a/backend/orm/Cargo.toml +++ b/backend/orm/Cargo.toml @@ -10,7 +10,7 @@ entity = { path = "../entity" } chrono = "0.4" [dependencies.sea-orm] -version = "^0.9.2" # sea-orm version +version = "^0.10.6" # sea-orm version features = [ "debug-print", "runtime-tokio-native-tls", diff --git a/backend/orm/src/mutation.rs b/backend/orm/src/mutation.rs index 3213c40..1abcfc0 100644 --- a/backend/orm/src/mutation.rs +++ b/backend/orm/src/mutation.rs @@ -1,14 +1,37 @@ //use ::entity::entities::prelude::Book; //use ::entity::entities::{prelude::*, *}; -use ::entity::entities::{book::Entity as Book}; +use ::entity::entities::user::Entity as User; +use ::entity::entities::book::Entity as Book; //, book_author::Entity as Author, book_person::Entity as Person, book_place::Entity as Place, book_subject::Entity as Subject, book_time::Entity as Time, book_isbn::Entity as ISBN}; -use ::entity::entities::{book,book_author,book_person,book_place,book_subject,book_time,book_isbn}; +use ::entity::entities::{book,book_author,book_person,book_place,book_subject,book_time,book_isbn,user}; use sea_orm::*; //use ::entity::entities::prelude::Book; pub struct Mutation; impl Mutation { + pub async fn create_user( + db: &DbConn, + form_data: user::Model, + ) -> Result, DbErr> { + let record = user::ActiveModel { + user_name: Set(form_data.user_name.to_owned()), + password_hash: Set(form_data.password_hash.to_owned()), + ..Default::default() + }; + User::insert(record).exec(db).await + } + + pub async fn delete_user(db: &DbConn, id: i32) -> Result { + let user: user::ActiveModel = User::find_by_id(id) + .one(db) + .await? + .ok_or(DbErr::Custom("Cannot find user.".to_owned())) + .map(Into::into)?; + + user.delete(db).await + } + pub async fn create_book( db: &DbConn, form_data: book::Model, @@ -26,6 +49,7 @@ impl Mutation { time_added: Set(form_data.time_added.to_owned()), rating: Set(form_data.rating.to_owned()), comments: Set(form_data.comments.to_owned()), + user_id: Set(form_data.user_id.to_owned()), ..Default::default() }; Book::insert(record).exec(db).await @@ -134,6 +158,7 @@ impl Mutation { time_added: Set(form_data.time_added.to_owned()), rating: Set(form_data.rating.to_owned()), comments: Set(form_data.comments.to_owned()), + user_id: Set(form_data.user_id.to_owned()), }.update(db).await } diff --git a/backend/orm/src/query.rs b/backend/orm/src/query.rs index b931c68..533d25e 100644 --- a/backend/orm/src/query.rs +++ b/backend/orm/src/query.rs @@ -59,11 +59,13 @@ impl Query { /// If ok, returns (post models, num pages). pub async fn find_books_in_page( db: &DbConn, - page: usize, - posts_per_page: usize, - ) -> Result<(Vec, usize), DbErr> { + page: u64, + posts_per_page: u64, + userid: i32, + ) -> Result<(Vec, u64), DbErr> { // Setup paginator let paginator = Book::find() + .filter(book::Column::UserId.eq(userid)) .order_by_asc(book::Column::Id) .paginate(db, posts_per_page); let num_pages = paginator.num_pages().await?; @@ -76,9 +78,10 @@ pub async fn find_books_plus_meta_in_page( db: &DbConn, page: usize, posts_per_page: usize, - ) -> Result<(Vec, usize), DbErr> { + userid: i32, + ) -> Result<(Vec, u64), DbErr> { // Setup paginator - let books = Self::find_books_in_page(db,page,posts_per_page).await?; + let books = Self::find_books_in_page(db,page.try_into().unwrap(),posts_per_page.try_into().unwrap(),userid).await?; let book_ids: Vec = books.0.clone().into_iter().map(|b| b.id).collect(); let mut resbooks: Vec = Vec::with_capacity(book_ids.len()); for book in books.0.iter() { diff --git a/backend/search/src/lib.rs b/backend/search/src/lib.rs index cf2b257..181f312 100644 --- a/backend/search/src/lib.rs +++ b/backend/search/src/lib.rs @@ -25,9 +25,9 @@ pub struct BookMeili { pub isbn: Vec, } -pub async fn create_or_update_book(book: BookMeili, client: &Client) { +pub async fn create_or_update_book(book: BookMeili, userid: i32, client: &Client) { // An index is where the documents are stored. - let books = client.index("books"); + let books = client.index(format!("books{}",userid)); // Add some movies in the index. If the index 'movies' does not exist, Meilisearch creates it when you first add the documents. books.add_or_replace(&[ book @@ -35,16 +35,16 @@ pub async fn create_or_update_book(book: BookMeili, client: &Client) { } -pub async fn delete_book(bookid: i32, client: &Client) { +pub async fn delete_book(bookid: i32, userid: i32, client: &Client) { // An index is where the documents are stored. - let books = client.index("books"); + let books = client.index(format!("books{}",userid)); books.delete_document(bookid).await.unwrap(); } -pub async fn search_book(search: &str, page: usize, client: &Client) -> Result<(Vec, usize), meilisearch_sdk::errors::Error> { +pub async fn search_book(search: &str, page: usize, userid: i32, client: &Client) -> Result<(Vec, usize), meilisearch_sdk::errors::Error> { // An index is where the documents are stored. - let books = client.index("books"); + let books = client.index(format!("books{}",userid)); let results : SearchResults = books.search().with_query(search).with_offset((page-1)*12) .execute::().await.unwrap();