diff --git a/backend/src/endpoints/auth.rs b/backend/src/endpoints/auth.rs index 6690521..e629e47 100644 --- a/backend/src/endpoints/auth.rs +++ b/backend/src/endpoints/auth.rs @@ -1,7 +1,7 @@ use std::sync::Mutex; use actix_web::{web, post, Responder, HttpResponse}; use serde_json::json; -use crate::{State, models::administrator}; +use crate::State; #[derive(serde::Deserialize)] pub struct LoginInfo { diff --git a/backend/src/endpoints/benchmark.rs b/backend/src/endpoints/benchmark.rs index 39986a7..401b1aa 100644 --- a/backend/src/endpoints/benchmark.rs +++ b/backend/src/endpoints/benchmark.rs @@ -1,5 +1,5 @@ use std::sync::Mutex; -use actix_web::{web, get, post, patch, delete, Responder, HttpResponse, HttpRequest}; +use actix_web::{web, get, Responder, HttpResponse}; use crate::State; #[get("/benchmark")] diff --git a/backend/src/endpoints/car.rs b/backend/src/endpoints/car.rs index dece7c2..0fad886 100644 --- a/backend/src/endpoints/car.rs +++ b/backend/src/endpoints/car.rs @@ -27,7 +27,7 @@ pub async fn get_cars(state: web::Data>, request: HttpRequest) -> i } #[get("/{id}")] -pub async fn get_car(state: web::Data>, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder { +pub async fn get_car(state: web::Data>, 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"); @@ -73,7 +73,7 @@ pub async fn create_car(state: web::Data>, json: web::Json>, json: web::Json, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder { +pub async fn update_car(state: web::Data>, json: web::Json, 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"); @@ -96,7 +96,7 @@ pub async fn update_car(state: web::Data>, json: web::Json>, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder { +pub async fn delete_car(state: web::Data>, 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"); diff --git a/backend/src/endpoints/car_station.rs b/backend/src/endpoints/car_station.rs index a9caa11..3ef65a4 100644 --- a/backend/src/endpoints/car_station.rs +++ b/backend/src/endpoints/car_station.rs @@ -27,7 +27,7 @@ pub async fn get_car_stations(state: web::Data>, request: HttpReque } #[get("/{id}")] -pub async fn get_car_station(state: web::Data>, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder { +pub async fn get_car_station(state: web::Data>, 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"); @@ -73,7 +73,7 @@ pub async fn create_car_station(state: web::Data>, json: web::Json< } #[patch("/{id}")] -pub async fn update_car_station(state: web::Data>, json: web::Json, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder { +pub async fn update_car_station(state: web::Data>, json: web::Json, 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"); @@ -96,7 +96,7 @@ pub async fn update_car_station(state: web::Data>, json: web::Json< } #[delete("/{id}")] -pub async fn delete_car_station(state: web::Data>, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder { +pub async fn delete_car_station(state: web::Data>, 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"); diff --git a/backend/src/endpoints/client.rs b/backend/src/endpoints/client.rs index d1cc189..f0d3077 100644 --- a/backend/src/endpoints/client.rs +++ b/backend/src/endpoints/client.rs @@ -27,7 +27,7 @@ pub async fn get_clients(state: web::Data>, request: HttpRequest) - } #[get("/{id}")] -pub async fn get_client(state: web::Data>, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder { +pub async fn get_client(state: web::Data>, 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"); @@ -73,7 +73,7 @@ pub async fn create_client(state: web::Data>, json: web::Json>, json: web::Json, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder { +pub async fn update_client(state: web::Data>, json: web::Json, 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"); @@ -96,7 +96,7 @@ pub async fn update_client(state: web::Data>, json: web::Json>, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder { +pub async fn delete_client(state: web::Data>, 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"); diff --git a/backend/src/endpoints/owner.rs b/backend/src/endpoints/owner.rs index 7d70b28..c13227d 100644 --- a/backend/src/endpoints/owner.rs +++ b/backend/src/endpoints/owner.rs @@ -27,7 +27,7 @@ pub async fn get_owners(state: web::Data>, request: HttpRequest) -> } #[get("/{id}")] -pub async fn get_owner(state: web::Data>, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder { +pub async fn get_owner(state: web::Data>, 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"); @@ -73,7 +73,7 @@ pub async fn create_owner(state: web::Data>, json: web::Json>, json: web::Json, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder { +pub async fn update_owner(state: web::Data>, json: web::Json, 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"); @@ -96,7 +96,7 @@ pub async fn update_owner(state: web::Data>, json: web::Json>, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder { +pub async fn delete_owner(state: web::Data>, 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"); diff --git a/backend/src/endpoints/rent.rs b/backend/src/endpoints/rent.rs index d3acf38..d263302 100644 --- a/backend/src/endpoints/rent.rs +++ b/backend/src/endpoints/rent.rs @@ -12,8 +12,8 @@ pub async fn get_rents(state: web::Data>, request: HttpRequest) -> 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"); + 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()); } } @@ -27,7 +27,7 @@ pub async fn get_rents(state: web::Data>, request: HttpRequest) -> } #[get("/{id}")] -pub async fn get_rent(state: web::Data>, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder { +pub async fn get_rent(state: web::Data>, 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"); @@ -73,7 +73,7 @@ pub async fn create_rent(state: web::Data>, json: web::Json>, json: web::Json, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder { +pub async fn update_rent(state: web::Data>, json: web::Json, 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"); @@ -96,7 +96,7 @@ pub async fn update_rent(state: web::Data>, json: web::Json>, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder { +pub async fn delete_rent(state: web::Data>, 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"); @@ -119,7 +119,7 @@ pub async fn delete_rent(state: web::Data>, path: web::Path<(i32, ) } #[get("/{id}/end")] -pub async fn end_rent(state: web::Data>, path: web::Path<(i32, )>, request: HttpRequest) -> impl Responder { +pub async fn end_rent(state: web::Data>, 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"); diff --git a/backend/src/main.rs b/backend/src/main.rs index 181d294..2efa1bb 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -111,7 +111,7 @@ async fn main() -> std::io::Result<()> { .service(logout) ).service(benchmark) }) - .bind("127.0.0.1:8080")? + .bind("0.0.0.0:8080")? .run() .await } \ No newline at end of file diff --git a/backend/src/models/administrator.rs b/backend/src/models/administrator.rs index 3ba74cb..4213a61 100644 --- a/backend/src/models/administrator.rs +++ b/backend/src/models/administrator.rs @@ -2,11 +2,11 @@ use serde::{Serialize, Deserialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Administrator { - pub id: i32, + pub id: String, pub username: String, pub password: String, pub name: String, pub surname: String, pub middlename: String, - pub car_station_id: i32 + pub car_station_id: String } \ No newline at end of file diff --git a/backend/src/models/car.rs b/backend/src/models/car.rs index 8f74de3..1ed9756 100644 --- a/backend/src/models/car.rs +++ b/backend/src/models/car.rs @@ -3,12 +3,12 @@ use serde::{Serialize, Deserialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Car { - pub id: i32, + pub id: String, pub brand: String, pub model: String, pub price: Decimal, - pub owner_id: i32, - pub car_station_id: i32 + pub owner_id: String, + pub car_station_id: String } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -16,13 +16,13 @@ pub struct BindingCar { pub brand: String, pub model: String, pub price: Decimal, - pub owner_id: i32, - pub car_station_id: i32 + pub owner_id: String, + pub car_station_id: String } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Report { - pub car_id: i32, + pub car_id: String, pub brand: String, pub model: String, pub times: i64, diff --git a/backend/src/models/car_station.rs b/backend/src/models/car_station.rs index 1e208a2..28ae5d1 100644 --- a/backend/src/models/car_station.rs +++ b/backend/src/models/car_station.rs @@ -2,7 +2,7 @@ use serde::{Serialize, Deserialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct CarStation { - pub id: i32, + pub id: String, pub address: String } diff --git a/backend/src/models/client.rs b/backend/src/models/client.rs index 0e0f7a7..41786a4 100644 --- a/backend/src/models/client.rs +++ b/backend/src/models/client.rs @@ -2,7 +2,7 @@ use serde::{Serialize, Deserialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Client { - pub id: i32, + pub id: String, pub name: String, pub surname: String, pub middlename: Option, diff --git a/backend/src/models/rent.rs b/backend/src/models/rent.rs index 1566902..9da3e8a 100644 --- a/backend/src/models/rent.rs +++ b/backend/src/models/rent.rs @@ -2,16 +2,16 @@ use serde::{Serialize, Deserialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Rent { - pub id: i32, + pub id: String, pub start_time: chrono::NaiveDateTime, pub time_amount: Option, - pub client_id: i32, - pub car_id: i32 + pub client_id: String, + pub car_id: String } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct BindingRent { pub time_amount: Option, - pub client_id: i32, - pub car_id: i32 + pub client_id: String, + pub car_id: String } \ No newline at end of file diff --git a/backend/src/services/auth.rs b/backend/src/services/auth.rs index 184e818..79d3280 100644 --- a/backend/src/services/auth.rs +++ b/backend/src/services/auth.rs @@ -4,7 +4,7 @@ use sha2::Sha256; use std::cell::RefCell; pub struct AuthService { - authed: RefCell> + authed: RefCell> } impl AuthService { @@ -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, i32, i32), String> { + 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::(password.as_bytes(), dotenv_codegen::dotenv!("SALT").as_bytes(), 4096).iter().map(|x| format!("{:x}", x)).collect::() != 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, administrator.id, administrator.car_station_id)) + self.authed.borrow_mut().push((token.clone(), administrator.id.clone())); + Ok((token, administrator.id.clone(), administrator.car_station_id)) } } @@ -28,7 +28,7 @@ impl AuthService { } pub async fn administrator_by_token(&self, storage: &dyn AdministratorRepository, token: &str) -> Result { - let id = self.authed.borrow().iter().find(|&x| x.0.starts_with(token)).ok_or("Invalid token")?.1; + let id = self.authed.borrow().iter().find(|&x| x.0.starts_with(token)).ok_or("Invalid token")?.1.clone(); storage.read(id).await } } \ No newline at end of file diff --git a/backend/src/storages/mod.rs b/backend/src/storages/mod.rs index 4acaacf..a60a3bc 100644 --- a/backend/src/storages/mod.rs +++ b/backend/src/storages/mod.rs @@ -1,2 +1,3 @@ pub mod traits; -pub mod postgres; \ No newline at end of file +pub mod postgres; +pub mod mongo; \ No newline at end of file diff --git a/backend/src/storages/mongo/mod.rs b/backend/src/storages/mongo/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/backend/src/storages/postgres/administrator.rs b/backend/src/storages/postgres/administrator.rs index 312adda..1603fad 100644 --- a/backend/src/storages/postgres/administrator.rs +++ b/backend/src/storages/postgres/administrator.rs @@ -10,21 +10,21 @@ pub struct PostgresAdministratorRepository { #[async_trait] impl AdministratorRepository for PostgresAdministratorRepository { - async fn read(&self, id: i32) -> Result { + async fn read(&self, id: String) -> Result { let result = self.connection.query( - "SELECT * FROM administrator WHERE id = $1", &[&id] + "SELECT * FROM administrator WHERE id = $1", &[&id.parse::().unwrap()] ).await; if let Ok(rows) = result { let row = rows.get(0).ok_or("Administrator not found".to_owned())?; Ok(Administrator { - id: row.get("id"), + 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("car_station_id") + car_station_id: row.get::<&str, i32>("car_station_id").to_string() }) } else { Err(result.unwrap_err().to_string()) @@ -37,13 +37,13 @@ impl AdministratorRepository for PostgresAdministratorRepository { if let Ok(rows) = result { let row = rows.get(0).ok_or("Administrator not found".to_owned())?; Ok(Administrator { - id: row.get("id"), + 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("car_station_id") + car_station_id: row.get::<&str, i32>("car_station_id").to_string() }) } else { Err(result.unwrap_err().to_string()) diff --git a/backend/src/storages/postgres/benchmark.rs b/backend/src/storages/postgres/benchmark.rs index 1143190..59fa652 100644 --- a/backend/src/storages/postgres/benchmark.rs +++ b/backend/src/storages/postgres/benchmark.rs @@ -17,7 +17,7 @@ impl BenchmarkRepository for PostgresBenchmarkRepository { let start_time = chrono::Utc::now().naive_local(); - for n in 1..201 { + 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, @@ -36,7 +36,7 @@ impl BenchmarkRepository for PostgresBenchmarkRepository { ).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( + let _: i32 = self.connection.query( &str, &[] ).await.unwrap().get(0).unwrap().get(0); diff --git a/backend/src/storages/postgres/car.rs b/backend/src/storages/postgres/car.rs index 0fb80b8..19714f1 100644 --- a/backend/src/storages/postgres/car.rs +++ b/backend/src/storages/postgres/car.rs @@ -14,38 +14,38 @@ impl CarRepository for PostgresCarRepository { 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, &car.car_station_id] + &[&car.brand, &car.model, &car.price, &car.owner_id.parse::().unwrap(), &car.car_station_id.parse::().unwrap()] ).await; if let Ok(rows) = result { let row = rows.get(0).unwrap(); Ok(Car { - id: row.get("id"), + id: row.get::<&str, i32>("id").to_string(), brand: row.get("brand"), model: row.get("model"), price: row.get("price"), - owner_id: row.get("owner_id"), - car_station_id: row.get("car_station_id") + 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: i32) -> Result { + async fn read(&self, id: String) -> Result { let result = self.connection.query( - "SELECT * FROM Car WHERE id = $1", &[&id] + "SELECT * FROM Car WHERE id = $1", &[&id.parse::().unwrap()] ).await; if let Ok(rows) = result { let row = rows.get(0).ok_or("Car not found".to_owned())?; Ok(Car { - id: row.get("id"), + id: row.get::<&str, i32>("id").to_string(), brand: row.get("brand"), model: row.get("model"), price: row.get("price"), - owner_id: row.get("owner_id"), - car_station_id: row.get("car_station_id") + 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()) @@ -60,12 +60,12 @@ impl CarRepository for PostgresCarRepository { if let Ok(rows) = result { Ok( rows.into_iter().map(|r| Car { - id: r.get("id"), + id: r.get::<&str, i32>("id").to_string(), brand: r.get("brand"), model: r.get("model"), price: r.get("price"), - owner_id: r.get("owner_id"), - car_station_id: r.get("car_station_id") + owner_id: r.get::<&str, i32>("owner_id").to_string(), + car_station_id: r.get::<&str, i32>("car_station_id").to_string() }).collect() ) } else { @@ -73,31 +73,31 @@ impl CarRepository for PostgresCarRepository { } } - async fn update(&self, id: i32, car: BindingCar) -> Result { + async fn update(&self, id: String, car: BindingCar) -> Result { 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, &car.car_station_id, &id] + &[&car.brand, &car.model, &car.price, &car.owner_id.parse::().unwrap(), &car.car_station_id.parse::().unwrap(), &id.parse::().unwrap()] ).await; if let Ok(rows) = &result { let row = rows.get(0).unwrap(); Ok(Car { - id: row.get("id"), + id: row.get::<&str, i32>("id").to_string(), brand: row.get("brand"), model: row.get("model"), price: row.get("price"), - owner_id: row.get("owner_id"), - car_station_id: row.get("car_station_id") + 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: i32) -> Result<(), String> { + async fn delete(&self, id: String) -> Result<(), String> { let result = self.connection.execute( "DELETE FROM Car WHERE id = $1", - &[&id] + &[&id.parse::().unwrap()] ).await; if let Ok(rows) = result { @@ -129,7 +129,7 @@ impl CarRepository for PostgresCarRepository { Ok( rows.into_iter() .map(|row| Report { - car_id: row.get("car_id"), + car_id: row.get::<&str, i32>("car_id").to_string(), brand: row.get("brand"), model: row.get("model"), times: row.get("times"), diff --git a/backend/src/storages/postgres/car_station.rs b/backend/src/storages/postgres/car_station.rs index 1fb7805..8b3d6c7 100644 --- a/backend/src/storages/postgres/car_station.rs +++ b/backend/src/storages/postgres/car_station.rs @@ -19,7 +19,7 @@ impl CarStationRepository for PostgresCarStationRepository { if let Ok(rows) = result { let row = rows.get(0).unwrap(); Ok(CarStation { - id: row.get("id"), + id: row.get::<&str, i32>("id").to_string(), address: row.get("address") }) } else { @@ -27,15 +27,15 @@ impl CarStationRepository for PostgresCarStationRepository { } } - async fn read(&self, id: i32) -> Result { + async fn read(&self, id: String) -> Result { let result = self.connection.query( - "SELECT * FROM Car_Station WHERE id = $1", &[&id] + "SELECT * FROM Car_Station WHERE id = $1", &[&id.parse::().unwrap()] ).await; if let Ok(rows) = result { let row = rows.get(0).ok_or("CarStation not found".to_owned())?; Ok(CarStation { - id: row.get("id"), + id: row.get::<&str, i32>("id").to_string(), address: row.get("address") }) } else { @@ -51,7 +51,7 @@ impl CarStationRepository for PostgresCarStationRepository { if let Ok(rows) = result { Ok( rows.into_iter().map(|r| CarStation { - id: r.get("id"), + id: r.get::<&str, i32>("id").to_string(), address: r.get("address") }).collect() ) @@ -60,16 +60,16 @@ impl CarStationRepository for PostgresCarStationRepository { } } - async fn update(&self, id: i32, car_station: BindingCarStation) -> Result { + async fn update(&self, id: String, car_station: BindingCarStation) -> Result { let result = self.connection.query( "UPDATE Car_Station SET address = $1 WHERE id = $2 RETURNING *", - &[&car_station.address, &id] + &[&car_station.address, &id.parse::().unwrap()] ).await; if let Ok(rows) = result { let row = rows.get(0).unwrap(); Ok(CarStation { - id: row.get("id"), + id: row.get::<&str, i32>("id").to_string(), address: row.get("address") }) } else { @@ -77,10 +77,10 @@ impl CarStationRepository for PostgresCarStationRepository { } } - async fn delete(&self, id: i32) -> Result<(), String> { + async fn delete(&self, id: String) -> Result<(), String> { let result = self.connection.execute( "DELETE FROM Car_Station WHERE id = $1", - &[&id] + &[&id.parse::().unwrap()] ).await; if let Ok(rows) = result { diff --git a/backend/src/storages/postgres/client.rs b/backend/src/storages/postgres/client.rs index 6ad6b23..f3d5aba 100644 --- a/backend/src/storages/postgres/client.rs +++ b/backend/src/storages/postgres/client.rs @@ -20,7 +20,7 @@ impl ClientRepository for PostgresClientRepository { if let Ok(rows) = &result { let row = rows.get(0).unwrap(); Ok(Client { - id: row.get("id"), + id: row.get::<&str, i32>("id").to_string(), name: row.get("name"), surname: row.get("surname"), middlename: row.get("middlename"), @@ -31,16 +31,16 @@ impl ClientRepository for PostgresClientRepository { } } - async fn read(&self, id: i32) -> Result { + async fn read(&self, id: String) -> Result { let result= self.connection.query( - "SELECT * FROM Client WHERE id = $1", &[&id] + "SELECT * FROM Client WHERE id = $1", &[&id.parse::().unwrap()] ).await; if let Ok(rows) = result { let row = rows.get(0).ok_or("Client not found".to_owned())?; Ok(Client { - id: row.get("id"), + id: row.get::<&str, i32>("id").to_string(), name: row.get("name"), surname: row.get("surname"), middlename: row.get("middlename"), @@ -59,7 +59,7 @@ impl ClientRepository for PostgresClientRepository { if let Ok(rows) = result { Ok( rows.into_iter().map(|r| Client { - id: r.get("id"), + id: r.get::<&str, i32>("id").to_string(), name: r.get("name"), surname: r.get("surname"), middlename: r.get("middlename"), @@ -71,17 +71,17 @@ impl ClientRepository for PostgresClientRepository { } } - async fn update(&self, id: i32, client: BindingClient) -> Result { + async fn update(&self, id: String, client: BindingClient) -> Result { 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] + &[&client.name, &client.surname, &client.middlename, &client.phone, &id.parse::().unwrap()] ).await; if let Ok(rows) = result { let row = rows.get(0).unwrap(); Ok(Client { - id: row.get("id"), + id: row.get::<&str, i32>("id").to_string(), name: row.get("name"), surname: row.get("surname"), middlename: row.get("middlename"), @@ -92,10 +92,10 @@ impl ClientRepository for PostgresClientRepository { } } - async fn delete(&self, id: i32) -> Result<(), String> { + async fn delete(&self, id: String) -> Result<(), String> { let result = self.connection.execute( "DELETE FROM Client WHERE id = $1", - &[&id] + &[&id.parse::().unwrap()] ).await; if let Ok(rows) = result { diff --git a/backend/src/storages/postgres/owner.rs b/backend/src/storages/postgres/owner.rs index 6ae5f1c..504c851 100644 --- a/backend/src/storages/postgres/owner.rs +++ b/backend/src/storages/postgres/owner.rs @@ -20,7 +20,7 @@ impl OwnerRepository for PostgresOwnerRepository { if let Ok(rows) = &result { let row = rows.get(0).unwrap(); Ok(Client { - id: row.get("id"), + id: row.get::<&str, i32>("id").to_string(), name: row.get("name"), surname: row.get("surname"), middlename: row.get("middlename"), @@ -31,16 +31,16 @@ impl OwnerRepository for PostgresOwnerRepository { } } - async fn read(&self, id: i32) -> Result { + async fn read(&self, id: String) -> Result { let result= self.connection.query( - "SELECT * FROM Owner WHERE id = $1", &[&id] + "SELECT * FROM Owner WHERE id = $1", &[&id.parse::().unwrap()] ).await; if let Ok(rows) = result { let row = rows.get(0).ok_or("Owner not found".to_owned())?; Ok(Client { - id: row.get("id"), + id: row.get::<&str, i32>("id").to_string(), name: row.get("name"), surname: row.get("surname"), middlename: row.get("middlename"), @@ -59,7 +59,7 @@ impl OwnerRepository for PostgresOwnerRepository { if let Ok(rows) = result { Ok( rows.into_iter().map(|r| Client { - id: r.get("id"), + id: r.get::<&str, i32>("id").to_string(), name: r.get("name"), surname: r.get("surname"), middlename: r.get("middlename"), @@ -71,17 +71,17 @@ impl OwnerRepository for PostgresOwnerRepository { } } - async fn update(&self, id: i32, client: BindingClient) -> Result { + async fn update(&self, id: String, client: BindingClient) -> Result { 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] + &[&client.name, &client.surname, &client.middlename, &client.phone, &id.parse::().unwrap()] ).await; if let Ok(rows) = result { let row = rows.get(0).unwrap(); Ok(Client { - id: row.get("id"), + id: row.get::<&str, i32>("id").to_string(), name: row.get("name"), surname: row.get("surname"), middlename: row.get("middlename"), @@ -92,10 +92,10 @@ impl OwnerRepository for PostgresOwnerRepository { } } - async fn delete(&self, id: i32) -> Result<(), String> { + async fn delete(&self, id: String) -> Result<(), String> { let result = self.connection.execute( "DELETE FROM Owner WHERE id = $1", - &[&id] + &[&id.parse::().unwrap()] ).await; if let Ok(rows) = result { diff --git a/backend/src/storages/postgres/rent.rs b/backend/src/storages/postgres/rent.rs index 84ed6a9..b498bcb 100644 --- a/backend/src/storages/postgres/rent.rs +++ b/backend/src/storages/postgres/rent.rs @@ -13,7 +13,7 @@ impl RentRepository for PostgresRentRepository { async fn create(&self, rent: BindingRent) -> Result { let query_result = self.connection.query( "SELECT * FROM Rent WHERE car_id=$1 AND time_amount IS NULL", - &[&rent.car_id] + &[&rent.car_id.parse::().unwrap()] ).await; if let Ok(rows) = query_result { @@ -27,36 +27,36 @@ impl RentRepository for PostgresRentRepository { 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, &rent.car_id] + &[&rent.time_amount, &rent.client_id.parse::().unwrap(), &rent.car_id.parse::().unwrap()] ).await; if let Ok(rows) = result { let row = rows.get(0).unwrap(); Ok(Rent { - id: row.get("id"), + id: row.get::<&str, i32>("id").to_string(), start_time: row.get("start_time"), time_amount: row.get("time_amount"), - client_id: row.get("client_id"), - car_id: row.get("car_id") + 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: i32) -> Result { + async fn read(&self, id: String) -> Result { let result = self.connection.query( - "SELECT * FROM Rent WHERE id = $1", &[&id] + "SELECT * FROM Rent WHERE id = $1", &[&id.parse::().unwrap()] ).await; if let Ok(rows) = result { let row = rows.get(0).ok_or("Rent not found".to_owned())?; Ok(Rent { - id: row.get("id"), + id: row.get::<&str, i32>("id").to_string(), start_time: row.get("start_time"), time_amount: row.get("time_amount"), - client_id: row.get("client_id"), - car_id: row.get("car_id") + 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()) @@ -71,11 +71,11 @@ impl RentRepository for PostgresRentRepository { if let Ok(rows) = result { Ok( rows.into_iter().map(|r| Rent { - id: r.get("id"), + id: r.get::<&str, i32>("id").to_string(), start_time: r.get("start_time"), time_amount: r.get("time_amount"), - client_id: r.get("client_id"), - car_id: r.get("car_id") + client_id: r.get::<&str, i32>("client_id").to_string(), + car_id: r.get::<&str, i32>("car_id").to_string() }).collect() ) } else { @@ -83,29 +83,29 @@ impl RentRepository for PostgresRentRepository { } } - async fn update(&self, id: i32, rent: BindingRent) -> Result { + async fn update(&self, id: String, rent: BindingRent) -> Result { 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, &rent.car_id, &id] + &[&rent.time_amount, &rent.client_id.parse::().unwrap(), &rent.car_id.parse::().unwrap(), &id.parse::().unwrap()] ).await; if let Ok(rows) = result { let row = rows.get(0).unwrap(); Ok(Rent { - id: row.get("id"), + id: row.get::<&str, i32>("id").to_string(), start_time: row.get("start_time"), time_amount: row.get("time_amount"), - client_id: row.get("client_id"), - car_id: row.get("car_id") + 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: i32) -> Result<(), String> { + async fn delete(&self, id: String) -> Result<(), String> { let result = self.connection.execute( - "DELETE FROM Rent WHERE id = $1", &[&id] + "DELETE FROM Rent WHERE id = $1", &[&id.parse::().unwrap()] ).await; if let Ok(_) = result { @@ -115,8 +115,8 @@ impl RentRepository for PostgresRentRepository { } } - async fn end_rent(&self, id: i32) -> Result<(), String> { - let current_rent = self.read(id).await?; + 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()); @@ -124,7 +124,7 @@ impl RentRepository for PostgresRentRepository { let time_amount = (self.connection.query("SELECT now()::timestamp", &[]).await.unwrap().get(0).unwrap().get::(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 { + match self.connection.execute("UPDATE Rent SET time_amount = $2 WHERE id = $1", &[&id.parse::().unwrap(), &time_amount]).await { Ok(_) => Ok(()), Err(err) => Err(err.to_string()) } diff --git a/backend/src/storages/traits.rs b/backend/src/storages/traits.rs index bf03e53..865f32f 100644 --- a/backend/src/storages/traits.rs +++ b/backend/src/storages/traits.rs @@ -8,53 +8,53 @@ use async_trait::async_trait; #[async_trait] pub trait ClientRepository { async fn create(&self, client: BindingClient) -> Result; - async fn read(&self, id: i32) -> Result; + async fn read(&self, id: String) -> Result; async fn read_all(&self) -> Result, String>; - async fn update(&self, id: i32, client: BindingClient) -> Result; - async fn delete(&self, id: i32) -> Result<(), String>; + async fn update(&self, id: String, client: BindingClient) -> Result; + async fn delete(&self, id: String) -> Result<(), String>; } #[async_trait] pub trait CarRepository { async fn create(&self, car: BindingCar) -> Result; - async fn read(&self, id: i32) -> Result; + async fn read(&self, id: String) -> Result; async fn read_all(&self) -> Result, String>; - async fn update(&self, id: i32, car: BindingCar) -> Result; - async fn delete(&self, id: i32) -> Result<(), String>; + async fn update(&self, id: String, car: BindingCar) -> Result; + async fn delete(&self, id: String) -> Result<(), String>; async fn get_report(&self) -> Result, String>; } #[async_trait] pub trait CarStationRepository { async fn create(&self, car_station: BindingCarStation) -> Result; - async fn read(&self, id: i32) -> Result; + async fn read(&self, id: String) -> Result; async fn read_all(&self) -> Result, String>; - async fn update(&self, id: i32, car_station: BindingCarStation) -> Result; - async fn delete(&self, id: i32) -> Result<(), String>; + async fn update(&self, id: String, car_station: BindingCarStation) -> Result; + async fn delete(&self, id: String) -> Result<(), String>; } #[async_trait] pub trait RentRepository { async fn create(&self, rent: BindingRent) -> Result; - async fn read(&self, id: i32) -> Result; + async fn read(&self, id: String) -> Result; async fn read_all(&self) -> Result, String>; - async fn update(&self, id: i32, rent: BindingRent) -> Result; - async fn delete(&self, id: i32) -> Result<(), String>; - async fn end_rent(&self, id: i32) -> Result<(), String>; + async fn update(&self, id: String, rent: BindingRent) -> Result; + 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; - async fn read(&self, id: i32) -> Result; + async fn read(&self, id: String) -> Result; async fn read_all(&self) -> Result, String>; - async fn update(&self, id: i32, client: BindingClient) -> Result; - async fn delete(&self, id: i32) -> Result<(), String>; + async fn update(&self, id: String, client: BindingClient) -> Result; + async fn delete(&self, id: String) -> Result<(), String>; } #[async_trait] pub trait AdministratorRepository { - async fn read(&self, id: i32) -> Result; + async fn read(&self, id: String) -> Result; async fn find_by_username(&self, username: &str) -> Result; } diff --git a/frontend/app.py b/frontend/app.py index 72e406e..4dbc1d0 100644 --- a/frontend/app.py +++ b/frontend/app.py @@ -420,4 +420,4 @@ def admin(): if __name__ == '__main__': - app.run() + app.run(host='0.0.0.0', port='5000')