Migrate to sqlx
This commit is contained in:
parent
bd44dc3183
commit
f3d08aab80
19 changed files with 1488 additions and 588 deletions
src/handlers/v1
|
@ -3,6 +3,8 @@ use actix_web::{delete, get, post, put, web, HttpResponse, Responder};
|
|||
use serde::{Deserialize, Serialize};
|
||||
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::*;
|
||||
|
@ -20,38 +22,59 @@ pub struct RequestSchedule {
|
|||
}
|
||||
|
||||
#[get("/api/v1/schedules")]
|
||||
pub async fn index() -> impl Responder {
|
||||
let schedules = get_schedules();
|
||||
let return_schedules: Vec<ReturnSchedule> =
|
||||
schedules.iter().map(ReturnSchedule::from).collect();
|
||||
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 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(&pool);
|
||||
}
|
||||
|
||||
HttpResponse::Ok().json(return_schedules)
|
||||
}
|
||||
|
||||
#[get("/api/v1/schedules/tag/{tag}")]
|
||||
pub async fn tagged(path: web::Path<(String,)>) -> impl Responder {
|
||||
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(&tag);
|
||||
if tag_db.is_err() {
|
||||
return HttpResponse::from(tag_db.unwrap_err());
|
||||
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(&tag_db);
|
||||
let return_schedules: Vec<ReturnSchedule> =
|
||||
let schedules = get_schedules_by_tag(&pool, &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(&pool);
|
||||
}
|
||||
HttpResponse::Ok().json(return_schedules)
|
||||
}
|
||||
|
||||
#[get("/api/v1/schedules/{schedule_id}")]
|
||||
pub async fn show(path: web::Path<(String,)>) -> impl Responder {
|
||||
pub async fn show(pool: web::Data<Pool<Sqlite>>, path: web::Path<(String,)>) -> impl Responder {
|
||||
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(uid);
|
||||
let schedule = get_schedule_by_uid(&pool, &uid).await;
|
||||
match schedule {
|
||||
Ok(ok) => HttpResponse::Ok().json(ReturnSchedule::from(ok)),
|
||||
Ok(ok) => {
|
||||
let mut return_schedule = ReturnSchedule::from(ok);
|
||||
return_schedule.load_tags(&pool);
|
||||
HttpResponse::Ok().json(return_schedule)
|
||||
},
|
||||
Err(err) => HttpResponse::from(err),
|
||||
}
|
||||
}
|
||||
|
@ -60,35 +83,40 @@ pub async fn show(path: web::Path<(String,)>) -> impl Responder {
|
|||
}
|
||||
|
||||
#[post("/api/v1/schedules")]
|
||||
pub async fn add(data: web::Json<RequestSchedule>) -> impl Responder {
|
||||
let new_schedule = create_schedule(&data.name, &data.periods);
|
||||
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;
|
||||
|
||||
if new_schedule.is_err() {
|
||||
return HttpResponse::from(new_schedule.unwrap_err());
|
||||
if let Err(err) = new_schedule {
|
||||
return HttpResponse::from(err);
|
||||
}
|
||||
let new_schedule = new_schedule.unwrap();
|
||||
|
||||
let result = set_schedule_tags(&new_schedule, data.tags.as_slice());
|
||||
if result.is_err() {
|
||||
return HttpResponse::from(result.unwrap_err());
|
||||
let result = set_schedule_tags(&pool, &new_schedule, data.tags.as_slice()).await;
|
||||
if let Err(err) = result {
|
||||
return HttpResponse::from(err);
|
||||
}
|
||||
|
||||
HttpResponse::Created().json(ReturnSchedule::from(new_schedule))
|
||||
let mut return_schedule = ReturnSchedule::from(new_schedule);
|
||||
return_schedule.load_tags(&pool);
|
||||
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?;
|
||||
|
||||
set_schedule_tags(pool, &new_schedule, request_schedule.tags.as_slice()).await?;
|
||||
|
||||
Ok(new_schedule)
|
||||
}
|
||||
|
||||
#[post("/api/v1/schedules/list")]
|
||||
pub async fn add_list(data: web::Json<Vec<RequestSchedule>>) -> impl Responder {
|
||||
let result: Vec<Result<Schedule, DatabaseError>> = data
|
||||
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
|
||||
.as_slice()
|
||||
.iter()
|
||||
.map(|request_schedule| {
|
||||
let new_schedule = create_schedule(&request_schedule.name, &request_schedule.periods)?;
|
||||
|
||||
set_schedule_tags(&new_schedule, request_schedule.tags.as_slice())?;
|
||||
|
||||
Ok(new_schedule)
|
||||
})
|
||||
.collect();
|
||||
.map(|request_schedule| add_list_single(&pool, request_schedule))
|
||||
).await;
|
||||
|
||||
match vec_has_error(&result) {
|
||||
true => HttpResponse::from(
|
||||
|
@ -99,10 +127,14 @@ pub async fn add_list(data: web::Json<Vec<RequestSchedule>>) -> impl Responder {
|
|||
.unwrap_err(),
|
||||
),
|
||||
false => {
|
||||
let return_schedules: Vec<ReturnSchedule> = result
|
||||
let mut return_schedules: Vec<ReturnSchedule> = result
|
||||
.iter()
|
||||
.map(|s| ReturnSchedule::from(s.as_ref().unwrap()))
|
||||
.collect();
|
||||
|
||||
for schedule in return_schedules.iter_mut() {
|
||||
schedule.load_tags(&pool);
|
||||
}
|
||||
HttpResponse::Created().json(return_schedules)
|
||||
}
|
||||
}
|
||||
|
@ -110,38 +142,41 @@ pub async fn add_list(data: web::Json<Vec<RequestSchedule>>) -> impl Responder {
|
|||
|
||||
#[put("/api/v1/schedules/{schedule_id}")]
|
||||
pub async fn update(
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
path: web::Path<(String,)>,
|
||||
data: web::Json<RequestSchedule>,
|
||||
) -> impl Responder {
|
||||
let (schedule_uid,) = path.into_inner();
|
||||
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(HandlerError::BadUid));
|
||||
if emgauwa_uid.is_err() {
|
||||
return HttpResponse::from(emgauwa_uid.unwrap_err());
|
||||
if let Err(err) = emgauwa_uid {
|
||||
return HttpResponse::from(err);
|
||||
}
|
||||
let emgauwa_uid = emgauwa_uid.unwrap();
|
||||
|
||||
let schedule = get_schedule_by_uid(emgauwa_uid);
|
||||
if schedule.is_err() {
|
||||
return HttpResponse::from(schedule.unwrap_err());
|
||||
let schedule = get_schedule_by_uid(&pool, &emgauwa_uid, ).await;
|
||||
if let Err(err) = schedule {
|
||||
return HttpResponse::from(err);
|
||||
}
|
||||
let schedule = schedule.unwrap();
|
||||
|
||||
let schedule = update_schedule(&schedule, data.name.as_str(), data.periods.borrow());
|
||||
if schedule.is_err() {
|
||||
return HttpResponse::from(schedule.unwrap_err());
|
||||
let schedule = update_schedule(&pool, &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(&schedule, data.tags.as_slice());
|
||||
if result.is_err() {
|
||||
return HttpResponse::from(result.unwrap_err());
|
||||
let result = set_schedule_tags(&pool, &schedule, data.tags.as_slice()).await;
|
||||
if let Err(err) = result {
|
||||
return HttpResponse::from(err);
|
||||
}
|
||||
|
||||
HttpResponse::Ok().json(ReturnSchedule::from(schedule))
|
||||
let mut return_schedule = ReturnSchedule::from(schedule);
|
||||
return_schedule.load_tags(&pool);
|
||||
HttpResponse::Ok().json(return_schedule)
|
||||
}
|
||||
|
||||
#[delete("/api/v1/schedules/{schedule_id}")]
|
||||
pub async fn delete(path: web::Path<(String,)>) -> impl Responder {
|
||||
pub async fn delete(pool: web::Data<Pool<Sqlite>>, path: web::Path<(String,)>) -> impl Responder {
|
||||
let (schedule_uid,) = path.into_inner();
|
||||
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(HandlerError::BadUid));
|
||||
|
||||
|
@ -149,7 +184,7 @@ pub async fn delete(path: web::Path<(String,)>) -> impl Responder {
|
|||
Ok(uid) => match uid {
|
||||
EmgauwaUid::Off => HttpResponse::from(HandlerError::ProtectedSchedule),
|
||||
EmgauwaUid::On => HttpResponse::from(HandlerError::ProtectedSchedule),
|
||||
EmgauwaUid::Any(_) => match delete_schedule_by_uid(uid) {
|
||||
EmgauwaUid::Any(_) => match delete_schedule_by_uid(&pool, 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