finga
a4a1234f06
The thread handling the reminders is unparked and reschedules the execution of sending out reminders.
70 lines
1.6 KiB
Rust
70 lines
1.6 KiB
Rust
use crate::{schema, AppState, NewReminder};
|
|
use anyhow::Error;
|
|
use axum::{
|
|
extract::State,
|
|
http::StatusCode,
|
|
response::{IntoResponse, Response, Result},
|
|
Json,
|
|
};
|
|
use diesel::prelude::*;
|
|
use serde::Deserialize;
|
|
use time::OffsetDateTime;
|
|
use tracing::{error, trace};
|
|
|
|
pub struct ServerError(Error);
|
|
|
|
impl IntoResponse for ServerError {
|
|
fn into_response(self) -> Response {
|
|
error!("{}", self.0);
|
|
(StatusCode::INTERNAL_SERVER_ERROR, "Something went wrong.").into_response()
|
|
}
|
|
}
|
|
|
|
impl<E> From<E> for ServerError
|
|
where
|
|
E: Into<Error>,
|
|
{
|
|
fn from(err: E) -> Self {
|
|
Self(err.into())
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct Reminder {
|
|
#[serde(with = "time::serde::iso8601")]
|
|
planned: OffsetDateTime,
|
|
title: String,
|
|
message: String,
|
|
receiver: String,
|
|
}
|
|
|
|
#[allow(clippy::unused_async)]
|
|
pub async fn create_reminder(
|
|
State(state): State<AppState>,
|
|
Json(data): Json<Reminder>,
|
|
) -> Result<impl IntoResponse, ServerError> {
|
|
let reminder = NewReminder {
|
|
created: OffsetDateTime::now_utc(),
|
|
planned: data.planned,
|
|
title: &data.title,
|
|
message: &data.message,
|
|
receiver: &data.receiver,
|
|
};
|
|
|
|
trace!(?data, "received data");
|
|
|
|
diesel::insert_into(schema::reminders::table)
|
|
.values(&reminder)
|
|
.execute(&mut state.db_pool.get()?)?;
|
|
|
|
trace!("unpark reminder thread to reschedule next run");
|
|
state.reminder.thread().unpark();
|
|
|
|
Ok((StatusCode::CREATED, "Reminder created".to_string()))
|
|
}
|
|
|
|
#[allow(clippy::unused_async)]
|
|
pub async fn not_found() -> impl IntoResponse {
|
|
(StatusCode::NOT_FOUND, "Page not found")
|
|
}
|