diff --git a/moneymgr_backend/src/controllers/movement_controller.rs b/moneymgr_backend/src/controllers/movement_controller.rs index 1e90425..4f5663c 100644 --- a/moneymgr_backend/src/controllers/movement_controller.rs +++ b/moneymgr_backend/src/controllers/movement_controller.rs @@ -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) -> 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?)) +} diff --git a/moneymgr_backend/src/main.rs b/moneymgr_backend/src/main.rs index 5aa73d8..f5279ef 100644 --- a/moneymgr_backend/src/main.rs +++ b/moneymgr_backend/src/main.rs @@ -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( diff --git a/moneymgr_backend/src/services/movements_service.rs b/moneymgr_backend/src/services/movements_service.rs index f7c678d..e85c17f 100644 --- a/moneymgr_backend/src/services/movements_service.rs +++ b/moneymgr_backend/src/services/movements_service.rs @@ -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) - .await + 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> { + Ok(movements::table + .filter(movements::dsl::account_id.eq(account_id.0)) + .get_results(&mut db()?)?) +}