Add strict mode and defaults for GET tagged schedules

This commit is contained in:
Tobias Reisinger 2024-05-06 16:27:42 +02:00
parent f9ad8f9399
commit 3c70ae1da3
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
4 changed files with 41 additions and 75 deletions
src/handlers/v1

View file

@ -3,9 +3,7 @@ use actix_web::{delete, get, post, put, web, HttpResponse};
use emgauwa_common::db::{DbController, DbJunctionRelaySchedule, DbSchedule, DbTag};
use emgauwa_common::errors::{ApiError, DatabaseError, EmgauwaError};
use emgauwa_common::models::{convert_db_list, FromDbModel, Schedule};
use emgauwa_common::types::{
ControllerWsAction, RequestScheduleCreate, RequestScheduleUpdate, ScheduleUid,
};
use emgauwa_common::types::{ControllerWsAction, RequestScheduleCreate, RequestScheduleGetTagged, RequestScheduleUpdate, ScheduleUid};
use itertools::Itertools;
use sqlx::pool::PoolConnection;
use sqlx::{Pool, Sqlite};
@ -28,6 +26,7 @@ pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, Emgauw
pub async fn tagged(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String,)>,
query: web::Query<RequestScheduleGetTagged>,
) -> Result<HttpResponse, EmgauwaError> {
let mut pool_conn = pool.acquire().await?;
@ -36,7 +35,20 @@ pub async fn tagged(
.await?
.ok_or(DatabaseError::NotFound)?;
let db_schedules = DbSchedule::get_by_tag(&mut pool_conn, &tag_db).await?;
let mut db_schedules = DbSchedule::get_by_tag(&mut pool_conn, &tag_db).await?;
// only add default schedules if strict is false
if !query.strict.unwrap_or(false) {
if !db_schedules.iter().any(|s| s.uid == ScheduleUid::Off) {
let off = DbSchedule::get_off(&mut pool_conn).await?;
db_schedules.push(off);
}
if !db_schedules.iter().any(|s| s.uid == ScheduleUid::On) {
let on = DbSchedule::get_on(&mut pool_conn).await?;
db_schedules.push(on);
}
}
let schedules: Vec<Schedule> = convert_db_list(&mut pool_conn, db_schedules)?;
Ok(HttpResponse::Ok().json(schedules))