Add service layer to abstract business logic
Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
97
src/service.rs
Normal file
97
src/service.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
use crate::dag::{build_flow_dag, DagResult, FlowDag};
|
||||
use crate::models::{Actor, Context, Flow, Job, Message, Runner};
|
||||
use crate::storage::RedisDriver;
|
||||
|
||||
pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
pub struct AppService {
|
||||
redis: RedisDriver,
|
||||
}
|
||||
|
||||
impl AppService {
|
||||
pub fn new(redis: RedisDriver) -> Self {
|
||||
Self { redis }
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Context
|
||||
// -----------------------------
|
||||
pub async fn create_context(&self, ctx: Context) -> Result<Context, BoxError> {
|
||||
self.redis.save_context(&ctx).await?;
|
||||
Ok(ctx)
|
||||
}
|
||||
|
||||
pub async fn load_context(&self, id: u32) -> Result<Context, BoxError> {
|
||||
let ctx = self.redis.load_context(id).await?;
|
||||
Ok(ctx)
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Actor
|
||||
// -----------------------------
|
||||
pub async fn create_actor(&self, context_id: u32, actor: Actor) -> Result<Actor, BoxError> {
|
||||
self.redis.save_actor(context_id, &actor).await?;
|
||||
Ok(actor)
|
||||
}
|
||||
|
||||
pub async fn load_actor(&self, context_id: u32, id: u32) -> Result<Actor, BoxError> {
|
||||
let actor = self.redis.load_actor(context_id, id).await?;
|
||||
Ok(actor)
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Runner
|
||||
// -----------------------------
|
||||
pub async fn create_runner(&self, context_id: u32, runner: Runner) -> Result<Runner, BoxError> {
|
||||
self.redis.save_runner(context_id, &runner).await?;
|
||||
Ok(runner)
|
||||
}
|
||||
|
||||
pub async fn load_runner(&self, context_id: u32, id: u32) -> Result<Runner, BoxError> {
|
||||
let runner = self.redis.load_runner(context_id, id).await?;
|
||||
Ok(runner)
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Flow
|
||||
// -----------------------------
|
||||
pub async fn create_flow(&self, context_id: u32, flow: Flow) -> Result<Flow, BoxError> {
|
||||
self.redis.save_flow(context_id, &flow).await?;
|
||||
Ok(flow)
|
||||
}
|
||||
|
||||
pub async fn load_flow(&self, context_id: u32, id: u32) -> Result<Flow, BoxError> {
|
||||
let flow = self.redis.load_flow(context_id, id).await?;
|
||||
Ok(flow)
|
||||
}
|
||||
|
||||
pub async fn flow_dag(&self, context_id: u32, flow_id: u32) -> DagResult<FlowDag> {
|
||||
build_flow_dag(&self.redis, context_id, flow_id).await
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Job
|
||||
// -----------------------------
|
||||
pub async fn create_job(&self, context_id: u32, job: Job) -> Result<Job, BoxError> {
|
||||
self.redis.save_job(context_id, &job).await?;
|
||||
Ok(job)
|
||||
}
|
||||
|
||||
pub async fn load_job(&self, context_id: u32, caller_id: u32, id: u32) -> Result<Job, BoxError> {
|
||||
let job = self.redis.load_job(context_id, caller_id, id).await?;
|
||||
Ok(job)
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Message
|
||||
// -----------------------------
|
||||
pub async fn create_message(&self, context_id: u32, message: Message) -> Result<Message, BoxError> {
|
||||
self.redis.save_message(context_id, &message).await?;
|
||||
Ok(message)
|
||||
}
|
||||
|
||||
pub async fn load_message(&self, context_id: u32, caller_id: u32, id: u32) -> Result<Message, BoxError> {
|
||||
let msg = self.redis.load_message(context_id, caller_id, id).await?;
|
||||
Ok(msg)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user