Бэкенд: Что-то с чем-то. Оно реально работает
This commit is contained in:
parent
202f9711ef
commit
f275ca7e27
254
backend/Cargo.lock
generated
254
backend/Cargo.lock
generated
@ -245,6 +245,12 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "arrayvec"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6"
|
||||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.68"
|
||||
@ -273,6 +279,8 @@ dependencies = [
|
||||
"dotenv_codegen",
|
||||
"nanoid",
|
||||
"pbkdf2",
|
||||
"rust_decimal",
|
||||
"rust_decimal_macros",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
@ -292,6 +300,18 @@ version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "bitvec"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"
|
||||
dependencies = [
|
||||
"funty",
|
||||
"radium",
|
||||
"tap",
|
||||
"wyz",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "block-buffer"
|
||||
version = "0.10.4"
|
||||
@ -301,6 +321,51 @@ dependencies = [
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "borsh"
|
||||
version = "0.10.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4114279215a005bc675e386011e594e1d9b800918cea18fcadadcce864a2046b"
|
||||
dependencies = [
|
||||
"borsh-derive",
|
||||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "borsh-derive"
|
||||
version = "0.10.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0754613691538d51f329cce9af41d7b7ca150bc973056f1156611489475f54f7"
|
||||
dependencies = [
|
||||
"borsh-derive-internal",
|
||||
"borsh-schema-derive-internal",
|
||||
"proc-macro-crate",
|
||||
"proc-macro2",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "borsh-derive-internal"
|
||||
version = "0.10.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "afb438156919598d2c7bad7e1c0adf3d26ed3840dbc010db1a882a65583ca2fb"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "borsh-schema-derive-internal"
|
||||
version = "0.10.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "634205cc43f74a1b9046ef87c4540ebda95696ec0f315024860cad7c5b0f5ccd"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "brotli"
|
||||
version = "3.3.4"
|
||||
@ -328,6 +393,28 @@ version = "3.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535"
|
||||
|
||||
[[package]]
|
||||
name = "bytecheck"
|
||||
version = "0.6.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b6372023ac861f6e6dc89c8344a8f398fb42aaba2b5dbc649ca0c0e9dbcb627"
|
||||
dependencies = [
|
||||
"bytecheck_derive",
|
||||
"ptr_meta",
|
||||
"simdutf8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bytecheck_derive"
|
||||
version = "0.6.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7ec4c6f261935ad534c0c22dbef2201b45918860eb1c574b972bd213a76af61"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.4.3"
|
||||
@ -578,6 +665,12 @@ dependencies = [
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "funty"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
|
||||
|
||||
[[package]]
|
||||
name = "futures-channel"
|
||||
version = "0.3.28"
|
||||
@ -677,6 +770,9 @@ name = "hashbrown"
|
||||
version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
|
||||
dependencies = [
|
||||
"ahash 0.7.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
@ -1012,6 +1108,20 @@ version = "0.3.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160"
|
||||
|
||||
[[package]]
|
||||
name = "postgres"
|
||||
version = "0.19.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0bed5017bc2ff49649c0075d0d7a9d676933c1292480c1d137776fb205b5cd18"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"fallible-iterator",
|
||||
"futures-util",
|
||||
"log",
|
||||
"tokio",
|
||||
"tokio-postgres",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "postgres-protocol"
|
||||
version = "0.6.5"
|
||||
@ -1048,6 +1158,15 @@ version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-crate"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785"
|
||||
dependencies = [
|
||||
"toml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-hack"
|
||||
version = "0.5.20+deprecated"
|
||||
@ -1063,6 +1182,26 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ptr_meta"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1"
|
||||
dependencies = [
|
||||
"ptr_meta_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ptr_meta_derive"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "16b845dbfca988fa33db069c0e230574d15a3088f147a87b64c7589eb662c9ac"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.26"
|
||||
@ -1072,6 +1211,12 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "radium"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.5"
|
||||
@ -1128,6 +1273,73 @@ version = "0.6.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
|
||||
|
||||
[[package]]
|
||||
name = "rend"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "581008d2099240d37fb08d77ad713bcaec2c4d89d50b5b21a8bb1996bbab68ab"
|
||||
dependencies = [
|
||||
"bytecheck",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rkyv"
|
||||
version = "0.7.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0200c8230b013893c0b2d6213d6ec64ed2b9be2e0e016682b7224ff82cff5c58"
|
||||
dependencies = [
|
||||
"bitvec",
|
||||
"bytecheck",
|
||||
"hashbrown",
|
||||
"ptr_meta",
|
||||
"rend",
|
||||
"rkyv_derive",
|
||||
"seahash",
|
||||
"tinyvec",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rkyv_derive"
|
||||
version = "0.7.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2e06b915b5c230a17d7a736d1e2e63ee753c256a8614ef3f5147b13a4f5541d"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust_decimal"
|
||||
version = "1.29.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26bd36b60561ee1fb5ec2817f198b6fd09fa571c897a5e86d1487cfc2b096dfc"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"borsh",
|
||||
"bytecheck",
|
||||
"byteorder",
|
||||
"bytes",
|
||||
"num-traits",
|
||||
"postgres",
|
||||
"rand",
|
||||
"rkyv",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tokio-postgres",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust_decimal_macros"
|
||||
version = "1.29.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0e773fd3da1ed42472fdf3cfdb4972948a555bc3d73f5e0bdb99d17e7b54c687"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"rust_decimal",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc_version"
|
||||
version = "0.4.0"
|
||||
@ -1155,6 +1367,12 @@ version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1792db035ce95be60c3f8853017b3999209281c24e2ba5bc8e59bf97a0c590c1"
|
||||
|
||||
[[package]]
|
||||
name = "seahash"
|
||||
version = "4.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b"
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "1.0.17"
|
||||
@ -1235,6 +1453,12 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "simdutf8"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a"
|
||||
|
||||
[[package]]
|
||||
name = "siphasher"
|
||||
version = "0.3.10"
|
||||
@ -1314,6 +1538,12 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tap"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
|
||||
|
||||
[[package]]
|
||||
name = "termcolor"
|
||||
version = "1.2.0"
|
||||
@ -1444,6 +1674,15 @@ dependencies = [
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.5.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing"
|
||||
version = "0.1.37"
|
||||
@ -1509,6 +1748,12 @@ dependencies = [
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4dad5567ad0cf5b760e5665964bec1b47dfd077ba8a2544b513f3556d3d239a2"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.4"
|
||||
@ -1687,6 +1932,15 @@ version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
|
||||
|
||||
[[package]]
|
||||
name = "wyz"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed"
|
||||
dependencies = [
|
||||
"tap",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zstd"
|
||||
version = "0.12.3+zstd.1.5.2"
|
||||
|
@ -18,3 +18,5 @@ dotenv_codegen = "0.15.0"
|
||||
pbkdf2 = "0.12.1"
|
||||
nanoid = "0.4.0"
|
||||
sha2 = "0.10.6"
|
||||
rust_decimal = { version = "1.29", features = ["db-tokio-postgres"] }
|
||||
rust_decimal_macros = "1.29"
|
@ -1,6 +1,7 @@
|
||||
use std::sync::Mutex;
|
||||
use actix_web::{web, post, Responder, HttpResponse};
|
||||
use crate::State;
|
||||
use serde_json::json;
|
||||
use crate::{State, models::administrator};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct LoginInfo {
|
||||
@ -12,7 +13,7 @@ pub struct LoginInfo {
|
||||
pub async fn login(state: web::Data<Mutex<State>>, json: web::Json<LoginInfo>) -> impl Responder {
|
||||
match state.lock() {
|
||||
Ok(guard) => match guard.auth_service.login(guard.administrator_repository.as_ref(), &json.username, &json.password).await {
|
||||
Ok(token) => HttpResponse::Ok().json(token),
|
||||
Ok((token, administrator_id, car_station_id)) => HttpResponse::Ok().json(json!({"token": token, "administrator_id": administrator_id, "car_station_id": car_station_id}) ),
|
||||
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
|
||||
},
|
||||
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
|
||||
|
14
backend/src/endpoints/benchmark.rs
Normal file
14
backend/src/endpoints/benchmark.rs
Normal file
@ -0,0 +1,14 @@
|
||||
use std::sync::Mutex;
|
||||
use actix_web::{web, get, post, patch, delete, Responder, HttpResponse, HttpRequest};
|
||||
use crate::State;
|
||||
|
||||
#[get("/benchmark")]
|
||||
async fn benchmark(state: web::Data<Mutex<State>>) -> impl Responder {
|
||||
match state.lock() {
|
||||
Ok(guard) => match guard.benchmark_repository.benchmark().await {
|
||||
Ok(result) => HttpResponse::Ok().json(result),
|
||||
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
|
||||
},
|
||||
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
|
||||
}
|
||||
}
|
@ -117,3 +117,26 @@ pub async fn delete_car(state: web::Data<Mutex<State>>, path: web::Path<(i32, )>
|
||||
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/report")]
|
||||
pub async fn get_report(state: web::Data<Mutex<State>>, request: HttpRequest) -> impl Responder {
|
||||
let token = request.headers().get("Authorization");
|
||||
if token.is_none() {
|
||||
return HttpResponse::Unauthorized().body("There is no token");
|
||||
}
|
||||
let token = token.unwrap();
|
||||
|
||||
if let Ok(guard) = state.lock() {
|
||||
if guard.auth_service.administrator_by_token(guard.administrator_repository.as_ref(), token.to_str().unwrap()).await.is_err() {
|
||||
return HttpResponse::Unauthorized().body("Invalid token");
|
||||
}
|
||||
}
|
||||
|
||||
match state.lock() {
|
||||
Ok(guard) => match guard.car_repository.get_report().await {
|
||||
Ok(result) => HttpResponse::Ok().json(result),
|
||||
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
|
||||
},
|
||||
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
|
||||
}
|
||||
}
|
@ -4,3 +4,4 @@ pub mod car;
|
||||
pub mod rent;
|
||||
pub mod owner;
|
||||
pub mod auth;
|
||||
pub mod benchmark;
|
@ -117,3 +117,26 @@ pub async fn delete_rent(state: web::Data<Mutex<State>>, path: web::Path<(i32, )
|
||||
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/{id}/end")]
|
||||
pub async fn end_rent(state: web::Data<Mutex<State>>, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder {
|
||||
let token = request.headers().get("Authorization");
|
||||
if token.is_none() {
|
||||
return HttpResponse::Unauthorized().body("There is no token");
|
||||
}
|
||||
let token = token.unwrap();
|
||||
|
||||
if let Ok(guard) = state.lock() {
|
||||
if guard.auth_service.administrator_by_token(guard.administrator_repository.as_ref(), token.to_str().unwrap()).await.is_err() {
|
||||
return HttpResponse::Unauthorized().body("Invalid token");
|
||||
}
|
||||
}
|
||||
|
||||
match state.lock() {
|
||||
Ok(guard) => match guard.rent_repository.end_rent(path.into_inner().0).await {
|
||||
Ok(result) => HttpResponse::Ok().json(result),
|
||||
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
|
||||
},
|
||||
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
use services::auth::AuthService;
|
||||
use storages::traits::{OwnerRepository, AdministratorRepository};
|
||||
use storages::traits::{OwnerRepository, AdministratorRepository, BenchmarkRepository};
|
||||
|
||||
use crate::storages::traits::{CarRepository, CarStationRepository, ClientRepository, RentRepository};
|
||||
|
||||
@ -15,5 +15,6 @@ pub struct State {
|
||||
pub owner_repository: Box<dyn OwnerRepository + Send>,
|
||||
pub rent_repository: Box<dyn RentRepository + Send>,
|
||||
pub administrator_repository: Box<dyn AdministratorRepository + Send>,
|
||||
pub benchmark_repository: Box<dyn BenchmarkRepository + Send>,
|
||||
pub auth_service: AuthService
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
use actix_web::{App, HttpServer, web};
|
||||
use actix_web::web::Data;
|
||||
use backend::endpoints::auth::*;
|
||||
use backend::endpoints::benchmark::benchmark;
|
||||
use backend::endpoints::{auth::*};
|
||||
use backend::storages::postgres::administrator::PostgresAdministratorRepository;
|
||||
use dotenv_codegen::dotenv;
|
||||
use backend::{State, services};
|
||||
@ -11,6 +12,7 @@ use backend::storages::postgres::car_station::PostgresCarStationRepository;
|
||||
use backend::storages::postgres::client::PostgresClientRepository;
|
||||
use backend::storages::postgres::rent::PostgresRentRepository;
|
||||
use backend::storages::postgres::owner::PostgresOwnerRepository;
|
||||
use backend::storages::postgres::benchmark::PostgresBenchmarkRepository;
|
||||
use backend::endpoints::car_station::*;
|
||||
use backend::endpoints::car::*;
|
||||
use backend::endpoints::client::*;
|
||||
@ -44,6 +46,8 @@ async fn main() -> std::io::Result<()> {
|
||||
|
||||
let administrator_repository = PostgresAdministratorRepository { connection: Arc::clone(&client) };
|
||||
|
||||
let benchmark_repository = PostgresBenchmarkRepository { connection: Arc::clone(&client) };
|
||||
|
||||
let auth_service = services::auth::AuthService::new();
|
||||
|
||||
let state = Data::new(Mutex::new(State {
|
||||
@ -53,6 +57,7 @@ async fn main() -> std::io::Result<()> {
|
||||
owner_repository: Box::new(owner_repository),
|
||||
rent_repository: Box::new(rent_repository),
|
||||
administrator_repository: Box::new(administrator_repository),
|
||||
benchmark_repository: Box::new(benchmark_repository),
|
||||
auth_service
|
||||
}));
|
||||
|
||||
@ -63,6 +68,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(
|
||||
web::scope("/cars")
|
||||
.service(get_cars)
|
||||
.service(get_report)
|
||||
.service(get_car)
|
||||
.service(create_car)
|
||||
.service(update_car)
|
||||
@ -99,10 +105,11 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(create_rent)
|
||||
.service(update_rent)
|
||||
.service(delete_rent)
|
||||
.service(end_rent)
|
||||
)
|
||||
.service(login)
|
||||
.service(logout)
|
||||
)
|
||||
).service(benchmark)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
|
@ -1,3 +1,4 @@
|
||||
use rust_decimal::Decimal;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@ -5,7 +6,7 @@ pub struct Car {
|
||||
pub id: i32,
|
||||
pub brand: String,
|
||||
pub model: String,
|
||||
pub price: f64,
|
||||
pub price: Decimal,
|
||||
pub owner_id: i32,
|
||||
pub car_station_id: i32
|
||||
}
|
||||
@ -14,7 +15,16 @@ pub struct Car {
|
||||
pub struct BindingCar {
|
||||
pub brand: String,
|
||||
pub model: String,
|
||||
pub price: f64,
|
||||
pub owner_id: u32,
|
||||
pub car_station_id: u32
|
||||
pub price: Decimal,
|
||||
pub owner_id: i32,
|
||||
pub car_station_id: i32
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Report {
|
||||
pub car_id: i32,
|
||||
pub brand: String,
|
||||
pub model: String,
|
||||
pub times: i64,
|
||||
pub income: Decimal
|
||||
}
|
@ -3,7 +3,7 @@ use serde::{Serialize, Deserialize};
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Rent {
|
||||
pub id: i32,
|
||||
pub start_time: chrono::NaiveTime,
|
||||
pub start_time: chrono::NaiveDateTime,
|
||||
pub time_amount: Option<i32>,
|
||||
pub client_id: i32,
|
||||
pub car_id: i32
|
||||
@ -11,7 +11,7 @@ pub struct Rent {
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct BindingRent {
|
||||
pub time_amount: Option<u32>,
|
||||
pub client_id: u32,
|
||||
pub car_id: u32
|
||||
pub time_amount: Option<i32>,
|
||||
pub client_id: i32,
|
||||
pub car_id: i32
|
||||
}
|
@ -12,14 +12,14 @@ impl AuthService {
|
||||
AuthService { authed: RefCell::new(Vec::new()) }
|
||||
}
|
||||
|
||||
pub async fn login(&self, storage: &dyn AdministratorRepository, username: &str, password: &str) -> Result<String, String> {
|
||||
pub async fn login(&self, storage: &dyn AdministratorRepository, username: &str, password: &str) -> Result<(String, i32, i32), String> {
|
||||
let administrator = storage.find_by_username(&username).await?;
|
||||
if pbkdf2::pbkdf2_hmac_array::<Sha256, 32>(password.as_bytes(), dotenv_codegen::dotenv!("SALT").as_bytes(), 4096).iter().map(|x| format!("{:x}", x)).collect::<String>() != administrator.password {
|
||||
Err("Invalid password or login!".to_owned())
|
||||
} else {
|
||||
let token = nanoid!(32);
|
||||
self.authed.borrow_mut().push((token.clone(), administrator.id));
|
||||
Ok(token)
|
||||
Ok((token, administrator.id, administrator.car_station_id))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,7 @@ impl AdministratorRepository for PostgresAdministratorRepository {
|
||||
car_station_id: row.get("car_station_id")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during reading of Administrator".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,7 +46,7 @@ impl AdministratorRepository for PostgresAdministratorRepository {
|
||||
car_station_id: row.get("car_station_id")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during reading of Administrator".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
}
|
49
backend/src/storages/postgres/benchmark.rs
Normal file
49
backend/src/storages/postgres/benchmark.rs
Normal file
@ -0,0 +1,49 @@
|
||||
use tokio_postgres::Client as PgClient;
|
||||
use std::sync::Arc;
|
||||
use crate::storages::traits::BenchmarkRepository;
|
||||
use async_trait::async_trait;
|
||||
|
||||
pub struct PostgresBenchmarkRepository {
|
||||
pub connection: Arc<PgClient>
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl BenchmarkRepository for PostgresBenchmarkRepository {
|
||||
async fn benchmark(&self) -> Result<i64, String> {
|
||||
self.connection.execute("DELETE FROM Rent", &[]).await.unwrap();
|
||||
self.connection.execute("DELETE FROM Car", &[]).await.unwrap();
|
||||
self.connection.execute("DELETE FROM Owner", &[]).await.unwrap();
|
||||
self.connection.execute("DELETE FROM Client", &[]).await.unwrap();
|
||||
|
||||
let start_time = chrono::Utc::now().naive_local();
|
||||
|
||||
for n in 1..201 {
|
||||
let str = format!("INSERT INTO Owner(name, surname, middlename, phone) VALUES ('Иван{0}', 'Иванов{0}', 'Иванович{0}', {0}) RETURNING id", n);
|
||||
let owner_id: i32 = self.connection.query(
|
||||
&str,
|
||||
&[]
|
||||
).await.unwrap().get(0).unwrap().get(0);
|
||||
|
||||
let client_id: i32 = self.connection.query(
|
||||
&str.replace("Owner", "Client").replace("Иван", "Петр"),
|
||||
&[]
|
||||
).await.unwrap().get(0).unwrap().get(0);
|
||||
|
||||
let str = format!("INSERT INTO Car(Brand, Model, Price, owner_id, car_station_id) VALUES ('Лада{0}', 'Гранта{0}', {0}.00, {1}, 2) RETURNING id", n, owner_id);
|
||||
let car_id: i32 = self.connection.query(
|
||||
&str,
|
||||
&[]
|
||||
).await.unwrap().get(0).unwrap().get(0);
|
||||
|
||||
let str = format!("INSERT INTO Rent(car_id, client_id, start_time, time_amount) VALUES ({1}, {2}, now()::timestamp, {0}) RETURNING id", n, car_id, client_id);
|
||||
let rent_id: i32 = self.connection.query(
|
||||
&str,
|
||||
&[]
|
||||
).await.unwrap().get(0).unwrap().get(0);
|
||||
}
|
||||
|
||||
let elapsed = chrono::Utc::now().naive_local() - start_time;
|
||||
|
||||
Ok(elapsed.num_microseconds().unwrap())
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
use tokio_postgres::Client as PgClient;
|
||||
use std::sync::Arc;
|
||||
use crate::models::car::{BindingCar, Car};
|
||||
use crate::models::car::{BindingCar, Car, Report};
|
||||
use crate::storages::traits::CarRepository;
|
||||
use async_trait::async_trait;
|
||||
|
||||
@ -28,7 +28,7 @@ impl CarRepository for PostgresCarRepository {
|
||||
car_station_id: row.get("car_station_id")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during creation of Car".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -48,7 +48,7 @@ impl CarRepository for PostgresCarRepository {
|
||||
car_station_id: row.get("car_station_id")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during reading of Car".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -69,7 +69,7 @@ impl CarRepository for PostgresCarRepository {
|
||||
}).collect()
|
||||
)
|
||||
} else {
|
||||
Err("Something gone wrong during reading CarStations".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,7 +90,7 @@ impl CarRepository for PostgresCarRepository {
|
||||
car_station_id: row.get("car_station_id")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during updating of Car".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -107,7 +107,38 @@ impl CarRepository for PostgresCarRepository {
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
Err("Something gone wrong during deleting of Car".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_report(&self) -> Result<Vec<Report>, String> {
|
||||
let result = self.connection.query(
|
||||
"SELECT
|
||||
c.id AS \"car_id\",
|
||||
c.brand AS \"brand\",
|
||||
c.model AS \"model\",
|
||||
COUNT(r.id) AS \"times\",
|
||||
ROUND(SUM(c.price * r.time_amount), 2) AS \"income\"
|
||||
FROM car c
|
||||
JOIN rent r ON c.id = r.car_id
|
||||
WHERE date_trunc('month', r.start_time) = date_trunc('month', CURRENT_DATE)
|
||||
GROUP BY c.id;", &[]
|
||||
).await;
|
||||
|
||||
if let Ok(rows) = result {
|
||||
Ok(
|
||||
rows.into_iter()
|
||||
.map(|row| Report {
|
||||
car_id: row.get("car_id"),
|
||||
brand: row.get("brand"),
|
||||
model: row.get("model"),
|
||||
times: row.get("times"),
|
||||
income: row.get("income")
|
||||
})
|
||||
.collect()
|
||||
)
|
||||
} else {
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
}
|
@ -23,7 +23,7 @@ impl CarStationRepository for PostgresCarStationRepository {
|
||||
address: row.get("address")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during creation of CarStation".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,7 +39,7 @@ impl CarStationRepository for PostgresCarStationRepository {
|
||||
address: row.get("address")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during reading of CarStation".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -56,7 +56,7 @@ impl CarStationRepository for PostgresCarStationRepository {
|
||||
}).collect()
|
||||
)
|
||||
} else {
|
||||
Err("Something gone wrong during reading CarStations".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,7 +73,7 @@ impl CarStationRepository for PostgresCarStationRepository {
|
||||
address: row.get("address")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during updating of CarStation".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,7 +90,7 @@ impl CarStationRepository for PostgresCarStationRepository {
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
Err("Something gone wrong during deleting of CarStation".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
}
|
@ -27,7 +27,7 @@ impl ClientRepository for PostgresClientRepository {
|
||||
phone: row.get("phone")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during creation of Client".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -47,7 +47,7 @@ impl ClientRepository for PostgresClientRepository {
|
||||
phone: row.get("phone")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during reading of Client".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,7 +67,7 @@ impl ClientRepository for PostgresClientRepository {
|
||||
}).collect()
|
||||
)
|
||||
} else {
|
||||
Err("Something gone wrong during reading Clients".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -88,7 +88,7 @@ impl ClientRepository for PostgresClientRepository {
|
||||
phone: row.get("phone")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during updating of Client".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -105,7 +105,7 @@ impl ClientRepository for PostgresClientRepository {
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
Err("Something gone wrong during deleting of Client".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,3 +4,4 @@ pub mod car;
|
||||
pub mod rent;
|
||||
pub mod owner;
|
||||
pub mod administrator;
|
||||
pub mod benchmark;
|
@ -27,7 +27,7 @@ impl OwnerRepository for PostgresOwnerRepository {
|
||||
phone: row.get("phone")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during creation of Owner".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -47,7 +47,7 @@ impl OwnerRepository for PostgresOwnerRepository {
|
||||
phone: row.get("phone")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during reading of Owner".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,7 +67,7 @@ impl OwnerRepository for PostgresOwnerRepository {
|
||||
}).collect()
|
||||
)
|
||||
} else {
|
||||
Err("Something gone wrong during reading Owner".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -88,7 +88,7 @@ impl OwnerRepository for PostgresOwnerRepository {
|
||||
phone: row.get("phone")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during updating of Owner".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -105,7 +105,7 @@ impl OwnerRepository for PostgresOwnerRepository {
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
Err("Something gone wrong during deleting of Owner".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ pub struct PostgresRentRepository {
|
||||
impl RentRepository for PostgresRentRepository {
|
||||
async fn create(&self, rent: BindingRent) -> Result<Rent, String> {
|
||||
let query_result = self.connection.query(
|
||||
"SELECT * FROM Rent WHERE car_id=$1 AND time_amount IS NOT NULL",
|
||||
"SELECT * FROM Rent WHERE car_id=$1 AND time_amount IS NULL",
|
||||
&[&rent.car_id]
|
||||
).await;
|
||||
|
||||
@ -21,7 +21,7 @@ impl RentRepository for PostgresRentRepository {
|
||||
return Err("The car is already occupied".to_owned());
|
||||
}
|
||||
} else {
|
||||
return Err("Something gone wrong during checking for existing rents".to_owned());
|
||||
return Err(query_result.unwrap_err().to_string());
|
||||
}
|
||||
|
||||
let result = self.connection.query(
|
||||
@ -40,7 +40,7 @@ impl RentRepository for PostgresRentRepository {
|
||||
car_id: row.get("car_id")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during creation of Rent".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -59,7 +59,7 @@ impl RentRepository for PostgresRentRepository {
|
||||
car_id: row.get("car_id")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during reading of Rent".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -79,7 +79,7 @@ impl RentRepository for PostgresRentRepository {
|
||||
}).collect()
|
||||
)
|
||||
} else {
|
||||
Err("Something gone wrong during reading CarStations".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,7 +99,7 @@ impl RentRepository for PostgresRentRepository {
|
||||
car_id: row.get("car_id")
|
||||
})
|
||||
} else {
|
||||
Err("Something gone wrong during updating of Rent".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -111,7 +111,22 @@ impl RentRepository for PostgresRentRepository {
|
||||
if let Ok(_) = result {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("Something gone wrong during deletion of Rent".to_owned())
|
||||
Err(result.unwrap_err().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
async fn end_rent(&self, id: i32) -> Result<(), String> {
|
||||
let current_rent = self.read(id).await?;
|
||||
|
||||
if current_rent.time_amount.is_some() {
|
||||
return Err("Rent is already ended".to_owned());
|
||||
}
|
||||
|
||||
let time_amount = (self.connection.query("SELECT now()::timestamp", &[]).await.unwrap().get(0).unwrap().get::<usize, chrono::NaiveDateTime>(0) - current_rent.start_time).num_minutes() as i32;
|
||||
|
||||
match self.connection.execute("UPDATE Rent SET time_amount = $2 WHERE id = $1", &[&id, &time_amount]).await {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => Err(err.to_string())
|
||||
}
|
||||
}
|
||||
}
|
@ -21,6 +21,7 @@ pub trait CarRepository {
|
||||
async fn read_all(&self) -> Result<Vec<Car>, String>;
|
||||
async fn update(&self, id: i32, car: BindingCar) -> Result<Car, String>;
|
||||
async fn delete(&self, id: i32) -> Result<(), String>;
|
||||
async fn get_report(&self) -> Result<Vec<Report>, String>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@ -39,6 +40,7 @@ pub trait RentRepository {
|
||||
async fn read_all(&self) -> Result<Vec<Rent>, String>;
|
||||
async fn update(&self, id: i32, rent: BindingRent) -> Result<Rent, String>;
|
||||
async fn delete(&self, id: i32) -> Result<(), String>;
|
||||
async fn end_rent(&self, id: i32) -> Result<(), String>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@ -55,3 +57,8 @@ pub trait AdministratorRepository {
|
||||
async fn read(&self, id: i32) -> Result<Administrator, String>;
|
||||
async fn find_by_username(&self, username: &str) -> Result<Administrator, String>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait BenchmarkRepository {
|
||||
async fn benchmark(&self) -> Result<i64, String>;
|
||||
}
|
Loading…
Reference in New Issue
Block a user