Compare commits

...

4 Commits

24 changed files with 2806 additions and 8 deletions

12
.gitignore vendored
View File

@ -1,16 +1,12 @@
# ---> Rust
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# 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
backend/debug/
backend/target/
backend/.env
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
backend/*.pdb

1696
backend/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

17
backend/Cargo.toml Normal file
View 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"

View File

@ -0,0 +1,59 @@
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 {
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<(u32, )>) -> impl Responder {
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>) -> impl Responder {
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<(u32, )>) -> impl Responder {
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<(u32, )>) -> impl Responder {
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())
}
}

View File

@ -0,0 +1,59 @@
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 {
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<(u32, )>) -> impl Responder {
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>) -> impl Responder {
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<(u32, )>) -> impl Responder {
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<(u32, )>) -> impl Responder {
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

@ -0,0 +1,59 @@
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 {
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<(u32, )>) -> impl Responder {
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>) -> impl Responder {
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<(u32, )>) -> impl Responder {
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<(u32, )>) -> impl Responder {
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

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

View File

@ -0,0 +1,59 @@
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_owners(state: web::Data<Mutex<State>>) -> impl Responder {
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<(u32, )>) -> impl Responder {
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>) -> impl Responder {
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<(u32, )>) -> impl Responder {
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<(u32, )>) -> impl Responder {
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

@ -0,0 +1,59 @@
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 {
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<(u32, )>) -> impl Responder {
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>) -> impl Responder {
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<(u32, )>) -> impl Responder {
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<(u32, )>) -> impl Responder {
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())
}
}

15
backend/src/lib.rs Normal file
View File

@ -0,0 +1,15 @@
use storages::traits::OwnerRepository;
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 owner_repository: Box<dyn OwnerRepository + Send>,
pub rent_repository: Box<dyn RentRepository + Send>
}

98
backend/src/main.rs Normal file
View File

@ -0,0 +1,98 @@
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::storages::postgres::owner::PostgresOwnerRepository;
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!("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 owner_repository = PostgresOwnerRepository { 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),
owner_repository: Box::new(owner_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("/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)
)
})
.bind("127.0.0.1:8080")?
.run()
.await
}

20
backend/src/models/car.rs Normal file
View 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
}

View 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
}

View 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
}

View File

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

View File

@ -0,0 +1,17 @@
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 time_amount: Option<u32>,
pub client_id: u32,
pub car_id: u32
}

View File

@ -0,0 +1,2 @@
pub mod traits;
pub mod postgres;

View 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())
}
}
}

View 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())
}
}
}

View 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())
}
}
}

View File

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

View 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::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("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 Owner".to_owned())
}
}
async fn read(&self, id: u32) -> Result<Client, String> {
let result= self.connection.query(
"SELECT * FROM Owner WHERE id = $1", &[&id]
).await;
if let Ok(rows) = result {
let row = rows.get(0).ok_or("Owner 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 Owner".to_owned())
}
}
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("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 Owner".to_owned())
}
}
async fn update(&self, id: u32, 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]
).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 Owner".to_owned())
}
}
async fn delete(&self, id: u32) -> Result<(), String> {
let result = self.connection.execute(
"DELETE FROM Owner WHERE id = $1",
&[&id]
).await;
if let Ok(rows) = result {
if rows == 0 {
Err("Owner not found".to_owned())
} else {
Ok(())
}
} else {
Err("Something gone wrong during deleting of Owner".to_owned())
}
}
}

View File

@ -0,0 +1,117 @@
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 NOT NULL",
&[&rent.car_id]
).await;
if let Ok(rows) = query_result {
if rows.len() > 0 {
return Err("The car is already occupied".to_owned());
}
} else {
return Err("Something gone wrong during checking for existing rents".to_owned());
}
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]
).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 time_amount = $2, client_id = $3, car_id = $4 WHERE id = $5 RETURNING *",
&[&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())
}
}
}

View File

@ -0,0 +1,50 @@
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>;
}
#[async_trait]
pub trait OwnerRepository {
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>;
}