Added web ui with templates
This commit is contained in:
parent
d2cb5b3031
commit
90b02eef79
24 changed files with 1403 additions and 78 deletions
62
webserver/src/routes/api/transactions.rs
Normal file
62
webserver/src/routes/api/transactions.rs
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use axum::extract::{Json, Path, Query, State};
|
||||
use chrono::{offset::Utc, DateTime};
|
||||
use hyper::StatusCode;
|
||||
use serde::Deserialize;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use accounters::models::Transaction;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct TransactionContent {
|
||||
description: String,
|
||||
timestamp: DateTime<Utc>,
|
||||
category: Option<String>,
|
||||
amount: i32,
|
||||
}
|
||||
|
||||
pub async fn create(
|
||||
State(db): State<Arc<SqlitePool>>,
|
||||
Path(account): Path<i32>,
|
||||
Json(txcnt): Json<TransactionContent>,
|
||||
) -> (StatusCode, String) {
|
||||
match Transaction::new(
|
||||
db.as_ref(),
|
||||
account,
|
||||
&txcnt.description,
|
||||
&txcnt.timestamp,
|
||||
None,
|
||||
txcnt.amount,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(tx) => (StatusCode::OK, serde_json::to_string(&tx).unwrap()),
|
||||
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct PaginationOptions {
|
||||
pub limit: Option<i32>,
|
||||
pub offset: Option<i32>,
|
||||
}
|
||||
|
||||
pub async fn list(
|
||||
State(db): State<Arc<SqlitePool>>,
|
||||
Path(account): Path<i32>,
|
||||
Query(pagination): Query<PaginationOptions>,
|
||||
) -> (StatusCode, String) {
|
||||
match Transaction::list(
|
||||
db.as_ref(),
|
||||
account,
|
||||
pagination.limit.unwrap_or(100),
|
||||
pagination.offset.unwrap_or(0),
|
||||
true,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(txs) => (StatusCode::OK, serde_json::to_string(&txs).unwrap()),
|
||||
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")),
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue