Бекэнд: вроде как готов, требуется рефакторинг
This commit is contained in:
parent
e56396d2c3
commit
6a05f40245
11
.gitignore
vendored
11
.gitignore
vendored
@ -1,16 +1,13 @@
|
|||||||
# ---> Rust
|
# ---> Rust
|
||||||
# Generated by Cargo
|
# Generated by Cargo
|
||||||
# will have compiled files and executables
|
# will have compiled files and executables
|
||||||
debug/
|
backend/debug/
|
||||||
target/
|
backend/target/
|
||||||
|
backend/.env
|
||||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
|
||||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
|
||||||
Cargo.lock
|
|
||||||
|
|
||||||
# These are backup files generated by rustfmt
|
# These are backup files generated by rustfmt
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|
||||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||||
*.pdb
|
backend/*.pdb
|
||||||
|
|
||||||
|
1696
backend/Cargo.lock
generated
Normal file
1696
backend/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
17
backend/Cargo.toml
Normal file
17
backend/Cargo.toml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
[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"
|
29
backend/src/endpoints/car.rs
Normal file
29
backend/src/endpoints/car.rs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
use std::sync::Mutex;
|
||||||
|
use crate::models::car::*;
|
||||||
|
use actix_web::{web, get, post, patch, delete, Responder, HttpResponse};
|
||||||
|
use crate::State;
|
||||||
|
|
||||||
|
#[get("/")]
|
||||||
|
pub async fn get_cars(state: web::Data<Mutex<State>>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().car_repository.read_all().await.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/{id}")]
|
||||||
|
pub async fn get_car(state: web::Data<Mutex<State>>, path: web::Path<(u32, )>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().car_repository.read(path.into_inner().0).await.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/")]
|
||||||
|
pub async fn create_car(state: web::Data<Mutex<State>>, json: web::Json<BindingCar>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().car_repository.create(json.0).await.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[patch("/{id}")]
|
||||||
|
pub async fn update_car(state: web::Data<Mutex<State>>, json: web::Json<BindingCar>, path: web::Path<(u32, )>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().car_repository.update(path.into_inner().0, json.0).await.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[delete("/{id}")]
|
||||||
|
pub async fn delete_car(state: web::Data<Mutex<State>>, path: web::Path<(u32, )>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().car_repository.delete(path.into_inner().0).await.unwrap())
|
||||||
|
}
|
29
backend/src/endpoints/car_station.rs
Normal file
29
backend/src/endpoints/car_station.rs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
use std::sync::Mutex;
|
||||||
|
use crate::models::car_station::*;
|
||||||
|
use actix_web::{web, get, post, patch, delete, Responder, HttpResponse};
|
||||||
|
use crate::State;
|
||||||
|
|
||||||
|
#[get("/")]
|
||||||
|
pub async fn get_car_stations(state: web::Data<Mutex<State>>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().car_station_repository.read_all().await.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/{id}")]
|
||||||
|
pub async fn get_car_station(state: web::Data<Mutex<State>>, path: web::Path<(u32, )>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().car_station_repository.read(path.into_inner().0).await.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/")]
|
||||||
|
pub async fn create_car_station(state: web::Data<Mutex<State>>, json: web::Json<BindingCarStation>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().car_station_repository.create(json.0).await.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[patch("/{id}")]
|
||||||
|
pub async fn update_car_station(state: web::Data<Mutex<State>>, json: web::Json<BindingCarStation>, path: web::Path<(u32, )>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().car_station_repository.update(path.into_inner().0, json.0).await.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[delete("/{id}")]
|
||||||
|
pub async fn delete_car_station(state: web::Data<Mutex<State>>, path: web::Path<(u32, )>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().car_station_repository.delete(path.into_inner().0).await.unwrap())
|
||||||
|
}
|
29
backend/src/endpoints/client.rs
Normal file
29
backend/src/endpoints/client.rs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
use std::sync::Mutex;
|
||||||
|
use crate::models::client::*;
|
||||||
|
use actix_web::{web, get, post, patch, delete, Responder, HttpResponse};
|
||||||
|
use crate::State;
|
||||||
|
|
||||||
|
#[get("/")]
|
||||||
|
pub async fn get_clients(state: web::Data<Mutex<State>>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().client_repository.read_all().await.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/{id}")]
|
||||||
|
pub async fn get_client(state: web::Data<Mutex<State>>, path: web::Path<(u32, )>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().client_repository.read(path.into_inner().0).await.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/")]
|
||||||
|
pub async fn create_client(state: web::Data<Mutex<State>>, json: web::Json<BindingClient>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().client_repository.create(json.0).await.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[patch("/{id}")]
|
||||||
|
pub async fn update_client(state: web::Data<Mutex<State>>, json: web::Json<BindingClient>, path: web::Path<(u32, )>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().client_repository.update(path.into_inner().0, json.0).await.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[delete("/{id}")]
|
||||||
|
pub async fn delete_client(state: web::Data<Mutex<State>>, path: web::Path<(u32, )>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().client_repository.delete(path.into_inner().0).await.unwrap())
|
||||||
|
}
|
4
backend/src/endpoints/mod.rs
Normal file
4
backend/src/endpoints/mod.rs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
pub mod client;
|
||||||
|
pub mod car_station;
|
||||||
|
pub mod car;
|
||||||
|
pub mod rent;
|
29
backend/src/endpoints/rent.rs
Normal file
29
backend/src/endpoints/rent.rs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
use std::sync::Mutex;
|
||||||
|
use crate::models::rent::*;
|
||||||
|
use actix_web::{web, get, post, patch, delete, Responder, HttpResponse};
|
||||||
|
use crate::State;
|
||||||
|
|
||||||
|
#[get("/")]
|
||||||
|
pub async fn get_rents(state: web::Data<Mutex<State>>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().rent_repository.read_all().await.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/{id}")]
|
||||||
|
pub async fn get_rent(state: web::Data<Mutex<State>>, path: web::Path<(u32, )>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().rent_repository.read(path.into_inner().0).await.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/")]
|
||||||
|
pub async fn create_rent(state: web::Data<Mutex<State>>, json: web::Json<BindingRent>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().rent_repository.create(json.0).await.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[patch("/{id}")]
|
||||||
|
pub async fn update_rent(state: web::Data<Mutex<State>>, json: web::Json<BindingRent>, path: web::Path<(u32, )>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().rent_repository.update(path.into_inner().0, json.0).await.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[delete("/{id}")]
|
||||||
|
pub async fn delete_rent(state: web::Data<Mutex<State>>, path: web::Path<(u32, )>) -> impl Responder {
|
||||||
|
HttpResponse::Ok().json(state.lock().unwrap().rent_repository.delete(path.into_inner().0).await.unwrap())
|
||||||
|
}
|
12
backend/src/lib.rs
Normal file
12
backend/src/lib.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
use crate::storages::traits::{CarRepository, CarStationRepository, ClientRepository, RentRepository};
|
||||||
|
|
||||||
|
pub mod endpoints;
|
||||||
|
pub mod models;
|
||||||
|
pub mod storages;
|
||||||
|
|
||||||
|
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 rent_repository: Box<dyn RentRepository + Send>
|
||||||
|
}
|
85
backend/src/main.rs
Normal file
85
backend/src/main.rs
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
use actix_web::{App, HttpServer, web};
|
||||||
|
use actix_web::web::Data;
|
||||||
|
use dotenv_codegen::dotenv;
|
||||||
|
use backend::State;
|
||||||
|
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::endpoints::car_station::*;
|
||||||
|
use backend::endpoints::car::*;
|
||||||
|
use backend::endpoints::client::*;
|
||||||
|
use backend::endpoints::rent::*;
|
||||||
|
|
||||||
|
#[actix_web::main]
|
||||||
|
async fn main() -> std::io::Result<()> {
|
||||||
|
let (client, connection) = tokio_postgres::connect(
|
||||||
|
&format!("host={} user={} password={} dbname={}", dotenv!("HOST"), dotenv!("USER"), dotenv!("PASSWORD"), dotenv!("DBNAME")),
|
||||||
|
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 rent_repository = PostgresRentRepository { connection: Arc::clone(&client) };
|
||||||
|
|
||||||
|
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),
|
||||||
|
rent_repository: Box::new(rent_repository)
|
||||||
|
}));
|
||||||
|
|
||||||
|
HttpServer::new(move || {
|
||||||
|
App::new()
|
||||||
|
.app_data(Data::clone(&state))
|
||||||
|
.service(
|
||||||
|
web::scope("/cars")
|
||||||
|
.service(get_cars)
|
||||||
|
.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("/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)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.bind("127.0.0.1:8080")?
|
||||||
|
.run()
|
||||||
|
.await
|
||||||
|
}
|
20
backend/src/models/car.rs
Normal file
20
backend/src/models/car.rs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct Car {
|
||||||
|
pub id: i32,
|
||||||
|
pub brand: String,
|
||||||
|
pub model: String,
|
||||||
|
pub price: f64,
|
||||||
|
pub owner_id: i32,
|
||||||
|
pub car_station_id: i32
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct BindingCar {
|
||||||
|
pub brand: String,
|
||||||
|
pub model: String,
|
||||||
|
pub price: f64,
|
||||||
|
pub owner_id: u32,
|
||||||
|
pub car_station_id: u32
|
||||||
|
}
|
12
backend/src/models/car_station.rs
Normal file
12
backend/src/models/car_station.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct CarStation {
|
||||||
|
pub id: i32,
|
||||||
|
pub address: String
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct BindingCarStation {
|
||||||
|
pub address: String
|
||||||
|
}
|
18
backend/src/models/client.rs
Normal file
18
backend/src/models/client.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct Client {
|
||||||
|
pub id: i32,
|
||||||
|
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
|
||||||
|
}
|
4
backend/src/models/mod.rs
Normal file
4
backend/src/models/mod.rs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
pub mod client;
|
||||||
|
pub mod car_station;
|
||||||
|
pub mod car;
|
||||||
|
pub mod rent;
|
18
backend/src/models/rent.rs
Normal file
18
backend/src/models/rent.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct Rent {
|
||||||
|
pub id: i32,
|
||||||
|
pub start_time: chrono::NaiveTime,
|
||||||
|
pub time_amount: Option<i32>,
|
||||||
|
pub client_id: i32,
|
||||||
|
pub car_id: i32
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct BindingRent {
|
||||||
|
pub start_time: chrono::NaiveDateTime,
|
||||||
|
pub time_amount: Option<u32>,
|
||||||
|
pub client_id: u32,
|
||||||
|
pub car_id: u32
|
||||||
|
}
|
2
backend/src/storages/mod.rs
Normal file
2
backend/src/storages/mod.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub mod traits;
|
||||||
|
pub mod postgres;
|
113
backend/src/storages/postgres/car.rs
Normal file
113
backend/src/storages/postgres/car.rs
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
use tokio_postgres::Client as PgClient;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use crate::models::car::{BindingCar, Car};
|
||||||
|
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, &car.car_station_id]
|
||||||
|
).await;
|
||||||
|
|
||||||
|
if let Ok(rows) = result {
|
||||||
|
let row = rows.get(0).unwrap();
|
||||||
|
Ok(Car {
|
||||||
|
id: row.get("id"),
|
||||||
|
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")
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during creation of Car".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read(&self, id: u32) -> Result<Car, String> {
|
||||||
|
let result = self.connection.query(
|
||||||
|
"SELECT * FROM Car WHERE id = $1", &[&id]
|
||||||
|
).await;
|
||||||
|
|
||||||
|
if let Ok(rows) = result {
|
||||||
|
let row = rows.get(0).ok_or("Car not found".to_owned())?;
|
||||||
|
Ok(Car {
|
||||||
|
id: row.get("id"),
|
||||||
|
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")
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during reading of Car".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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("id"),
|
||||||
|
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")
|
||||||
|
}).collect()
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during reading CarStations".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update(&self, id: u32, 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, &car.car_station_id, &id]
|
||||||
|
).await;
|
||||||
|
|
||||||
|
if let Ok(rows) = &result {
|
||||||
|
let row = rows.get(0).unwrap();
|
||||||
|
Ok(Car {
|
||||||
|
id: row.get("id"),
|
||||||
|
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")
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during updating of Car".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete(&self, id: u32) -> Result<(), String> {
|
||||||
|
let result = self.connection.execute(
|
||||||
|
"DELETE FROM Car WHERE id = $1",
|
||||||
|
&[&id]
|
||||||
|
).await;
|
||||||
|
|
||||||
|
if let Ok(rows) = result {
|
||||||
|
if rows == 0 {
|
||||||
|
Err("Car not found".to_owned())
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during deleting of Car".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
96
backend/src/storages/postgres/car_station.rs
Normal file
96
backend/src/storages/postgres/car_station.rs
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
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("id"),
|
||||||
|
address: row.get("address")
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during creation of CarStation".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read(&self, id: u32) -> Result<CarStation, String> {
|
||||||
|
let result = self.connection.query(
|
||||||
|
"SELECT * FROM Car_Station WHERE id = $1", &[&id]
|
||||||
|
).await;
|
||||||
|
|
||||||
|
if let Ok(rows) = result {
|
||||||
|
let row = rows.get(0).ok_or("CarStation not found".to_owned())?;
|
||||||
|
Ok(CarStation {
|
||||||
|
id: row.get("id"),
|
||||||
|
address: row.get("address")
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during reading of CarStation".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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("id"),
|
||||||
|
address: r.get("address")
|
||||||
|
}).collect()
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during reading CarStations".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update(&self, id: u32, 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]
|
||||||
|
).await;
|
||||||
|
|
||||||
|
if let Ok(rows) = result {
|
||||||
|
let row = rows.get(0).unwrap();
|
||||||
|
Ok(CarStation {
|
||||||
|
id: row.get("id"),
|
||||||
|
address: row.get("address")
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during updating of CarStation".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete(&self, id: u32) -> Result<(), String> {
|
||||||
|
let result = self.connection.execute(
|
||||||
|
"DELETE FROM Car_Station WHERE id = $1",
|
||||||
|
&[&id]
|
||||||
|
).await;
|
||||||
|
|
||||||
|
if let Ok(rows) = result {
|
||||||
|
if rows == 0 {
|
||||||
|
Err("CarStation not found".to_owned())
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during deleting of CarStation".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
111
backend/src/storages/postgres/client.rs
Normal file
111
backend/src/storages/postgres/client.rs
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
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("id"),
|
||||||
|
name: row.get("name"),
|
||||||
|
surname: row.get("surname"),
|
||||||
|
middlename: row.get("middlename"),
|
||||||
|
phone: row.get("phone")
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during creation of Client".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read(&self, id: u32) -> Result<Client, String> {
|
||||||
|
let result= self.connection.query(
|
||||||
|
"SELECT * FROM Client WHERE id = $1", &[&id]
|
||||||
|
).await;
|
||||||
|
|
||||||
|
if let Ok(rows) = result {
|
||||||
|
|
||||||
|
let row = rows.get(0).ok_or("Client not found".to_owned())?;
|
||||||
|
Ok(Client {
|
||||||
|
id: row.get("id"),
|
||||||
|
name: row.get("name"),
|
||||||
|
surname: row.get("surname"),
|
||||||
|
middlename: row.get("middlename"),
|
||||||
|
phone: row.get("phone")
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during reading of Client".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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("id"),
|
||||||
|
name: r.get("name"),
|
||||||
|
surname: r.get("surname"),
|
||||||
|
middlename: r.get("middlename"),
|
||||||
|
phone: r.get("phone")
|
||||||
|
}).collect()
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during reading Clients".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update(&self, id: u32, 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]
|
||||||
|
).await;
|
||||||
|
|
||||||
|
if let Ok(rows) = result {
|
||||||
|
let row = rows.get(0).unwrap();
|
||||||
|
Ok(Client {
|
||||||
|
id: row.get("id"),
|
||||||
|
name: row.get("name"),
|
||||||
|
surname: row.get("surname"),
|
||||||
|
middlename: row.get("middlename"),
|
||||||
|
phone: row.get("phone")
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during updating of Client".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete(&self, id: u32) -> Result<(), String> {
|
||||||
|
let result = self.connection.execute(
|
||||||
|
"DELETE FROM Client WHERE id = $1",
|
||||||
|
&[&id]
|
||||||
|
).await;
|
||||||
|
|
||||||
|
if let Ok(rows) = result {
|
||||||
|
if rows == 0 {
|
||||||
|
Err("Client not found".to_owned())
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during deleting of Client".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
4
backend/src/storages/postgres/mod.rs
Normal file
4
backend/src/storages/postgres/mod.rs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
pub mod client;
|
||||||
|
pub mod car_station;
|
||||||
|
pub mod car;
|
||||||
|
pub mod rent;
|
104
backend/src/storages/postgres/rent.rs
Normal file
104
backend/src/storages/postgres/rent.rs
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
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 result = self.connection.query(
|
||||||
|
"INSERT INTO Rent(start_time, time_amount, client_id, car_id) \
|
||||||
|
VALUES ($1, $2, $3, $4) RETURNING *",
|
||||||
|
&[&rent.start_time, &rent.time_amount, &rent.client_id, &rent.car_id]
|
||||||
|
).await;
|
||||||
|
|
||||||
|
if let Ok(rows) = result {
|
||||||
|
let row = rows.get(0).unwrap();
|
||||||
|
Ok(Rent {
|
||||||
|
id: row.get("id"),
|
||||||
|
start_time: row.get("start_time"),
|
||||||
|
time_amount: row.get("time_amount"),
|
||||||
|
client_id: row.get("client_id"),
|
||||||
|
car_id: row.get("car_id")
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during creation of Rent".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read(&self, id: u32) -> Result<Rent, String> {
|
||||||
|
let result = self.connection.query(
|
||||||
|
"SELECT * FROM Rent WHERE id = $1", &[&id]
|
||||||
|
).await;
|
||||||
|
|
||||||
|
if let Ok(rows) = result {
|
||||||
|
let row = rows.get(0).ok_or("Rent not found".to_owned())?;
|
||||||
|
Ok(Rent {
|
||||||
|
id: row.get("id"),
|
||||||
|
start_time: row.get("start_time"),
|
||||||
|
time_amount: row.get("time_amount"),
|
||||||
|
client_id: row.get("client_id"),
|
||||||
|
car_id: row.get("car_id")
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during reading of Rent".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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("id"),
|
||||||
|
start_time: r.get("start_time"),
|
||||||
|
time_amount: r.get("time_amount"),
|
||||||
|
client_id: r.get("client_id"),
|
||||||
|
car_id: r.get("car_id")
|
||||||
|
}).collect()
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during reading CarStations".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update(&self, id: u32, rent: BindingRent) -> Result<Rent, String> {
|
||||||
|
let result = self.connection.query(
|
||||||
|
"UPDATE Rent SET start_time = $1, time_amount = $2, client_id = $3, car_id = $4 WHERE id = $5 RETURNING *",
|
||||||
|
&[&rent.start_time, &rent.time_amount, &rent.client_id, &rent.car_id, &id]
|
||||||
|
).await;
|
||||||
|
|
||||||
|
if let Ok(rows) = result {
|
||||||
|
let row = rows.get(0).unwrap();
|
||||||
|
Ok(Rent {
|
||||||
|
id: row.get("id"),
|
||||||
|
start_time: row.get("start_time"),
|
||||||
|
time_amount: row.get("time_amount"),
|
||||||
|
client_id: row.get("client_id"),
|
||||||
|
car_id: row.get("car_id")
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during updating of Rent".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete(&self, id: u32) -> Result<(), String> {
|
||||||
|
let result = self.connection.execute(
|
||||||
|
"DELETE FROM Rent WHERE id = $1", &[&id]
|
||||||
|
).await;
|
||||||
|
|
||||||
|
if let Ok(_) = result {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err("Something gone wrong during deletion of Rent".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
41
backend/src/storages/traits.rs
Normal file
41
backend/src/storages/traits.rs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
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: u32) -> Result<Client, String>;
|
||||||
|
async fn read_all(&self) -> Result<Vec<Client>, String>;
|
||||||
|
async fn update(&self, id: u32, client: BindingClient) -> Result<Client, String>;
|
||||||
|
async fn delete(&self, id: u32) -> Result<(), String>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait CarRepository {
|
||||||
|
async fn create(&self, car: BindingCar) -> Result<Car, String>;
|
||||||
|
async fn read(&self, id: u32) -> Result<Car, String>;
|
||||||
|
async fn read_all(&self) -> Result<Vec<Car>, String>;
|
||||||
|
async fn update(&self, id: u32, car: BindingCar) -> Result<Car, String>;
|
||||||
|
async fn delete(&self, id: u32) -> Result<(), String>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait CarStationRepository {
|
||||||
|
async fn create(&self, car_station: BindingCarStation) -> Result<CarStation, String>;
|
||||||
|
async fn read(&self, id: u32) -> Result<CarStation, String>;
|
||||||
|
async fn read_all(&self) -> Result<Vec<CarStation>, String>;
|
||||||
|
async fn update(&self, id: u32, car_station: BindingCarStation) -> Result<CarStation, String>;
|
||||||
|
async fn delete(&self, id: u32) -> Result<(), String>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait RentRepository {
|
||||||
|
async fn create(&self, rent: BindingRent) -> Result<Rent, String>;
|
||||||
|
async fn read(&self, id: u32) -> Result<Rent, String>;
|
||||||
|
async fn read_all(&self) -> Result<Vec<Rent>, String>;
|
||||||
|
async fn update(&self, id: u32, rent: BindingRent) -> Result<Rent, String>;
|
||||||
|
async fn delete(&self, id: u32) -> Result<(), String>;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user