diff --git a/backend-actix/Cargo.lock b/backend-actix/Cargo.lock index 2b50c28..f9752fa 100644 --- a/backend-actix/Cargo.lock +++ b/backend-actix/Cargo.lock @@ -295,12 +295,12 @@ dependencies = [ [[package]] name = "anstyle-wincon" -version = "3.0.7" +version = "3.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" +checksum = "6680de5231bd6ee4c6191b8a1325daa282b415391ec9d3a37bd34f2060dc73fa" dependencies = [ "anstyle", - "once_cell", + "once_cell_polyfill", "windows-sys 0.59.0", ] @@ -328,13 +328,10 @@ version = "0.1.0" dependencies = [ "actix-cors", "actix-web", - "argon2", "chrono", - "diesel", - "diesel-derive-enum", - "diesel_migrations", "env_logger", "gamenight-api-client-rs", + "gamenight-database", "jsonwebtoken", "rand_core 0.9.3", "serde", @@ -356,7 +353,7 @@ dependencies = [ "miniz_oxide", "object", "rustc-demangle", - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -373,9 +370,9 @@ checksum = "89e25b6adfb930f02d1981565a6e5d9c547ac15a96606256d3b59040e5cd4ca3" [[package]] name = "bitflags" -version = "2.9.0" +version = "2.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" +checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" [[package]] name = "blake2" @@ -445,9 +442,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.22" +version = "1.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32db95edf998450acc7881c932f94cd9b05c87b4b2599e8bab064753da4acfd1" +checksum = "d0fc897dc1e865cc67c0e05a836d9d3f1df3cbe442aa4a9473b18e12624a4951" dependencies = [ "jobserver", "libc", @@ -814,6 +811,20 @@ dependencies = [ "url", ] +[[package]] +name = "gamenight-database" +version = "0.1.0" +dependencies = [ + "argon2", + "chrono", + "diesel", + "diesel-derive-enum", + "diesel_migrations", + "rand_core 0.9.3", + "serde", + "uuid", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -970,17 +981,21 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.11" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "497bbc33a26fdd4af9ed9c70d63f61cf56a938375fbb32df34db9b1cd6d643f2" +checksum = "b1c293b6b3d21eca78250dc7dbebd6b9210ec5530e038cbfe0661b5c47ab06e8" dependencies = [ + "base64", "bytes", "futures-channel", + "futures-core", "futures-util", "http 1.3.1", "http-body", "hyper", + "ipnet", "libc", + "percent-encoding", "pin-project-lite", "socket2", "tokio", @@ -1061,9 +1076,9 @@ checksum = "00210d6893afc98edb752b664b8890f0ef174c8adbb8d0be9710fa66fbbf72d3" [[package]] name = "icu_properties" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2549ca8c7241c82f59c80ba2a6f415d931c5b58d24fb8412caa1a1f02c49139a" +checksum = "016c619c1eeb94efb86809b015c58f479963de65bdb6253345c1a1276f22e32b" dependencies = [ "displaydoc", "icu_collections", @@ -1077,9 +1092,9 @@ dependencies = [ [[package]] name = "icu_properties_data" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8197e866e47b68f8f7d95249e172903bec06004b18b2937f1095d40a0c57de04" +checksum = "298459143998310acd25ffe6810ed544932242d3f07083eee1084d83a71bd632" [[package]] name = "icu_provider" @@ -1147,6 +1162,16 @@ version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" +[[package]] +name = "iri-string" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbc5ebe9c3a1a7a5127f920a418f7585e9e758e911d0466ed004f393b0e380b2" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -1161,9 +1186,9 @@ checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" [[package]] name = "jiff" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f02000660d30638906021176af16b17498bd0d12813dbfe7b276d8bc7f3c0806" +checksum = "a194df1107f33c79f4f93d02c80798520551949d59dfad22b6157048a88cca93" dependencies = [ "jiff-static", "log", @@ -1174,9 +1199,9 @@ dependencies = [ [[package]] name = "jiff-static" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3c30758ddd7188629c6713fc45d1188af4f44c90582311d0c8d8c9907f60c48" +checksum = "6c6e1db7ed32c6c71b759497fae34bf7933636f75a251b9e736555da426f6442" dependencies = [ "proc-macro2", "quote", @@ -1255,9 +1280,9 @@ checksum = "4d873d7c67ce09b42110d801813efbc9364414e356be9935700d368351657487" [[package]] name = "lock_api" -version = "0.4.12" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +checksum = "96936507f153605bddfcda068dd804796c84324ed2510809e5b2a624c81da765" dependencies = [ "autocfg", "scopeguard", @@ -1323,14 +1348,14 @@ dependencies = [ [[package]] name = "mio" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" +checksum = "78bed444cc8a2160f01cbcf811ef18cac863ad68ae8ca62092e8db51d51c761c" dependencies = [ "libc", "log", "wasi 0.11.0+wasi-snapshot-preview1", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -1389,10 +1414,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] -name = "parking_lot" -version = "0.12.3" +name = "once_cell_polyfill" +version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" + +[[package]] +name = "parking_lot" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70d58bf43669b5795d1576d0641cfb6fbb2057bf629506267a92807158584a13" dependencies = [ "lock_api", "parking_lot_core", @@ -1400,15 +1431,15 @@ dependencies = [ [[package]] name = "parking_lot_core" -version = "0.9.10" +version = "0.9.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +checksum = "bc838d2a56b5b1a6c25f55575dfc605fabb63bb2365f6c2353ef9159aa69e4a5" dependencies = [ "cfg-if", "libc", "redox_syscall", "smallvec", - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -1607,6 +1638,9 @@ name = "rand_core" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.16", +] [[package]] name = "rand_core" @@ -1663,9 +1697,9 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "reqwest" -version = "0.12.15" +version = "0.12.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d19c46a6fdd48bc4dab94b6103fccc55d34c67cc0ad04653aad4ea2a07cd7bbb" +checksum = "e98ff6b0dbbe4d5a37318f433d4fc82babd21631f194d370409ceb2e40b2f0b5" dependencies = [ "base64", "bytes", @@ -1690,12 +1724,12 @@ dependencies = [ "sync_wrapper", "tokio", "tower", + "tower-http", "tower-service", "url", "wasm-bindgen", "wasm-bindgen-futures", "web-sys", - "windows-registry", ] [[package]] @@ -1720,9 +1754,9 @@ checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] name = "rustversion" -version = "1.0.20" +version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2" +checksum = "8a0d197bd2c9dc6e53b84da9556a69ba4cdfab8619eb41a8bd1cc2027a0f6b1d" [[package]] name = "ryu" @@ -1864,9 +1898,9 @@ checksum = "8917285742e9f3e1683f0a9c4e6b57960b7314d0b08d30d1ecd426713ee2eee9" [[package]] name = "socket2" -version = "0.5.9" +version = "0.5.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f5fd57c80058a56cf5c777ab8a126398ece8e442983605d280a44ce79d0edef" +checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678" dependencies = [ "libc", "windows-sys 0.52.0", @@ -1984,9 +2018,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.45.0" +version = "1.45.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2513ca694ef9ede0fb23fe71a4ee4107cb102b9dc1930f6d0fd77aae068ae165" +checksum = "75ef51a33ef1da925cea3e4eb122833cb377c61439ca401b770f54902b806779" dependencies = [ "backtrace", "bytes", @@ -2068,6 +2102,24 @@ dependencies = [ "tower-service", ] +[[package]] +name = "tower-http" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fdb0c213ca27a9f57ab69ddb290fd80d970922355b83ae380b395d3986b8a2e" +dependencies = [ + "bitflags", + "bytes", + "futures-util", + "http 1.3.1", + "http-body", + "iri-string", + "pin-project-lite", + "tower", + "tower-layer", + "tower-service", +] + [[package]] name = "tower-layer" version = "0.3.3" @@ -2186,12 +2238,14 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "uuid" -version = "1.16.0" +version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "458f7a779bf54acc9f347480ac654f68407d3aab21269a6e3c9f922acd9e2da9" +checksum = "3cf4199d1e5d15ddd86a694e4d0dffa9c323ce759fea589f00fef9d81cc1931d" dependencies = [ "getrandom 0.3.3", + "js-sys", "serde", + "wasm-bindgen", ] [[package]] @@ -2343,15 +2397,15 @@ dependencies = [ [[package]] name = "windows-core" -version = "0.61.0" +version = "0.61.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4763c1de310c86d75a878046489e2e5ba02c649d185f21c67d4cf8a56d098980" +checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3" dependencies = [ "windows-implement", "windows-interface", "windows-link", "windows-result", - "windows-strings 0.4.0", + "windows-strings", ] [[package]] @@ -2382,40 +2436,20 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76840935b766e1b0a05c0066835fb9ec80071d4c09a16f6bd5f7e655e3c14c38" -[[package]] -name = "windows-registry" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4286ad90ddb45071efd1a66dfa43eb02dd0dfbae1545ad6cc3c51cf34d7e8ba3" -dependencies = [ - "windows-result", - "windows-strings 0.3.1", - "windows-targets 0.53.0", -] - [[package]] name = "windows-result" -version = "0.3.2" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c64fd11a4fd95df68efcfee5f44a294fe71b8bc6a91993e2791938abcc712252" +checksum = "56f42bd332cc6c8eac5af113fc0c1fd6a8fd2aa08a0119358686e5160d0586c6" dependencies = [ "windows-link", ] [[package]] name = "windows-strings" -version = "0.3.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87fa48cc5d406560701792be122a10132491cff9d0aeb23583cc2dcafc847319" -dependencies = [ - "windows-link", -] - -[[package]] -name = "windows-strings" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a2ba9642430ee452d5a7aa78d72907ebe8cfda358e8cb7918a2050581322f97" +checksum = "56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57" dependencies = [ "windows-link", ] @@ -2426,7 +2460,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -2435,7 +2469,7 @@ version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -2444,30 +2478,14 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.6", - "windows_aarch64_msvc 0.52.6", - "windows_i686_gnu 0.52.6", - "windows_i686_gnullvm 0.52.6", - "windows_i686_msvc 0.52.6", - "windows_x86_64_gnu 0.52.6", - "windows_x86_64_gnullvm 0.52.6", - "windows_x86_64_msvc 0.52.6", -] - -[[package]] -name = "windows-targets" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1e4c7e8ceaaf9cb7d7507c974735728ab453b67ef8f18febdd7c11fe59dca8b" -dependencies = [ - "windows_aarch64_gnullvm 0.53.0", - "windows_aarch64_msvc 0.53.0", - "windows_i686_gnu 0.53.0", - "windows_i686_gnullvm 0.53.0", - "windows_i686_msvc 0.53.0", - "windows_x86_64_gnu 0.53.0", - "windows_x86_64_gnullvm 0.53.0", - "windows_x86_64_msvc 0.53.0", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] [[package]] @@ -2476,96 +2494,48 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86b8d5f90ddd19cb4a147a5fa63ca848db3df085e25fee3cc10b39b6eebae764" - [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" -[[package]] -name = "windows_aarch64_msvc" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7651a1f62a11b8cbd5e0d42526e55f2c99886c77e007179efff86c2b137e66c" - [[package]] name = "windows_i686_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" -[[package]] -name = "windows_i686_gnu" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1dc67659d35f387f5f6c479dc4e28f1d4bb90ddd1a5d3da2e5d97b42d6272c3" - [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" -[[package]] -name = "windows_i686_gnullvm" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ce6ccbdedbf6d6354471319e781c0dfef054c81fbc7cf83f338a4296c0cae11" - [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" -[[package]] -name = "windows_i686_msvc" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "581fee95406bb13382d2f65cd4a908ca7b1e4c2f1917f143ba16efe98a589b5d" - [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" -[[package]] -name = "windows_x86_64_gnu" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e55b5ac9ea33f2fc1716d1742db15574fd6fc8dadc51caab1c16a3d3b4190ba" - [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a6e035dd0599267ce1ee132e51c27dd29437f63325753051e71dd9e42406c57" - [[package]] name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" -[[package]] -name = "windows_x86_64_msvc" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486" - [[package]] name = "winnow" version = "0.7.10" diff --git a/backend-actix/Cargo.toml b/backend-actix/Cargo.toml index 5f17212..af0e539 100644 --- a/backend-actix/Cargo.toml +++ b/backend-actix/Cargo.toml @@ -7,15 +7,12 @@ edition = "2021" [dependencies] gamenight-api-client-rs = { path = "../gamenight-api-client-rs" } +gamenight-database = { path = "../gamenight-database"} actix-web = "4" actix-cors = "0.7" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" uuid = { version = "1.3.0", features = ["serde", "v4"] } -diesel = { version = "2.0", features = ["postgres", "r2d2", "uuid", "chrono"] } -diesel-derive-enum = { version = "2.0", features = ["postgres"] } -diesel_migrations = "2.0" -argon2 = "0.5" chrono = { version = "0.4", features = ["serde"] } jsonwebtoken = "9.3" validator = { version = "0.20", features = ["derive"] } diff --git a/backend-actix/gamenight-api.yaml b/backend-actix/gamenight-api.yaml index 1e78062..acbe07f 100644 --- a/backend-actix/gamenight-api.yaml +++ b/backend-actix/gamenight-api.yaml @@ -99,9 +99,6 @@ components: type: string owner_id: type: string - participants: - schema: - $ref: "#/components/schemas/User" required: - id - name diff --git a/backend-actix/src/main.rs b/backend-actix/src/main.rs index 076dba0..e45e8ca 100644 --- a/backend-actix/src/main.rs +++ b/backend-actix/src/main.rs @@ -1,6 +1,4 @@ pub mod request; -pub mod schema; -pub mod util; use actix_cors::Cors; use actix_web::middleware::Logger; @@ -8,31 +6,15 @@ use actix_web::HttpServer; use actix_web::App; use actix_web::http; use actix_web::web; -use diesel::PgConnection; -use diesel::r2d2::{ConnectionManager, Pool}; -use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; use request::{login, register, gamenights, gamenight_post, gamenight_get}; -use util::GetConnection; use tracing_actix_web::TracingLogger; - -pub(crate) type DbPool = Pool>; - -pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!(); - -fn run_migration(conn: &mut PgConnection) { - conn.run_pending_migrations(MIGRATIONS).unwrap(); -} +use gamenight_database::*; #[actix_web::main] async fn main() -> std::io::Result<()> { let url = "postgres://root:root@127.0.0.1/gamenight"; - let manager = ConnectionManager::::new(url); - // Refer to the `r2d2` documentation for more methods to use - // when building a connection pool - let pool = Pool::builder() - .test_on_check_out(true) - .build(manager) - .expect("Could not build connection pool"); + + let pool = get_connection_pool(url); let mut conn = pool.get_conn(); run_migration(&mut conn); diff --git a/backend-actix/src/request/authorization.rs b/backend-actix/src/request/authorization.rs index b6ed670..9675921 100644 --- a/backend-actix/src/request/authorization.rs +++ b/backend-actix/src/request/authorization.rs @@ -6,7 +6,7 @@ use jsonwebtoken::{encode, Header, EncodingKey, decode, DecodingKey, Validation} use serde::{Serialize, Deserialize}; use uuid::Uuid; -use crate::{schema::user::{User, get_user}, DbPool}; +use gamenight_database::{user::{get_user, Role, User}, DbPool}; use super::error::ApiError; @@ -16,6 +16,24 @@ pub struct Claims { uid: Uuid } +pub struct AuthUser { + pub id: Uuid, + pub username: String, + pub email: String, + pub role: Role, +} + +impl From for AuthUser { + fn from(value: User) -> Self { + Self{ + id: value.id, + username: value.username, + email: value.email, + role: value.role, + } + } +} + fn get_claims(req: &HttpRequest) -> Result { let token = req.headers() .get(http::header::AUTHORIZATION) @@ -43,18 +61,19 @@ pub fn get_token(user: &User) -> Result { &EncodingKey::from_secret(secret.as_bytes()))?) } -impl FromRequest for User { +impl FromRequest for AuthUser { type Error = ApiError; type Future = Ready>; fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future { ready( - (|| -> Result{ + (|| -> Result{ let pool = req.app_data::>().expect("No database configured"); let mut conn = pool.get().expect("couldn't get db connection from pool"); let uid = get_claims(req)?.uid; + let user = get_user(&mut conn, uid)?; - Ok(get_user(&mut conn, uid)?) + Ok(user.into()) })() ) } diff --git a/backend-actix/src/request/error.rs b/backend-actix/src/request/error.rs index 683febc..97691ea 100644 --- a/backend-actix/src/request/error.rs +++ b/backend-actix/src/request/error.rs @@ -3,7 +3,7 @@ use actix_web::{ResponseError, error::BlockingError, HttpResponse, http::{header use serde::{Serialize, Deserialize}; use validator::ValidationErrors; -use crate::schema::error::DatabaseError; +use gamenight_database::error::DatabaseError; #[derive(Serialize, Deserialize, Debug)] pub struct ApiError { diff --git a/backend-actix/src/request/gamenight_handlers.rs b/backend-actix/src/request/gamenight_handlers.rs index 94f9062..2d6a45b 100644 --- a/backend-actix/src/request/gamenight_handlers.rs +++ b/backend-actix/src/request/gamenight_handlers.rs @@ -2,18 +2,18 @@ use actix_web::{get, web, Responder, http::header::ContentType, HttpResponse, po use chrono::{DateTime, ParseError}; use uuid::Uuid; -use crate::schema::{self}; -use crate::schema::user::User; +use gamenight_database::{gamenight::Gamenight, DbPool, GetConnection}; + +use crate::request::authorization::AuthUser; use crate::request::requests::GamenightGet; use crate::request::requests::GamenightPost; use crate::request::responses::GameNightsResponse; use crate::request::error::ApiError; -use crate::DbPool; -use crate::util::GetConnection; + impl GamenightPost { - pub fn into_with_user(&self, user: User) -> Result { - Ok(schema::gamenight::Gamenight { + pub fn into_with_user(&self, user: AuthUser) -> Result { + Ok(Gamenight { datetime: DateTime::parse_from_rfc3339(&self.datetime)?.with_timezone(&chrono::Utc), id: Uuid::new_v4(), name: self.name.clone(), @@ -29,9 +29,9 @@ impl From for Uuid { } #[get("/gamenights")] -pub async fn gamenights(pool: web::Data, _user: User) -> Result { +pub async fn gamenights(pool: web::Data, _user: AuthUser) -> Result { let mut conn = pool.get_conn(); - let gamenights: GameNightsResponse = schema::gamenights(&mut conn)?; + let gamenights: GameNightsResponse = gamenight_database::gamenights(&mut conn)?; Ok(HttpResponse::Ok() .content_type(ContentType::json()) @@ -40,19 +40,19 @@ pub async fn gamenights(pool: web::Data, _user: User) -> Result, user: User, gamenight_data: web::Json) -> Result { +pub async fn gamenight_post(pool: web::Data, user: AuthUser, gamenight_data: web::Json) -> Result { let mut conn = pool.get_conn(); - schema::gamenight::add_gamenight(&mut conn, gamenight_data.into_with_user(user)?)?; + gamenight_database::gamenight::add_gamenight(&mut conn, gamenight_data.into_with_user(user)?)?; Ok(HttpResponse::Ok()) } #[get("/gamenight")] -pub async fn gamenight_get(pool: web::Data, _user: User, gamenight_data: web::Json) -> Result { +pub async fn gamenight_get(pool: web::Data, _user: AuthUser, gamenight_data: web::Json) -> Result { let mut conn = pool.get_conn(); - let gamenight = schema::gamenight::get_gamenight(&mut conn, gamenight_data.into_inner().into())?; + let gamenight = gamenight_database::gamenight::get_gamenight(&mut conn, gamenight_data.into_inner().into())?; //let participants = schema::user::get_participants(&mut conn, gamenight_id); Ok(HttpResponse::Ok() diff --git a/backend-actix/src/request/requests.rs b/backend-actix/src/request/requests.rs index 2778bc9..3d6c87f 100644 --- a/backend-actix/src/request/requests.rs +++ b/backend-actix/src/request/requests.rs @@ -1,10 +1,6 @@ +use gamenight_database::{DbPool, GetConnection, user::count_users_with_username, user::count_users_with_email}; use serde::{Serialize, Deserialize}; -use validator::Validate; -use diesel::PgConnection; -use diesel::r2d2::ConnectionManager; -use diesel::r2d2::Pool; - -use crate::schema::user::{unique_email, unique_username}; +use validator::{Validate, ValidationError}; #[derive(Serialize, Deserialize, Clone)] pub struct Login { @@ -13,7 +9,29 @@ pub struct Login { } pub struct RegisterContext<'v_a> { - pub pool: &'v_a Pool> + pub pool: &'v_a DbPool +} + +pub fn unique_username(username: &String, context: &RegisterContext) -> Result<(), ValidationError> { + let mut conn = context.pool.get_conn(); + + match count_users_with_username(&mut conn, username) + { + Ok(0) => Ok(()), + Ok(_) => Err(ValidationError::new("User already exists")), + Err(_) => Err(ValidationError::new("Database error while validating user")), + } +} + +pub fn unique_email(email: &String, context: &RegisterContext) -> Result<(), ValidationError> { + let mut conn = context.pool.get_conn(); + + match count_users_with_email(&mut conn, email) + { + Ok(0) => Ok(()), + Ok(_) => Err(ValidationError::new("email already exists")), + Err(_) => Err(ValidationError::new("Database error while validating email")) + } } #[derive(Serialize, Deserialize, Clone, Validate)] diff --git a/backend-actix/src/request/responses.rs b/backend-actix/src/request/responses.rs index 3de8340..5729f76 100644 --- a/backend-actix/src/request/responses.rs +++ b/backend-actix/src/request/responses.rs @@ -1,7 +1,7 @@ use serde::{Serialize, Deserialize}; use uuid::Uuid; -use crate::schema::gamenight::Gamenight; +use gamenight_database::gamenight::Gamenight; #[derive(Serialize, Deserialize)] pub struct LoginResponse { diff --git a/backend-actix/src/request/user_handlers.rs b/backend-actix/src/request/user_handlers.rs index ab0711d..5257adb 100644 --- a/backend-actix/src/request/user_handlers.rs +++ b/backend-actix/src/request/user_handlers.rs @@ -2,27 +2,25 @@ use actix_web::http::header::ContentType; use actix_web::{web, get, post, HttpResponse, Responder}; use validator::ValidateArgs; -use crate::DbPool; use crate::request::requests::{Login, Register, RegisterContext}; use crate::request::error::ApiError; use crate::request::responses::LoginResponse; use crate::request::authorization::get_token; -use crate::util::GetConnection; -use crate::schema::{self}; use serde_json; +use gamenight_database::{DbPool, GetConnection}; -impl From for schema::user::LoginUser { +impl From for gamenight_database::user::LoginUser { fn from(val: Login) -> Self { - schema::user::LoginUser { + gamenight_database::user::LoginUser { username: val.username, password: val.password } } } -impl From for schema::user::Register { +impl From for gamenight_database::user::Register { fn from(val: Register) -> Self { - schema::user::Register { + gamenight_database::user::Register { email: val.email, username: val.username, password: val.password @@ -36,7 +34,7 @@ pub async fn login(pool: web::Data, login_data: web::Json) -> Res if let Ok(Some(user)) = web::block(move || { let mut conn = pool.get_conn(); - schema::login(&mut conn, data.into()) + gamenight_database::login(&mut conn, data.into()) }) .await? { @@ -58,7 +56,7 @@ pub async fn register(pool: web::Data, register_data: web::Json where T: ManageConnection { - fn get_conn(&self) -> PooledConnection; -} - -impl GetConnection for Pool { - fn get_conn(&self) -> PooledConnection { - self.get().expect("Couldn't get db connection from pool") - } -} \ No newline at end of file diff --git a/gamenight-cli/Cargo.lock b/gamenight-cli/Cargo.lock index acfe83e..954a786 100644 --- a/gamenight-cli/Cargo.lock +++ b/gamenight-cli/Cargo.lock @@ -78,9 +78,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.9.0" +version = "2.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" +checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" [[package]] name = "bumpalo" @@ -102,9 +102,9 @@ checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" [[package]] name = "cc" -version = "1.2.22" +version = "1.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32db95edf998450acc7881c932f94cd9b05c87b4b2599e8bab064753da4acfd1" +checksum = "d0fc897dc1e865cc67c0e05a836d9d3f1df3cbe442aa4a9473b18e12624a4951" dependencies = [ "shlex", ] @@ -333,17 +333,21 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.11" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "497bbc33a26fdd4af9ed9c70d63f61cf56a938375fbb32df34db9b1cd6d643f2" +checksum = "b1c293b6b3d21eca78250dc7dbebd6b9210ec5530e038cbfe0661b5c47ab06e8" dependencies = [ + "base64", "bytes", "futures-channel", + "futures-core", "futures-util", "http", "http-body", "hyper", + "ipnet", "libc", + "percent-encoding", "pin-project-lite", "socket2", "tokio", @@ -424,9 +428,9 @@ checksum = "00210d6893afc98edb752b664b8890f0ef174c8adbb8d0be9710fa66fbbf72d3" [[package]] name = "icu_properties" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2549ca8c7241c82f59c80ba2a6f415d931c5b58d24fb8412caa1a1f02c49139a" +checksum = "016c619c1eeb94efb86809b015c58f479963de65bdb6253345c1a1276f22e32b" dependencies = [ "displaydoc", "icu_collections", @@ -440,9 +444,9 @@ dependencies = [ [[package]] name = "icu_properties_data" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8197e866e47b68f8f7d95249e172903bec06004b18b2937f1095d40a0c57de04" +checksum = "298459143998310acd25ffe6810ed544932242d3f07083eee1084d83a71bd632" [[package]] name = "icu_provider" @@ -488,7 +492,7 @@ version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fddf93031af70e75410a2511ec04d49e758ed2f26dad3404a934e0fb45cc12a" dependencies = [ - "bitflags 2.9.0", + "bitflags 2.9.1", "chrono", "crossterm", "dyn-clone", @@ -506,6 +510,16 @@ version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" +[[package]] +name = "iri-string" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbc5ebe9c3a1a7a5127f920a418f7585e9e758e911d0466ed004f393b0e380b2" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "itoa" version = "1.0.15" @@ -536,9 +550,9 @@ checksum = "241eaef5fd12c88705a01fc1066c48c4b36e0dd4377dcdc7ec3942cea7a69956" [[package]] name = "lock_api" -version = "0.4.12" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +checksum = "96936507f153605bddfcda068dd804796c84324ed2510809e5b2a624c81da765" dependencies = [ "autocfg", "scopeguard", @@ -595,13 +609,13 @@ dependencies = [ [[package]] name = "mio" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" +checksum = "78bed444cc8a2160f01cbcf811ef18cac863ad68ae8ca62092e8db51d51c761c" dependencies = [ "libc", "wasi", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -639,9 +653,9 @@ checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] name = "parking_lot" -version = "0.12.3" +version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +checksum = "70d58bf43669b5795d1576d0641cfb6fbb2057bf629506267a92807158584a13" dependencies = [ "lock_api", "parking_lot_core", @@ -649,9 +663,9 @@ dependencies = [ [[package]] name = "parking_lot_core" -version = "0.9.10" +version = "0.9.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +checksum = "bc838d2a56b5b1a6c25f55575dfc605fabb63bb2365f6c2353ef9159aa69e4a5" dependencies = [ "cfg-if", "libc", @@ -711,14 +725,14 @@ version = "0.5.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "928fca9cf2aa042393a8325b9ead81d2f0df4cb12e1e24cef072922ccd99c5af" dependencies = [ - "bitflags 2.9.0", + "bitflags 2.9.1", ] [[package]] name = "reqwest" -version = "0.12.15" +version = "0.12.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d19c46a6fdd48bc4dab94b6103fccc55d34c67cc0ad04653aad4ea2a07cd7bbb" +checksum = "e98ff6b0dbbe4d5a37318f433d4fc82babd21631f194d370409ceb2e40b2f0b5" dependencies = [ "base64", "bytes", @@ -743,12 +757,12 @@ dependencies = [ "sync_wrapper", "tokio", "tower", + "tower-http", "tower-service", "url", "wasm-bindgen", "wasm-bindgen-futures", "web-sys", - "windows-registry", ] [[package]] @@ -759,9 +773,9 @@ checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] name = "rustversion" -version = "1.0.20" +version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2" +checksum = "8a0d197bd2c9dc6e53b84da9556a69ba4cdfab8619eb41a8bd1cc2027a0f6b1d" [[package]] name = "ryu" @@ -874,9 +888,9 @@ checksum = "8917285742e9f3e1683f0a9c4e6b57960b7314d0b08d30d1ecd426713ee2eee9" [[package]] name = "socket2" -version = "0.5.9" +version = "0.5.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f5fd57c80058a56cf5c777ab8a126398ece8e442983605d280a44ce79d0edef" +checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678" dependencies = [ "libc", "windows-sys 0.52.0", @@ -941,14 +955,14 @@ dependencies = [ [[package]] name = "tokio" -version = "1.45.0" +version = "1.45.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2513ca694ef9ede0fb23fe71a4ee4107cb102b9dc1930f6d0fd77aae068ae165" +checksum = "75ef51a33ef1da925cea3e4eb122833cb377c61439ca401b770f54902b806779" dependencies = [ "backtrace", "bytes", "libc", - "mio 1.0.3", + "mio 1.0.4", "parking_lot", "pin-project-lite", "signal-hook-registry", @@ -983,6 +997,24 @@ dependencies = [ "tower-service", ] +[[package]] +name = "tower-http" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fdb0c213ca27a9f57ab69ddb290fd80d970922355b83ae380b395d3986b8a2e" +dependencies = [ + "bitflags 2.9.1", + "bytes", + "futures-util", + "http", + "http-body", + "iri-string", + "pin-project-lite", + "tower", + "tower-layer", + "tower-service", +] + [[package]] name = "tower-layer" version = "0.3.3" @@ -1181,15 +1213,15 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-core" -version = "0.61.0" +version = "0.61.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4763c1de310c86d75a878046489e2e5ba02c649d185f21c67d4cf8a56d098980" +checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3" dependencies = [ "windows-implement", "windows-interface", "windows-link", "windows-result", - "windows-strings 0.4.0", + "windows-strings", ] [[package]] @@ -1220,40 +1252,20 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76840935b766e1b0a05c0066835fb9ec80071d4c09a16f6bd5f7e655e3c14c38" -[[package]] -name = "windows-registry" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4286ad90ddb45071efd1a66dfa43eb02dd0dfbae1545ad6cc3c51cf34d7e8ba3" -dependencies = [ - "windows-result", - "windows-strings 0.3.1", - "windows-targets 0.53.0", -] - [[package]] name = "windows-result" -version = "0.3.2" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c64fd11a4fd95df68efcfee5f44a294fe71b8bc6a91993e2791938abcc712252" +checksum = "56f42bd332cc6c8eac5af113fc0c1fd6a8fd2aa08a0119358686e5160d0586c6" dependencies = [ "windows-link", ] [[package]] name = "windows-strings" -version = "0.3.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87fa48cc5d406560701792be122a10132491cff9d0aeb23583cc2dcafc847319" -dependencies = [ - "windows-link", -] - -[[package]] -name = "windows-strings" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a2ba9642430ee452d5a7aa78d72907ebe8cfda358e8cb7918a2050581322f97" +checksum = "56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57" dependencies = [ "windows-link", ] @@ -1276,6 +1288,15 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-targets" version = "0.48.5" @@ -1300,29 +1321,13 @@ dependencies = [ "windows_aarch64_gnullvm 0.52.6", "windows_aarch64_msvc 0.52.6", "windows_i686_gnu 0.52.6", - "windows_i686_gnullvm 0.52.6", + "windows_i686_gnullvm", "windows_i686_msvc 0.52.6", "windows_x86_64_gnu 0.52.6", "windows_x86_64_gnullvm 0.52.6", "windows_x86_64_msvc 0.52.6", ] -[[package]] -name = "windows-targets" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1e4c7e8ceaaf9cb7d7507c974735728ab453b67ef8f18febdd7c11fe59dca8b" -dependencies = [ - "windows_aarch64_gnullvm 0.53.0", - "windows_aarch64_msvc 0.53.0", - "windows_i686_gnu 0.53.0", - "windows_i686_gnullvm 0.53.0", - "windows_i686_msvc 0.53.0", - "windows_x86_64_gnu 0.53.0", - "windows_x86_64_gnullvm 0.53.0", - "windows_x86_64_msvc 0.53.0", -] - [[package]] name = "windows_aarch64_gnullvm" version = "0.48.5" @@ -1335,12 +1340,6 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86b8d5f90ddd19cb4a147a5fa63ca848db3df085e25fee3cc10b39b6eebae764" - [[package]] name = "windows_aarch64_msvc" version = "0.48.5" @@ -1353,12 +1352,6 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" -[[package]] -name = "windows_aarch64_msvc" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7651a1f62a11b8cbd5e0d42526e55f2c99886c77e007179efff86c2b137e66c" - [[package]] name = "windows_i686_gnu" version = "0.48.5" @@ -1371,24 +1364,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" -[[package]] -name = "windows_i686_gnu" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1dc67659d35f387f5f6c479dc4e28f1d4bb90ddd1a5d3da2e5d97b42d6272c3" - [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" -[[package]] -name = "windows_i686_gnullvm" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ce6ccbdedbf6d6354471319e781c0dfef054c81fbc7cf83f338a4296c0cae11" - [[package]] name = "windows_i686_msvc" version = "0.48.5" @@ -1401,12 +1382,6 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" -[[package]] -name = "windows_i686_msvc" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "581fee95406bb13382d2f65cd4a908ca7b1e4c2f1917f143ba16efe98a589b5d" - [[package]] name = "windows_x86_64_gnu" version = "0.48.5" @@ -1419,12 +1394,6 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" -[[package]] -name = "windows_x86_64_gnu" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e55b5ac9ea33f2fc1716d1742db15574fd6fc8dadc51caab1c16a3d3b4190ba" - [[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" @@ -1437,12 +1406,6 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a6e035dd0599267ce1ee132e51c27dd29437f63325753051e71dd9e42406c57" - [[package]] name = "windows_x86_64_msvc" version = "0.48.5" @@ -1455,12 +1418,6 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" -[[package]] -name = "windows_x86_64_msvc" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486" - [[package]] name = "writeable" version = "0.6.1" diff --git a/gamenight-database/.gitignore b/gamenight-database/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/gamenight-database/.gitignore @@ -0,0 +1 @@ +target diff --git a/gamenight-database/Cargo.toml b/gamenight-database/Cargo.toml new file mode 100644 index 0000000..7e17204 --- /dev/null +++ b/gamenight-database/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "gamenight-database" +version = "0.1.0" +edition = "2024" + +[dependencies] +diesel = { version = "2.0", features = ["postgres", "r2d2", "uuid", "chrono"] } +diesel-derive-enum = { version = "2.0", features = ["postgres"] } +diesel_migrations = "2.0" +argon2 = { version = "0.5", features = ["std"] } +uuid = { version = "1.17.0", features = ["serde", "v4"] } +serde = { version = "1.0", features = ["derive"] } +chrono = { version = "0.4", features = ["serde"] } +rand_core = "0.9" + diff --git a/backend-actix/diesel.toml b/gamenight-database/diesel.toml similarity index 100% rename from backend-actix/diesel.toml rename to gamenight-database/diesel.toml diff --git a/backend-actix/migrations/2022-03-19-191822_initial/down.sql b/gamenight-database/migrations/2022-03-19-191822_initial/down.sql similarity index 100% rename from backend-actix/migrations/2022-03-19-191822_initial/down.sql rename to gamenight-database/migrations/2022-03-19-191822_initial/down.sql diff --git a/backend-actix/migrations/2022-03-19-191822_initial/up.sql b/gamenight-database/migrations/2022-03-19-191822_initial/up.sql similarity index 100% rename from backend-actix/migrations/2022-03-19-191822_initial/up.sql rename to gamenight-database/migrations/2022-03-19-191822_initial/up.sql diff --git a/backend-actix/migrations/2022-04-17-175115_user-system/down.sql b/gamenight-database/migrations/2022-04-17-175115_user-system/down.sql similarity index 100% rename from backend-actix/migrations/2022-04-17-175115_user-system/down.sql rename to gamenight-database/migrations/2022-04-17-175115_user-system/down.sql diff --git a/backend-actix/migrations/2022-04-17-175115_user-system/up.sql b/gamenight-database/migrations/2022-04-17-175115_user-system/up.sql similarity index 100% rename from backend-actix/migrations/2022-04-17-175115_user-system/up.sql rename to gamenight-database/migrations/2022-04-17-175115_user-system/up.sql diff --git a/backend-actix/migrations/2022-05-14-104118_gamenight_owners/down.sql b/gamenight-database/migrations/2022-05-14-104118_gamenight_owners/down.sql similarity index 100% rename from backend-actix/migrations/2022-05-14-104118_gamenight_owners/down.sql rename to gamenight-database/migrations/2022-05-14-104118_gamenight_owners/down.sql diff --git a/backend-actix/migrations/2022-05-14-104118_gamenight_owners/up.sql b/gamenight-database/migrations/2022-05-14-104118_gamenight_owners/up.sql similarity index 100% rename from backend-actix/migrations/2022-05-14-104118_gamenight_owners/up.sql rename to gamenight-database/migrations/2022-05-14-104118_gamenight_owners/up.sql diff --git a/backend-actix/migrations/2022-05-27-183310_gamenight_gamelist/down.sql b/gamenight-database/migrations/2022-05-27-183310_gamenight_gamelist/down.sql similarity index 100% rename from backend-actix/migrations/2022-05-27-183310_gamenight_gamelist/down.sql rename to gamenight-database/migrations/2022-05-27-183310_gamenight_gamelist/down.sql diff --git a/backend-actix/migrations/2022-05-27-183310_gamenight_gamelist/up.sql b/gamenight-database/migrations/2022-05-27-183310_gamenight_gamelist/up.sql similarity index 100% rename from backend-actix/migrations/2022-05-27-183310_gamenight_gamelist/up.sql rename to gamenight-database/migrations/2022-05-27-183310_gamenight_gamelist/up.sql diff --git a/backend-actix/migrations/2022-05-28-142526_gamenight participants/down.sql b/gamenight-database/migrations/2022-05-28-142526_gamenight participants/down.sql similarity index 58% rename from backend-actix/migrations/2022-05-28-142526_gamenight participants/down.sql rename to gamenight-database/migrations/2022-05-28-142526_gamenight participants/down.sql index e32c462..10d8fb1 100644 --- a/backend-actix/migrations/2022-05-28-142526_gamenight participants/down.sql +++ b/gamenight-database/migrations/2022-05-28-142526_gamenight participants/down.sql @@ -1,3 +1,3 @@ -- This file should undo anything in `up.sql` -drop table gamenight_participants; \ No newline at end of file +drop table gamenight_participant; \ No newline at end of file diff --git a/backend-actix/migrations/2022-05-28-142526_gamenight participants/up.sql b/gamenight-database/migrations/2022-05-28-142526_gamenight participants/up.sql similarity index 89% rename from backend-actix/migrations/2022-05-28-142526_gamenight participants/up.sql rename to gamenight-database/migrations/2022-05-28-142526_gamenight participants/up.sql index 1115fc9..51fd33a 100644 --- a/backend-actix/migrations/2022-05-28-142526_gamenight participants/up.sql +++ b/gamenight-database/migrations/2022-05-28-142526_gamenight participants/up.sql @@ -1,6 +1,6 @@ -- Your SQL goes here -create table gamenight_participants ( +create table gamenight_participant ( gamenight_id UUID NOT NULL, user_id UUID NOT NULL, CONSTRAINT FK_gamenight_id FOREIGN KEY (gamenight_id) REFERENCES gamenight(id) ON DELETE CASCADE, diff --git a/backend-actix/migrations/2022-06-04-141858_registration_tokens/down.sql b/gamenight-database/migrations/2022-06-04-141858_registration_tokens/down.sql similarity index 100% rename from backend-actix/migrations/2022-06-04-141858_registration_tokens/down.sql rename to gamenight-database/migrations/2022-06-04-141858_registration_tokens/down.sql diff --git a/backend-actix/migrations/2022-06-04-141858_registration_tokens/up.sql b/gamenight-database/migrations/2022-06-04-141858_registration_tokens/up.sql similarity index 100% rename from backend-actix/migrations/2022-06-04-141858_registration_tokens/up.sql rename to gamenight-database/migrations/2022-06-04-141858_registration_tokens/up.sql diff --git a/backend-actix/src/schema/error.rs b/gamenight-database/src/error.rs similarity index 100% rename from backend-actix/src/schema/error.rs rename to gamenight-database/src/error.rs diff --git a/backend-actix/src/schema/gamenight.rs b/gamenight-database/src/gamenight.rs similarity index 85% rename from backend-actix/src/schema/gamenight.rs rename to gamenight-database/src/gamenight.rs index 4bf7685..49442b9 100644 --- a/backend-actix/src/schema/gamenight.rs +++ b/gamenight-database/src/gamenight.rs @@ -2,7 +2,7 @@ use chrono::{DateTime, Utc}; use diesel::{Insertable, Queryable, PgConnection, RunQueryDsl, insert_into, QueryDsl}; use serde::{Serialize, Deserialize}; use uuid::Uuid; -use crate::schema::schema::gamenight; +use crate::schema::gamenight; use super::error::DatabaseError; @@ -23,6 +23,6 @@ pub fn add_gamenight(conn: &mut PgConnection, gamenight: Gamenight) -> Result Result { +pub fn get_gamenight(conn: &mut PgConnection, id: Uuid) -> Result { Ok(gamenight::table.find(id).first(conn)?) } \ No newline at end of file diff --git a/backend-actix/src/schema/gamenight_participants.rs b/gamenight-database/src/gamenight_participants.rs similarity index 73% rename from backend-actix/src/schema/gamenight_participants.rs rename to gamenight-database/src/gamenight_participants.rs index c866584..d88b083 100644 --- a/backend-actix/src/schema/gamenight_participants.rs +++ b/gamenight-database/src/gamenight_participants.rs @@ -1,21 +1,22 @@ use diesel::{ExpressionMethods, Insertable, PgConnection, QueryDsl, Queryable, RunQueryDsl}; use serde::{Serialize, Deserialize}; use uuid::Uuid; -use crate::schema::schema::gamenight_participants; + +use crate::schema::gamenight_participant; use super::error::DatabaseError; #[derive(Serialize, Deserialize, Debug, Insertable, Queryable)] #[diesel(belongs_to(Gamenight))] #[diesel(belongs_to(User))] -#[diesel(table_name = gamenight_participants)] +#[diesel(table_name = gamenight_participant)] pub struct GamenightParticipants { pub gamenight_id: Uuid, pub user_id: Uuid, } pub fn gamenight_participants(conn: &mut PgConnection, id: Uuid) -> Result, DatabaseError> { - Ok(gamenight_participants::table - .filter(gamenight_participants::gamenight_id.eq(id)) + Ok(gamenight_participant::table + .filter(gamenight_participant::gamenight_id.eq(id)) .get_results(conn)?) } \ No newline at end of file diff --git a/gamenight-database/src/lib.rs b/gamenight-database/src/lib.rs new file mode 100644 index 0000000..efcc8cd --- /dev/null +++ b/gamenight-database/src/lib.rs @@ -0,0 +1,46 @@ +pub mod user; +pub mod error; +pub mod schema; +pub mod gamenight; +pub mod gamenight_participants; + +use diesel::r2d2::ConnectionManager; +use diesel::r2d2::ManageConnection; +use diesel::r2d2::Pool; +use diesel::r2d2::PooledConnection; +use diesel::PgConnection; +use diesel_migrations::embed_migrations; +use diesel_migrations::EmbeddedMigrations; + +use diesel_migrations::MigrationHarness; +pub use user::login; +pub use user::register; +pub use gamenight::gamenights; +pub use gamenight_participants::gamenight_participants; + +pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!(); +pub type DbConnection = PgConnection; +pub type DbPool = Pool>; + +pub fn run_migration(conn: &mut DbConnection) { + conn.run_pending_migrations(MIGRATIONS).unwrap(); +} + +pub fn get_connection_pool(url: &str) -> DbPool { + let manager = ConnectionManager::::new(url); + // Refer to the `r2d2` documentation for more methods to use + // when building a connection pool + Pool::builder() + .test_on_check_out(true) + .build(manager) + .expect("Could not build connection pool") +} +pub trait GetConnection where T: ManageConnection { + fn get_conn(&self) -> PooledConnection; +} + +impl GetConnection for Pool { + fn get_conn(&self) -> PooledConnection { + self.get().expect("Couldn't get db connection from pool") + } +} \ No newline at end of file diff --git a/backend-actix/src/schema/schema.rs b/gamenight-database/src/schema.rs similarity index 87% rename from backend-actix/src/schema/schema.rs rename to gamenight-database/src/schema.rs index 8ee25f4..cd1f2af 100644 --- a/backend-actix/src/schema/schema.rs +++ b/gamenight-database/src/schema.rs @@ -23,7 +23,7 @@ diesel::table! { } diesel::table! { - gamenight_participants (gamenight_id, user_id) { + gamenight_participant (gamenight_id, user_id) { gamenight_id -> Uuid, user_id -> Uuid, } @@ -68,14 +68,14 @@ diesel::table! { diesel::joinable!(gamenight -> users (owner_id)); diesel::joinable!(gamenight_gamelist -> gamenight (gamenight_id)); diesel::joinable!(gamenight_gamelist -> known_games (game_id)); -diesel::joinable!(gamenight_participants -> gamenight (gamenight_id)); -diesel::joinable!(gamenight_participants -> users (user_id)); +diesel::joinable!(gamenight_participant -> gamenight (gamenight_id)); +diesel::joinable!(gamenight_participant -> users (user_id)); diesel::joinable!(pwd -> users (user_id)); diesel::allow_tables_to_appear_in_same_query!( gamenight, gamenight_gamelist, - gamenight_participants, + gamenight_participant, known_games, pwd, registration_tokens, diff --git a/backend-actix/src/schema/user.rs b/gamenight-database/src/user.rs similarity index 68% rename from backend-actix/src/schema/user.rs rename to gamenight-database/src/user.rs index 7b7aa22..3e0f65e 100644 --- a/backend-actix/src/schema/user.rs +++ b/gamenight-database/src/user.rs @@ -2,19 +2,21 @@ use argon2::password_hash::Salt; use diesel::Connection; use serde::{Serialize, Deserialize}; use uuid::Uuid; -use diesel::{PgConnection, ExpressionMethods, QueryDsl, RunQueryDsl, Insertable, Queryable}; +use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl, Insertable, Queryable}; use diesel_derive_enum::DbEnum; use argon2::password_hash::SaltString; use argon2::PasswordHash; use argon2::PasswordVerifier; use argon2::Argon2; use argon2::password_hash::PasswordHasher; -use validator::ValidationError; -use crate::util::GetConnection; +use argon2::password_hash::rand_core::OsRng; +use argon2::password_hash::rand_core::RngCore; +use crate::DbConnection; + use super::schema::{pwd, users}; pub use super::error::DatabaseError; -use ::rand_core::{OsRng,TryRngCore}; -use crate::request::requests::RegisterContext; + + #[derive(Serialize, Deserialize, Debug, Insertable, Queryable)] #[diesel(table_name = pwd)] @@ -33,7 +35,7 @@ pub struct User { } #[derive(DbEnum, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] -#[ExistingTypePath = "crate::schema::schema::sql_types::Role"] +#[ExistingTypePath = "crate::schema::sql_types::Role"] pub enum Role { Admin, User @@ -62,7 +64,7 @@ pub struct Register { pub password: String } -pub fn login(conn: &mut PgConnection, user: LoginUser) -> Result, DatabaseError> { +pub fn login(conn: &mut DbConnection, user: LoginUser) -> Result, DatabaseError> { let id: Uuid = users::table .filter(users::username.eq(&user.username)) .or_filter(users::email.eq(&user.username)) @@ -86,41 +88,11 @@ pub fn login(conn: &mut PgConnection, user: LoginUser) -> Result, D } } -pub fn get_user(conn: &mut PgConnection, id: Uuid) -> Result { +pub fn get_user(conn: &mut DbConnection, id: Uuid) -> Result { Ok(users::table.find(id).first(conn)?) } -pub fn unique_username(username: &String, context: &RegisterContext) -> Result<(), ValidationError> { - let mut conn = context.pool.get().expect("Couldn't get db connection from pool"); - - match users::table - .count() - .filter(users::username.eq(username)) - .get_result(&mut conn) - { - Ok(0) => Ok(()), - Ok(_) => Err(ValidationError::new("User already exists")), - Err(_) => Err(ValidationError::new("Database error while validating user")), - } -} - -pub fn unique_email(email: &String, context: &RegisterContext) -> Result<(), ValidationError> { - let mut conn = context.pool.get_conn(); - - match users::table - .count() - .filter(users::email.eq(email)) - .get_result(&mut conn) - { - Ok(0) => Ok(()), - Ok(_) => Err(ValidationError::new("email already exists")), - Err(_) => Err(ValidationError::new( - "Database error while validating email", - )), - } -} - -pub fn register(conn: &mut PgConnection, register: Register) -> Result<(), DatabaseError> { +pub fn register(conn: &mut DbConnection, register: Register) -> Result<(), DatabaseError> { let mut bytes = [0u8; Salt::RECOMMENDED_LENGTH]; OsRng.try_fill_bytes(&mut bytes).unwrap(); let salt = SaltString::encode_b64(&bytes).unwrap(); @@ -151,4 +123,18 @@ pub fn register(conn: &mut PgConnection, register: Register) -> Result<(), Datab Ok(()) }) +} + +pub fn count_users_with_username(conn: &mut DbConnection, username: &String) -> Result { + Ok(users::table + .count() + .filter(users::username.eq(username)) + .get_result::(conn)?) +} + +pub fn count_users_with_email(conn: &mut DbConnection, email: &String) -> Result { + Ok(users::table + .count() + .filter(users::email.eq(email)) + .get_result::(conn)?) } \ No newline at end of file