Skip to content

Commit

Permalink
remove last-modified-at headers from index routes
Browse files Browse the repository at this point in the history
they were resulting in cache-invalidation errors because the max updated_at timestamp did not change on older record deletion
  • Loading branch information
jbr committed Jul 13, 2023
1 parent a4ee9a8 commit d909ed6
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 47 deletions.
6 changes: 1 addition & 5 deletions src/routes/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub async fn show(conn: &mut Conn, account: Account) -> Json<Account> {
Json(account)
}

pub async fn index(conn: &mut Conn, (user, db): (User, Db)) -> Result<impl Handler, Error> {
pub async fn index(_: &mut Conn, (user, db): (User, Db)) -> Result<impl Handler, Error> {
let accounts = if user.is_admin() {
Accounts::find().all(&db).await?
} else {
Expand All @@ -57,10 +57,6 @@ pub async fn index(conn: &mut Conn, (user, db): (User, Db)) -> Result<impl Handl
.await?
};

if let Some(last_modified) = accounts.iter().map(|account| account.updated_at).max() {
conn.set_last_modified(last_modified.into());
}

Ok(Json(accounts))
}

Expand Down
13 changes: 4 additions & 9 deletions src/routes/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,12 @@ async fn index(conn: &mut Conn, db: Db) -> Result<Json<Vec<Model>>, 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<Model> {
Expand Down
4 changes: 3 additions & 1 deletion src/routes/aggregators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -62,7 +63,8 @@ impl FromConn for Aggregator {
}
}

pub async fn show(_: &mut Conn, aggregator: Aggregator) -> Json<Aggregator> {
pub async fn show(conn: &mut Conn, aggregator: Aggregator) -> Json<Aggregator> {
conn.set_last_modified(aggregator.updated_at.into());
Json(aggregator)
}

Expand Down
20 changes: 7 additions & 13 deletions src/routes/api_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<impl Handler, Error> {
let api_tokens = account
pub async fn index(_: &mut Conn, (account, db): (Account, Db)) -> Result<impl Handler, Error> {
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]
Expand Down
19 changes: 8 additions & 11 deletions src/routes/memberships.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<impl Handler, Error> {
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<impl Handler, Error> {
account
.find_related(Memberships)
.all(&db)
.await
.map_err(Error::from)
.map(Json)
}

pub async fn create(
Expand Down
13 changes: 7 additions & 6 deletions src/routes/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<impl Handler, Error> {
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<impl Handler, Error> {
account
.find_related(Tasks)
.all(&db)
.await
.map(Json)
.map_err(Error::from)
}

#[trillium::async_trait]
Expand Down
4 changes: 2 additions & 2 deletions tests/api_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod index {
assert_ok!(conn);

let api_tokens: Vec<ApiToken> = conn.response_json().await;
assert_same_json_representation(&api_tokens, &vec![token1, token2]);
assert_same_json_representation(&api_tokens, &vec![token2, token1]);
Ok(())
}

Expand Down Expand Up @@ -86,7 +86,7 @@ mod index {

assert_ok!(conn);
let api_tokens: Vec<ApiToken> = 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(())
}
}
Expand Down

0 comments on commit d909ed6

Please sign in to comment.