use crate::tina::data::date_time::LocalDateTime;
use crate::tina::data::AppResult;
use crate::tina::server::application::{AppConfig, Application};
use futures_util::lock::Mutex;
pub struct RequestTime(Application, Mutex<Option<LocalDateTime>>);
impl RequestTime {
pub fn new(application: Application) -> Self {
Self(application, Mutex::new(None))
}
pub async fn get_date_time(&self) -> AppResult<LocalDateTime> {
let mut lock = self.1.lock().await;
match lock.as_ref() {
None => {
let service = self.0.get_date_time_service()?;
let date_time = service.get_current_date_time().await?;
(*lock) = Some(date_time);
Ok(date_time)
}
Some(date_time) => Ok(*date_time),
}
}
}
impl Default for RequestTime {
fn default() -> Self {
Self(Application::from(AppConfig::new()), Mutex::new(Some(LocalDateTime::now())))
}
}