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/>.
|
|
|
|
*/
|
|
|
|
|
2024-02-01 15:12:35 +00:00
|
|
|
use async_trait::async_trait;
|
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},
|
|
|
|
};
|
2024-01-18 12:43:18 +00:00
|
|
|
use thiserror::Error;
|
2023-12-29 00:09:12 +00:00
|
|
|
use tokio::runtime::Runtime;
|
2023-12-22 20:08:34 +00:00
|
|
|
|
2024-01-18 12:43:18 +00:00
|
|
|
#[derive(Debug, Error)]
|
2023-12-29 00:09:12 +00:00
|
|
|
pub enum AppError {
|
2024-01-18 12:43:18 +00:00
|
|
|
#[error("no database loaded")]
|
2023-12-22 20:08:34 +00:00
|
|
|
NoDatabase,
|
2024-01-18 12:43:18 +00:00
|
|
|
#[error("failed to open the database")]
|
2023-12-29 00:09:12 +00:00
|
|
|
FailedToOpenDatabase,
|
2024-01-18 12:43:18 +00:00
|
|
|
#[error("unhandled error")]
|
2023-12-29 00:09:12 +00:00
|
|
|
Unhandled,
|
2023-12-22 20:08:34 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 15:12:35 +00:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum ReadError {
|
|
|
|
#[error("no database loaded")]
|
|
|
|
NoDatabase,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum WriteError {
|
|
|
|
#[error("no database loaded")]
|
|
|
|
NoDatabase,
|
|
|
|
#[error("unhandled error")]
|
|
|
|
Unhandled,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
pub trait RecordProvider: Send + Sync {
|
|
|
|
async fn records(
|
|
|
|
&self,
|
|
|
|
start: NaiveDate,
|
|
|
|
end: NaiveDate,
|
|
|
|
) -> Result<Vec<Record<TraxRecord>>, ReadError>;
|
|
|
|
async fn put_record(&self, record: TraxRecord) -> Result<RecordId, WriteError>;
|
|
|
|
async fn update_record(&self, record: Record<TraxRecord>) -> Result<(), WriteError>;
|
|
|
|
async fn delete_record(&self, id: RecordId) -> Result<(), WriteError>;
|
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
);
|
|
|
|
|
2024-01-20 19:35:10 +00:00
|
|
|
Self {
|
2023-12-29 00:09:12 +00:00
|
|
|
runtime,
|
2023-12-22 20:08:34 +00:00
|
|
|
database: Arc::new(RwLock::new(database)),
|
2024-01-20 19:35:10 +00:00
|
|
|
}
|
2023-12-22 20:08:34 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 15:12:35 +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()
|
|
|
|
}
|
2024-01-29 15:22:21 +00:00
|
|
|
|
2024-02-19 21:27:11 +00:00
|
|
|
pub fn database_is_open(&self) -> bool {
|
|
|
|
self.database.read().unwrap().is_some()
|
2024-01-29 15:22:21 +00:00
|
|
|
}
|
2024-02-01 15:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl RecordProvider for App {
|
|
|
|
async fn records(
|
2023-12-29 00:09:12 +00:00
|
|
|
&self,
|
|
|
|
start: NaiveDate,
|
|
|
|
end: NaiveDate,
|
2024-02-01 15:12:35 +00:00
|
|
|
) -> Result<Vec<Record<TraxRecord>>, ReadError> {
|
2023-12-29 00:09:12 +00:00
|
|
|
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,
|
|
|
|
))
|
2024-01-20 19:35:10 +00:00
|
|
|
.cloned()
|
2023-12-29 00:09:12 +00:00
|
|
|
.collect::<Vec<Record<TraxRecord>>>();
|
|
|
|
Ok(records)
|
|
|
|
} else {
|
2024-02-01 15:12:35 +00:00
|
|
|
Err(ReadError::NoDatabase)
|
2023-12-29 00:09:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2024-02-01 15:12:35 +00:00
|
|
|
async fn put_record(&self, record: TraxRecord) -> Result<RecordId, WriteError> {
|
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() {
|
|
|
|
let id = db.put(record).unwrap();
|
|
|
|
Ok(id)
|
|
|
|
} else {
|
|
|
|
Err(AppError::NoDatabase)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2024-02-01 15:12:35 +00:00
|
|
|
.map_err(|_| WriteError::Unhandled)
|
2023-12-29 00:09:12 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 15:12:35 +00:00
|
|
|
async fn update_record(&self, record: Record<TraxRecord>) -> Result<(), WriteError> {
|
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()
|
2024-02-01 15:12:35 +00:00
|
|
|
.map_err(|_| WriteError::Unhandled)
|
2023-12-22 20:08:34 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 04:36:03 +00:00
|
|
|
async fn delete_record(&self, _id: RecordId) -> Result<(), WriteError> {
|
2024-02-01 15:12:35 +00:00
|
|
|
unimplemented!()
|
2023-12-22 20:08:34 +00:00
|
|
|
}
|
|
|
|
}
|