Add AppState to Controller and split up models
This commit is contained in:
parent
8dc9072fe8
commit
83c1f033d5
11 changed files with 261 additions and 150 deletions
emgauwa-controller/src
48
emgauwa-controller/src/app_state.rs
Normal file
48
emgauwa-controller/src/app_state.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
use actix::{Actor, Context, Handler, Message};
|
||||
use emgauwa_lib::errors::EmgauwaError;
|
||||
use emgauwa_lib::models::Controller;
|
||||
use futures::executor::block_on;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Result<(), EmgauwaError>")]
|
||||
pub struct Reload {}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Controller")]
|
||||
pub struct GetThis {}
|
||||
|
||||
pub struct AppState {
|
||||
pub pool: Pool<Sqlite>,
|
||||
pub this: Controller,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new(pool: Pool<Sqlite>, this: Controller) -> AppState {
|
||||
AppState { pool, this }
|
||||
}
|
||||
}
|
||||
|
||||
impl Actor for AppState {
|
||||
type Context = Context<Self>;
|
||||
}
|
||||
|
||||
impl Handler<Reload> for AppState {
|
||||
type Result = Result<(), EmgauwaError>;
|
||||
|
||||
fn handle(&mut self, _msg: Reload, _ctx: &mut Self::Context) -> Self::Result {
|
||||
let mut pool_conn = block_on(self.pool.acquire())?;
|
||||
|
||||
self.this.reload(&mut pool_conn)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler<GetThis> for AppState {
|
||||
type Result = Controller;
|
||||
|
||||
fn handle(&mut self, _msg: GetThis, _ctx: &mut Self::Context) -> Self::Result {
|
||||
self.this.clone()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue