Make use of pool.acquire to prevent out-of-order db-actions
This commit is contained in:
parent
f3d08aab80
commit
271b24b70d
8 changed files with 248 additions and 131 deletions
src/handlers/v1
|
@ -1,10 +1,10 @@
|
|||
use crate::db::errors::DatabaseError;
|
||||
use actix_web::{delete, get, post, put, web, HttpResponse, Responder};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::pool::PoolConnection;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use std::borrow::Borrow;
|
||||
use std::convert::TryFrom;
|
||||
use futures::future;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
use crate::db::models::{Periods, Schedule};
|
||||
use crate::db::schedules::*;
|
||||
|
@ -23,31 +23,14 @@ pub struct RequestSchedule {
|
|||
|
||||
#[get("/api/v1/schedules")]
|
||||
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> impl Responder {
|
||||
let schedules = get_schedules(&pool).await;
|
||||
|
||||
if let Err(err) = schedules {
|
||||
return HttpResponse::from(err);
|
||||
let pool_conn = pool.acquire().await;
|
||||
if let Err(err) = pool_conn {
|
||||
return HttpResponse::from(DatabaseError::from(err));
|
||||
}
|
||||
let schedules = schedules.unwrap();
|
||||
let mut pool_conn = pool_conn.unwrap();
|
||||
|
||||
let mut return_schedules: Vec<ReturnSchedule> = schedules.iter().map(ReturnSchedule::from).collect();
|
||||
for schedule in return_schedules.iter_mut() {
|
||||
schedule.load_tags(&pool);
|
||||
}
|
||||
let schedules = get_schedules(&mut pool_conn).await;
|
||||
|
||||
HttpResponse::Ok().json(return_schedules)
|
||||
}
|
||||
|
||||
#[get("/api/v1/schedules/tag/{tag}")]
|
||||
pub async fn tagged(pool: web::Data<Pool<Sqlite>>, path: web::Path<(String,)>) -> impl Responder {
|
||||
let (tag,) = path.into_inner();
|
||||
let tag_db = get_tag(&pool, &tag).await;
|
||||
if let Err(err) = tag_db {
|
||||
return HttpResponse::from(err);
|
||||
}
|
||||
let tag_db = tag_db.unwrap();
|
||||
|
||||
let schedules = get_schedules_by_tag(&pool, &tag_db).await;
|
||||
if let Err(err) = schedules {
|
||||
return HttpResponse::from(err);
|
||||
}
|
||||
|
@ -56,25 +39,61 @@ pub async fn tagged(pool: web::Data<Pool<Sqlite>>, path: web::Path<(String,)>) -
|
|||
let mut return_schedules: Vec<ReturnSchedule> =
|
||||
schedules.iter().map(ReturnSchedule::from).collect();
|
||||
for schedule in return_schedules.iter_mut() {
|
||||
schedule.load_tags(&pool);
|
||||
schedule.load_tags(&mut pool_conn);
|
||||
}
|
||||
|
||||
HttpResponse::Ok().json(return_schedules)
|
||||
}
|
||||
|
||||
#[get("/api/v1/schedules/tag/{tag}")]
|
||||
pub async fn tagged(pool: web::Data<Pool<Sqlite>>, path: web::Path<(String,)>) -> impl Responder {
|
||||
let pool_conn = pool.acquire().await;
|
||||
if let Err(err) = pool_conn {
|
||||
return HttpResponse::from(DatabaseError::from(err));
|
||||
}
|
||||
let mut pool_conn = pool_conn.unwrap();
|
||||
|
||||
let (tag,) = path.into_inner();
|
||||
let tag_db = get_tag(&mut pool_conn, &tag).await;
|
||||
if let Err(err) = tag_db {
|
||||
return HttpResponse::from(err);
|
||||
}
|
||||
let tag_db = tag_db.unwrap();
|
||||
|
||||
let schedules = get_schedules_by_tag(&mut pool_conn, &tag_db).await;
|
||||
if let Err(err) = schedules {
|
||||
return HttpResponse::from(err);
|
||||
}
|
||||
let schedules = schedules.unwrap();
|
||||
|
||||
let mut return_schedules: Vec<ReturnSchedule> =
|
||||
schedules.iter().map(ReturnSchedule::from).collect();
|
||||
for schedule in return_schedules.iter_mut() {
|
||||
schedule.load_tags(&mut pool_conn);
|
||||
}
|
||||
HttpResponse::Ok().json(return_schedules)
|
||||
}
|
||||
|
||||
#[get("/api/v1/schedules/{schedule_id}")]
|
||||
pub async fn show(pool: web::Data<Pool<Sqlite>>, path: web::Path<(String,)>) -> impl Responder {
|
||||
let pool_conn = pool.acquire().await;
|
||||
if let Err(err) = pool_conn {
|
||||
return HttpResponse::from(DatabaseError::from(err));
|
||||
}
|
||||
let mut pool_conn = pool_conn.unwrap();
|
||||
|
||||
let (schedule_uid,) = path.into_inner();
|
||||
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(HandlerError::BadUid));
|
||||
|
||||
match emgauwa_uid {
|
||||
Ok(uid) => {
|
||||
let schedule = get_schedule_by_uid(&pool, &uid).await;
|
||||
let schedule = get_schedule_by_uid(&mut pool_conn, &uid).await;
|
||||
match schedule {
|
||||
Ok(ok) => {
|
||||
let mut return_schedule = ReturnSchedule::from(ok);
|
||||
return_schedule.load_tags(&pool);
|
||||
return_schedule.load_tags(&mut pool_conn);
|
||||
HttpResponse::Ok().json(return_schedule)
|
||||
},
|
||||
}
|
||||
Err(err) => HttpResponse::from(err),
|
||||
}
|
||||
}
|
||||
|
@ -83,40 +102,63 @@ pub async fn show(pool: web::Data<Pool<Sqlite>>, path: web::Path<(String,)>) ->
|
|||
}
|
||||
|
||||
#[post("/api/v1/schedules")]
|
||||
pub async fn add(pool: web::Data<Pool<Sqlite>>, data: web::Json<RequestSchedule>) -> impl Responder {
|
||||
let new_schedule = create_schedule(&pool, &data.name, &data.periods).await;
|
||||
pub async fn add(
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
data: web::Json<RequestSchedule>,
|
||||
) -> impl Responder {
|
||||
let pool_conn = pool.acquire().await;
|
||||
if let Err(err) = pool_conn {
|
||||
return HttpResponse::from(DatabaseError::from(err));
|
||||
}
|
||||
let mut pool_conn = pool_conn.unwrap();
|
||||
|
||||
let new_schedule = create_schedule(&mut pool_conn, &data.name, &data.periods).await;
|
||||
|
||||
if let Err(err) = new_schedule {
|
||||
return HttpResponse::from(err);
|
||||
}
|
||||
let new_schedule = new_schedule.unwrap();
|
||||
|
||||
let result = set_schedule_tags(&pool, &new_schedule, data.tags.as_slice()).await;
|
||||
let result = set_schedule_tags(&mut pool_conn, &new_schedule, data.tags.as_slice()).await;
|
||||
if let Err(err) = result {
|
||||
return HttpResponse::from(err);
|
||||
}
|
||||
|
||||
let mut return_schedule = ReturnSchedule::from(new_schedule);
|
||||
return_schedule.load_tags(&pool);
|
||||
return_schedule.load_tags(&mut pool_conn);
|
||||
HttpResponse::Created().json(return_schedule)
|
||||
}
|
||||
|
||||
async fn add_list_single(pool: &Pool<Sqlite>, request_schedule: &RequestSchedule) -> Result<Schedule, DatabaseError> {
|
||||
let new_schedule = create_schedule(pool, &request_schedule.name, &request_schedule.periods).await?;
|
||||
async fn add_list_single(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
request_schedule: &RequestSchedule,
|
||||
) -> Result<Schedule, DatabaseError> {
|
||||
let new_schedule =
|
||||
create_schedule(conn, &request_schedule.name, &request_schedule.periods).await?;
|
||||
|
||||
set_schedule_tags(pool, &new_schedule, request_schedule.tags.as_slice()).await?;
|
||||
set_schedule_tags(conn, &new_schedule, request_schedule.tags.as_slice()).await?;
|
||||
|
||||
Ok(new_schedule)
|
||||
}
|
||||
|
||||
#[post("/api/v1/schedules/list")]
|
||||
pub async fn add_list(pool: web::Data<Pool<Sqlite>>, data: web::Json<Vec<RequestSchedule>>) -> impl Responder {
|
||||
let result: Vec<Result<Schedule, DatabaseError>> = future::join_all(
|
||||
data
|
||||
pub async fn add_list(
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
data: web::Json<Vec<RequestSchedule>>,
|
||||
) -> impl Responder {
|
||||
let pool_conn = pool.acquire().await;
|
||||
if let Err(err) = pool_conn {
|
||||
return HttpResponse::from(DatabaseError::from(err));
|
||||
}
|
||||
let mut pool_conn = pool_conn.unwrap();
|
||||
|
||||
let result: Vec<Result<Schedule, DatabaseError>> = data
|
||||
.as_slice()
|
||||
.iter()
|
||||
.map(|request_schedule| add_list_single(&pool, request_schedule))
|
||||
).await;
|
||||
.map(|request_schedule| {
|
||||
futures::executor::block_on(add_list_single(&mut pool_conn, request_schedule))
|
||||
})
|
||||
.collect();
|
||||
|
||||
match vec_has_error(&result) {
|
||||
true => HttpResponse::from(
|
||||
|
@ -133,7 +175,7 @@ pub async fn add_list(pool: web::Data<Pool<Sqlite>>, data: web::Json<Vec<Request
|
|||
.collect();
|
||||
|
||||
for schedule in return_schedules.iter_mut() {
|
||||
schedule.load_tags(&pool);
|
||||
schedule.load_tags(&mut pool_conn);
|
||||
}
|
||||
HttpResponse::Created().json(return_schedules)
|
||||
}
|
||||
|
@ -146,6 +188,12 @@ pub async fn update(
|
|||
path: web::Path<(String,)>,
|
||||
data: web::Json<RequestSchedule>,
|
||||
) -> impl Responder {
|
||||
let pool_conn = pool.acquire().await;
|
||||
if let Err(err) = pool_conn {
|
||||
return HttpResponse::from(DatabaseError::from(err));
|
||||
}
|
||||
let mut pool_conn = pool_conn.unwrap();
|
||||
|
||||
let (schedule_uid,) = path.into_inner();
|
||||
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(HandlerError::BadUid));
|
||||
if let Err(err) = emgauwa_uid {
|
||||
|
@ -153,30 +201,42 @@ pub async fn update(
|
|||
}
|
||||
let emgauwa_uid = emgauwa_uid.unwrap();
|
||||
|
||||
let schedule = get_schedule_by_uid(&pool, &emgauwa_uid, ).await;
|
||||
let schedule = get_schedule_by_uid(&mut pool_conn, &emgauwa_uid).await;
|
||||
if let Err(err) = schedule {
|
||||
return HttpResponse::from(err);
|
||||
}
|
||||
let schedule = schedule.unwrap();
|
||||
|
||||
let schedule = update_schedule(&pool, &schedule, data.name.as_str(), data.periods.borrow()).await;
|
||||
let schedule = update_schedule(
|
||||
&mut pool_conn,
|
||||
&schedule,
|
||||
data.name.as_str(),
|
||||
data.periods.borrow(),
|
||||
)
|
||||
.await;
|
||||
if let Err(err) = schedule {
|
||||
return HttpResponse::from(err);
|
||||
}
|
||||
let schedule = schedule.unwrap();
|
||||
|
||||
let result = set_schedule_tags(&pool, &schedule, data.tags.as_slice()).await;
|
||||
let result = set_schedule_tags(&mut pool_conn, &schedule, data.tags.as_slice()).await;
|
||||
if let Err(err) = result {
|
||||
return HttpResponse::from(err);
|
||||
}
|
||||
|
||||
let mut return_schedule = ReturnSchedule::from(schedule);
|
||||
return_schedule.load_tags(&pool);
|
||||
return_schedule.load_tags(&mut pool_conn);
|
||||
HttpResponse::Ok().json(return_schedule)
|
||||
}
|
||||
|
||||
#[delete("/api/v1/schedules/{schedule_id}")]
|
||||
pub async fn delete(pool: web::Data<Pool<Sqlite>>, path: web::Path<(String,)>) -> impl Responder {
|
||||
let pool_conn = pool.acquire().await;
|
||||
if let Err(err) = pool_conn {
|
||||
return HttpResponse::from(DatabaseError::from(err));
|
||||
}
|
||||
let mut pool_conn = pool_conn.unwrap();
|
||||
|
||||
let (schedule_uid,) = path.into_inner();
|
||||
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(HandlerError::BadUid));
|
||||
|
||||
|
@ -184,7 +244,7 @@ pub async fn delete(pool: web::Data<Pool<Sqlite>>, path: web::Path<(String,)>) -
|
|||
Ok(uid) => match uid {
|
||||
EmgauwaUid::Off => HttpResponse::from(HandlerError::ProtectedSchedule),
|
||||
EmgauwaUid::On => HttpResponse::from(HandlerError::ProtectedSchedule),
|
||||
EmgauwaUid::Any(_) => match delete_schedule_by_uid(&pool, uid).await {
|
||||
EmgauwaUid::Any(_) => match delete_schedule_by_uid(&mut pool_conn, uid).await {
|
||||
Ok(_) => HttpResponse::Ok().json("schedule got deleted"),
|
||||
Err(err) => HttpResponse::from(err),
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue