Skip to content

Commit daed220

Browse files
committed
Extract sub-states in insertion handlers
1 parent b3c0a71 commit daed220

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/handlers/insert/api.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use crate::db::write;
1+
use crate::db::{write, Database};
22
use crate::errors::{Error, JsonErrorResponse};
33
use crate::id::Id;
4-
use crate::AppState;
54
use axum::extract::State;
65
use axum::Json;
76
use rand::Rng;
@@ -38,7 +37,7 @@ impl From<Entry> for write::Entry {
3837
}
3938

4039
pub async fn post(
41-
state: State<AppState>,
40+
State(db): State<Database>,
4241
Json(entry): Json<Entry>,
4342
) -> Result<Json<RedirectResponse>, JsonErrorResponse> {
4443
let id: Id = tokio::task::spawn_blocking(|| {
@@ -51,7 +50,7 @@ pub async fn post(
5150

5251
let entry: write::Entry = entry.into();
5352
let path = format!("/{}", id.to_url_path(&entry));
54-
state.db.insert(id, entry).await?;
53+
db.insert(id, entry).await?;
5554

5655
Ok(Json::from(RedirectResponse { path }))
5756
}

src/handlers/insert/form.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::db::write;
1+
use crate::db::{write, Database};
22
use crate::handlers::html::make_error;
33
use crate::id::Id;
4-
use crate::{AppState, Error};
4+
use crate::{Error, Page};
55
use axum::extract::{Form, State};
66
use axum::http::HeaderMap;
77
use axum::response::{IntoResponse, Redirect};
@@ -43,7 +43,8 @@ impl From<Entry> for write::Entry {
4343
}
4444

4545
pub async fn post(
46-
state: State<AppState>,
46+
State(page): State<Page>,
47+
State(db): State<Database>,
4748
jar: SignedCookieJar,
4849
headers: HeaderMap,
4950
Form(entry): Form<Entry>,
@@ -77,7 +78,7 @@ pub async fn post(
7778
.parse::<i64>()
7879
.map_err(|err| Error::CookieParsing(err.to_string()))?
7980
} else {
80-
state.db.next_uid().await?
81+
db.next_uid().await?
8182
};
8283

8384
let mut entry: write::Entry = entry.into();
@@ -89,7 +90,7 @@ pub async fn post(
8990
url = format!("burn/{url}");
9091
}
9192

92-
state.db.insert(id, entry).await?;
93+
db.insert(id, entry).await?;
9394
let url = format!("/{url}");
9495

9596
let cookie = Cookie::build(("uid", uid.to_string()))
@@ -101,7 +102,7 @@ pub async fn post(
101102
Ok((jar.add(cookie), Redirect::to(&url)))
102103
}
103104
.await
104-
.map_err(|err| make_error(err, state.page.clone()))
105+
.map_err(|err| make_error(err, page))
105106
}
106107

107108
#[cfg(test)]

0 commit comments

Comments
 (0)