get endpoint works

This commit is contained in:
2026-03-03 12:14:39 +01:00
parent 3db69454e4
commit 0ad039a597
5 changed files with 42 additions and 25 deletions

View File

@@ -1,4 +1,4 @@
use chrono::{DateTime, Local};
use chrono::{DateTime, Local, Utc};
use sea_orm::{
ActiveModelTrait, ActiveValue::Set, ColumnTrait, ConnectionTrait, DbErr, EntityTrait, QueryFilter
};
@@ -8,7 +8,7 @@ use crate::model::{self, alarm};
pub async fn create_alarm<C: ConnectionTrait>(
db: &C,
title: &str,
time: DateTime<Local>,
time: DateTime<Utc>,
) -> Result<alarm::Model, DbErr> {
let alarm_to_create = model::alarm::ActiveModel {
title: Set(title.to_string()),

View File

@@ -1,13 +1,15 @@
use axum::{Json, debug_handler, extract::Query, http::StatusCode, response::IntoResponse};
use chrono::{DateTime, Local};
use axum::{Json, debug_handler, extract::{Query, State}, http::StatusCode, response::IntoResponse};
use chrono::{DateTime, Local, Utc};
use serde::{Deserialize, Serialize};
use utoipa::{IntoParams, IntoResponses, ToSchema};
use crate::{AppState, types::Alarm};
#[derive(ToSchema, Serialize)]
pub struct OkResponse {
name: String,
enabled: bool,
time: DateTime<Local>,
time: DateTime<Utc>,
}
#[derive(IntoResponses)]
@@ -34,16 +36,26 @@ pub struct RequestQuery {
enabled: Option<bool>
}
impl From<Alarm> for OkResponse {
fn from(value: Alarm) -> Self {
Self {
name: value.time.to_string(),
enabled: value.enabled,
time: value.time,
}
}
}
#[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!()
pub async fn get_handler(State(AppState{ scheduler }): State<AppState>, Query(RequestQuery { enabled }): Query<RequestQuery>) -> Responses {
let result = scheduler.get_alarms(enabled).await;
match result {
Ok(alarms) => {
Responses::Ok(alarms.into_iter().map(|alarm| alarm.into()).collect())
},
None => {
todo!()
Err(error) => {
Responses::DBError(error.to_string())
}
}
}

View File

@@ -3,7 +3,7 @@ use axum::{Json};
use axum::http::StatusCode;
use axum::debug_handler;
use axum::response::IntoResponse;
use chrono::{DateTime, Local};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use utoipa::{IntoResponses, ToSchema};
@@ -12,12 +12,12 @@ use crate::types::Alarm;
#[derive(Debug, Deserialize, ToSchema)]
pub struct RequestBody {
time: DateTime<Local>,
time: DateTime<Utc>,
}
#[derive(ToSchema, Serialize)]
pub struct OkResponseBody {
time: DateTime<Local>,
time: DateTime<Utc>,
enabled: bool,
}

View File

@@ -1,6 +1,6 @@
use std::sync::{Arc, Mutex};
use chrono::{DateTime, Local, Timelike};
use chrono::{DateTime, Local, Timelike, Utc};
use cron_tab::Cron;
use sea_orm::{DatabaseConnection, DbErr};
@@ -48,7 +48,7 @@ impl Scheduler {
Ok(())
}
pub async fn add_alarm(&self, time: DateTime<Local>) -> Result<Alarm, String> {
pub async fn add_alarm(&self, time: DateTime<Utc>) -> Result<Alarm, String> {
let cron_schedule = format!("{} {} {} * * *", "*", time.minute(), time.hour());
let alarm = Alarm::new(true, time);
@@ -63,7 +63,12 @@ impl Scheduler {
}
pub async fn get_alarms(&self, enabled: Option<bool>) -> Result<Vec<Alarm>, DbErr> {
get_alarms(&*self.db, enabled).await.map(|a| a.into())
get_alarms(&*self.db, enabled).await
.map(|alarms| alarms
.into_iter()
.map(|a| a.into())
.collect()
)
}
pub fn start(&self) {

View File

@@ -1,4 +1,4 @@
use chrono::{DateTime, Local};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::model;
@@ -6,11 +6,11 @@ use crate::model;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Alarm {
pub enabled: bool,
pub time: DateTime<Local>,
pub time: DateTime<Utc>,
}
impl Alarm {
pub fn new(enabled: bool, time: DateTime<Local>) -> Self {
pub fn new(enabled: bool, time: DateTime<Utc>) -> Self {
Self { enabled, time }
}
}
@@ -19,7 +19,7 @@ impl From<model::alarm::Model> for Alarm {
fn from(value: model::alarm::Model) -> Self {
Self {
enabled: value.enabled,
time: DateTime::fromna,
time: DateTime::from_utc(value.time, Utc),
}
}
}