35 lines
997 B
Rust
Raw Normal View History

use crate::{
2021-07-19 22:44:37 +08:00
entities::app::{App, CreateAppParams, *},
errors::*,
module::WorkspaceUser,
sql_tables::app::*,
};
use flowy_database::{prelude::*, schema::app_table};
use std::sync::Arc;
pub struct AppController {
user: Arc<dyn WorkspaceUser>,
}
impl AppController {
pub fn new(user: Arc<dyn WorkspaceUser>) -> Self { Self { user } }
2021-07-19 22:44:37 +08:00
pub fn save_app(&self, params: CreateAppParams) -> Result<App, WorkspaceError> {
let app_table = AppTable::new(params);
let conn = self.user.db_connection()?;
2021-07-19 22:44:37 +08:00
let app: App = app_table.clone().into();
let _ = diesel::insert_into(app_table::table)
2021-07-19 22:44:37 +08:00
.values(app_table)
.execute(&*conn)?;
2021-07-19 22:44:37 +08:00
Ok(app)
}
pub fn update_app(&self, params: UpdateAppParams) -> Result<(), WorkspaceError> {
let changeset = AppTableChangeset::new(params);
let conn = self.user.db_connection()?;
diesel_update_table!(app_table, changeset, conn);
Ok(())
}
}