From 3c70ae1da34fb290f5acb2dd434e5105d4fe549d Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Mon, 6 May 2024 16:27:42 +0200 Subject: [PATCH] Add strict mode and defaults for GET tagged schedules --- Cargo.lock | Bin 71436 -> 70393 bytes api.v1.json | 8 ++++++++ src/handlers/v1/schedules.rs | 20 ++++++++++++++++---- src/main.rs | 22 +++++++--------------- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1de0abbe52e0d9e02fceb41dcaa92a5f9cfc594f..0f8c11992d6f091fd81dec4c9a062f01f488492f 100644 GIT binary patch delta 444 zcmWlVv1`;p5QRCo!bAvn0ds|wjabR-&g|@L;TaMO5sP1}4Y)JAYn@8LD!ttb=|qr_ zMr=;-pRl((u(C3pg<#=YKltAF-rLXfKbP|l$Nl%OMSuEnr=LFB-<%#VUTlu8-f@Rl zCu?i06cuB1Mk!kixj@S7*tdYWkP3)s%Fzu(^N z_jgyDpWEkq{pihfSW{3b6aZ^Yk}x%m;Hh!RAlRajv<8S~Xqsd24o8Pm~8*Ef4|eu9-lmvSS_Qunw!|TAyEMz6O;nNRzf08&KD1ag-RWrr4X~C*0IIX&4pC3$z+(_Ac#72pWv_b>xL;}|FF-c_bJ`@9l*8CVBhAK-)xn{MWp7fnii$+-n+Iz{ z_N|NhUY<5zJ|BCJ7y^gLu~249uFvq4j4wa|1X7kvDJV;q&klhUyfB+U3AN4K95lGU zrG0vD^MT>$;=;MNI9HL|*T}D)zpzvm7Z=*hFMIdAGBv4rU3zFAy!dR40ybDF0NF8! z3uvmavazXz7;VkTD?p6ScD@}&>AWFl_TUJl9sO<4?mpf8_S)ZgSjS?u3GdokIsD^o z-#)rEKj$olN|wpwP?9gjDmj+KK?1Ni4vA~0qbYST(WuQ4(6J4~SkSejUkuyVk7t|R za-cnXGPUy8<1^daugB+SO~1#KYtj%9J4yn=Y$`$l6rrBL7n_YrF;R15Zyglnz}EOs zTl{KQ`|}@dkB*-jXVOq@1|br~R3mo=g@Q#B!9oMNo2Wzz+cun76`KoMFkvr$meLNVeA`X$+c}^I(H!Ec0BS<#Ux469j L;g{Pt+?jd_SXd$k diff --git a/api.v1.json b/api.v1.json index c733ec3..e6988fa 100644 --- a/api.v1.json +++ b/api.v1.json @@ -209,6 +209,14 @@ "in": "path", "required": true, "description": "" + }, + { + "schema": { + "type": "boolean" + }, + "name": "strict", + "in": "query", + "description": "Normally on and off will always be included. When strict is set to true, only schedules with the given tag will be returned." } ], "get": { diff --git a/src/handlers/v1/schedules.rs b/src/handlers/v1/schedules.rs index 06037ed..9acc734 100644 --- a/src/handlers/v1/schedules.rs +++ b/src/handlers/v1/schedules.rs @@ -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>) -> Result>, path: web::Path<(String,)>, + query: web::Query, ) -> Result { 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 = convert_db_list(&mut pool_conn, db_schedules)?; Ok(HttpResponse::Ok().json(schedules)) diff --git a/src/main.rs b/src/main.rs index cc333e1..003633f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,11 +4,12 @@ use actix::{Actor, Arbiter}; use actix_cors::Cors; use actix_web::middleware::TrailingSlash; use actix_web::{middleware, web, App, HttpServer}; +use serde_json::Value; +use utoipa_swagger_ui::SwaggerUi; + use emgauwa_common::db::DbController; use emgauwa_common::errors::EmgauwaError; use emgauwa_common::utils::{drop_privileges, init_logging}; -use serde_json::json; -use utoipa_swagger_ui::SwaggerUi; use crate::app_state::AppState; @@ -46,6 +47,9 @@ async fn main() -> Result<(), std::io::Error> { AppState::new(app_state_pool) }); + let api_v1_json: Value = + serde_json::from_str(include_str!("../api.v1.json")).map_err(EmgauwaError::from)?; + log::info!( "Starting server on {}:{}", settings.server.host, @@ -63,18 +67,6 @@ async fn main() -> Result<(), std::io::Error> { }), }; - let api_default = json!({ - "openapi": "3.0.0", - "info": { - "version": "0.0.0", - "title": "Failed to load API documentation", - } - }); - - let api_v1_json = - serde_json::from_str(include_str!("../api.v1.json")).unwrap_or(api_default.clone()); - - App::new() .wrap(cors) .wrap(middleware::Logger::default()) @@ -83,7 +75,7 @@ async fn main() -> Result<(), std::io::Error> { .app_data(web::Data::new(app_state.clone())) .service( SwaggerUi::new("/api/docs/{_:.*}") - .external_urls_from_iter_unchecked([("/api/v1.json", api_v1_json)]), + .external_urls_from_iter_unchecked([("/api/v1.json", api_v1_json.clone())]), ) .service(web::redirect("/api/docs", "/api/docs/")) .service(