Бэкенд: добавлена проверка в аренду

This commit is contained in:
Сергей Полевой 2023-04-02 17:18:21 +04:00
parent 41f99133e2
commit 2c90088b45

View File

@ -11,9 +11,22 @@ pub struct PostgresRentRepository {
#[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, $4) RETURNING *",
VALUES ($1, $2, $3) RETURNING *",
&[&rent.time_amount, &rent.client_id, &rent.car_id]
).await;