Block creation of identical movements

This commit is contained in:
Pierre HUBERT 2025-04-15 22:49:45 +02:00
parent b4cf6624f7
commit 08538c3911

View File

@ -44,7 +44,15 @@ impl UpdateMovementQuery {
}
}
// TODO : check for conflict
// Check for conflict with other movements
if let Ok(_) =
get_by_account_label_amount_time(self.account_id, &self.label, self.amount, self.time)
.await
{
return Ok(Some(
"A movement taken at the same time with the same label and the same amount already exists!",
));
}
Ok(None)
}
@ -87,3 +95,28 @@ pub async fn update(id: MovementID, q: &UpdateMovementQuery) -> anyhow::Result<(
Ok(())
}
/// Get a single movement by its id
pub async fn get_by_id(movement_id: MovementID) -> anyhow::Result<Movement> {
Ok(movements::table
.filter(movements::dsl::id.eq(movement_id.0))
.get_result(&mut db()?)?)
}
/// Get a single movement by account_id, label, amount and time
pub async fn get_by_account_label_amount_time(
account_id: AccountID,
label: &str,
amount: f32,
time: u64,
) -> anyhow::Result<Movement> {
Ok(movements::table
.filter(
movements::dsl::account_id
.eq(account_id.0)
.and(movements::dsl::label.eq(label))
.and(movements::dsl::amount.eq(amount))
.and(movements::dsl::time.eq(time as i64)),
)
.get_result(&mut db()?)?)
}