From a7947a31bcf0c50875c75472564188ea0fb9c576 Mon Sep 17 00:00:00 2001 From: quirinecker Date: Tue, 3 Mar 2026 10:00:46 +0100 Subject: [PATCH] implementing get endpoint for alarms wip --- src/dao/alarm.rs | 16 ++++++++++- src/resources/alarm/get.rs | 59 ++++++++++++++++++++++++-------------- src/scheduler.rs | 8 ++++-- src/types.rs | 11 +++++++ 4 files changed, 70 insertions(+), 24 deletions(-) diff --git a/src/dao/alarm.rs b/src/dao/alarm.rs index 33accba..a5987c3 100644 --- a/src/dao/alarm.rs +++ b/src/dao/alarm.rs @@ -1,6 +1,6 @@ use chrono::{DateTime, Local}; use sea_orm::{ - ActiveModelTrait, ActiveValue::Set, ConnectionTrait, DbErr + ActiveModelTrait, ActiveValue::Set, ColumnTrait, ConnectionTrait, DbErr, EntityTrait, QueryFilter }; use crate::model::{self, alarm}; @@ -20,3 +20,17 @@ pub async fn create_alarm( Ok(alarm_to_create.insert(db).await?) } +pub async fn get_alarms(db: &C, enabled: Option) -> Result, DbErr> { + let query = model::alarm::Entity::find(); + + let query = match enabled { + Some(enabled) => { + query.filter(model::alarm::Column::Enabled.eq(enabled)) + }, + None => { + query + } + }; + + query.all(db).await +} diff --git a/src/resources/alarm/get.rs b/src/resources/alarm/get.rs index bd13357..6e332bd 100644 --- a/src/resources/alarm/get.rs +++ b/src/resources/alarm/get.rs @@ -1,32 +1,49 @@ -use axum::{Json, http::StatusCode, response::IntoResponse}; +use axum::{Json, debug_handler, extract::Query, http::StatusCode, response::IntoResponse}; use chrono::{DateTime, Local}; -use utoipa::{IntoResponses, ToSchema}; +use serde::{Deserialize, Serialize}; +use utoipa::{IntoParams, IntoResponses, ToSchema}; -#[derive(ToSchema)] -struct OkResponse { - name: String, - enabled: bool, - time: DateTime +#[derive(ToSchema, Serialize)] +pub struct OkResponse { + name: String, + enabled: bool, + time: DateTime, } #[derive(IntoResponses)] -enum Responses { - #[response(status = 200)] - Ok(#[to_schema] Vec), - #[response(status = 500)] - DBError(String) +pub enum Responses { + #[response(status = 200)] + Ok(#[to_schema] Vec), + #[response(status = 500)] + DBError(String), } impl IntoResponse for Responses { - fn into_response(self) -> axum::response::Response { - match self { - Responses::Ok(body) => (StatusCode::OK, Json(body)).into_response(), - Responses::DBError(message) => (StatusCode::INTERNAL_SERVER_ERROR, Json(message)).into_response(), + fn into_response(self) -> axum::response::Response { + match self { + Responses::Ok(body) => (StatusCode::OK, Json(body)).into_response(), + Responses::DBError(message) => { + (StatusCode::INTERNAL_SERVER_ERROR, Json(message)).into_response() + } + } + } +} + +#[derive(IntoParams, Deserialize)] +pub struct RequestQuery { + enabled: Option +} + +#[utoipa::path(get, path = "", responses(Responses), params(RequestQuery))] +#[debug_handler] +pub async fn get_handler(Query(RequestQuery { enabled }): Query) -> Responses { + match enabled { + Some(enabled) => { + let alarms = crate::db::alarm::get_alarms(enabled).await; + todo!() + }, + None => { + todo!() } } - -} - -pub fn get_handler() -> Responses { - todo!() } diff --git a/src/scheduler.rs b/src/scheduler.rs index 5eb1a45..13b93ab 100644 --- a/src/scheduler.rs +++ b/src/scheduler.rs @@ -2,9 +2,9 @@ use std::sync::{Arc, Mutex}; use chrono::{DateTime, Local, Timelike}; use cron_tab::Cron; -use sea_orm::DatabaseConnection; +use sea_orm::{DatabaseConnection, DbErr}; -use crate::dao::alarm::create_alarm; +use crate::dao::alarm::{self, create_alarm, get_alarms}; use crate::ringer::Ringer; use crate::types::Alarm; @@ -62,6 +62,10 @@ impl Scheduler { Ok(alarm) } + pub async fn get_alarms(&self, enabled: Option) -> Result, DbErr> { + get_alarms(&*self.db, enabled).await.map(|a| a.into()) + } + pub fn start(&self) { self.cron.lock().expect("Failed to lock cron").start(); } diff --git a/src/types.rs b/src/types.rs index ce00ccb..fa445b3 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,6 +1,8 @@ use chrono::{DateTime, Local}; use serde::{Deserialize, Serialize}; +use crate::model; + #[derive(Debug, Serialize, Deserialize, Clone)] pub struct Alarm { pub enabled: bool, @@ -12,3 +14,12 @@ impl Alarm { Self { enabled, time } } } + +impl From for Alarm { + fn from(value: model::alarm::Model) -> Self { + Self { + enabled: value.enabled, + time: DateTime::fromna, + } + } +}