use crate::api::MmexContext;
use crate::domain::currencies::{Currency, CurrencyError, CurrencyId, CurrencyUpdate};
use crate::MmexError;
use std::sync::{Arc, Mutex};
#[derive(uniffi::Object)]
pub struct CurrencyManager {
pub(crate) context: Arc<Mutex<MmexContext>>,
}
#[uniffi::export]
impl CurrencyManager {
pub fn get_all(&self) -> Result<Vec<Currency>, CurrencyError> {
let ctx = self
.context
.lock()
.map_err(|e| CurrencyError::Common(MmexError::Internal(e.to_string())))?;
Ok(ctx.currencies().get_all_currencies()?)
}
pub fn get_by_id(&self, id: i64) -> Result<Option<Currency>, CurrencyError> {
let ctx = self
.context
.lock()
.map_err(|e| CurrencyError::Common(MmexError::Internal(e.to_string())))?;
Ok(ctx.currencies().get_currency_by_id(CurrencyId { v1: id })?)
}
pub fn get_by_symbol(&self, symbol: String) -> Result<Option<Currency>, CurrencyError> {
let ctx = self
.context
.lock()
.map_err(|e| CurrencyError::Common(MmexError::Internal(e.to_string())))?;
Ok(ctx.currencies().get_currency_by_symbol(&symbol)?)
}
pub fn create(&self, currency: Currency) -> Result<Currency, CurrencyError> {
let ctx = self
.context
.lock()
.map_err(|e| CurrencyError::Common(MmexError::Internal(e.to_string())))?;
Ok(ctx.currencies().create_currency(¤cy)?)
}
pub fn update(&self, currency: Currency) -> Result<(), CurrencyError> {
let ctx = self
.context
.lock()
.map_err(|e| CurrencyError::Common(MmexError::Internal(e.to_string())))?;
ctx.currencies().update_currency(¤cy)?;
Ok(())
}
pub fn update_partial(&self, id: i64, update: CurrencyUpdate) -> Result<(), CurrencyError> {
let ctx = self
.context
.lock()
.map_err(|e| CurrencyError::Common(MmexError::Internal(e.to_string())))?;
ctx.currencies()
.update_currency_partial(CurrencyId { v1: id }, update)?;
Ok(())
}
pub fn delete(&self, id: i64) -> Result<(), CurrencyError> {
let ctx = self
.context
.lock()
.map_err(|e| CurrencyError::Common(MmexError::Internal(e.to_string())))?;
ctx.currencies().delete_currency(CurrencyId { v1: id })?;
Ok(())
}
pub fn get_all_json(&self) -> Result<String, CurrencyError> {
let currencies = self.get_all()?;
serde_json::to_string(¤cies)
.map_err(|e| CurrencyError::Common(MmexError::Internal(e.to_string())))
}
}