Can get the list of movements of an account

This commit is contained in:
Pierre HUBERT 2025-04-19 21:45:40 +02:00
parent 08538c3911
commit 811e885bf3
3 changed files with 21 additions and 3 deletions

View File

@ -1,4 +1,5 @@
use crate::controllers::HttpResult;
use crate::extractors::account_extractor::AccountInPath;
use crate::extractors::auth_extractor::AuthExtractor;
use crate::services::movements_service;
use crate::services::movements_service::UpdateMovementQuery;
@ -14,3 +15,9 @@ pub async fn create(auth: AuthExtractor, req: web::Json<UpdateMovementQuery>) ->
Ok(HttpResponse::Created().finish())
}
/// Get the list of movements of an account
pub async fn get_list_of_account(account_id: AccountInPath) -> HttpResult {
Ok(HttpResponse::Ok()
.json(movements_service::get_list_account(account_id.as_ref().id()).await?))
}

View File

@ -131,6 +131,10 @@ async fn main() -> std::io::Result<()> {
)
// Movement controller
.route("/api/movement", web::post().to(movement_controller::create))
.route(
"/api/account/{account_id}/movements",
web::get().to(movement_controller::get_list_of_account),
)
// Static assets
.route("/", web::get().to(static_controller::root_index))
.route(

View File

@ -45,9 +45,9 @@ impl UpdateMovementQuery {
}
// Check for conflict with other movements
if let Ok(_) =
get_by_account_label_amount_time(self.account_id, &self.label, self.amount, self.time)
if get_by_account_label_amount_time(self.account_id, &self.label, self.amount, self.time)
.await
.is_ok()
{
return Ok(Some(
"A movement taken at the same time with the same label and the same amount already exists!",
@ -120,3 +120,10 @@ pub async fn get_by_account_label_amount_time(
)
.get_result(&mut db()?)?)
}
/// Get the list of movements of an account
pub async fn get_list_account(account_id: AccountID) -> anyhow::Result<Vec<Movement>> {
Ok(movements::table
.filter(movements::dsl::account_id.eq(account_id.0))
.get_results(&mut db()?)?)
}