saving might work idk
This commit is contained in:
@@ -53,3 +53,87 @@ pub fn delete_app(conn: &mut SqliteConnection, app_id: &str) -> usize {
|
||||
.execute(conn)
|
||||
.expect("Error deleting app")
|
||||
}
|
||||
|
||||
pub fn create_blog_db(
|
||||
conn: &mut SqliteConnection,
|
||||
title: &str,
|
||||
content: &str,
|
||||
linked_app_ids: Option<&[String]>,
|
||||
) -> models::Blog {
|
||||
use crate::db::schema::blog;
|
||||
|
||||
// json seralize the referenced app ids, store as string
|
||||
if let Some(ids) = linked_app_ids {
|
||||
for id in ids {
|
||||
// Check if the app exists in the database
|
||||
let app_exists = crate::db::schema::app::dsl::app
|
||||
.filter(crate::db::schema::app::dsl::id.eq(id))
|
||||
.first::<models::App>(conn)
|
||||
.optional()
|
||||
.expect("Error checking if app exists");
|
||||
|
||||
if app_exists.is_none() {
|
||||
panic!("App with id {} does not exist", id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if there are linked app ids, serialize them to json, else set to None
|
||||
let reference: Option<String> = if let Some(ids) = linked_app_ids {
|
||||
Some(serde_json::to_string(&ids).expect("Failed to convert to JSON"))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let new_blog = models::Blog {
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
title: title.to_string(),
|
||||
content: content.to_string(),
|
||||
reference,
|
||||
};
|
||||
|
||||
diesel::insert_into(blog::table)
|
||||
.values(&new_blog)
|
||||
.execute(conn)
|
||||
.expect("Error saving new blog");
|
||||
|
||||
new_blog
|
||||
}
|
||||
|
||||
// Will be used to update an existing blog post OR create a new one if it doesn't exist
|
||||
pub fn save_blog(
|
||||
conn: &mut SqliteConnection,
|
||||
blog_id: Option<&str>,
|
||||
title: &str,
|
||||
content: &str,
|
||||
linked_app_ids: Option<&[String]>,
|
||||
) -> models::Blog {
|
||||
use crate::db::schema::blog;
|
||||
|
||||
// json seralize the referenced app ids, store as string
|
||||
let reference: Option<String> = if let Some(ids) = linked_app_ids {
|
||||
Some(serde_json::to_string(&ids).expect("Failed to convert to JSON"))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if let Some(id) = blog_id {
|
||||
// Update existing blog
|
||||
diesel::update(blog::table.filter(blog::id.eq(id)))
|
||||
.set((
|
||||
blog::title.eq(title),
|
||||
blog::content.eq(content),
|
||||
blog::reference.eq(reference),
|
||||
))
|
||||
.execute(conn)
|
||||
.expect("Error updating blog");
|
||||
|
||||
blog::table
|
||||
.filter(blog::id.eq(id))
|
||||
.first::<models::Blog>(conn)
|
||||
.expect("Error loading updated blog")
|
||||
} else {
|
||||
// Create new blog
|
||||
create_blog_db(conn, title, content, linked_app_ids)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ pub struct App {
|
||||
pub ssh_address: Option<String>, // default to normal address
|
||||
}
|
||||
|
||||
#[derive(Queryable, Selectable, Debug, Serialize, Deserialize)]
|
||||
#[derive(Queryable, Selectable, Debug, Insertable, Serialize, Deserialize)]
|
||||
#[diesel(table_name = crate::db::schema::blog)]
|
||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub struct Blog {
|
||||
@@ -22,7 +22,7 @@ pub struct Blog {
|
||||
pub reference: Option<String>, // uuid
|
||||
}
|
||||
|
||||
#[derive(Queryable, Selectable, Debug, Serialize, Deserialize)]
|
||||
#[derive(Queryable, Selectable, Debug, Insertable, Serialize, Deserialize)]
|
||||
#[diesel(table_name = crate::db::schema::identities)]
|
||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub struct Identity {
|
||||
|
||||
+17
-7
@@ -27,14 +27,12 @@ async fn ping_target(address: &str) -> bool {
|
||||
|
||||
let ip_target: IpAddr = socket_addr.ip();
|
||||
|
||||
// 2. Configure unprivileged ICMP requestor
|
||||
let timeout = Some(Duration::from_secs(2));
|
||||
let pinger = match IcmpEchoRequestor::new(ip_target, None, None, timeout) {
|
||||
Ok(p) => p,
|
||||
Err(_) => return false,
|
||||
};
|
||||
|
||||
// 3. Send ICMP Echo request asynchronously
|
||||
match pinger.send().await {
|
||||
Ok(reply) => {
|
||||
// Check if the status returned is a valid success response
|
||||
@@ -46,7 +44,7 @@ async fn ping_target(address: &str) -> bool {
|
||||
|
||||
#[tauri::command]
|
||||
fn new_app(name: &str, address: &str, description: Option<&str>, ssh_address: Option<&str>) -> App {
|
||||
let mut conn = &mut establish_connection();
|
||||
let mut conn = establish_connection();
|
||||
let app = create_app(&mut conn, name, address, description, ssh_address);
|
||||
|
||||
println!("{:#?}", app);
|
||||
@@ -55,7 +53,7 @@ fn new_app(name: &str, address: &str, description: Option<&str>, ssh_address: Op
|
||||
|
||||
#[tauri::command]
|
||||
fn get_app() -> Vec<App> {
|
||||
let mut conn = &mut establish_connection();
|
||||
let mut conn = establish_connection();
|
||||
let apps = get_apps(&mut conn);
|
||||
|
||||
println!("{:#?}", apps);
|
||||
@@ -64,8 +62,8 @@ fn get_app() -> Vec<App> {
|
||||
|
||||
#[tauri::command]
|
||||
fn delete_apps(id: &str) {
|
||||
let conn = &mut establish_connection();
|
||||
delete_app(conn, id);
|
||||
let mut conn = establish_connection();
|
||||
delete_app(&mut conn, id);
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -76,6 +74,17 @@ async fn ping(address: &str) -> Result<bool, String> {
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn save_blog(
|
||||
id: Option<&str>,
|
||||
title: &str,
|
||||
content: &str,
|
||||
linked_app_ids: Option<Vec<String>>,
|
||||
) -> db::models::Blog {
|
||||
let mut conn = establish_connection();
|
||||
db::save_blog(&mut conn, id, title, content, linked_app_ids.as_deref())
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
@@ -84,7 +93,8 @@ pub fn run() {
|
||||
new_app,
|
||||
get_app,
|
||||
delete_apps,
|
||||
ping
|
||||
ping,
|
||||
save_blog
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
Reference in New Issue
Block a user