2023-12-22 20:08:34 +00:00
|
|
|
/*
|
|
|
|
Copyright 2023, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
|
|
|
|
|
|
|
This file is part of FitnessTrax.
|
|
|
|
|
|
|
|
FitnessTrax is free software: you can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
General Public License as published by the Free Software Foundation, either version 3 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
FitnessTrax is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
|
|
|
|
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-12-29 00:09:12 +00:00
|
|
|
use chrono::NaiveDate;
|
|
|
|
use emseries::{time_range, Record, RecordId, Series, Timestamp};
|
2023-12-22 20:08:34 +00:00
|
|
|
use ft_core::TraxRecord;
|
|
|
|
use std::{
|
2023-12-27 22:14:47 +00:00
|
|
|
path::PathBuf,
|
2023-12-22 20:08:34 +00:00
|
|
|
sync::{Arc, RwLock},
|
|
|
|
};
|
2023-12-29 00:09:12 +00:00
|
|
|
use tokio::runtime::Runtime;
|
2023-12-22 20:08:34 +00:00
|
|
|
|
2023-12-29 00:09:12 +00:00
|
|
|
pub enum AppError {
|
2023-12-22 20:08:34 +00:00
|
|
|
NoDatabase,
|
2023-12-29 00:09:12 +00:00
|
|
|
FailedToOpenDatabase,
|
|
|
|
Unhandled,
|
2023-12-22 20:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The real, headless application. This is where all of the logic will reside.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct App {
|
2023-12-29 00:09:12 +00:00
|
|
|
runtime: Arc<Runtime>,
|
2023-12-22 20:08:34 +00:00
|
|
|
database: Arc<RwLock<Option<Series<TraxRecord>>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl App {
|
|
|
|
pub fn new(db_path: Option<PathBuf>) -> Self {
|
|
|
|
let database = db_path.map(|path| Series::open(path).unwrap());
|
2023-12-29 00:09:12 +00:00
|
|
|
let runtime = Arc::new(
|
|
|
|
tokio::runtime::Builder::new_multi_thread()
|
|
|
|
.enable_all()
|
|
|
|
.build()
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
|
2023-12-22 20:08:34 +00:00
|
|
|
let s = Self {
|
2023-12-29 00:09:12 +00:00
|
|
|
runtime,
|
2023-12-22 20:08:34 +00:00
|
|
|
database: Arc::new(RwLock::new(database)),
|
|
|
|
};
|
|
|
|
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
2023-12-29 00:09:12 +00:00
|
|
|
pub async fn records(
|
|
|
|
&self,
|
|
|
|
start: NaiveDate,
|
|
|
|
end: NaiveDate,
|
|
|
|
) -> Result<Vec<Record<TraxRecord>>, AppError> {
|
|
|
|
let db = self.database.clone();
|
|
|
|
self.runtime
|
|
|
|
.spawn_blocking(move || {
|
|
|
|
if let Some(ref db) = *db.read().unwrap() {
|
|
|
|
let records = db
|
|
|
|
.search(time_range(
|
|
|
|
Timestamp::Date(start),
|
|
|
|
true,
|
|
|
|
Timestamp::Date(end),
|
|
|
|
true,
|
|
|
|
))
|
|
|
|
.map(|record| record.clone())
|
|
|
|
.collect::<Vec<Record<TraxRecord>>>();
|
|
|
|
Ok(records)
|
|
|
|
} else {
|
|
|
|
Err(AppError::NoDatabase)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn put_record(&self, record: TraxRecord) -> Result<RecordId, AppError> {
|
|
|
|
let db = self.database.clone();
|
|
|
|
self.runtime
|
|
|
|
.spawn_blocking(move || {
|
|
|
|
if let Some(ref mut db) = *db.write().unwrap() {
|
|
|
|
let id = db.put(record).unwrap();
|
|
|
|
Ok(id)
|
|
|
|
} else {
|
|
|
|
Err(AppError::NoDatabase)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.map_err(|_| AppError::Unhandled)
|
|
|
|
}
|
|
|
|
|
2023-12-28 14:51:41 +00:00
|
|
|
pub async fn update_record(&self, record: Record<TraxRecord>) -> Result<(), AppError> {
|
2023-12-29 00:09:12 +00:00
|
|
|
let db = self.database.clone();
|
|
|
|
self.runtime
|
|
|
|
.spawn_blocking(move || {
|
|
|
|
if let Some(ref mut db) = *db.write().unwrap() {
|
|
|
|
db.update(record).map_err(|_| AppError::Unhandled)
|
2023-12-22 20:08:34 +00:00
|
|
|
} else {
|
2023-12-29 00:09:12 +00:00
|
|
|
Err(AppError::NoDatabase)
|
2023-12-22 20:08:34 +00:00
|
|
|
}
|
2023-12-29 00:09:12 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.map_err(|_| AppError::Unhandled)
|
2023-12-22 20:08:34 +00:00
|
|
|
}
|
|
|
|
|
2023-12-29 00:09:12 +00:00
|
|
|
pub async fn open_db(&self, path: PathBuf) -> Result<(), AppError> {
|
|
|
|
let db_ref = self.database.clone();
|
|
|
|
self.runtime
|
|
|
|
.spawn_blocking(move || {
|
|
|
|
let db = Series::open(path).map_err(|_| AppError::FailedToOpenDatabase)?;
|
|
|
|
*db_ref.write().unwrap() = Some(db);
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2023-12-22 20:08:34 +00:00
|
|
|
}
|
|
|
|
}
|