-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implements backend of the settings modal
- Loading branch information
1 parent
2824d56
commit b8df16a
Showing
13 changed files
with
180 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod note; | ||
pub mod setting; | ||
pub mod user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use crate::model::setting::Setting; | ||
use crate::model::{DatabaseManager, SqliteManager}; | ||
use crate::schema::settings::dsl::settings; | ||
use diesel::{insert_into, prelude::*}; | ||
use serde::{Deserialize, Serialize}; | ||
use tauri::State; | ||
|
||
use crate::{ | ||
schema::settings::dsl::id as setting_id, | ||
schema::settings::dsl::run_on_background as setting_run_on_background, | ||
schema::settings::dsl::sync as setting_sync, | ||
}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct ParsedSetting { | ||
pub id: i32, | ||
pub sync: bool, | ||
#[serde(rename = "runOnBackground")] | ||
pub run_on_background: bool, | ||
} | ||
|
||
impl ParsedSetting { | ||
fn parse(setting: Setting) -> ParsedSetting { | ||
ParsedSetting { | ||
id: setting.id, | ||
sync: i16_to_bool(setting.sync), | ||
run_on_background: i16_to_bool(setting.run_on_background), | ||
} | ||
} | ||
} | ||
|
||
#[tauri::command] | ||
pub fn get_setting(state: State<DatabaseManager<SqliteManager>>) -> Result<ParsedSetting, String> { | ||
let connection = &mut state.pool.get().map_err(|err| err.to_string())?; | ||
|
||
let result = settings | ||
.select(Setting::as_select()) | ||
.first(connection) | ||
.optional() | ||
.map_err(|err| err.to_string())?; | ||
|
||
let setting = match result { | ||
Some(setting) => setting, | ||
None => create_setting(true, true, state)?, | ||
}; | ||
|
||
Ok(ParsedSetting::parse(setting)) | ||
} | ||
|
||
#[tauri::command] | ||
pub fn set_setting( | ||
sync: bool, | ||
run_on_background: bool, | ||
state: State<DatabaseManager<SqliteManager>>, | ||
) -> Result<ParsedSetting, String> { | ||
let connection = &mut state.pool.get().map_err(|err| err.to_string())?; | ||
|
||
let updated_setting = diesel::update(settings.filter(setting_id.eq(1))) | ||
.set(( | ||
setting_sync.eq(bool_to_i16(sync)), | ||
setting_run_on_background.eq(bool_to_i16(run_on_background)), | ||
)) | ||
.returning(Setting::as_returning()) | ||
.get_result(connection) | ||
.map_err(|err| err.to_string())?; | ||
|
||
Ok(ParsedSetting::parse(updated_setting)) | ||
} | ||
|
||
fn create_setting( | ||
sync: bool, | ||
run_on_background: bool, | ||
state: State<DatabaseManager<SqliteManager>>, | ||
) -> Result<Setting, String> { | ||
let connection = &mut state.pool.get().map_err(|err| err.to_string())?; | ||
|
||
let result = insert_into(settings) | ||
.values(( | ||
setting_sync.eq(bool_to_i16(sync)), | ||
setting_run_on_background.eq(bool_to_i16(run_on_background)), | ||
)) | ||
.returning(Setting::as_returning()) | ||
.get_result(connection) | ||
.map_err(|err| err.to_string())?; | ||
|
||
Ok(result) | ||
} | ||
|
||
fn bool_to_i16(v: bool) -> i16 { | ||
match v { | ||
true => 1, | ||
false => 0, | ||
} | ||
} | ||
|
||
fn i16_to_bool(v: i16) -> bool { | ||
match v { | ||
1 => true, | ||
_ => false, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use diesel::prelude::*; | ||
use serde::Serialize; | ||
|
||
#[derive(Serialize, Queryable, Selectable)] | ||
#[diesel(table_name = crate::schema::settings)] | ||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))] | ||
pub struct Setting { | ||
pub id: i32, | ||
pub run_on_background: i16, | ||
pub sync: i16, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { invoke } from '@tauri-apps/api/tauri'; | ||
import { UserSettings, SettingsProvider } from '../types'; | ||
|
||
class SettingsService implements SettingsProvider { | ||
async getSetting() { | ||
const setting = await invoke<UserSettings>('get_setting'); | ||
|
||
return setting; | ||
} | ||
|
||
async setSetting(settings: UserSettings) { | ||
const updatedSetting = await invoke<UserSettings>('set_setting', { | ||
...settings | ||
}); | ||
|
||
return updatedSetting; | ||
} | ||
} | ||
|
||
export default new SettingsService(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters