Added expenses.

This commit is contained in:
2026-09-20 21:52:49 +02:00
parent 15b8ae8f62
commit b9d18c4dd1
8 changed files with 226 additions and 1 deletions
+41
View File
@@ -0,0 +1,41 @@
use axum::{Json, extract::State};
use sqlx::PgPool;
use crate::{
database::dtos::expense::RecurringExpenseRow,
error::AppError,
routes::dtos::expense::{Expense, RecurringExpense},
};
pub fn create_router() -> axum::Router<PgPool> {
axum::Router::new().route("/", axum::routing::get(get_expenses))
}
#[tracing::instrument(skip(pool))]
async fn get_expenses(State(pool): State<PgPool>) -> Result<Json<Vec<Expense>>, AppError> {
let rows = sqlx::query_as::<_, RecurringExpenseRow>(
r#"
SELECT
id,
name,
amount_cents,
starts_at,
ends_at,
recurrence_every,
recurrence_unit,
weekend_adjustment
FROM recurring_expenses
ORDER BY starts_at DESC, id DESC
"#,
)
.fetch_all(&pool)
.await?;
let expenses = rows
.into_iter()
.map(RecurringExpense::try_from)
.map(|result| result.map(Expense::from))
.collect::<Result<Vec<_>, _>>()?;
Ok(Json(expenses))
}