implementing get endpoint for alarms wip
This commit is contained in:
@@ -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<C: ConnectionTrait>(
|
||||
Ok(alarm_to_create.insert(db).await?)
|
||||
}
|
||||
|
||||
pub async fn get_alarms<C: ConnectionTrait>(db: &C, enabled: Option<bool>) -> Result<Vec<alarm::Model>, 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
|
||||
}
|
||||
|
||||
@@ -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<Local>
|
||||
#[derive(ToSchema, Serialize)]
|
||||
pub struct OkResponse {
|
||||
name: String,
|
||||
enabled: bool,
|
||||
time: DateTime<Local>,
|
||||
}
|
||||
|
||||
#[derive(IntoResponses)]
|
||||
enum Responses {
|
||||
#[response(status = 200)]
|
||||
Ok(#[to_schema] Vec<OkResponse>),
|
||||
#[response(status = 500)]
|
||||
DBError(String)
|
||||
pub enum Responses {
|
||||
#[response(status = 200)]
|
||||
Ok(#[to_schema] Vec<OkResponse>),
|
||||
#[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<bool>
|
||||
}
|
||||
|
||||
#[utoipa::path(get, path = "", responses(Responses), params(RequestQuery))]
|
||||
#[debug_handler]
|
||||
pub async fn get_handler(Query(RequestQuery { enabled }): Query<RequestQuery>) -> Responses {
|
||||
match enabled {
|
||||
Some(enabled) => {
|
||||
let alarms = crate::db::alarm::get_alarms(enabled).await;
|
||||
todo!()
|
||||
},
|
||||
None => {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn get_handler() -> Responses {
|
||||
todo!()
|
||||
}
|
||||
|
||||
@@ -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<bool>) -> Result<Vec<Alarm>, DbErr> {
|
||||
get_alarms(&*self.db, enabled).await.map(|a| a.into())
|
||||
}
|
||||
|
||||
pub fn start(&self) {
|
||||
self.cron.lock().expect("Failed to lock cron").start();
|
||||
}
|
||||
|
||||
11
src/types.rs
11
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<model::alarm::Model> for Alarm {
|
||||
fn from(value: model::alarm::Model) -> Self {
|
||||
Self {
|
||||
enabled: value.enabled,
|
||||
time: DateTime::fromna,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user