Add WIP relays to database and api
This commit is contained in:
parent
4e3df272c3
commit
734f8b291c
14 changed files with 387 additions and 88 deletions
emgauwa-lib/src/handlers/v1
|
@ -5,11 +5,10 @@ use sqlx::{Pool, Sqlite};
|
|||
|
||||
use crate::db::errors::DatabaseError;
|
||||
use crate::db::{Periods, Schedule};
|
||||
use crate::db::tag::Tag;
|
||||
use crate::db::Tag;
|
||||
use crate::db::types::ScheduleUid;
|
||||
use crate::handlers::errors::ApiError;
|
||||
use crate::return_models::ReturnSchedule;
|
||||
use crate::utils::vec_has_error;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct RequestSchedule {
|
||||
|
@ -24,11 +23,8 @@ pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiErr
|
|||
|
||||
let schedules = Schedule::get_all(&mut pool_conn).await?;
|
||||
|
||||
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);
|
||||
}
|
||||
let return_schedules: Vec<ReturnSchedule> =
|
||||
schedules.iter().map(|s| ReturnSchedule::from_schedule_ref(s, &mut pool_conn)).collect();
|
||||
|
||||
Ok(HttpResponse::Ok().json(return_schedules))
|
||||
}
|
||||
|
@ -45,11 +41,9 @@ pub async fn tagged(
|
|||
|
||||
let schedules = Schedule::get_by_tag(&mut pool_conn, &tag_db).await?;
|
||||
|
||||
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);
|
||||
}
|
||||
let return_schedules: Vec<ReturnSchedule> =
|
||||
schedules.iter().map(|s| ReturnSchedule::from_schedule_ref(s, &mut pool_conn)).collect();
|
||||
|
||||
Ok(HttpResponse::Ok().json(return_schedules))
|
||||
}
|
||||
|
||||
|
@ -65,8 +59,7 @@ pub async fn show(
|
|||
|
||||
let schedule = Schedule::get_by_uid(&mut pool_conn, &uid).await?;
|
||||
|
||||
let mut return_schedule = ReturnSchedule::from(schedule);
|
||||
return_schedule.load_tags(&mut pool_conn);
|
||||
let return_schedule = ReturnSchedule::from_schedule(schedule, &mut pool_conn);
|
||||
Ok(HttpResponse::Ok().json(return_schedule))
|
||||
}
|
||||
|
||||
|
@ -83,8 +76,7 @@ pub async fn add(
|
|||
.set_tags(&mut pool_conn, data.tags.as_slice())
|
||||
.await?;
|
||||
|
||||
let mut return_schedule = ReturnSchedule::from(new_schedule);
|
||||
return_schedule.load_tags(&mut pool_conn);
|
||||
let return_schedule = ReturnSchedule::from_schedule(new_schedule, &mut pool_conn);
|
||||
Ok(HttpResponse::Created().json(return_schedule))
|
||||
}
|
||||
|
||||
|
@ -117,26 +109,14 @@ pub async fn add_list(
|
|||
})
|
||||
.collect();
|
||||
|
||||
match vec_has_error(&result) {
|
||||
true => Ok(HttpResponse::from(
|
||||
result
|
||||
.into_iter()
|
||||
.find(|r| r.is_err())
|
||||
.unwrap()
|
||||
.unwrap_err(),
|
||||
)),
|
||||
false => {
|
||||
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(&mut pool_conn);
|
||||
}
|
||||
Ok(HttpResponse::Created().json(return_schedules))
|
||||
let mut return_schedules: Vec<ReturnSchedule> = Vec::new();
|
||||
for schedule in result {
|
||||
match schedule {
|
||||
Ok(schedule) => return_schedules.push(ReturnSchedule::from_schedule(schedule, &mut pool_conn)),
|
||||
Err(e) => return Ok(HttpResponse::from(e)),
|
||||
}
|
||||
}
|
||||
Ok(HttpResponse::Created().json(return_schedules))
|
||||
}
|
||||
|
||||
#[put("/api/v1/schedules/{schedule_id}")]
|
||||
|
@ -160,8 +140,7 @@ pub async fn update(
|
|||
.set_tags(&mut pool_conn, data.tags.as_slice())
|
||||
.await?;
|
||||
|
||||
let mut return_schedule = ReturnSchedule::from(schedule);
|
||||
return_schedule.load_tags(&mut pool_conn);
|
||||
let return_schedule = ReturnSchedule::from_schedule(schedule, &mut pool_conn);
|
||||
Ok(HttpResponse::Ok().json(return_schedule))
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue