Фронтенд: заготовка для дальнейшей работы

This commit is contained in:
Сергей Полевой 2023-04-14 21:29:53 +04:00
parent f7eb534027
commit 9f43a1c04c
31 changed files with 580 additions and 1985 deletions

12
.gitignore vendored
View File

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

View File

@ -1,59 +0,0 @@
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

@ -1,59 +0,0 @@
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

@ -1,59 +0,0 @@
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

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

View File

@ -1,59 +0,0 @@
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

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

View File

@ -1,15 +0,0 @@
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>
}

View File

@ -1,98 +0,0 @@
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
}

View File

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

View File

@ -1,113 +0,0 @@
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

@ -1,96 +0,0 @@
use tokio_postgres::Client as PgClient;
use std::sync::Arc;
use crate::models::car_station::{BindingCarStation, CarStation};
use crate::storages::traits::CarStationRepository;
use async_trait::async_trait;
pub struct PostgresCarStationRepository {
pub connection: Arc<PgClient>
}
#[async_trait]
impl CarStationRepository for PostgresCarStationRepository {
async fn create(&self, car_station: BindingCarStation) -> Result<CarStation, String> {
let result = self.connection.query(
"INSERT INTO Car_Station(address) \
VALUES ($1) RETURNING *",
&[&car_station.address]
).await;
if let Ok(rows) = result {
let row = rows.get(0).unwrap();
Ok(CarStation {
id: row.get("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

@ -1,111 +0,0 @@
use tokio_postgres::Client as PgClient;
use std::sync::Arc;
use crate::models::client::{BindingClient, Client};
use crate::storages::traits::ClientRepository;
use async_trait::async_trait;
pub struct PostgresClientRepository {
pub connection: Arc<PgClient>
}
#[async_trait]
impl ClientRepository for PostgresClientRepository {
async fn create(&self, client: BindingClient) -> Result<Client, String> {
let result = self.connection.query(
"INSERT INTO Client(name, surname, middlename, phone) \
VALUES ($1, $2, $3, $4) RETURNING *",
&[&client.name, &client.surname, &client.middlename, &client.phone]
).await;
if let Ok(rows) = &result {
let row = rows.get(0).unwrap();
Ok(Client {
id: row.get("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

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

View File

@ -1,111 +0,0 @@
use tokio_postgres::Client as PgClient;
use std::sync::Arc;
use crate::models::client::{BindingClient, Client};
use crate::storages::traits::OwnerRepository;
use async_trait::async_trait;
pub struct PostgresOwnerRepository {
pub connection: Arc<PgClient>
}
#[async_trait]
impl OwnerRepository for PostgresOwnerRepository {
async fn create(&self, client: BindingClient) -> Result<Client, String> {
let result = self.connection.query(
"INSERT INTO Owner(name, surname, middlename, phone) \
VALUES ($1, $2, $3, $4) RETURNING *",
&[&client.name, &client.surname, &client.middlename, &client.phone]
).await;
if let Ok(rows) = &result {
let row = rows.get(0).unwrap();
Ok(Client {
id: row.get("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

@ -1,117 +0,0 @@
use tokio_postgres::Client as PgClient;
use std::sync::Arc;
use crate::models::rent::{BindingRent, Rent};
use crate::storages::traits::RentRepository;
use async_trait::async_trait;
pub struct PostgresRentRepository {
pub connection: Arc<PgClient>
}
#[async_trait]
impl RentRepository for PostgresRentRepository {
async fn create(&self, rent: BindingRent) -> Result<Rent, String> {
let query_result = self.connection.query(
"SELECT * FROM Rent WHERE car_id=$1 AND time_amount IS 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

@ -1,50 +0,0 @@
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>;
}

File diff suppressed because it is too large Load Diff

View File

@ -1,17 +1,13 @@
[package]
name = "backend"
name = "frontend"
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"] }
yew = { version = "0.20.0", features = ["csr"] }
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"
yew-router = "0.17"

8
frontend/index.html Normal file
View File

@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://unpkg.com/chota@latest">
<title>Каршеринг от Гуся</title>
</head>
</html>

View File

@ -0,0 +1,16 @@
use yew::prelude::*;
use yew_router::{BrowserRouter, Switch};
use super::router::*;
#[function_component]
pub fn App() -> Html {
html! {
<div class="container">
<div>
<BrowserRouter>
<Switch<Route> render={switch} />
</BrowserRouter>
</div>
</div>
}
}

View File

@ -0,0 +1,51 @@
use yew::prelude::*;
use yew_router::{prelude::use_navigator};
use super::router::*;
#[function_component]
pub fn Header() -> Html {
let navigator = use_navigator().unwrap();
html! {
<nav class="tabs is-center">
<details class="dropdown">
<summary class="button outline">{ "Автомобили" }</summary>
<div class="card">
<p><button class="button outline" onclick={ Callback::from({let navigator = navigator.clone(); move |_| navigator.push(&Route::CarsAdd)} ) } >{ "Добавить" }</button></p>
<p><button class="button outline" onclick={ Callback::from({let navigator = navigator.clone(); move |_| navigator.push(&Route::CarsAll)} ) } >{ "Все" }</button></p>
<p><button class="button outline" onclick={ Callback::from({let navigator = navigator.clone(); move |_| navigator.push(&Route::CarsOccupied)} ) } >{ "Занятые" }</button></p>
<p><button class="button outline" onclick={ Callback::from({let navigator = navigator.clone(); move |_| navigator.push(&Route::CarsFree)} ) } >{ "Свободные" }</button></p>
</div>
</details>
<details class="dropdown">
<summary class="button outline">{ "Владельцы" }</summary>
<div class="card">
<p><button class="button outline" onclick={ Callback::from({let navigator = navigator.clone(); move |_| navigator.push(&Route::OwnersAdd)} ) } >{ "Добавить" }</button></p>
<p><button class="button outline" onclick={ Callback::from({let navigator = navigator.clone(); move |_| navigator.push(&Route::OwnersAll)} ) } >{ "Все" }</button></p>
</div>
</details>
<details class="dropdown">
<summary class="button outline">{ "Клиенты" }</summary>
<div class="card">
<p><button class="button outline" onclick={ Callback::from({let navigator = navigator.clone(); move |_| navigator.push(&Route::ClientsAdd)} ) } >{ "Добавить" }</button></p>
<p><button class="button outline" onclick={ Callback::from({let navigator = navigator.clone(); move |_| navigator.push(&Route::ClientsAll)} ) } >{ "Все" }</button></p>
</div>
</details>
<details class="dropdown">
<summary class="button outline">{ "Парковки" }</summary>
<div class="card">
<p><button class="button outline" onclick={ Callback::from({let navigator = navigator.clone(); move |_| navigator.push(&Route::CarStationsAdd)} ) } >{ "Добавить" }</button></p>
<p><button class="button outline" onclick={ Callback::from({let navigator = navigator.clone(); move |_| navigator.push(&Route::CarStationsAll)} ) } >{ "Все" }</button></p>
</div>
</details>
<details class="dropdown">
<summary class="button outline">{ "Аренды" }</summary>
<div class="card">
<p><button class="button outline" onclick={ Callback::from({let navigator = navigator.clone(); move |_| navigator.push(&Route::RentsAdd)} ) } >{ "Добавить" }</button></p>
<p><button class="button outline" onclick={ Callback::from({let navigator = navigator.clone(); move |_| navigator.push(&Route::RentsAll)} ) } >{ "Все" }</button></p>
<p><button class="button outline" onclick={ Callback::from({let navigator = navigator.clone(); move |_| navigator.push(&Route::RentsFinished)} ) } >{ "Завершённые" }</button></p>
<p><button class="button outline" onclick={ Callback::from({let navigator = navigator.clone(); move |_| navigator.push(&Route::RentsOngoing)} ) } >{ "Незавершённые" }</button></p>
</div>
</details>
</nav>
}
}

View File

@ -0,0 +1,3 @@
pub mod app;
pub mod header;
pub mod router;

View File

@ -0,0 +1,82 @@
use yew::prelude::*;
use yew_router::prelude::*;
use super::header::Header;
#[derive(Routable, PartialEq, Clone)]
pub enum Route {
#[at("/")]
Home,
#[at("/cars/add")]
CarsAdd,
#[at("/cars/")]
CarsAll,
#[at("/cars/occupied")]
CarsOccupied,
#[at("/cars/free")]
CarsFree,
#[at("/cars/:id")]
CarView { id: u32 },
#[at("/owners/add")]
OwnersAdd,
#[at("/owners/")]
OwnersAll,
#[at("/owners/:id")]
OwnerView { id: u32 },
#[at("/clients/add")]
ClientsAdd,
#[at("/clients/")]
ClientsAll,
#[at("/clients/:id")]
ClientView { id: u32 },
#[at("/car_stations/add")]
CarStationsAdd,
#[at("/car_stations/")]
CarStationsAll,
#[at("/car_stations/:id")]
CarStationsView { id: u32 },
#[at("/rents/add")]
RentsAdd,
#[at("/rents/")]
RentsAll,
#[at("/rents/finished")]
RentsFinished,
#[at("/rents/ongoing")]
RentsOngoing,
#[at("/rents/:id")]
RentView { id: u32 },
}
pub fn switch(route: Route) -> Html {
match route {
Route::Home => html!{<Header />},
Route::CarsAdd => html!{},
Route::CarsAll => html!{},
Route::CarsOccupied => html!{},
Route::CarsFree => html!{},
Route::CarView { id } => html!{},
Route::OwnersAdd => html!{},
Route::OwnersAll => html!{},
Route::OwnerView { id } => html!{},
Route::ClientsAdd => html!{},
Route::ClientsAll => html!{},
Route::ClientView { id } => html!{},
Route::CarStationsAdd => html!{},
Route::CarStationsAll => html!{},
Route::CarStationsView { id } => html!{},
Route::RentsAdd => html!{},
Route::RentsAll => html!{},
Route::RentsFinished => html!{},
Route::RentsOngoing => html!{},
Route::RentView { id } => html!{},
}
}

2
frontend/src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod models;
pub mod components;

5
frontend/src/main.rs Normal file
View File

@ -0,0 +1,5 @@
mod components;
fn main() {
yew::Renderer::<components::app::App>::new().render();
}

View File

@ -1,4 +1,4 @@
use serde::{Serialize, Deserialize};
use serde::{ Serialize, Deserialize };
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Car {

View File

@ -1,4 +1,4 @@
use serde::{Serialize, Deserialize};
use serde::{ Serialize, Deserialize };
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CarStation {

View File

@ -1,4 +1,4 @@
use serde::{Serialize, Deserialize};
use serde::{ Serialize, Deserialize };
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Client {

View File

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

View File

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