use crate::api::MmexContext;
use crate::domain::scheduled_transactions::{
ScheduledError, ScheduledTransaction, ScheduledUpdate,
};
use crate::MmexError;
use std::sync::{Arc, Mutex};
#[derive(uniffi::Object)]
pub struct ScheduledManager {
pub(crate) context: Arc<Mutex<MmexContext>>,
}
#[uniffi::export]
impl ScheduledManager {
pub fn get_all(&self) -> Result<Vec<ScheduledTransaction>, ScheduledError> {
let ctx = self
.context
.lock()
.map_err(|e| ScheduledError::Common(MmexError::Internal(e.to_string())))?;
Ok(ctx.scheduled().get_all_scheduled()?)
}
pub fn get_by_id(&self, id: i64) -> Result<Option<ScheduledTransaction>, ScheduledError> {
let ctx = self
.context
.lock()
.map_err(|e| ScheduledError::Common(MmexError::Internal(e.to_string())))?;
Ok(ctx.scheduled().get_scheduled_by_id(id)?)
}
pub fn create(
&self,
transaction: ScheduledTransaction,
) -> Result<ScheduledTransaction, ScheduledError> {
let ctx = self
.context
.lock()
.map_err(|e| ScheduledError::Common(MmexError::Internal(e.to_string())))?;
Ok(ctx.scheduled().create_scheduled(&transaction)?)
}
pub fn update(&self, transaction: ScheduledTransaction) -> Result<(), ScheduledError> {
let ctx = self
.context
.lock()
.map_err(|e| ScheduledError::Common(MmexError::Internal(e.to_string())))?;
ctx.scheduled().update_scheduled(&transaction)?;
Ok(())
}
pub fn update_partial(&self, id: i64, update: ScheduledUpdate) -> Result<(), ScheduledError> {
let ctx = self
.context
.lock()
.map_err(|e| ScheduledError::Common(MmexError::Internal(e.to_string())))?;
ctx.scheduled().update_scheduled_partial(id, update)?;
Ok(())
}
pub fn delete(&self, id: i64) -> Result<(), ScheduledError> {
let ctx = self
.context
.lock()
.map_err(|e| ScheduledError::Common(MmexError::Internal(e.to_string())))?;
ctx.scheduled().delete_scheduled(id)?;
Ok(())
}
pub fn get_all_json(&self) -> Result<String, ScheduledError> {
let scheduled = self.get_all()?;
serde_json::to_string(&scheduled)
.map_err(|e| ScheduledError::Common(MmexError::Internal(e.to_string())))
}
}