169 lines
4.8 KiB
Rust
169 lines
4.8 KiB
Rust
use actix_web::{delete, get, post, put, web, HttpResponse};
|
|
use emgauwa_lib::db::{DbSchedule, DbTag};
|
|
use emgauwa_lib::errors::{ApiError, DatabaseError, EmgauwaError};
|
|
use emgauwa_lib::models::{convert_db_list, FromDbModel, Schedule};
|
|
use emgauwa_lib::types::{RequestCreateSchedule, RequestUpdateSchedule, ScheduleUid};
|
|
use sqlx::pool::PoolConnection;
|
|
use sqlx::{Pool, Sqlite};
|
|
|
|
#[get("/api/v1/schedules")]
|
|
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, EmgauwaError> {
|
|
let mut pool_conn = pool.acquire().await?;
|
|
|
|
let db_schedules = DbSchedule::get_all(&mut pool_conn).await?;
|
|
let schedules: Vec<Schedule> = convert_db_list(&mut pool_conn, db_schedules)?;
|
|
|
|
Ok(HttpResponse::Ok().json(schedules))
|
|
}
|
|
|
|
#[get("/api/v1/schedules/tag/{tag}")]
|
|
pub async fn tagged(
|
|
pool: web::Data<Pool<Sqlite>>,
|
|
path: web::Path<(String,)>,
|
|
) -> Result<HttpResponse, EmgauwaError> {
|
|
let mut pool_conn = pool.acquire().await?;
|
|
|
|
let (tag,) = path.into_inner();
|
|
let tag_db = DbTag::get_by_tag(&mut pool_conn, &tag)
|
|
.await?
|
|
.ok_or(DatabaseError::NotFound)?;
|
|
|
|
let db_schedules = DbSchedule::get_by_tag(&mut pool_conn, &tag_db).await?;
|
|
let schedules: Vec<Schedule> = convert_db_list(&mut pool_conn, db_schedules)?;
|
|
|
|
Ok(HttpResponse::Ok().json(schedules))
|
|
}
|
|
|
|
#[get("/api/v1/schedules/{schedule_id}")]
|
|
pub async fn show(
|
|
pool: web::Data<Pool<Sqlite>>,
|
|
path: web::Path<(String,)>,
|
|
) -> Result<HttpResponse, EmgauwaError> {
|
|
let mut pool_conn = pool.acquire().await?;
|
|
|
|
let (schedule_uid,) = path.into_inner();
|
|
let uid = ScheduleUid::try_from(schedule_uid.as_str())?;
|
|
|
|
let schedule = DbSchedule::get_by_uid(&mut pool_conn, &uid)
|
|
.await?
|
|
.ok_or(DatabaseError::NotFound)?;
|
|
|
|
let return_schedule = Schedule::from_db_model(&mut pool_conn, schedule)?;
|
|
Ok(HttpResponse::Ok().json(return_schedule))
|
|
}
|
|
|
|
#[post("/api/v1/schedules")]
|
|
pub async fn add(
|
|
pool: web::Data<Pool<Sqlite>>,
|
|
data: web::Json<RequestCreateSchedule>,
|
|
) -> Result<HttpResponse, EmgauwaError> {
|
|
let mut pool_conn = pool.acquire().await?;
|
|
|
|
let new_schedule = DbSchedule::create(
|
|
&mut pool_conn,
|
|
ScheduleUid::default(),
|
|
&data.name,
|
|
&data.periods,
|
|
)
|
|
.await?;
|
|
|
|
if let Some(tags) = &data.tags {
|
|
new_schedule
|
|
.set_tags(&mut pool_conn, tags.as_slice())
|
|
.await?;
|
|
}
|
|
|
|
let return_schedule = Schedule::from_db_model(&mut pool_conn, new_schedule)?;
|
|
Ok(HttpResponse::Created().json(return_schedule))
|
|
}
|
|
|
|
async fn add_list_single(
|
|
conn: &mut PoolConnection<Sqlite>,
|
|
request_schedule: &RequestCreateSchedule,
|
|
) -> Result<DbSchedule, DatabaseError> {
|
|
let new_schedule = DbSchedule::create(
|
|
conn,
|
|
ScheduleUid::default(),
|
|
&request_schedule.name,
|
|
&request_schedule.periods,
|
|
)
|
|
.await?;
|
|
|
|
if let Some(tags) = &request_schedule.tags {
|
|
new_schedule.set_tags(conn, 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<RequestCreateSchedule>>,
|
|
) -> Result<HttpResponse, EmgauwaError> {
|
|
let mut pool_conn = pool.acquire().await?;
|
|
|
|
let mut db_schedules: Vec<DbSchedule> = Vec::new();
|
|
for s in data.iter() {
|
|
let new_s = futures::executor::block_on(add_list_single(&mut pool_conn, s))?;
|
|
db_schedules.push(new_s);
|
|
}
|
|
|
|
let schedules: Vec<Schedule> = convert_db_list(&mut pool_conn, db_schedules)?;
|
|
Ok(HttpResponse::Created().json(schedules))
|
|
}
|
|
|
|
#[put("/api/v1/schedules/{schedule_id}")]
|
|
pub async fn update(
|
|
pool: web::Data<Pool<Sqlite>>,
|
|
path: web::Path<(String,)>,
|
|
data: web::Json<RequestUpdateSchedule>,
|
|
) -> Result<HttpResponse, EmgauwaError> {
|
|
let mut pool_conn = pool.acquire().await?;
|
|
|
|
let (schedule_uid,) = path.into_inner();
|
|
let uid = ScheduleUid::try_from(schedule_uid.as_str())?;
|
|
|
|
let schedule = DbSchedule::get_by_uid(&mut pool_conn, &uid)
|
|
.await?
|
|
.ok_or(DatabaseError::NotFound)?;
|
|
|
|
let name = match &data.name {
|
|
None => schedule.name.as_str(),
|
|
Some(name) => name.as_str(),
|
|
};
|
|
|
|
let periods = match &data.periods {
|
|
None => &schedule.periods,
|
|
Some(period) => period,
|
|
};
|
|
|
|
let schedule = schedule.update(&mut pool_conn, name, periods).await?;
|
|
|
|
if let Some(tags) = &data.tags {
|
|
schedule.set_tags(&mut pool_conn, tags.as_slice()).await?;
|
|
}
|
|
|
|
let return_schedule = Schedule::from_db_model(&mut pool_conn, schedule)?;
|
|
Ok(HttpResponse::Ok().json(return_schedule))
|
|
}
|
|
|
|
#[delete("/api/v1/schedules/{schedule_id}")]
|
|
pub async fn delete(
|
|
pool: web::Data<Pool<Sqlite>>,
|
|
path: web::Path<(String,)>,
|
|
) -> Result<HttpResponse, EmgauwaError> {
|
|
let mut pool_conn = pool.acquire().await?;
|
|
|
|
let (schedule_uid,) = path.into_inner();
|
|
let uid = ScheduleUid::try_from(schedule_uid.as_str())?;
|
|
|
|
match uid {
|
|
ScheduleUid::Off => Err(EmgauwaError::from(ApiError::ProtectedSchedule)),
|
|
ScheduleUid::On => Err(EmgauwaError::from(ApiError::ProtectedSchedule)),
|
|
ScheduleUid::Any(_) => {
|
|
DbSchedule::delete_by_uid(&mut pool_conn, uid).await?;
|
|
Ok(HttpResponse::Ok().json("schedule got deleted"))
|
|
}
|
|
}
|
|
}
|