Added expenses.
This commit is contained in:
@@ -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))
|
||||
}
|
||||
Reference in New Issue
Block a user