diff --git a/src/api/account.rs b/src/api/account.rs index 5a5ed4f..d7f4533 100644 --- a/src/api/account.rs +++ b/src/api/account.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use axum::{Router, extract::Path, http::StatusCode, routing::get}; use serde::Serialize; +use tracing::instrument; use uuid::Uuid; use crate::{ @@ -29,7 +30,10 @@ pub struct ListAccounts { accounts: Vec, } +#[instrument(skip(state))] pub async fn account_info(EState(state): State, _: Auth, Path(id): Path) {} + +#[instrument(skip(state))] pub async fn account_transactions( EState(state): State, auth: Auth, @@ -53,6 +57,8 @@ pub async fn account_transactions( Ok(Json(result)) } + +#[instrument(skip(state))] pub async fn list_accounts( EState(state): State, _: Auth, diff --git a/src/api/mod.rs b/src/api/mod.rs index 87951f3..7d19080 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -404,7 +404,7 @@ impl RequestPagination { } #[inline] pub const fn offset(&self) -> i64 { - self.limit as i64 + self.offset as i64 } } diff --git a/src/api/transactions.rs b/src/api/transactions.rs index 5f0c4ea..b897ace 100644 --- a/src/api/transactions.rs +++ b/src/api/transactions.rs @@ -333,7 +333,7 @@ impl MakePayment { } } -#[derive(Deserialize)] +#[derive(Debug, Deserialize)] pub struct TransactionQuery { #[serde(flatten)] pub pagination: RequestPagination, diff --git a/src/api/user.rs b/src/api/user.rs index b3b6786..c53adb0 100644 --- a/src/api/user.rs +++ b/src/api/user.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use axum::{Router, extract::Path, http::StatusCode, routing::get}; use serde::{Deserialize, Serialize}; +use tracing::instrument; use uuid::Uuid; use crate::{ @@ -69,12 +70,16 @@ pub async fn user_info( } Err(ApiError::NOT_FOUND.into()) } + +#[instrument(skip(state))] pub async fn user_balance(EState(state): State, auth: Auth) -> Result, Error> { let conn = state.conn().await?; let info = Account::list_for_user(&conn, auth.user_id()).await?; let balance = info.iter().map(|info| info.balance).sum(); Ok(Json(UserBalance { balance })) } + +#[instrument(skip(state))] pub async fn list_users( EState(state): State, _: Auth, @@ -84,6 +89,8 @@ pub async fn list_users( let users = User::list(&conn, pagination).await?; Ok(Json(users)) } + +#[instrument(skip(state))] pub async fn me_transaction_history( EState(state): State, auth: Auth, @@ -97,6 +104,7 @@ pub async fn me_transaction_history( Ok(Json(result)) } +#[instrument(skip(state))] pub async fn user_accounts(EState(state): State, auth: Auth) -> Result, Error> { let user = auth.user_id(); let conn = state.conn().await?; diff --git a/src/model/account.rs b/src/model/account.rs index d636045..11a10da 100644 --- a/src/model/account.rs +++ b/src/model/account.rs @@ -96,7 +96,7 @@ impl Account { .prepare_cached("select id,\"user\",name from accounts limit $1 offset $2") .await?; let stmt_count = client - .prepare_cached("select count(*) from accounts limit $1 offset $2") + .prepare_cached("select count(*) from accounts") .await?; let users = client .query(&stmt, &[&pagination.limit(), &pagination.offset()]) diff --git a/src/model/mod.rs b/src/model/mod.rs index a46f62d..a1705c0 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -5,6 +5,7 @@ use garde::Validate; use regex::Regex; use serde::{Deserialize, Serialize}; use tokio_postgres::{ToStatement, types::ToSql}; +use tracing::{info, instrument}; use uuid::Uuid; mod account; @@ -50,7 +51,8 @@ pub struct IdWithName { pub name: Name, } -async fn count( +#[instrument(skip(client))] +async fn count( client: &impl GenericClient, statement: &T, params: &[&(dyn ToSql + Sync)], diff --git a/src/model/transaction.rs b/src/model/transaction.rs index fa21e01..24ba54d 100644 --- a/src/model/transaction.rs +++ b/src/model/transaction.rs @@ -8,7 +8,7 @@ use crate::api::{Pagination, RequestPagination}; use super::{User, account::ReducedAccountInfo}; -#[derive(Deserialize)] +#[derive(Debug, Deserialize)] #[cfg_attr(feature = "schemas", derive(schemars::JsonSchema))] #[serde(rename_all = "lowercase")] pub enum Direction { diff --git a/src/model/user.rs b/src/model/user.rs index a95e646..2cc97f0 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -100,21 +100,14 @@ impl User { let stmt = client .prepare_cached("select id,name from users limit $1 offset $2") .await?; - let stmt_count = client - .prepare_cached("select count(*) from users limit $1 offset $2") - .await?; + let stmt_count = client.prepare_cached("select count(*) from users").await?; let users = client .query(&stmt, &[&pagination.limit(), &pagination.offset()]) .await? .into_iter() .map(User::from) .collect(); - let count = count( - client, - &stmt_count, - &[&pagination.limit(), &pagination.offset()], - ) - .await?; + let count = count(client, &stmt_count, &[]).await?; Ok(Pagination::new(users, count, pagination)) } diff --git a/tests/integration/account-list.hurl b/tests/integration/account-list.hurl new file mode 100644 index 0000000..76bd8fd --- /dev/null +++ b/tests/integration/account-list.hurl @@ -0,0 +1,25 @@ +POST {{host}}/api/login +{ + "name": "user1", + "password": "this-is-a-password" +} +HTTP 200 + +[Captures] +token: jsonpath "$.token" + +GET {{host}}/api/accounts?limit=50 +Authorization: Bearer {{token}} +HTTP 200 +[Asserts] +jsonpath "$.pagination.total" == 7 +jsonpath "$.pagination.limit" == 50 +jsonpath "$.pagination.offset" == 0 +jsonpath "$.result" isCollection +jsonpath "$.result[0].name" == "user1" +jsonpath "$.result[1].name" == "user2" +jsonpath "$.result[2].name" == "user3" +jsonpath "$.result[3].name" == "user4" +jsonpath "$.result[4].name" == "user5" +jsonpath "$.result[5].name" == "user6" +jsonpath "$.result[6].name" == "test-user" diff --git a/tests/integration/register.hurl b/tests/integration/register.hurl index d616f02..3845b06 100644 --- a/tests/integration/register.hurl +++ b/tests/integration/register.hurl @@ -18,3 +18,10 @@ POST {{host}}/api/login "password": "this-is-a-test" } HTTP 200 + +[Captures] +token: jsonpath "$.token" + +GET {{host}}/api/users/@me +Authorization: Bearer {{token}} +HTTP 200 diff --git a/tests/integration/user-list.hurl b/tests/integration/user-list.hurl new file mode 100644 index 0000000..d99b215 --- /dev/null +++ b/tests/integration/user-list.hurl @@ -0,0 +1,25 @@ +POST {{host}}/api/login +{ + "name": "user1", + "password": "this-is-a-password" +} +HTTP 200 + +[Captures] +token: jsonpath "$.token" + +GET {{host}}/api/users?limit=50 +Authorization: Bearer {{token}} +HTTP 200 +[Asserts] +jsonpath "$.pagination.total" == 7 +jsonpath "$.pagination.limit" == 50 +jsonpath "$.pagination.offset" == 0 +jsonpath "$.result" isCollection +jsonpath "$.result[0].name" == "user1" +jsonpath "$.result[1].name" == "user2" +jsonpath "$.result[2].name" == "user3" +jsonpath "$.result[3].name" == "user4" +jsonpath "$.result[4].name" == "user5" +jsonpath "$.result[5].name" == "user6" +jsonpath "$.result[6].name" == "test-user"