Skip to content

Commit

Permalink
feat: add/remove projects
Browse files Browse the repository at this point in the history
  • Loading branch information
dancixx committed Dec 9, 2023
1 parent 1694e11 commit 844f7fa
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 63 deletions.
1 change: 1 addition & 0 deletions src-tauri/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
# will have compiled files and executables
/target/

/project_db/
4 changes: 0 additions & 4 deletions src-tauri/project_db/conf

This file was deleted.

Binary file removed src-tauri/project_db/db
Binary file not shown.
Binary file removed src-tauri/project_db/snap.00000000000000D3
Binary file not shown.
5 changes: 3 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod project_db;
mod utils;

use postgres::{get_schema_tables, get_sql_result, pg_connector};
use project_db::{get_project_details, get_projects, ProjectDB};
use project_db::{get_project_details, get_projects, remove_project, ProjectDB};
use std::sync::Arc;
#[cfg(debug_assertions)]
use tauri::Manager;
Expand Down Expand Up @@ -48,7 +48,8 @@ fn main() {
get_project_details,
get_schema_tables,
pg_connector,
get_sql_result
get_sql_result,
remove_project,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
128 changes: 71 additions & 57 deletions src-tauri/src/project_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,86 +5,100 @@ use crate::AppState;

#[tauri::command]
pub async fn get_projects(app_state: State<'_, AppState>) -> Result<Vec<String>> {
let project_db = app_state.project_db.lock().await;
let project_db = project_db.as_ref().unwrap();

let db = sled::open(&project_db.db_path).unwrap();
let projects = db
.iter()
.map(|r| {
let (project, _) = r.unwrap();
String::from_utf8(project.to_vec()).unwrap()
})
.collect();

Ok(projects)
let project_db = app_state.project_db.lock().await;
let project_db = project_db.as_ref().unwrap();

let db = sled::open(&project_db.db_path).unwrap();
let projects = db
.iter()
.map(|r| {
let (project, _) = r.unwrap();
String::from_utf8(project.to_vec()).unwrap()
})
.collect();

Ok(projects)
}

#[tauri::command]
pub async fn get_project_details(
project: String,
app_state: State<'_, AppState>,
project: String,
app_state: State<'_, AppState>,
) -> Result<ProjectDetails> {
let project_db = app_state.project_db.lock().await;
let project_db = project_db.as_ref().unwrap();
let project_db = app_state.project_db.lock().await;
let project_db = project_db.as_ref().unwrap();

let project_details = project_db.get_connection_string(project.as_str())?;
let project_details = project_db.get_connection_string(project.as_str())?;

Ok(project_details)
Ok(project_details)
}

#[tauri::command]
pub async fn remove_project(project: String, app_state: State<'_, AppState>) -> Result<()> {
let project_db = app_state.project_db.lock().await;
let project_db = project_db.as_ref().unwrap();
project_db.remove_project(project.as_str())?;
Ok(())
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct ProjectDetails {
pub user: String,
pub password: String,
pub host: String,
pub port: String,
pub user: String,
pub password: String,
pub host: String,
pub port: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProjectDB {
pub db_path: String,
pub db_path: String,
}

impl Default for ProjectDB {
fn default() -> Self {
Self::new()
}
fn default() -> Self {
Self::new()
}
}

impl ProjectDB {
pub fn new() -> Self {
Self {
db_path: String::from("project_db"),
pub fn new() -> Self {
Self {
db_path: String::from("project_db"),
}
}

pub fn get_connection_string(&self, project: &str) -> Result<ProjectDetails> {
let db = sled::open(&self.db_path).unwrap();
let connection_string = db.get(project).unwrap();
let mut project_details = ProjectDetails::default();

if let Some(connection_string) = connection_string {
let connection_string = connection_string.to_vec();
let connection_string = String::from_utf8(connection_string).unwrap();
let connection_string = connection_string.split(' ').collect::<Vec<&str>>();

for connection_string in connection_string {
let connection_string = connection_string.split('=').collect::<Vec<&str>>();
let key = connection_string[0];
let value = connection_string[1];

match key {
"user" => project_details.user = value.to_string(),
"password" => project_details.password = value.to_string(),
"host" => project_details.host = value.to_string(),
"port" => project_details.port = value.to_string(),
_ => (),
}
}
}

pub fn get_connection_string(&self, project: &str) -> Result<ProjectDetails> {
let db = sled::open(&self.db_path).unwrap();
let connection_string = db.get(project).unwrap();
let mut project_details = ProjectDetails::default();

if let Some(connection_string) = connection_string {
let connection_string = connection_string.to_vec();
let connection_string = String::from_utf8(connection_string).unwrap();
let connection_string = connection_string.split(" ").collect::<Vec<&str>>();

for connection_string in connection_string {
let connection_string = connection_string.split("=").collect::<Vec<&str>>();
let key = connection_string[0];
let value = connection_string[1];

match key {
"user" => project_details.user = value.to_string(),
"password" => project_details.password = value.to_string(),
"host" => project_details.host = value.to_string(),
"port" => project_details.port = value.to_string(),
_ => (),
}
}
}
Ok(project_details)
}

Ok(project_details)
}
}
pub fn remove_project(&self, project: &str) -> Result<()> {
let db = sled::open(&self.db_path).unwrap();
db.remove(project).unwrap();

Ok(())
}
}

0 comments on commit 844f7fa

Please sign in to comment.