get endpoint works
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use chrono::{DateTime, Local};
|
use chrono::{DateTime, Local, Utc};
|
||||||
use sea_orm::{
|
use sea_orm::{
|
||||||
ActiveModelTrait, ActiveValue::Set, ColumnTrait, ConnectionTrait, DbErr, EntityTrait, QueryFilter
|
ActiveModelTrait, ActiveValue::Set, ColumnTrait, ConnectionTrait, DbErr, EntityTrait, QueryFilter
|
||||||
};
|
};
|
||||||
@@ -8,7 +8,7 @@ use crate::model::{self, alarm};
|
|||||||
pub async fn create_alarm<C: ConnectionTrait>(
|
pub async fn create_alarm<C: ConnectionTrait>(
|
||||||
db: &C,
|
db: &C,
|
||||||
title: &str,
|
title: &str,
|
||||||
time: DateTime<Local>,
|
time: DateTime<Utc>,
|
||||||
) -> Result<alarm::Model, DbErr> {
|
) -> Result<alarm::Model, DbErr> {
|
||||||
let alarm_to_create = model::alarm::ActiveModel {
|
let alarm_to_create = model::alarm::ActiveModel {
|
||||||
title: Set(title.to_string()),
|
title: Set(title.to_string()),
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
use axum::{Json, debug_handler, extract::Query, http::StatusCode, response::IntoResponse};
|
use axum::{Json, debug_handler, extract::{Query, State}, http::StatusCode, response::IntoResponse};
|
||||||
use chrono::{DateTime, Local};
|
use chrono::{DateTime, Local, Utc};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use utoipa::{IntoParams, IntoResponses, ToSchema};
|
use utoipa::{IntoParams, IntoResponses, ToSchema};
|
||||||
|
|
||||||
|
use crate::{AppState, types::Alarm};
|
||||||
|
|
||||||
#[derive(ToSchema, Serialize)]
|
#[derive(ToSchema, Serialize)]
|
||||||
pub struct OkResponse {
|
pub struct OkResponse {
|
||||||
name: String,
|
name: String,
|
||||||
enabled: bool,
|
enabled: bool,
|
||||||
time: DateTime<Local>,
|
time: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(IntoResponses)]
|
#[derive(IntoResponses)]
|
||||||
@@ -34,16 +36,26 @@ pub struct RequestQuery {
|
|||||||
enabled: Option<bool>
|
enabled: Option<bool>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[utoipa::path(get, path = "", responses(Responses), params(RequestQuery))]
|
impl From<Alarm> for OkResponse {
|
||||||
#[debug_handler]
|
fn from(value: Alarm) -> Self {
|
||||||
pub async fn get_handler(Query(RequestQuery { enabled }): Query<RequestQuery>) -> Responses {
|
Self {
|
||||||
match enabled {
|
name: value.time.to_string(),
|
||||||
Some(enabled) => {
|
enabled: value.enabled,
|
||||||
let alarms = crate::db::alarm::get_alarms(enabled).await;
|
time: value.time,
|
||||||
todo!()
|
}
|
||||||
},
|
}
|
||||||
None => {
|
}
|
||||||
todo!()
|
|
||||||
|
#[utoipa::path(get, path = "", responses(Responses), params(RequestQuery))]
|
||||||
|
#[debug_handler]
|
||||||
|
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())
|
||||||
|
},
|
||||||
|
Err(error) => {
|
||||||
|
Responses::DBError(error.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use axum::{Json};
|
|||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use axum::debug_handler;
|
use axum::debug_handler;
|
||||||
use axum::response::IntoResponse;
|
use axum::response::IntoResponse;
|
||||||
use chrono::{DateTime, Local};
|
use chrono::{DateTime, Utc};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use utoipa::{IntoResponses, ToSchema};
|
use utoipa::{IntoResponses, ToSchema};
|
||||||
|
|
||||||
@@ -12,12 +12,12 @@ use crate::types::Alarm;
|
|||||||
|
|
||||||
#[derive(Debug, Deserialize, ToSchema)]
|
#[derive(Debug, Deserialize, ToSchema)]
|
||||||
pub struct RequestBody {
|
pub struct RequestBody {
|
||||||
time: DateTime<Local>,
|
time: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(ToSchema, Serialize)]
|
#[derive(ToSchema, Serialize)]
|
||||||
pub struct OkResponseBody {
|
pub struct OkResponseBody {
|
||||||
time: DateTime<Local>,
|
time: DateTime<Utc>,
|
||||||
enabled: bool,
|
enabled: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use chrono::{DateTime, Local, Timelike};
|
use chrono::{DateTime, Local, Timelike, Utc};
|
||||||
use cron_tab::Cron;
|
use cron_tab::Cron;
|
||||||
use sea_orm::{DatabaseConnection, DbErr};
|
use sea_orm::{DatabaseConnection, DbErr};
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ impl Scheduler {
|
|||||||
Ok(())
|
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 cron_schedule = format!("{} {} {} * * *", "*", time.minute(), time.hour());
|
||||||
let alarm = Alarm::new(true, time);
|
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> {
|
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) {
|
pub fn start(&self) {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use chrono::{DateTime, Local};
|
use chrono::{DateTime, Utc};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::model;
|
use crate::model;
|
||||||
@@ -6,11 +6,11 @@ use crate::model;
|
|||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct Alarm {
|
pub struct Alarm {
|
||||||
pub enabled: bool,
|
pub enabled: bool,
|
||||||
pub time: DateTime<Local>,
|
pub time: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Alarm {
|
impl Alarm {
|
||||||
pub fn new(enabled: bool, time: DateTime<Local>) -> Self {
|
pub fn new(enabled: bool, time: DateTime<Utc>) -> Self {
|
||||||
Self { enabled, time }
|
Self { enabled, time }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,7 @@ impl From<model::alarm::Model> for Alarm {
|
|||||||
fn from(value: model::alarm::Model) -> Self {
|
fn from(value: model::alarm::Model) -> Self {
|
||||||
Self {
|
Self {
|
||||||
enabled: value.enabled,
|
enabled: value.enabled,
|
||||||
time: DateTime::fromna,
|
time: DateTime::from_utc(value.time, Utc),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user