send back errors if database is locked
This commit is contained in:
parent
3acd78e76c
commit
28ea420ecb
1 changed files with 49 additions and 31 deletions
80
src/main.rs
80
src/main.rs
|
|
@ -4,7 +4,7 @@ use askama::Template;
|
|||
use axum::{
|
||||
Router,
|
||||
extract::{MatchedPath, Path, State},
|
||||
http::Request,
|
||||
http::{Request, StatusCode},
|
||||
response::Html,
|
||||
routing::{get, post},
|
||||
};
|
||||
|
|
@ -169,67 +169,85 @@ async fn main() -> Result<(), std::io::Error> {
|
|||
axum::serve(listener, app).await
|
||||
}
|
||||
|
||||
fn get_foods(conn: &ConnState) -> Vec<Food> {
|
||||
fn get_foods(conn: &ConnState) -> rusqlite::Result<Vec<Food>> {
|
||||
let conn = conn.lock();
|
||||
let mut stmt = PreparedStatements::get_foods(&conn);
|
||||
let foods: Vec<_> = stmt
|
||||
.query_map((), Food::from_row)
|
||||
.unwrap()
|
||||
.collect::<Result<_, _>>()
|
||||
.unwrap();
|
||||
.query_map((), Food::from_row)?
|
||||
.collect::<Result<_, _>>()?;
|
||||
debug!(num_foods = foods.len());
|
||||
foods
|
||||
Ok(foods)
|
||||
}
|
||||
|
||||
fn get_sum(conn: &Arc<Mutex<Connection>>) -> i32 {
|
||||
fn get_sum(conn: &Arc<Mutex<Connection>>) -> rusqlite::Result<i32> {
|
||||
let conn = conn.lock();
|
||||
let mut stmt = PreparedStatements::get_sum(&conn);
|
||||
let sum = stmt.query_one((), |row| row.get(0)).unwrap();
|
||||
let sum = stmt.query_one((), |row| row.get(0))?;
|
||||
debug!(sum);
|
||||
sum
|
||||
Ok(sum)
|
||||
}
|
||||
|
||||
async fn root(State(conn): State<ConnState>) -> Html<String> {
|
||||
let foods = get_foods(&conn);
|
||||
let sum = get_sum(&conn);
|
||||
async fn root(State(conn): State<ConnState>) -> Result<Html<String>, StatusCode> {
|
||||
let foods = get_foods(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let sum = get_sum(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let index = IndexTemplate { foods, sum };
|
||||
Html(index.render().unwrap())
|
||||
Ok(Html(
|
||||
index
|
||||
.render()
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
|
||||
))
|
||||
}
|
||||
|
||||
fn do_increase(conn: &Arc<Mutex<Connection>>, id: i32) {
|
||||
fn do_increase(conn: &Arc<Mutex<Connection>>, id: i32) -> rusqlite::Result<()> {
|
||||
let conn = conn.lock();
|
||||
let mut stmt = PreparedStatements::increase(&conn);
|
||||
let new: i32 = stmt.query_one((id,), |row| row.get(0)).unwrap();
|
||||
let new: i32 = stmt.query_one((id,), |row| row.get(0))?;
|
||||
debug!(id, new_serving_count = new, "increase");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_food(conn: &Arc<Mutex<Connection>>, id: i32) -> Food {
|
||||
fn get_food(conn: &Arc<Mutex<Connection>>, id: i32) -> rusqlite::Result<Food> {
|
||||
let conn = conn.lock();
|
||||
let mut stmt = PreparedStatements::get_food(&conn);
|
||||
let food = stmt.query_one((id,), Food::from_row).unwrap();
|
||||
let food = stmt.query_one((id,), Food::from_row)?;
|
||||
debug!(?food);
|
||||
food
|
||||
Ok(food)
|
||||
}
|
||||
|
||||
async fn increase(State(conn): State<Arc<Mutex<Connection>>>, Path(id): Path<i32>) -> Html<String> {
|
||||
do_increase(&conn, id);
|
||||
let food = get_food(&conn, id);
|
||||
let sum = get_sum(&conn);
|
||||
async fn increase(
|
||||
State(conn): State<Arc<Mutex<Connection>>>,
|
||||
Path(id): Path<i32>,
|
||||
) -> Result<Html<String>, StatusCode> {
|
||||
do_increase(&conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let food = get_food(&conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let sum = get_sum(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let update = FoodUpdateTemplate { food, sum };
|
||||
Html(update.render().unwrap())
|
||||
Ok(Html(
|
||||
update
|
||||
.render()
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
|
||||
))
|
||||
}
|
||||
|
||||
fn do_decrease(conn: &Arc<Mutex<Connection>>, id: i32) {
|
||||
fn do_decrease(conn: &Arc<Mutex<Connection>>, id: i32) -> rusqlite::Result<()> {
|
||||
let conn = conn.lock();
|
||||
let mut stmt = PreparedStatements::decrease(&conn);
|
||||
let new: i32 = stmt.query_one((id,), |row| row.get(0)).unwrap();
|
||||
let new: i32 = stmt.query_one((id,), |row| row.get(0))?;
|
||||
debug!(id, new_serving_count = new, "decrease");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn decrease(State(conn): State<Arc<Mutex<Connection>>>, Path(id): Path<i32>) -> Html<String> {
|
||||
do_decrease(&conn, id);
|
||||
let food = get_food(&conn, id);
|
||||
let sum = get_sum(&conn);
|
||||
async fn decrease(
|
||||
State(conn): State<Arc<Mutex<Connection>>>,
|
||||
Path(id): Path<i32>,
|
||||
) -> Result<Html<String>, StatusCode> {
|
||||
do_decrease(&conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let food = get_food(&conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let sum = get_sum(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let update = FoodUpdateTemplate { food, sum };
|
||||
Html(update.render().unwrap())
|
||||
Ok(Html(
|
||||
update
|
||||
.render()
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
|
||||
))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue