diff --git a/src/routes/accounts.rs b/src/routes/accounts.rs index 3e1b5705..29445eab 100644 --- a/src/routes/accounts.rs +++ b/src/routes/accounts.rs @@ -46,7 +46,7 @@ pub async fn show(conn: &mut Conn, account: Account) -> Json { Json(account) } -pub async fn index(conn: &mut Conn, (user, db): (User, Db)) -> Result { +pub async fn index(_: &mut Conn, (user, db): (User, Db)) -> Result { let accounts = if user.is_admin() { Accounts::find().all(&db).await? } else { @@ -57,10 +57,6 @@ pub async fn index(conn: &mut Conn, (user, db): (User, Db)) -> Result Result>, Error> { _ => {} } - let queue = find - .order_by_desc(Column::UpdatedAt) + find.order_by_desc(Column::UpdatedAt) .limit(100) .all(&db) - .await?; - - if let Some(first) = queue.first() { - conn.set_last_modified(first.updated_at.into()); - } - - Ok(Json(queue)) + .await + .map(Json) + .map_err(Error::from) } async fn show(conn: &mut Conn, queue_job: Model) -> Json { diff --git a/src/routes/aggregators.rs b/src/routes/aggregators.rs index 65a62b37..3f4f7444 100644 --- a/src/routes/aggregators.rs +++ b/src/routes/aggregators.rs @@ -12,6 +12,7 @@ use sea_orm::{ }; use trillium::{Conn, Handler, Status}; use trillium_api::{FromConn, Json}; +use trillium_caching_headers::CachingHeadersExt; use trillium_router::RouterConnExt; use uuid::Uuid; @@ -62,7 +63,8 @@ impl FromConn for Aggregator { } } -pub async fn show(_: &mut Conn, aggregator: Aggregator) -> Json { +pub async fn show(conn: &mut Conn, aggregator: Aggregator) -> Json { + conn.set_last_modified(aggregator.updated_at.into()); Json(aggregator) } diff --git a/src/routes/api_tokens.rs b/src/routes/api_tokens.rs index b59b996d..fd9e7bf3 100644 --- a/src/routes/api_tokens.rs +++ b/src/routes/api_tokens.rs @@ -6,26 +6,20 @@ use crate::{ user::User, Db, }; -use sea_orm::{prelude::*, ActiveModelTrait, ModelTrait}; +use sea_orm::{prelude::*, ActiveModelTrait, ModelTrait, QueryOrder}; use trillium::{Conn, Handler, Status}; use trillium_api::{FromConn, Json}; -use trillium_caching_headers::CachingHeadersExt; use trillium_router::RouterConnExt; -pub async fn index(conn: &mut Conn, (account, db): (Account, Db)) -> Result { - let api_tokens = account +pub async fn index(_: &mut Conn, (account, db): (Account, Db)) -> Result { + account .find_related(ApiTokens) .filter(ApiTokenColumn::DeletedAt.is_null()) + .order_by_desc(ApiTokenColumn::CreatedAt) .all(&db) - .await?; - if let Some(last_modified) = api_tokens - .iter() - .map(|api_token| api_token.updated_at) - .max() - { - conn.set_last_modified(last_modified.into()); - } - Ok(Json(api_tokens)) + .await + .map(Json) + .map_err(Error::from) } #[trillium::async_trait] diff --git a/src/routes/memberships.rs b/src/routes/memberships.rs index a0caa308..c359c503 100644 --- a/src/routes/memberships.rs +++ b/src/routes/memberships.rs @@ -12,19 +12,16 @@ use sea_orm::{ }; use trillium::{Conn, Handler, Status}; use trillium_api::Json; -use trillium_caching_headers::CachingHeadersExt; + use trillium_router::RouterConnExt; -pub async fn index(conn: &mut Conn, (account, db): (Account, Db)) -> Result { - let memberships = account.find_related(Memberships).all(&db).await?; - if let Some(last_modified) = memberships - .iter() - .map(|membership| membership.created_at) - .max() - { - conn.set_last_modified(last_modified.into()); - } - Ok(Json(memberships)) +pub async fn index(_: &mut Conn, (account, db): (Account, Db)) -> Result { + account + .find_related(Memberships) + .all(&db) + .await + .map_err(Error::from) + .map(Json) } pub async fn create( diff --git a/src/routes/tasks.rs b/src/routes/tasks.rs index b2cbf676..e09c956a 100644 --- a/src/routes/tasks.rs +++ b/src/routes/tasks.rs @@ -14,12 +14,13 @@ use trillium_caching_headers::CachingHeadersExt; use trillium_client::Client; use trillium_router::RouterConnExt; -pub async fn index(conn: &mut Conn, (account, db): (Account, Db)) -> Result { - let tasks = account.find_related(Tasks).all(&db).await?; - if let Some(last_modified) = tasks.iter().map(|task| task.updated_at).max() { - conn.set_last_modified(last_modified.into()); - } - Ok(Json(tasks)) +pub async fn index(_: &mut Conn, (account, db): (Account, Db)) -> Result { + account + .find_related(Tasks) + .all(&db) + .await + .map(Json) + .map_err(Error::from) } #[trillium::async_trait] diff --git a/tests/api_tokens.rs b/tests/api_tokens.rs index a6577f8a..142582e7 100644 --- a/tests/api_tokens.rs +++ b/tests/api_tokens.rs @@ -29,7 +29,7 @@ mod index { assert_ok!(conn); let api_tokens: Vec = conn.response_json().await; - assert_same_json_representation(&api_tokens, &vec![token1, token2]); + assert_same_json_representation(&api_tokens, &vec![token2, token1]); Ok(()) } @@ -86,7 +86,7 @@ mod index { assert_ok!(conn); let api_tokens: Vec = conn.response_json().await; - assert_same_json_representation(&api_tokens, &vec![api_token1, api_token2]); + assert_same_json_representation(&api_tokens, &vec![api_token2, api_token1]); Ok(()) } }