Бэкенд: рефакторинг и добавлена обработка ошибок

This commit is contained in:
Сергей Полевой 2023-04-02 16:54:54 +04:00
parent 6a05f40245
commit 41f99133e2
7 changed files with 144 additions and 26 deletions

1
.gitignore vendored
View File

@ -10,4 +10,3 @@ backend/.env
# MSVC Windows builds of rustc generate these, which store debugging information
backend/*.pdb

View File

@ -5,25 +5,55 @@ 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())
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 {
HttpResponse::Ok().json(state.lock().unwrap().car_repository.read(path.into_inner().0).await.unwrap())
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 {
HttpResponse::Ok().json(state.lock().unwrap().car_repository.create(json.0).await.unwrap())
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 {
HttpResponse::Ok().json(state.lock().unwrap().car_repository.update(path.into_inner().0, json.0).await.unwrap())
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 {
HttpResponse::Ok().json(state.lock().unwrap().car_repository.delete(path.into_inner().0).await.unwrap())
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

@ -5,25 +5,55 @@ 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())
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 {
HttpResponse::Ok().json(state.lock().unwrap().car_station_repository.read(path.into_inner().0).await.unwrap())
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 {
HttpResponse::Ok().json(state.lock().unwrap().car_station_repository.create(json.0).await.unwrap())
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 {
HttpResponse::Ok().json(state.lock().unwrap().car_station_repository.update(path.into_inner().0, json.0).await.unwrap())
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 {
HttpResponse::Ok().json(state.lock().unwrap().car_station_repository.delete(path.into_inner().0).await.unwrap())
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

@ -5,25 +5,55 @@ 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())
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 {
HttpResponse::Ok().json(state.lock().unwrap().client_repository.read(path.into_inner().0).await.unwrap())
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 {
HttpResponse::Ok().json(state.lock().unwrap().client_repository.create(json.0).await.unwrap())
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 {
HttpResponse::Ok().json(state.lock().unwrap().client_repository.update(path.into_inner().0, json.0).await.unwrap())
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 {
HttpResponse::Ok().json(state.lock().unwrap().client_repository.delete(path.into_inner().0).await.unwrap())
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

@ -5,25 +5,55 @@ 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())
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 {
HttpResponse::Ok().json(state.lock().unwrap().rent_repository.read(path.into_inner().0).await.unwrap())
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 {
HttpResponse::Ok().json(state.lock().unwrap().rent_repository.create(json.0).await.unwrap())
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 {
HttpResponse::Ok().json(state.lock().unwrap().rent_repository.update(path.into_inner().0, json.0).await.unwrap())
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 {
HttpResponse::Ok().json(state.lock().unwrap().rent_repository.delete(path.into_inner().0).await.unwrap())
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

@ -11,7 +11,6 @@ pub struct Rent {
#[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

View File

@ -12,9 +12,9 @@ pub struct PostgresRentRepository {
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) \
"INSERT INTO Rent(time_amount, client_id, car_id) \
VALUES ($1, $2, $3, $4) RETURNING *",
&[&rent.start_time, &rent.time_amount, &rent.client_id, &rent.car_id]
&[&rent.time_amount, &rent.client_id, &rent.car_id]
).await;
if let Ok(rows) = result {
@ -72,8 +72,8 @@ impl RentRepository for PostgresRentRepository {
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]
"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 {