use crate::api::MmexContext;
use crate::domain::assets::{Asset, AssetError, AssetId, AssetUpdate};
use crate::MmexError;
use std::sync::{Arc, Mutex};
#[derive(uniffi::Object)]
pub struct AssetManager {
pub(crate) context: Arc<Mutex<MmexContext>>,
}
#[uniffi::export]
impl AssetManager {
pub fn get_all(&self) -> Result<Vec<Asset>, AssetError> {
let ctx = self
.context
.lock()
.map_err(|e| AssetError::Common(MmexError::Internal(e.to_string())))?;
Ok(ctx.assets().get_all_assets()?)
}
pub fn get_by_id(&self, id: i64) -> Result<Option<Asset>, AssetError> {
let ctx = self
.context
.lock()
.map_err(|e| AssetError::Common(MmexError::Internal(e.to_string())))?;
Ok(ctx.assets().get_asset_by_id(AssetId { v1: id })?)
}
pub fn create(&self, asset: Asset) -> Result<Asset, AssetError> {
let ctx = self
.context
.lock()
.map_err(|e| AssetError::Common(MmexError::Internal(e.to_string())))?;
Ok(ctx.assets().create_asset(&asset)?)
}
pub fn update(&self, asset: Asset) -> Result<(), AssetError> {
let ctx = self
.context
.lock()
.map_err(|e| AssetError::Common(MmexError::Internal(e.to_string())))?;
ctx.assets().update_asset(&asset)?;
Ok(())
}
pub fn update_partial(&self, id: i64, update: AssetUpdate) -> Result<(), AssetError> {
let ctx = self
.context
.lock()
.map_err(|e| AssetError::Common(MmexError::Internal(e.to_string())))?;
ctx.assets()
.update_asset_partial(AssetId { v1: id }, update)?;
Ok(())
}
pub fn delete(&self, id: i64) -> Result<(), AssetError> {
let ctx = self
.context
.lock()
.map_err(|e| AssetError::Common(MmexError::Internal(e.to_string())))?;
ctx.assets().delete_asset(AssetId { v1: id })?;
Ok(())
}
pub fn get_all_json(&self) -> Result<String, AssetError> {
let assets = self.get_all()?;
serde_json::to_string(&assets)
.map_err(|e| AssetError::Common(MmexError::Internal(e.to_string())))
}
}