mirror of
https://github.com/actix/examples
synced 2025-06-29 02:10:36 +02:00
Update db/basic, db/sqlx_todo and db/r2d2 to v4 (#506)
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
use actix_web::{web, Error as AWError};
|
||||
use actix_web::{error::InternalError, http::StatusCode, web};
|
||||
use failure::Error;
|
||||
use futures::{Future, TryFutureExt};
|
||||
use rusqlite::{Statement, NO_PARAMS};
|
||||
use rusqlite::Statement;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{thread::sleep, time::Duration};
|
||||
|
||||
@ -23,10 +22,10 @@ pub enum Queries {
|
||||
GetTopTenColdestMonths,
|
||||
}
|
||||
|
||||
pub fn execute(
|
||||
pub async fn execute(
|
||||
pool: &Pool,
|
||||
query: Queries,
|
||||
) -> impl Future<Output = Result<Vec<WeatherAgg>, AWError>> {
|
||||
) -> Result<Vec<WeatherAgg>, InternalError<Error>> {
|
||||
let pool = pool.clone();
|
||||
web::block(move || {
|
||||
// simulate an expensive query, see comments at top of main.rs
|
||||
@ -40,7 +39,9 @@ pub fn execute(
|
||||
};
|
||||
result.map_err(Error::from)
|
||||
})
|
||||
.map_err(AWError::from)
|
||||
.await
|
||||
.unwrap()
|
||||
.map_err(|e| InternalError::new(e, StatusCode::INTERNAL_SERVER_ERROR))
|
||||
}
|
||||
|
||||
fn get_hottest_years(conn: Connection) -> WeatherAggResult {
|
||||
@ -73,7 +74,7 @@ fn get_coldest_years(conn: Connection) -> WeatherAggResult {
|
||||
|
||||
fn get_rows_as_annual_agg(mut statement: Statement) -> WeatherAggResult {
|
||||
statement
|
||||
.query_map(NO_PARAMS, |row| {
|
||||
.query_map([], |row| {
|
||||
Ok(WeatherAgg::AnnualAgg {
|
||||
year: row.get(0)?,
|
||||
total: row.get(1)?,
|
||||
@ -112,7 +113,7 @@ fn get_coldest_months(conn: Connection) -> WeatherAggResult {
|
||||
|
||||
fn get_rows_as_month_agg(mut statement: Statement) -> WeatherAggResult {
|
||||
statement
|
||||
.query_map(NO_PARAMS, |row| {
|
||||
.query_map([], |row| {
|
||||
Ok(WeatherAgg::MonthAgg {
|
||||
year: row.get(0)?,
|
||||
month: row.get(1)?,
|
||||
|
@ -61,7 +61,7 @@ async fn main() -> io::Result<()> {
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
// store db pool as Data object
|
||||
.data(pool.clone())
|
||||
.app_data(web::Data::new(pool.clone()))
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(
|
||||
web::resource("/asyncio_weather").route(web::get().to(asyncio_weather)),
|
||||
|
Reference in New Issue
Block a user