Compare commits

..

No commits in common. "main" and "frontend" have entirely different histories.

40 changed files with 22 additions and 5217 deletions

3
.gitignore vendored
View File

@ -6,9 +6,6 @@ frontend/target/
frontend/.env
frontend/dist
backend/.env
backend/target
# These are backup files generated by rustfmt
**/*.rs.bk

2547
backend/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,24 +0,0 @@
[package]
name = "backend"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio = { version = "1", features = ["full"] }
dotenv = "0.15"
actix-web = "4"
tokio-postgres = { version = "0.7.8", features = ["with-chrono-0_4"] }
chrono = { version = "0.4.24", features = ["serde"] }
serde_json = "1.0"
serde = { version = "1.0.159", features = ["derive"] }
async-trait = "0.1.68"
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"
mongodb = { version = "2.5.0", features = ["bson-chrono-0_4"] }
futures = "0.3"

View File

@ -1,29 +0,0 @@
use std::sync::Mutex;
use actix_web::{web, post, Responder, HttpResponse};
use serde_json::json;
use crate::State;
#[derive(serde::Deserialize)]
pub struct LoginInfo {
username: String,
password: String
}
#[post("/login")]
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, 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())
}
}
#[post("/logout")]
pub async fn logout(state: web::Data<Mutex<State>>, json: web::Json<String>) -> impl Responder {
match state.lock() {
Ok(guard) => { guard.auth_service.logout(&json.0); HttpResponse::Ok().body("Success") },
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
}
}

View File

@ -1,14 +0,0 @@
use std::sync::Mutex;
use actix_web::{web, get, Responder, HttpResponse};
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())
}
}

View File

@ -1,142 +0,0 @@
use std::sync::Mutex;
use crate::models::car::*;
use actix_web::{web, get, post, patch, delete, Responder, HttpResponse, HttpRequest};
use crate::State;
#[get("/")]
pub async fn get_cars(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.read_all().await {
Ok(result) => HttpResponse::Ok().json(result),
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
},
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
}
}
#[get("/{id}")]
pub async fn get_car(state: web::Data<Mutex<State>>, path: web::Path<(String, )>, 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.read(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())
}
}
#[post("/")]
pub async fn create_car(state: web::Data<Mutex<State>>, json: web::Json<BindingCar>, 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.create(json.0).await {
Ok(result) => HttpResponse::Ok().json(result),
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
},
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
}
}
#[patch("/{id}")]
pub async fn update_car(state: web::Data<Mutex<State>>, json: web::Json<BindingCar>, path: web::Path<(String, )>, 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.update(path.into_inner().0, json.0).await {
Ok(result) => HttpResponse::Ok().json(result),
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
},
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
}
}
#[delete("/{id}")]
pub async fn delete_car(state: web::Data<Mutex<State>>, path: web::Path<(String, )>, 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.delete(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())
}
}
#[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())
}
}

View File

@ -1,119 +0,0 @@
use std::sync::Mutex;
use crate::models::car_station::*;
use actix_web::{web, get, post, patch, delete, Responder, HttpResponse, HttpRequest};
use crate::State;
#[get("/")]
pub async fn get_car_stations(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_station_repository.read_all().await {
Ok(result) => HttpResponse::Ok().json(result),
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
},
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
}
}
#[get("/{id}")]
pub async fn get_car_station(state: web::Data<Mutex<State>>, path: web::Path<(String, )>, 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_station_repository.read(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())
}
}
#[post("/")]
pub async fn create_car_station(state: web::Data<Mutex<State>>, json: web::Json<BindingCarStation>, 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_station_repository.create(json.0).await {
Ok(result) => HttpResponse::Ok().json(result),
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
},
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
}
}
#[patch("/{id}")]
pub async fn update_car_station(state: web::Data<Mutex<State>>, json: web::Json<BindingCarStation>, path: web::Path<(String, )>, 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_station_repository.update(path.into_inner().0, json.0).await {
Ok(result) => HttpResponse::Ok().json(result),
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
},
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
}
}
#[delete("/{id}")]
pub async fn delete_car_station(state: web::Data<Mutex<State>>, path: web::Path<(String, )>, 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_station_repository.delete(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())
}
}

View File

@ -1,119 +0,0 @@
use std::sync::Mutex;
use crate::models::client::*;
use actix_web::{web, get, post, patch, delete, Responder, HttpResponse, HttpRequest};
use crate::State;
#[get("/")]
pub async fn get_clients(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.client_repository.read_all().await {
Ok(result) => HttpResponse::Ok().json(result),
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
},
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
}
}
#[get("/{id}")]
pub async fn get_client(state: web::Data<Mutex<State>>, path: web::Path<(String, )>, 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.client_repository.read(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())
}
}
#[post("/")]
pub async fn create_client(state: web::Data<Mutex<State>>, json: web::Json<BindingClient>, 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.client_repository.create(json.0).await {
Ok(result) => HttpResponse::Ok().json(result),
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
},
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
}
}
#[patch("/{id}")]
pub async fn update_client(state: web::Data<Mutex<State>>, json: web::Json<BindingClient>, path: web::Path<(String, )>, 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.client_repository.update(path.into_inner().0, json.0).await {
Ok(result) => HttpResponse::Ok().json(result),
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
},
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
}
}
#[delete("/{id}")]
pub async fn delete_client(state: web::Data<Mutex<State>>, path: web::Path<(String, )>, 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.client_repository.delete(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())
}
}

View File

@ -1,7 +0,0 @@
pub mod client;
pub mod car_station;
pub mod car;
pub mod rent;
pub mod owner;
pub mod auth;
pub mod benchmark;

View File

@ -1,119 +0,0 @@
use std::sync::Mutex;
use crate::models::client::*;
use actix_web::{web, get, post, patch, delete, Responder, HttpResponse, HttpRequest};
use crate::State;
#[get("/")]
pub async fn get_owners(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.owner_repository.read_all().await {
Ok(result) => HttpResponse::Ok().json(result),
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
},
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
}
}
#[get("/{id}")]
pub async fn get_owner(state: web::Data<Mutex<State>>, path: web::Path<(String, )>, 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.owner_repository.read(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())
}
}
#[post("/")]
pub async fn create_owner(state: web::Data<Mutex<State>>, json: web::Json<BindingClient>, 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.owner_repository.create(json.0).await {
Ok(result) => HttpResponse::Ok().json(result),
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
},
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
}
}
#[patch("/{id}")]
pub async fn update_owner(state: web::Data<Mutex<State>>, json: web::Json<BindingClient>, path: web::Path<(String, )>, 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.owner_repository.update(path.into_inner().0, json.0).await {
Ok(result) => HttpResponse::Ok().json(result),
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
},
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
}
}
#[delete("/{id}")]
pub async fn delete_owner(state: web::Data<Mutex<State>>, path: web::Path<(String, )>, 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.owner_repository.delete(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())
}
}

View File

@ -1,142 +0,0 @@
use std::sync::Mutex;
use crate::models::rent::*;
use actix_web::{web, get, post, patch, delete, Responder, HttpResponse, HttpRequest};
use crate::State;
#[get("/")]
pub async fn get_rents(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 let Err(error) = guard.auth_service.administrator_by_token(guard.administrator_repository.as_ref(), token.to_str().unwrap()).await {
return HttpResponse::Unauthorized().body("Invalid token: ".to_owned() + error.as_str());
}
}
match state.lock() {
Ok(guard) => match guard.rent_repository.read_all().await {
Ok(result) => HttpResponse::Ok().json(result),
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
},
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
}
}
#[get("/{id}")]
pub async fn get_rent(state: web::Data<Mutex<State>>, path: web::Path<(String, )>, 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.read(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())
}
}
#[post("/")]
pub async fn create_rent(state: web::Data<Mutex<State>>, json: web::Json<BindingRent>, 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.create(json.0).await {
Ok(result) => HttpResponse::Ok().json(result),
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
},
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
}
}
#[patch("/{id}")]
pub async fn update_rent(state: web::Data<Mutex<State>>, json: web::Json<BindingRent>, path: web::Path<(String, )>, 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.update(path.into_inner().0, json.0).await {
Ok(result) => HttpResponse::Ok().json(result),
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
},
Err(error) => HttpResponse::InternalServerError().json(error.to_string())
}
}
#[delete("/{id}")]
pub async fn delete_rent(state: web::Data<Mutex<State>>, path: web::Path<(String, )>, 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.delete(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())
}
}
#[get("/{id}/end")]
pub async fn end_rent(state: web::Data<Mutex<State>>, path: web::Path<(String, )>, 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())
}
}

View File

@ -1,20 +0,0 @@
use services::auth::AuthService;
use storages::traits::{OwnerRepository, AdministratorRepository, BenchmarkRepository};
use crate::storages::traits::{CarRepository, CarStationRepository, ClientRepository, RentRepository};
pub mod endpoints;
pub mod models;
pub mod storages;
pub mod services;
pub struct State {
pub car_repository: Box<dyn CarRepository + Send>,
pub car_station_repository: Box<dyn CarStationRepository + Send>,
pub client_repository: Box<dyn ClientRepository + Send>,
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
}

View File

@ -1,147 +0,0 @@
use std::sync::{Arc, Mutex};
use actix_web::{App, HttpServer, web};
use actix_web::web::Data;
use backend::endpoints::benchmark::benchmark;
use backend::endpoints::{auth::*};
use backend::storages::postgres::administrator::PostgresAdministratorRepository;
use dotenv_codegen::dotenv;
use backend::{State, services};
use mongodb::Client;
use mongodb::options::ClientOptions;
use tokio_postgres::NoTls;
use backend::storages::postgres::car::PostgresCarRepository;
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::storages::mongo::car::MongoCarRepository;
use backend::storages::mongo::car_station::MongoCarStationRepository;
use backend::storages::mongo::administrator::MongoAdministratorRepository;
use backend::storages::mongo::client::MongoClientRepository;
use backend::storages::mongo::rent::MongoRentRepository;
use backend::storages::mongo::owner::MongoOwnerRepository;
use backend::storages::mongo::benchmark::MongoBenchmarkRepository;
use backend::storages::*;
use backend::endpoints::car_station::*;
use backend::endpoints::car::*;
use backend::endpoints::client::*;
use backend::endpoints::rent::*;
use backend::endpoints::owner::*;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
// let (client, connection) = tokio_postgres::connect(
// &format!("host={} user={} password={} dbname={}", dotenv!("HOST"), dotenv!("USERR"), dotenv!("PASSWORD"), dotenv!("DBNAMEE")),
// NoTls
// ).await.unwrap();
// tokio::spawn(async move {
// if let Err(e) = connection.await {
// eprintln!("connection error: {}", e);
// }
// });
// let client = Arc::new(client);
// let car_repository = PostgresCarRepository { connection: Arc::clone(&client) };
// let car_station_repository = PostgresCarStationRepository { connection: Arc::clone(&client) };
// let client_repository = PostgresClientRepository { connection: Arc::clone(&client) };
// let owner_repository = PostgresOwnerRepository { connection: Arc::clone(&client) };
// let rent_repository = PostgresRentRepository { connection: Arc::clone(&client) };
// let administrator_repository = PostgresAdministratorRepository { connection: Arc::clone(&client) };
// let benchmark_repository = PostgresBenchmarkRepository { connection: Arc::clone(&client) };
let client_options = ClientOptions::parse(dotenv!("MONGO")).await.unwrap();
let client = Client::with_options(client_options).unwrap();
let db = Arc::new(client.database("default_db"));
let car_repository = MongoCarRepository { database: Arc::clone(&db) };
let car_station_repository = MongoCarStationRepository { database: Arc::clone(&db) };
let client_repository = MongoClientRepository { database: Arc::clone(&db) };
let owner_repository = MongoOwnerRepository { database: Arc::clone(&db) };
let rent_repository = MongoRentRepository { database: Arc::clone(&db) };
let administrator_repository = MongoAdministratorRepository { database: Arc::clone(&db) };
let benchmark_repository = MongoBenchmarkRepository { database: Arc::clone(&db) };
let auth_service = services::auth::AuthService::new();
let state = Data::new(Mutex::new(State {
car_repository: Box::new(car_repository),
car_station_repository: Box::new(car_station_repository),
client_repository: Box::new(client_repository),
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
}));
HttpServer::new(move || {
App::new()
.app_data(Data::clone(&state))
.service(web::scope("/api")
.service(
web::scope("/cars")
.service(get_cars)
.service(get_report)
.service(get_car)
.service(create_car)
.service(update_car)
.service(delete_car)
)
.service(
web::scope("/clients")
.service(get_clients)
.service(get_client)
.service(create_client)
.service(update_client)
.service(delete_client)
)
.service(
web::scope("/owners")
.service(get_owners)
.service(get_owner)
.service(create_owner)
.service(update_owner)
.service(delete_owner)
)
.service(
web::scope("/car_stations")
.service(get_car_stations)
.service(get_car_station)
.service(create_car_station)
.service(update_car_station)
.service(delete_car_station)
)
.service(
web::scope("/rents")
.service(get_rents)
.service(get_rent)
.service(create_rent)
.service(update_rent)
.service(delete_rent)
.service(end_rent)
)
.service(login)
.service(logout)
).service(benchmark)
})
.bind("0.0.0.0:8080")?
.run()
.await
}

View File

@ -1,12 +0,0 @@
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Administrator {
pub id: String,
pub username: String,
pub password: String,
pub name: String,
pub surname: String,
pub middlename: String,
pub car_station_id: String
}

View File

@ -1,30 +0,0 @@
use rust_decimal::Decimal;
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Car {
pub id: String,
pub brand: String,
pub model: String,
pub price: Decimal,
pub owner_id: String,
pub car_station_id: String
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BindingCar {
pub brand: String,
pub model: String,
pub price: Decimal,
pub owner_id: String,
pub car_station_id: String
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Report {
pub car_id: String,
pub brand: String,
pub model: String,
pub times: i64,
pub income: Decimal
}

View File

@ -1,12 +0,0 @@
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CarStation {
pub id: String,
pub address: String
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BindingCarStation {
pub address: String
}

View File

@ -1,18 +0,0 @@
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Client {
pub id: String,
pub name: String,
pub surname: String,
pub middlename: Option<String>,
pub phone: String
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BindingClient {
pub name: String,
pub surname: String,
pub middlename: Option<String>,
pub phone: String
}

View File

@ -1,5 +0,0 @@
pub mod client;
pub mod car_station;
pub mod car;
pub mod rent;
pub mod administrator;

View File

@ -1,17 +0,0 @@
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Rent {
pub id: String,
pub start_time: chrono::NaiveDateTime,
pub time_amount: Option<i32>,
pub client_id: String,
pub car_id: String
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BindingRent {
pub time_amount: Option<i32>,
pub client_id: String,
pub car_id: String
}

View File

@ -1,34 +0,0 @@
use crate::{storages::traits::AdministratorRepository, models::administrator::Administrator};
use nanoid::nanoid;
use sha2::Sha256;
use std::cell::RefCell;
pub struct AuthService {
authed: RefCell<Vec::<(String, String)>>
}
impl AuthService {
pub fn new() -> Self {
AuthService { authed: RefCell::new(Vec::new()) }
}
pub async fn login(&self, storage: &dyn AdministratorRepository, username: &str, password: &str) -> Result<(String, String, String), 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.clone()));
Ok((token, administrator.id.clone(), administrator.car_station_id))
}
}
pub fn logout(&self, token: &str) {
self.authed.borrow_mut().retain(|x| x.0 != token);
}
pub async fn administrator_by_token(&self, storage: &dyn AdministratorRepository, token: &str) -> Result<Administrator, String> {
let id = self.authed.borrow().iter().find(|&x| x.0.starts_with(token)).ok_or("Invalid token")?.1.clone();
storage.read(id).await
}
}

View File

@ -1 +0,0 @@
pub mod auth;

View File

@ -1,3 +0,0 @@
pub mod traits;
pub mod postgres;
pub mod mongo;

View File

@ -1,63 +0,0 @@
use mongodb::Database;
use mongodb::bson::oid::ObjectId;
use mongodb::bson::{doc, Document, Bson};
use futures::stream::{TryStreamExt, StreamExt};
use mongodb::options::FindOptions;
use std::sync::Arc;
use crate::models::administrator::*;
use crate::storages::traits::AdministratorRepository;
use async_trait::async_trait;
pub struct MongoAdministratorRepository {
pub database: Arc<Database>
}
#[async_trait]
impl AdministratorRepository for MongoAdministratorRepository {
async fn read(&self, id: String) -> Result<Administrator, String> {
let collection = self.database.collection::<Document>("administrators");
let filter = doc! {
"_id": ObjectId::parse_str(&id).unwrap()
};
let result = collection.find_one(filter, None).await;
match result {
Ok(Some(document)) => {
let id = document.get_object_id("_id").unwrap().to_hex();
let username = document.get_str("username").unwrap().to_string();
let password = document.get_str("password").unwrap().to_string();
let name = document.get_str("name").unwrap().to_string();
let middlename = document.get_str("middlename").unwrap().to_string();
let surname = document.get_str("surname").unwrap().to_string();
let car_station_id = document.get_object_id("car_station_id").unwrap().to_hex();
let administrator = Administrator {id, username, password, name, surname, middlename, car_station_id};
Ok(administrator)
},
Ok(None) => Err("Administrator not found".to_string()),
Err(e) => Err(e.to_string())
}
}
async fn find_by_username(&self, username: &str) -> Result<Administrator, String> {
let collection = self.database.collection::<Document>("administrators");
let filter = doc! {
"username": username.to_string()
};
let result = collection.find_one(filter, None).await;
match result {
Ok(Some(document)) => {
let id = document.get_object_id("_id").unwrap().to_hex();
let username = document.get_str("username").unwrap().to_string();
let password = document.get_str("password").unwrap().to_string();
let name = document.get_str("name").unwrap().to_string();
let middlename = document.get_str("middlename").unwrap().to_string();
let surname = document.get_str("surname").unwrap().to_string();
let car_station_id = document.get_object_id("car_station_id").unwrap().to_hex();
let administrator = Administrator {id, username, password, name, surname, middlename, car_station_id};
Ok(administrator)
},
Ok(None) => Err("Administrator not found".to_string()),
Err(e) => Err(e.to_string())
}
}
}

View File

@ -1,68 +0,0 @@
use chrono::Utc;
use mongodb::Database;
use mongodb::bson::oid::ObjectId;
use mongodb::bson::{doc, Document, Bson};
use futures::stream::{TryStreamExt, StreamExt};
use mongodb::options::{FindOptions, AggregateOptions};
use rust_decimal::Decimal;
use rust_decimal::prelude::{ToPrimitive, FromPrimitive};
use std::default;
use std::sync::Arc;
use async_trait::async_trait;
use crate::storages::traits::BenchmarkRepository;
pub struct MongoBenchmarkRepository {
pub database: Arc<Database>
}
#[async_trait]
impl BenchmarkRepository for MongoBenchmarkRepository {
async fn benchmark(&self) -> Result<i64, String> {
let collection = self.database.collection::<Document>("clients");
collection.delete_many(doc! {}, None).await.unwrap();
let start_time = Utc::now().naive_local();
for i in 1..2001 {
let car_id = ObjectId::new();
let client_id = ObjectId::new();
let owner_id = ObjectId::new();
let rent_id = ObjectId::new();
collection.insert_one(doc! {
"_id": client_id,
"name": format!("Иван{}", i),
"surname": format!("Иванов{}", i),
"middlename": format!("Иванович{}", i),
"phone": i.to_string(),
"rents": [{
"_id": rent_id,
"start_time": chrono::Utc::now(),
"time_amount": i,
"car_id": car_id.clone()
}]
}, None).await.unwrap();
collection.insert_one(doc! {
"_id": owner_id,
"name": format!("Петр{}", i),
"surname": format!("Петров{}", i),
"middlename": format!("Петрович{}", i),
"phone": i.to_string(),
"rents": [],
"cars": [{
"_id": car_id,
"brand": format!("Лада{}", i),
"model": format!("Гранта{}", i),
"price": i as f64,
"car_station_id": ObjectId::parse_str("6466ca4c26b3b0b31d9a45bc").unwrap()
}]
}, None).await.unwrap();
}
return Ok((Utc::now().naive_local() - start_time).num_microseconds().unwrap())
}
}

View File

@ -1,221 +0,0 @@
use chrono::{Utc, Months};
use mongodb::Database;
use mongodb::bson::oid::ObjectId;
use mongodb::bson::{doc, Document, Bson};
use futures::stream::{TryStreamExt, StreamExt};
use mongodb::options::{FindOptions, AggregateOptions};
use rust_decimal::Decimal;
use rust_decimal::prelude::{ToPrimitive, FromPrimitive};
use std::default;
use std::sync::Arc;
use crate::models::car::*;
use crate::storages::traits::*;
use async_trait::async_trait;
use super::rent::MongoRentRepository;
pub struct MongoCarRepository {
pub database: Arc<Database>
}
#[async_trait]
impl CarRepository for MongoCarRepository {
async fn create(&self, car: BindingCar) -> Result<Car, String> {
let collection = self.database.collection::<Document>("clients");
let document = doc! {
"_id": ObjectId::parse_str(car.owner_id.clone()).unwrap()
};
let update = doc! {
"$push": {
"cars": doc! {
"_id": ObjectId::new(),
"brand": car.brand.clone(),
"model": car.model.clone(),
"price": car.price.to_f64().unwrap(),
"car_station_id": ObjectId::parse_str(car.car_station_id.to_string()).unwrap()
}
}
};
let result = collection.update_one(document, update, None).await;
match result {
Ok(result) => {
let brand = car.brand.clone();
let model = car.model.clone();
let price = car.price;
let car_station_id = car.car_station_id.clone();
let owner_id = car.owner_id.clone();
let car = Car { id: "".to_string(), brand, model, car_station_id, price, owner_id};
Ok(car)
},
Err(e) => Err(e.to_string())
}
}
async fn read(&self, id: String) -> Result<Car, String> {
match self.read_all().await.unwrap().into_iter().filter(|c| c.id.starts_with(&id)).next() {
Some(car) => Ok(car),
None => Err("Car not found".to_string())
}
}
async fn read_all(&self) -> Result<Vec<Car>, String> {
let collection = self.database.collection::<Document>("clients");
let mut cursor = collection.find(doc! {"cars": { "$exists": true }}, None).await.map_err(|e| e.to_string())?;
let mut cars = Vec::new();
while let Some(result) = cursor.next().await {
match result {
Ok(document) => {
for car_document in document.get_array("cars").unwrap() {
let car = car_document.as_document().unwrap();
let id = car.get("_id").unwrap().as_object_id().unwrap().to_hex();
let brand = car.get_str("brand").unwrap().to_string();
let model = car.get_str("model").unwrap().to_string();
let price = Decimal::from_f64(car.get_f64("price").unwrap()).unwrap();
let car_station_id = car.get_object_id("car_station_id").unwrap().to_hex();
let owner_id = document.get_object_id("_id").unwrap().to_hex();
let car = Car { id, brand, model, car_station_id, price, owner_id};
cars.push(car);
}
},
Err(e) => return Err(e.to_string())
}
}
Ok(cars)
}
async fn update(&self, id: String, car: BindingCar) -> Result<Car, String> {
let collection = self.database.collection::<Document>("clients");
let mut cursor = collection.find(None, None).await.map_err(|e| e.to_string())?;
while let Some(result) = cursor.next().await {
match result {
Ok(document) => {
for (i, car_document) in document.get_array("cars").unwrap().into_iter().enumerate() {
if car_document.as_document().unwrap().get_object_id("_id").unwrap().to_hex().starts_with(&id) {
collection.update_one(doc! {
"_id": ObjectId::parse_str(document.get_str("_id").unwrap()).unwrap()
}, doc! {
"$set": doc! {
format!("cars.${}.brand", i): car.brand.clone(),
format!("cars.${}.model", i): car.model.clone(),
format!("cars.${}.price", i): car.price.to_f64().unwrap(),
format!("cars.${}.car_station_id", i): ObjectId::parse_str(car.car_station_id.clone()).unwrap(),
}
}, None).await.unwrap();
return Ok(Car {..Default::default()})
}
}
},
Err(e) => return Err(e.to_string())
}
}
Err("Car not found".to_string())
}
async fn delete(&self, id: String) -> Result<(), String> {
let collection = self.database.collection::<Document>("clients");
let document = doc! {
"cars._id": ObjectId::parse_str(&id).unwrap()
};
let update = doc! {
"$pull": {
"cars": {
"_id": ObjectId::parse_str(&id).unwrap()
}
}
};
let result = collection.update_one(document, update, None).await;
match result {
Ok(result) => {
if result.modified_count == 0 {
Err("Car not found".to_string())
} else {
Ok(())
}
},
Err(e) => Err(e.to_string())
}
}
async fn get_report(&self) -> Result<Vec<Report>, String> {
let mut reports = Vec::new();
let rents = (MongoRentRepository {database: Arc::clone(&self.database)}).read_all().await.unwrap();
let cars = self.read_all().await.unwrap();
for car in cars {
let mut times = 0;
let mut sum = Decimal::from_f64(0.0).unwrap();
for rent in rents.iter() {
if rent.car_id.starts_with(&car.id) && rent.time_amount.is_some() {
times += 1;
sum += car.price * Decimal::from(rent.time_amount.unwrap());
}
}
reports.push(Report {car_id: car.id.clone(), brand: car.brand.clone(), model: car.model.clone(), times, income: sum})
}
// let pipeline = vec![
// doc! {
// "$match": {
// "rents.start_time": {
// "$gte": Utc::now().checked_sub_months(Months::new(1)).unwrap(),
// "$lt": Utc::now().checked_add_months(Months::new(1)).unwrap()
// }
// }
// },
// doc! {
// "$lookup": {
// "from": "cars",
// "localField": "rents.car_id",
// "foreignField": "_id",
// "as": "car"
// }
// },
// doc! {
// "$unwind": "$car"
// },
// doc! {
// "$group": {
// "_id": "$rents.car_id",
// "brand": { "$first": "$car.brand" },
// "model": { "$first": "$car.model" },
// "times": { "$sum": 1 },
// "income": { "$sum": { "$multiply": [ "$car.price", "$rents.time_amount" ] } }
// }
// },
// doc! {
// "$project": {
// "_id": 0,
// "car_id": { "$toString": "$_id" },
// "brand": 1,
// "model": 1,
// "times": 1,
// "income": { "$round": [ "$income", 2 ] }
// }
// }
// ];
// let collection = self.database.collection::<Document>("clients");
// let options = AggregateOptions::builder().build();
// let cursor = collection.aggregate(pipeline, options).await.unwrap();
// let reports = cursor
// .map(|doc| {
// let doc = doc.unwrap();
// let car_id = doc.get_str("car_id").unwrap().to_owned();
// let brand = doc.get_str("brand").unwrap().to_owned();
// let model = doc.get_str("model").unwrap().to_owned();
// let times = doc.get_i64("times").unwrap();
// let income = Decimal::from_f64(doc.get_f64("income").unwrap()).unwrap();
// Report { car_id, brand, model, times, income }
// })
// .collect::<Vec<_>>()
// .await;
Ok(reports)
}
}

View File

@ -1,101 +0,0 @@
use mongodb::Database;
use mongodb::bson::oid::ObjectId;
use mongodb::bson::{doc, Document, Bson};
use futures::stream::{TryStreamExt, StreamExt};
use mongodb::options::FindOptions;
use std::sync::Arc;
use crate::models::car_station::*;
use crate::storages::traits::CarStationRepository;
use async_trait::async_trait;
pub struct MongoCarStationRepository {
pub database: Arc<Database>
}
#[async_trait]
impl CarStationRepository for MongoCarStationRepository {
async fn create(&self, car_station: BindingCarStation) -> Result<CarStation, String> {
let collection = self.database.collection::<Document>("car_stations");
let document = doc! {
"address": car_station.address.clone()
};
let result = collection.insert_one(document, None).await;
match result {
Ok(result) => {
let id = result.inserted_id.as_object_id().unwrap().to_hex();
let car_station = CarStation { id, address: car_station.address };
Ok(car_station)
},
Err(e) => Err(e.to_string())
}
}
async fn read(&self, id: String) -> Result<CarStation, String> {
let collection = self.database.collection::<Document>("car_stations");
let filter = doc! {
"_id": ObjectId::parse_str(&id).unwrap()
};
let result = collection.find_one(filter, None).await;
match result {
Ok(Some(document)) => {
let id = document.get_object_id("_id").unwrap().to_hex();
let address = document.get_str("address").unwrap().to_string();
let car_station = CarStation { id, address };
Ok(car_station)
},
Ok(None) => Err("Car station not found".to_string()),
Err(e) => Err(e.to_string())
}
}
async fn read_all(&self) -> Result<Vec<CarStation>, String> {
let collection = self.database.collection::<Document>("car_stations");
let mut cursor = collection.find(None, None).await.map_err(|e| e.to_string())?;
let mut car_stations = Vec::new();
while let Some(result) = cursor.next().await {
match result {
Ok(document) => {
let id = document.get_object_id("_id").unwrap().to_hex();
let address = document.get_str("address").unwrap().to_string();
let car_station = CarStation { id, address };
car_stations.push(car_station);
},
Err(e) => return Err(e.to_string())
}
}
Ok(car_stations)
}
async fn update(&self, id: String, car_station: BindingCarStation) -> Result<CarStation, String> {
let collection = self.database.collection::<Document>("car_stations");
let filter = doc! {
"_id": ObjectId::parse_str(&id).unwrap()
};
let update = doc! {
"$set": {
"address": car_station.address.clone()
}
};
let result = collection.update_one(filter, update, None).await;
match result {
Ok(_) => {
let car_station = CarStation { id, address: car_station.address };
Ok(car_station)
},
Err(e) => Err(e.to_string())
}
}
async fn delete(&self, id: String) -> Result<(), String> {
let collection = self.database.collection::<Document>("car_stations");
let filter = doc! {
"_id": ObjectId::parse_str(&id).unwrap()
};
let result = collection.delete_one(filter, None).await;
match result {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string())
}
}
}

View File

@ -1,117 +0,0 @@
use mongodb::Database;
use mongodb::bson::oid::ObjectId;
use mongodb::bson::{doc, Document, Bson};
use futures::stream::{TryStreamExt, StreamExt};
use mongodb::options::{FindOptions, AggregateOptions};
use rust_decimal::Decimal;
use rust_decimal::prelude::{ToPrimitive, FromPrimitive};
use std::default;
use std::sync::Arc;
use crate::models::client::*;
use crate::storages::traits::ClientRepository;
use async_trait::async_trait;
pub struct MongoClientRepository {
pub database: Arc<Database>
}
#[async_trait]
impl ClientRepository for MongoClientRepository {
async fn create(&self, client: BindingClient) -> Result<Client, String> {
let collection = self.database.collection::<Document>("clients");
let document = doc! {
"name": client.name.clone(),
"surname": client.surname.clone(),
"middlename": client.middlename.clone(),
"phone": client.phone.clone(),
"rents": Bson::Array(Vec::<Bson>::new())
};
let result = collection.insert_one(document, None).await;
match result {
Ok(result) => {
let id = result.inserted_id.as_object_id().unwrap().to_hex();
let client = Client { id, name: client.name, surname: client.surname, middlename: client.middlename, phone: client.phone };
Ok(client)
},
Err(e) => Err(e.to_string())
}
}
async fn read(&self, id: String) -> Result<Client, String> {
let collection = self.database.collection::<Document>("clients");
let filter = doc! {
"_id": ObjectId::parse_str(&id).unwrap()
};
let result = collection.find_one(filter, None).await;
match result {
Ok(Some(document)) => {
let id = document.get_object_id("_id").unwrap().to_hex();
let name = document.get_str("name").unwrap().to_string();
let surname = document.get_str("surname").unwrap().to_string();
let middlename = document.get_str("middlename").map(|s| s.to_string());
let phone = document.get_str("phone").unwrap().to_string();
let client = Client { id, name, surname, middlename: Some(middlename.unwrap()), phone };
Ok(client)
},
Ok(None) => Err("Client not found".to_string()),
Err(e) => Err(e.to_string())
}
}
async fn read_all(&self) -> Result<Vec<Client>, String> {
let collection = self.database.collection::<Document>("clients");
let mut cursor = collection.find(None, None).await.map_err(|e| e.to_string())?;
let mut clients = Vec::new();
while let Some(result) = cursor.next().await {
match result {
Ok(document) => {
let id = document.get_object_id("_id").unwrap().to_hex();
let name = document.get_str("name").unwrap().to_string();
let surname = document.get_str("surname").unwrap().to_string();
let middlename = document.get_str("middlename").map(|s| s.to_string());
let phone = document.get_str("phone").unwrap().to_string();
let client = Client { id, name, surname, middlename: Some(middlename.unwrap()), phone };
clients.push(client);
},
Err(e) => return Err(e.to_string())
}
}
Ok(clients)
}
async fn update(&self, id: String, client: BindingClient) -> Result<Client, String> {
let collection = self.database.collection::<Document>("clients");
let filter = doc! {
"_id": ObjectId::parse_str(&id).unwrap()
};
let update = doc! {
"$set": {
"name": client.name.clone(),
"surname": client.surname.clone(),
"middlename": client.middlename.clone(),
"phone": client.phone.clone()
}
};
let result = collection.update_one(filter, update, None).await;
match result {
Ok(_) => {
let client = Client { id, name: client.name, surname: client.surname, middlename: client.middlename, phone: client.phone };
Ok(client)
},
Err(e) => Err(e.to_string())
}
}
async fn delete(&self, id: String) -> Result<(), String> {
let collection = self.database.collection::<Document>("clients");
let filter = doc! {
"_id": ObjectId::parse_str(&id).unwrap()
};
let result = collection.delete_one(filter, None).await;
match result {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string())
}
}
}

View File

@ -1,7 +0,0 @@
pub mod car_station;
pub mod administrator;
pub mod car;
pub mod client;
pub mod owner;
pub mod rent;
pub mod benchmark;

View File

@ -1,129 +0,0 @@
use mongodb::Database;
use mongodb::bson::oid::ObjectId;
use mongodb::bson::{doc, Document, Bson};
use futures::stream::{TryStreamExt, StreamExt};
use mongodb::options::{FindOptions, AggregateOptions};
use rust_decimal::Decimal;
use rust_decimal::prelude::{ToPrimitive, FromPrimitive};
use std::default;
use std::sync::Arc;
use crate::models::client::*;
use crate::storages::traits::OwnerRepository;
use async_trait::async_trait;
pub struct MongoOwnerRepository {
pub database: Arc<Database>
}
#[async_trait]
impl OwnerRepository for MongoOwnerRepository {
async fn create(&self, owner: BindingClient) -> Result<Client, String> {
let collection = self.database.collection::<Document>("clients");
let document = doc! {
"name": owner.name.clone(),
"surname": owner.surname.clone(),
"middlename": owner.middlename.clone(),
"phone": owner.phone.clone(),
"cars": Bson::Array(Vec::<Bson>::new()),
"rents": Bson::Array(Vec::<Bson>::new())
};
let result = collection.insert_one(document, None).await;
match result {
Ok(result) => {
let id = result.inserted_id.as_object_id().unwrap().to_hex();
let owner = Client { id, name: owner.name, surname: owner.surname, middlename: owner.middlename, phone: owner.phone };
Ok(owner)
},
Err(e) => Err(e.to_string())
}
}
async fn read(&self, id: String) -> Result<Client, String> {
let collection = self.database.collection::<Document>("clients");
let filter = doc! {
"_id": ObjectId::parse_str(&id).unwrap()
};
let result = collection.find_one(filter, None).await;
match result {
Ok(Some(document)) => {
let id = document.get_object_id("_id").unwrap().to_hex();
let name = document.get_str("name").unwrap().to_string();
let surname = document.get_str("surname").unwrap().to_string();
let middlename = document.get_str("middlename").map(|s| s.to_string());
let phone = document.get_str("phone").unwrap().to_string();
let owner = Client { id, name, surname, middlename: Some(middlename.unwrap()), phone };
Ok(owner)
},
Ok(None) => Err("Owner not found".to_string()),
Err(e) => Err(e.to_string())
}
}
async fn read_all(&self) -> Result<Vec<Client>, String> {
let collection = self.database.collection::<Document>("clients");
let mut cursor = collection.find(doc! {"cars": { "$exists": true }}, None).await.map_err(|e| e.to_string())?;
let mut owners = Vec::new();
while let Some(result) = cursor.next().await {
match result {
Ok(document) => {
let id = document.get_object_id("_id").unwrap().to_hex();
let name = document.get_str("name").unwrap().to_string();
let surname = document.get_str("surname").unwrap().to_string();
let middlename = document.get_str("middlename").map(|s| s.to_string());
let phone = document.get_str("phone").unwrap().to_string();
let owner = Client { id, name, surname, middlename: Some(middlename.unwrap()), phone };
owners.push(owner);
},
Err(e) => return Err(e.to_string())
}
}
Ok(owners)
}
async fn update(&self, id: String, owner: BindingClient) -> Result<Client, String> {
let collection = self.database.collection::<Document>("clients");
let filter = doc! {
"_id": ObjectId::parse_str(&id).unwrap()
};
let update = doc! {
"$set": {
"name": owner.name.clone(),
"surname": owner.surname.clone(),
"middlename": owner.middlename.clone(),
"phone": owner.phone.clone()
}
};
let result = collection.update_one(filter, update, None).await;
match result {
Ok(result) => {
if result.modified_count == 1 {
let owner = self.read(id).await?;
Ok(owner)
} else {
Err("Owner not found".to_string())
}
},
Err(e) => Err(e.to_string())
}
}
async fn delete(&self, id: String) -> Result<(), String> {
let collection = self.database.collection::<Document>("owners");
let filter = doc! {
"_id": ObjectId::parse_str(&id).unwrap()
};
let result = collection.delete_one(filter, None).await;
match result {
Ok(result) => {
if result.deleted_count == 1 {
Ok(())
} else {
Err("Owner not found".to_string())
}
},
Err(e) => Err(e.to_string())
}
}
}

View File

@ -1,153 +0,0 @@
use chrono::{NaiveDateTime, Utc};
use mongodb::Database;
use mongodb::bson::oid::ObjectId;
use mongodb::bson::{doc, Document, Bson};
use futures::stream::{TryStreamExt, StreamExt};
use mongodb::options::{FindOptions, AggregateOptions, FindOneOptions};
use rust_decimal::Decimal;
use rust_decimal::prelude::{ToPrimitive, FromPrimitive};
use std::default;
use std::sync::Arc;
use crate::models::rent::*;
use crate::storages::traits::RentRepository;
use async_trait::async_trait;
pub struct MongoRentRepository {
pub database: Arc<Database>
}
#[async_trait]
impl RentRepository for MongoRentRepository {
async fn create(&self, rent: BindingRent) -> Result<Rent, String> {
let collection = self.database.collection::<Document>("clients");
let start_time = chrono::Utc::now();
let filter = doc! {
"_id": ObjectId::parse_str(&rent.client_id).unwrap()
};
let update = doc! {
"$push": {
"rents": {
"_id": ObjectId::new(),
"start_time": start_time,
"time_amount": Bson::Null,
"car_id": ObjectId::parse_str(&rent.car_id).unwrap()
}
}
};
let result = collection.update_one(filter, update, None).await;
match result {
Ok(result) => {
if result.modified_count == 1 {
Ok(Rent {id: "".to_string(), start_time: Default::default(), time_amount: None, car_id: "".to_string(), client_id: "".to_string()})
} else {
Err("Rent not found".to_string())
}
},
Err(e) => Err(e.to_string())
}
}
async fn read(&self, id: String) -> Result<Rent, String> {
let collection = self.database.collection::<Document>("clients");
let filter = doc! {
"rents._id": ObjectId::parse_str(&id).unwrap()
};
let projection = doc! {
"rents.$": 1
};
let options = FindOneOptions::builder().projection(projection).build();
let result = collection.find_one(filter, options).await;
match result {
Ok(Some(document)) => {
let rent_document = document.get_array("rents").unwrap().get(0).unwrap().as_document().unwrap();
let client_id = document.get_object_id("_id").unwrap().to_hex();
let id = rent_document.get_object_id("_id").unwrap().to_hex();
let start_time = rent_document.get_datetime("start_time").unwrap();
let time_amount = rent_document.get_i32("time_amount").ok();
let car_id = rent_document.get_object_id("car_id").unwrap().to_hex();
let rent = Rent { id, start_time: NaiveDateTime::from_timestamp_millis(start_time.timestamp_millis()).unwrap(), time_amount, client_id, car_id };
Ok(rent)
},
Ok(None) => Err("Rent not found".to_string()),
Err(e) => Err(e.to_string())
}
}
async fn read_all(&self) -> Result<Vec<Rent>, String> {
let collection = self.database.collection::<Document>("clients");
let result = collection.find(None, None).await;
match result {
Ok(mut cursor) => {
let mut rents = Vec::new();
while let Some(document) = cursor.next().await {
let another = document.clone().unwrap();
let rents_array = another.get_array("rents").unwrap();
let client_id = document.unwrap().get_object_id("_id").unwrap().to_hex();
for rent_document in rents_array.iter().map(|x| x.as_document().unwrap()) {
let id = rent_document.get_object_id("_id").unwrap().to_hex();
let start_time = rent_document.get_datetime("start_time").unwrap();
let time_amount = rent_document.get_i32("time_amount").ok();
let car_id = rent_document.get_object_id("car_id").unwrap().to_hex();
let rent = Rent { id, start_time: NaiveDateTime::from_timestamp_millis(start_time.timestamp_millis()).unwrap(), time_amount, client_id: client_id.clone(), car_id };
rents.push(rent);
}
}
Ok(rents)
},
Err(e) => Err(e.to_string())
}
}
async fn update(&self, id: String, rent: BindingRent) -> Result<Rent, String> {
let collection = self.database.collection::<Document>("clients");
let filter = doc! {
"_id": ObjectId::parse_str(&rent.client_id).unwrap(),
"rents._id": ObjectId::parse_str(&id).unwrap()
};
let update = doc! {
"$set": {
"rents.$.time_amount": rent.time_amount
}
};
let result = collection.update_one(filter, update, None).await;
match result {
Ok(success) => self.read(success.upserted_id.unwrap().as_object_id().unwrap().to_hex()).await,
Err(e) => Err(e.to_string())
}
}
async fn delete(&self, id: String) -> Result<(), String> {
let collection = self.database.collection::<Document>("clients");
let rent = self.read(id.clone()).await.unwrap();
let filter = doc! {
"_id": ObjectId::parse_str(rent.client_id.clone()).unwrap(),
"rents._id": ObjectId::parse_str(id.clone()).unwrap()
};
let result = collection.update_one(filter, doc! {"$pull": {"rents": {"_id": ObjectId::parse_str(id.clone()).unwrap()}}}, None).await;
match result {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string())
}
}
async fn end_rent(&self, id: String) -> Result<(), String> {
let rent = self.read(id.clone()).await?;
if rent.time_amount.is_some() {
return Err("Rent is already ended".to_owned());
}
let time_amount = (Utc::now().naive_local() - rent.start_time).num_minutes() as i32;
let collection = self.database.collection::<Document>("clients");
let filter = doc! {"_id": ObjectId::parse_str(&rent.client_id).unwrap(), "rents._id": ObjectId::parse_str(&rent.id).unwrap()};
let update = doc! {"$set": {"rents.$.time_amount": time_amount}};
let result = collection.update_one(filter, update, None).await;
match result {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string())
}
}
}

View File

@ -1,52 +0,0 @@
use tokio_postgres::Client as PgClient;
use std::sync::Arc;
use crate::models::administrator::Administrator;
use crate::storages::traits::AdministratorRepository;
use async_trait::async_trait;
pub struct PostgresAdministratorRepository {
pub connection: Arc<PgClient>
}
#[async_trait]
impl AdministratorRepository for PostgresAdministratorRepository {
async fn read(&self, id: String) -> Result<Administrator, String> {
let result = self.connection.query(
"SELECT * FROM administrator WHERE id = $1", &[&id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = result {
let row = rows.get(0).ok_or("Administrator not found".to_owned())?;
Ok(Administrator {
id: row.get::<&str, i32>("id").to_string(),
username: row.get("username"),
password: row.get("password"),
name: row.get("name"),
surname: row.get("surname"),
middlename: row.get("middlename"),
car_station_id: row.get::<&str, i32>("car_station_id").to_string()
})
} else {
Err(result.unwrap_err().to_string())
}
}
async fn find_by_username(&self, username: &str) -> Result<Administrator, String> {
let result = self.connection.query("SELECT * FROM administrator WHERE username LIKE $1", &[&username]).await;
if let Ok(rows) = result {
let row = rows.get(0).ok_or("Administrator not found".to_owned())?;
Ok(Administrator {
id: row.get::<&str, i32>("id").to_string(),
username: row.get("username"),
password: row.get("password"),
name: row.get("name"),
surname: row.get("surname"),
middlename: row.get("middlename"),
car_station_id: row.get::<&str, i32>("car_station_id").to_string()
})
} else {
Err(result.unwrap_err().to_string())
}
}
}

View File

@ -1,49 +0,0 @@
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..2001 {
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 _: 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())
}
}

View File

@ -1,144 +0,0 @@
use tokio_postgres::Client as PgClient;
use std::sync::Arc;
use crate::models::car::{BindingCar, Car, Report};
use crate::storages::traits::CarRepository;
use async_trait::async_trait;
pub struct PostgresCarRepository {
pub connection: Arc<PgClient>
}
#[async_trait]
impl CarRepository for PostgresCarRepository {
async fn create(&self, car: BindingCar) -> Result<Car, String> {
let result = self.connection.query(
"INSERT INTO Car(brand, model, price, owner_id, car_station_id) \
VALUES ($1, $2, $3, $4, $5) RETURNING *",
&[&car.brand, &car.model, &car.price, &car.owner_id.parse::<i32>().unwrap(), &car.car_station_id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = result {
let row = rows.get(0).unwrap();
Ok(Car {
id: row.get::<&str, i32>("id").to_string(),
brand: row.get("brand"),
model: row.get("model"),
price: row.get("price"),
owner_id: row.get::<&str, i32>("owner_id").to_string(),
car_station_id: row.get::<&str, i32>("car_station_id").to_string()
})
} else {
Err(result.unwrap_err().to_string())
}
}
async fn read(&self, id: String) -> Result<Car, String> {
let result = self.connection.query(
"SELECT * FROM Car WHERE id = $1", &[&id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = result {
let row = rows.get(0).ok_or("Car not found".to_owned())?;
Ok(Car {
id: row.get::<&str, i32>("id").to_string(),
brand: row.get("brand"),
model: row.get("model"),
price: row.get("price"),
owner_id: row.get::<&str, i32>("owner_id").to_string(),
car_station_id: row.get::<&str, i32>("car_station_id").to_string()
})
} else {
Err(result.unwrap_err().to_string())
}
}
async fn read_all(&self) -> Result<Vec<Car>, String> {
let result = self.connection.query(
"SELECT * FROM Car", &[]
).await;
if let Ok(rows) = result {
Ok(
rows.into_iter().map(|r| Car {
id: r.get::<&str, i32>("id").to_string(),
brand: r.get("brand"),
model: r.get("model"),
price: r.get("price"),
owner_id: r.get::<&str, i32>("owner_id").to_string(),
car_station_id: r.get::<&str, i32>("car_station_id").to_string()
}).collect()
)
} else {
Err(result.unwrap_err().to_string())
}
}
async fn update(&self, id: String, car: BindingCar) -> Result<Car, String> {
let result = self.connection.query(
"UPDATE Car SET brand = $1, model = $2, price = $3, owner_id = $4, car_station_id = $5 WHERE id = $6 RETURNING *",
&[&car.brand, &car.model, &car.price, &car.owner_id.parse::<i32>().unwrap(), &car.car_station_id.parse::<i32>().unwrap(), &id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = &result {
let row = rows.get(0).unwrap();
Ok(Car {
id: row.get::<&str, i32>("id").to_string(),
brand: row.get("brand"),
model: row.get("model"),
price: row.get("price"),
owner_id: row.get::<&str, i32>("owner_id").to_string(),
car_station_id: row.get::<&str, i32>("car_station_id").to_string()
})
} else {
Err(result.unwrap_err().to_string())
}
}
async fn delete(&self, id: String) -> Result<(), String> {
let result = self.connection.execute(
"DELETE FROM Car WHERE id = $1",
&[&id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = result {
if rows == 0 {
Err("Car not found".to_owned())
} else {
Ok(())
}
} else {
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::<&str, i32>("car_id").to_string(),
brand: row.get("brand"),
model: row.get("model"),
times: row.get("times"),
income: row.get("income")
})
.collect()
)
} else {
Err(result.unwrap_err().to_string())
}
}
}

View File

@ -1,96 +0,0 @@
use tokio_postgres::Client as PgClient;
use std::sync::Arc;
use crate::models::car_station::{BindingCarStation, CarStation};
use crate::storages::traits::CarStationRepository;
use async_trait::async_trait;
pub struct PostgresCarStationRepository {
pub connection: Arc<PgClient>
}
#[async_trait]
impl CarStationRepository for PostgresCarStationRepository {
async fn create(&self, car_station: BindingCarStation) -> Result<CarStation, String> {
let result = self.connection.query(
"INSERT INTO Car_Station(address) \
VALUES ($1) RETURNING *",
&[&car_station.address]
).await;
if let Ok(rows) = result {
let row = rows.get(0).unwrap();
Ok(CarStation {
id: row.get::<&str, i32>("id").to_string(),
address: row.get("address")
})
} else {
Err(result.unwrap_err().to_string())
}
}
async fn read(&self, id: String) -> Result<CarStation, String> {
let result = self.connection.query(
"SELECT * FROM Car_Station WHERE id = $1", &[&id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = result {
let row = rows.get(0).ok_or("CarStation not found".to_owned())?;
Ok(CarStation {
id: row.get::<&str, i32>("id").to_string(),
address: row.get("address")
})
} else {
Err(result.unwrap_err().to_string())
}
}
async fn read_all(&self) -> Result<Vec<CarStation>, String> {
let result = self.connection.query(
"SELECT * FROM Car_Station", &[]
).await;
if let Ok(rows) = result {
Ok(
rows.into_iter().map(|r| CarStation {
id: r.get::<&str, i32>("id").to_string(),
address: r.get("address")
}).collect()
)
} else {
Err(result.unwrap_err().to_string())
}
}
async fn update(&self, id: String, car_station: BindingCarStation) -> Result<CarStation, String> {
let result = self.connection.query(
"UPDATE Car_Station SET address = $1 WHERE id = $2 RETURNING *",
&[&car_station.address, &id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = result {
let row = rows.get(0).unwrap();
Ok(CarStation {
id: row.get::<&str, i32>("id").to_string(),
address: row.get("address")
})
} else {
Err(result.unwrap_err().to_string())
}
}
async fn delete(&self, id: String) -> Result<(), String> {
let result = self.connection.execute(
"DELETE FROM Car_Station WHERE id = $1",
&[&id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = result {
if rows == 0 {
Err("CarStation not found".to_owned())
} else {
Ok(())
}
} else {
Err(result.unwrap_err().to_string())
}
}
}

View File

@ -1,111 +0,0 @@
use tokio_postgres::Client as PgClient;
use std::sync::Arc;
use crate::models::client::{BindingClient, Client};
use crate::storages::traits::ClientRepository;
use async_trait::async_trait;
pub struct PostgresClientRepository {
pub connection: Arc<PgClient>
}
#[async_trait]
impl ClientRepository for PostgresClientRepository {
async fn create(&self, client: BindingClient) -> Result<Client, String> {
let result = self.connection.query(
"INSERT INTO Client(name, surname, middlename, phone) \
VALUES ($1, $2, $3, $4) RETURNING *",
&[&client.name, &client.surname, &client.middlename, &client.phone]
).await;
if let Ok(rows) = &result {
let row = rows.get(0).unwrap();
Ok(Client {
id: row.get::<&str, i32>("id").to_string(),
name: row.get("name"),
surname: row.get("surname"),
middlename: row.get("middlename"),
phone: row.get("phone")
})
} else {
Err(result.unwrap_err().to_string())
}
}
async fn read(&self, id: String) -> Result<Client, String> {
let result= self.connection.query(
"SELECT * FROM Client WHERE id = $1", &[&id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = result {
let row = rows.get(0).ok_or("Client not found".to_owned())?;
Ok(Client {
id: row.get::<&str, i32>("id").to_string(),
name: row.get("name"),
surname: row.get("surname"),
middlename: row.get("middlename"),
phone: row.get("phone")
})
} else {
Err(result.unwrap_err().to_string())
}
}
async fn read_all(&self) -> Result<Vec<Client>, String> {
let result = self.connection.query(
"SELECT * FROM Client", &[]
).await;
if let Ok(rows) = result {
Ok(
rows.into_iter().map(|r| Client {
id: r.get::<&str, i32>("id").to_string(),
name: r.get("name"),
surname: r.get("surname"),
middlename: r.get("middlename"),
phone: r.get("phone")
}).collect()
)
} else {
Err(result.unwrap_err().to_string())
}
}
async fn update(&self, id: String, client: BindingClient) -> Result<Client, String> {
let result = self.connection.query(
"UPDATE Client SET name = $1, surname = $2, middlename = $3, phone = $4 \
WHERE id = $5 RETURNING *",
&[&client.name, &client.surname, &client.middlename, &client.phone, &id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = result {
let row = rows.get(0).unwrap();
Ok(Client {
id: row.get::<&str, i32>("id").to_string(),
name: row.get("name"),
surname: row.get("surname"),
middlename: row.get("middlename"),
phone: row.get("phone")
})
} else {
Err(result.unwrap_err().to_string())
}
}
async fn delete(&self, id: String) -> Result<(), String> {
let result = self.connection.execute(
"DELETE FROM Client WHERE id = $1",
&[&id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = result {
if rows == 0 {
Err("Client not found".to_owned())
} else {
Ok(())
}
} else {
Err(result.unwrap_err().to_string())
}
}
}

View File

@ -1,7 +0,0 @@
pub mod client;
pub mod car_station;
pub mod car;
pub mod rent;
pub mod owner;
pub mod administrator;
pub mod benchmark;

View File

@ -1,111 +0,0 @@
use tokio_postgres::Client as PgClient;
use std::sync::Arc;
use crate::models::client::{BindingClient, Client};
use crate::storages::traits::OwnerRepository;
use async_trait::async_trait;
pub struct PostgresOwnerRepository {
pub connection: Arc<PgClient>
}
#[async_trait]
impl OwnerRepository for PostgresOwnerRepository {
async fn create(&self, client: BindingClient) -> Result<Client, String> {
let result = self.connection.query(
"INSERT INTO Owner(name, surname, middlename, phone) \
VALUES ($1, $2, $3, $4) RETURNING *",
&[&client.name, &client.surname, &client.middlename, &client.phone]
).await;
if let Ok(rows) = &result {
let row = rows.get(0).unwrap();
Ok(Client {
id: row.get::<&str, i32>("id").to_string(),
name: row.get("name"),
surname: row.get("surname"),
middlename: row.get("middlename"),
phone: row.get("phone")
})
} else {
Err(result.unwrap_err().to_string())
}
}
async fn read(&self, id: String) -> Result<Client, String> {
let result= self.connection.query(
"SELECT * FROM Owner WHERE id = $1", &[&id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = result {
let row = rows.get(0).ok_or("Owner not found".to_owned())?;
Ok(Client {
id: row.get::<&str, i32>("id").to_string(),
name: row.get("name"),
surname: row.get("surname"),
middlename: row.get("middlename"),
phone: row.get("phone")
})
} else {
Err(result.unwrap_err().to_string())
}
}
async fn read_all(&self) -> Result<Vec<Client>, String> {
let result = self.connection.query(
"SELECT * FROM Owner", &[]
).await;
if let Ok(rows) = result {
Ok(
rows.into_iter().map(|r| Client {
id: r.get::<&str, i32>("id").to_string(),
name: r.get("name"),
surname: r.get("surname"),
middlename: r.get("middlename"),
phone: r.get("phone")
}).collect()
)
} else {
Err(result.unwrap_err().to_string())
}
}
async fn update(&self, id: String, client: BindingClient) -> Result<Client, String> {
let result = self.connection.query(
"UPDATE Owner SET name = $1, surname = $2, middlename = $3, phone = $4 \
WHERE id = $5 RETURNING *",
&[&client.name, &client.surname, &client.middlename, &client.phone, &id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = result {
let row = rows.get(0).unwrap();
Ok(Client {
id: row.get::<&str, i32>("id").to_string(),
name: row.get("name"),
surname: row.get("surname"),
middlename: row.get("middlename"),
phone: row.get("phone")
})
} else {
Err(result.unwrap_err().to_string())
}
}
async fn delete(&self, id: String) -> Result<(), String> {
let result = self.connection.execute(
"DELETE FROM Owner WHERE id = $1",
&[&id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = result {
if rows == 0 {
Err("Owner not found".to_owned())
} else {
Ok(())
}
} else {
Err(result.unwrap_err().to_string())
}
}
}

View File

@ -1,132 +0,0 @@
use tokio_postgres::Client as PgClient;
use std::sync::Arc;
use crate::models::rent::{BindingRent, Rent};
use crate::storages::traits::RentRepository;
use async_trait::async_trait;
pub struct PostgresRentRepository {
pub connection: Arc<PgClient>
}
#[async_trait]
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 NULL",
&[&rent.car_id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = query_result {
if rows.len() > 0 {
return Err("The car is already occupied".to_owned());
}
} else {
return Err(query_result.unwrap_err().to_string());
}
let result = self.connection.query(
"INSERT INTO Rent(time_amount, client_id, car_id) \
VALUES ($1, $2, $3) RETURNING *",
&[&rent.time_amount, &rent.client_id.parse::<i32>().unwrap(), &rent.car_id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = result {
let row = rows.get(0).unwrap();
Ok(Rent {
id: row.get::<&str, i32>("id").to_string(),
start_time: row.get("start_time"),
time_amount: row.get("time_amount"),
client_id: row.get::<&str, i32>("client_id").to_string(),
car_id: row.get::<&str, i32>("car_id").to_string()
})
} else {
Err(result.unwrap_err().to_string())
}
}
async fn read(&self, id: String) -> Result<Rent, String> {
let result = self.connection.query(
"SELECT * FROM Rent WHERE id = $1", &[&id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = result {
let row = rows.get(0).ok_or("Rent not found".to_owned())?;
Ok(Rent {
id: row.get::<&str, i32>("id").to_string(),
start_time: row.get("start_time"),
time_amount: row.get("time_amount"),
client_id: row.get::<&str, i32>("client_id").to_string(),
car_id: row.get::<&str, i32>("car_id").to_string()
})
} else {
Err(result.unwrap_err().to_string())
}
}
async fn read_all(&self) -> Result<Vec<Rent>, String> {
let result = self.connection.query(
"SELECT * FROM Rent", &[]
).await;
if let Ok(rows) = result {
Ok(
rows.into_iter().map(|r| Rent {
id: r.get::<&str, i32>("id").to_string(),
start_time: r.get("start_time"),
time_amount: r.get("time_amount"),
client_id: r.get::<&str, i32>("client_id").to_string(),
car_id: r.get::<&str, i32>("car_id").to_string()
}).collect()
)
} else {
Err(result.unwrap_err().to_string())
}
}
async fn update(&self, id: String, rent: BindingRent) -> Result<Rent, String> {
let result = self.connection.query(
"UPDATE Rent SET time_amount = $2, client_id = $3, car_id = $4 WHERE id = $5 RETURNING *",
&[&rent.time_amount, &rent.client_id.parse::<i32>().unwrap(), &rent.car_id.parse::<i32>().unwrap(), &id.parse::<i32>().unwrap()]
).await;
if let Ok(rows) = result {
let row = rows.get(0).unwrap();
Ok(Rent {
id: row.get::<&str, i32>("id").to_string(),
start_time: row.get("start_time"),
time_amount: row.get("time_amount"),
client_id: row.get::<&str, i32>("client_id").to_string(),
car_id: row.get::<&str, i32>("car_id").to_string()
})
} else {
Err(result.unwrap_err().to_string())
}
}
async fn delete(&self, id: String) -> Result<(), String> {
let result = self.connection.execute(
"DELETE FROM Rent WHERE id = $1", &[&id.parse::<i32>().unwrap()]
).await;
if let Ok(_) = result {
Ok(())
} else {
Err(result.unwrap_err().to_string())
}
}
async fn end_rent(&self, id: String) -> Result<(), String> {
let current_rent = self.read(id.clone()).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.parse::<i32>().unwrap(), &time_amount]).await {
Ok(_) => Ok(()),
Err(err) => Err(err.to_string())
}
}
}

View File

@ -1,64 +0,0 @@
use crate::models::administrator::Administrator;
use crate::models::client::*;
use crate::models::car_station::*;
use crate::models::car::*;
use crate::models::rent::*;
use async_trait::async_trait;
#[async_trait]
pub trait ClientRepository {
async fn create(&self, client: BindingClient) -> Result<Client, String>;
async fn read(&self, id: String) -> Result<Client, String>;
async fn read_all(&self) -> Result<Vec<Client>, String>;
async fn update(&self, id: String, client: BindingClient) -> Result<Client, String>;
async fn delete(&self, id: String) -> Result<(), String>;
}
#[async_trait]
pub trait CarRepository {
async fn create(&self, car: BindingCar) -> Result<Car, String>;
async fn read(&self, id: String) -> Result<Car, String>;
async fn read_all(&self) -> Result<Vec<Car>, String>;
async fn update(&self, id: String, car: BindingCar) -> Result<Car, String>;
async fn delete(&self, id: String) -> Result<(), String>;
async fn get_report(&self) -> Result<Vec<Report>, String>;
}
#[async_trait]
pub trait CarStationRepository {
async fn create(&self, car_station: BindingCarStation) -> Result<CarStation, String>;
async fn read(&self, id: String) -> Result<CarStation, String>;
async fn read_all(&self) -> Result<Vec<CarStation>, String>;
async fn update(&self, id: String, car_station: BindingCarStation) -> Result<CarStation, String>;
async fn delete(&self, id: String) -> Result<(), String>;
}
#[async_trait]
pub trait RentRepository {
async fn create(&self, rent: BindingRent) -> Result<Rent, String>;
async fn read(&self, id: String) -> Result<Rent, String>;
async fn read_all(&self) -> Result<Vec<Rent>, String>;
async fn update(&self, id: String, rent: BindingRent) -> Result<Rent, String>;
async fn delete(&self, id: String) -> Result<(), String>;
async fn end_rent(&self, id: String) -> Result<(), String>;
}
#[async_trait]
pub trait OwnerRepository {
async fn create(&self, client: BindingClient) -> Result<Client, String>;
async fn read(&self, id: String) -> Result<Client, String>;
async fn read_all(&self) -> Result<Vec<Client>, String>;
async fn update(&self, id: String, client: BindingClient) -> Result<Client, String>;
async fn delete(&self, id: String) -> Result<(), String>;
}
#[async_trait]
pub trait AdministratorRepository {
async fn read(&self, id: String) -> 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>;
}

View File

@ -47,7 +47,6 @@ def rents():
if not session['token']:
return redirect(url_for('login'))
a = requests.get('http://localhost:8080/api/rents/', headers={'Authorization': session['token']})
rents_data = requests.get('http://localhost:8080/api/rents/', headers={'Authorization': session['token']}).json()
cars = list(
filter(lambda c: c['car_station_id'] == session['car_station_id'],
@ -60,13 +59,8 @@ def rents():
from datetime import datetime
date_format = '%Y-%m-%dT%H:%M:%S.%f'
date_format_another = '%Y-%m-%dT%H:%M:%S'
try:
parsed_date = datetime.strptime(rent['start_time'], date_format)
except ValueError:
parsed_date = datetime.strptime(rent['start_time'], date_format_another)
rent['start_time'] = parsed_date.strftime('%Y-%m-%d %H:%M:%S')
for car in cars:
@ -90,8 +84,8 @@ def rents():
cars=cars,
clients=clients,
logged_in=True,
selected_client=request.args.get('client') if request.args.get('client') else None,
selected_car=request.args.get('car') if request.args.get('car') else None
selected_client=int(request.args.get('client')) if request.args.get('client') else None,
selected_car=int(request.args.get('car')) if request.args.get('car') else None
)
response = None
@ -100,8 +94,8 @@ def rents():
headers={'Authorization': session['token']})
else:
response = requests.post('http://localhost:8080/api/rents/', headers={'Authorization': session['token']}, json={
"client_id": request.form.get('client_id'),
"car_id": request.form.get('car_id')
"client_id": int(request.form.get('client_id')),
"car_id": int(request.form.get('car_id'))
})
if response.status_code != 200:
@ -113,8 +107,8 @@ def rents():
cars=cars,
clients=clients,
logged_in=True,
selected_client=request.args.get('client') if request.args.get('client') else None,
selected_car=request.args.get('car') if request.args.get('car') else None
selected_client=int(request.args.get('client')) if request.args.get('client') else None,
selected_car=int(request.args.get('car')) if request.args.get('car') else None
)
if request.form.get('id'):
@ -155,12 +149,12 @@ def rents():
cars=cars,
clients=clients,
logged_in=True,
selected_client=request.args.get('client') if request.args.get('client') else None,
selected_car=request.args.get('car') if request.args.get('car') else None
selected_client=int(request.args.get('client')) if request.args.get('client') else None,
selected_car=int(request.args.get('car')) if request.args.get('car') else None
)
@app.route('/rents/<id>', methods=['GET', 'POST'])
@app.route('/rents/<int:id>', methods=['GET', 'POST'])
def rent(id):
if not session['token']:
return redirect(url_for('login'))
@ -184,7 +178,7 @@ def rent(id):
@app.route('/clients', defaults={'id': 0}, methods=['GET', 'POST'])
@app.route('/clients/<id>', methods=['GET', 'POST'])
@app.route('/clients/<int:id>', methods=['GET', 'POST'])
def clients(id):
if not session['token']:
return redirect(url_for('login'))
@ -248,7 +242,7 @@ def clients(id):
@app.route('/owners', defaults={'id': 0}, methods=['GET', 'POST'])
@app.route('/owners/<id>', methods=['GET', 'POST'])
@app.route('/owners/<int:id>', methods=['GET', 'POST'])
def owners(id):
if not session['token']:
return redirect(url_for('login'))
@ -321,7 +315,7 @@ def owners(id):
@app.route('/cars', defaults={'id': 0}, methods=['GET', 'POST'])
@app.route('/cars/<id>', methods=['GET', 'POST'])
@app.route('/cars/<int:id>', methods=['GET', 'POST'])
def cars(id):
if not session['token']:
return redirect(url_for('login'))
@ -344,7 +338,7 @@ def cars(id):
cars=cars,
owners=owners,
logged_in=True,
selected_owner=request.args.get('owner') if request.args.get('owner') else None
selected_owner=int(request.args.get('owner')) if request.args.get('owner') else None
)
response = requests.patch(f'http://localhost:8080/api/cars/{id}', headers={'Authorization': session['token']},
@ -352,7 +346,7 @@ def cars(id):
'brand': request.form.get('brand'),
'model': request.form.get('model'),
'price': float(request.form.get('price')),
'owner_id': request.form.get('owner_id'),
'owner_id': int(request.form.get('owner_id')),
'car_station_id': session['car_station_id']
})
@ -365,14 +359,14 @@ def cars(id):
owners=owners,
current_car=list(filter(lambda c: c['id'] == id, cars))[0],
logged_in=True,
selected_owner=request.args.get('owner') if request.args.get('owner') else None
selected_owner=int(request.args.get('owner')) if request.args.get('owner') else None
)
return redirect(url_for('cars'))
else:
if not request.form.get('brand') or not request.form.get('model') or not request.form.get('price') or not request.form.get('owner_id'):
return render_template('cars.html', title='Автомобили', errors='Должны быть заполнены все поля!',
owners=owners, selected_owner=request.args.get('owner') if request.args.get('owner') else None,
owners=owners, selected_owner=int(request.args.get('owner')) if request.args.get('owner') else None,
cars=cars, logged_in=True)
response = requests.post('http://localhost:8080/api/cars/', headers={'Authorization': session['token']},
@ -380,21 +374,21 @@ def cars(id):
'brand': request.form.get('brand'),
'model': request.form.get('model'),
'price': float(request.form.get('price')),
'owner_id': request.form.get('owner_id'),
'owner_id': int(request.form.get('owner_id')),
'car_station_id': session['car_station_id']
})
if response.status_code != 200:
return render_template('cars.html', title='Автомобили', errors=response.json(), cars=cars,
owners=owners, selected_owner=request.args.get('owner') if request.args.get('owner') else None,
owners=owners, selected_owner=int(request.args.get('owner')) if request.args.get('owner') else None,
logged_in=True)
cars = requests.get('http://localhost:8080/api/cars/', headers={'Authorization': session['token']}).json()
if id != 0:
return render_template('cars.html', title='Изменение автомобиля', cars=cars, logged_in=True, owners=owners, selected_owner=request.args.get('owner') if request.args.get('owner') else None, current_car=list(filter(lambda c: c['id'] == id, cars))[0])
return render_template('cars.html', title='Изменение автомобиля', cars=cars, logged_in=True, owners=owners, selected_owner=int(request.args.get('owner')) if request.args.get('owner') else None, current_car=list(filter(lambda c: c['id'] == id, cars))[0])
return render_template('cars.html', title='Автомобили', cars=cars, logged_in=True, owners=owners, selected_owner=request.args.get('owner') if request.args.get('owner') else None)
return render_template('cars.html', title='Автомобили', cars=cars, logged_in=True, owners=owners, selected_owner=int(request.args.get('owner')) if request.args.get('owner') else None)
@app.route('/report')
@ -426,4 +420,4 @@ def admin():
if __name__ == '__main__':
app.run(host='0.0.0.0', port='5000')
app.run()