]> uap-core.de Git - note.git/commitdiff
prepare note save main
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 25 May 2026 16:53:35 +0000 (18:53 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 25 May 2026 16:53:35 +0000 (18:53 +0200)
application/src/backend.rs
application/src/note.rs
application/src/notebook.rs
application/src/window.rs
entity/src/note.rs

index 1c537af8a5f9aca5de83f683ff8c499cf45f462b..8469c36fc27e2bdebe83925279d878c573bc9646 100644 (file)
@@ -295,4 +295,14 @@ impl BackendHandle {
         });
         let _ = self.tx.send(cmd);
     }
+    
+    pub fn save_note<F>(&self, note: entity::note::ActiveModel, callback: F)
+    where F: FnOnce(Result<note::ActiveModel, DbErr>) + Send + 'static {
+        let backend = self.backend.clone();
+        let cmd = Box::pin(async move {
+            let result = note.save(&backend.db).await;
+            callback(result);
+        });
+        let _ = self.tx.send(cmd);
+    }
 }
index afac9dbc7d0984571b056a936487aa4ac3b71c76..14b7c64ae4c86aa0a3e4984e965cfb08f48490a7 100644 (file)
@@ -1,12 +1,19 @@
+use sea_orm::prelude::DateTimeWithTimeZone;
+use sea_orm::sea_query::prelude::Utc;
+use sea_orm::{NotSet, Set};
 use entity::note::NoteType;
 use ui_rs::{ui_actions, UiModel};
 use ui_rs::ui::*;
+use crate::backend::BackendHandle;
 use crate::window::NoteTypeTabView;
 
 #[derive(UiModel, Default)]
 pub struct Note {
     pub note_id: i32,
 
+    pub kind: NoteType,
+    pub created: DateTimeWithTimeZone,
+
     #[bind("note_type")]
     pub note_type: UiInteger,
 
@@ -25,4 +32,42 @@ impl Note {
         };
         self.note_type.set(tab as i64);
     }
+
+    pub fn save(&self, collection_id: i32, backend: &BackendHandle) {
+        let content = self.text.get();
+        let title = match generate_title(content.as_str()) {
+            Some(title) => title.to_string(),
+            None => "Note".to_string()
+        };
+
+        let note = entity::note::ActiveModel {
+            note_id: if self.note_id == 0 { NotSet } else { Set(self.note_id) },
+            collection_id: Set(collection_id),
+            kind: Set(self.kind.clone()),
+            title: Set(title),
+            content: Set(content),
+            lastmodified: Set(Utc::now().into()),
+            created: if self.note_id == 0 { Set(Utc::now().into()) } else { NotSet }
+        };
+
+        backend.save_note(note, |result|{
+            match result {
+                Ok(model) => {
+                    println!("Note saved");
+                },
+                Err(error) => {
+                    println!("Failed to save note: {}", error);
+                }
+            }
+        });
+    }
+}
+
+fn generate_title(s: &str) -> Option<&str> {
+    for line in s.lines() {
+        if !line.trim().is_empty() {
+            return Some(line);
+        }
+    }
+    None
 }
\ No newline at end of file
index 5ceeacfd3b3302fe865b6e2bc79e68fc913abc5d..2239b5ab9d3cc4a5590a74792b6fa8aa31f9c1a3 100644 (file)
@@ -2,10 +2,12 @@ use ui_rs::{action, ui_actions, UiModel};
 use ui_rs::ui::*;
 
 use entity::note::{Model as Note};
+use crate::backend::BackendHandle;
 
-#[derive(UiModel, Default)]
+#[derive(UiModel)]
 pub struct Notebook {
     pub doc_ref: UiDocRef<Notebook>,
+    pub backend: BackendHandle,
     pub collection_id: i32,
 
     pub selected_note: Option<UiDoc<crate::note::Note>>,
@@ -16,9 +18,13 @@ pub struct Notebook {
 
 #[ui_actions]
 impl Notebook {
-    pub fn new(id: i32) -> Self {
+    pub fn new(id: i32, backend: &BackendHandle) -> Self {
         Notebook {
-            collection_id: id, ..Default::default()
+            doc_ref: Default::default(),
+            backend: backend.clone(),
+            collection_id: id,
+            selected_note: None,
+            notes: Default::default()
         }
     }
 
@@ -37,8 +43,20 @@ impl Notebook {
             if let Some(note) = s.selected_element(self.notes.data()) {
                 // detach the current note
                 if let Some(current) = &self.selected_note {
-                    self.doc_ref.get_doc().unwrap().ctx.detach(current);
-                    self.selected_note = None;
+                    // doc_ref.get_doc() should never fail here
+                    if let Some(mut doc) = self.doc_ref.get_doc() {
+                        // save the note
+                        let note_proxy = current.doc_proxy();
+                        let backend = self.backend.clone();
+                        let collection_id = self.collection_id;
+                        note_proxy.call_mainthread(move |doc, note|{
+                            note.save(collection_id, &backend);
+                        });
+                        
+                        // detach the note
+                        doc.ctx.detach(current);
+                        self.selected_note = None;
+                    }
                 }
 
                 // Create the new note
index 8534825cf260960e7c865bc93b812ffc9649a3c8..ac5e96b0c9a19579d2c07f1e93b9a7dddfcea1d2 100644 (file)
@@ -132,7 +132,7 @@ pub fn create_window(app: &App, ctx: &AppContext<MainWindow>) -> UiObject<MainWi
                                 e.obj.ctx.detach(current);
                             }
 
-                            let notebook = Notebook::new(nb.collection_id);
+                            let notebook = Notebook::new(nb.collection_id, &e.data.backend);
                             let doc = UiDoc::new2(notebook, |notebook, doc| {
                                 notebook.doc_ref = doc.doc_ref();
                             });
index 53f4c5e7887b0c95038cb4476698b2343bad1da9..54bed383f565f870949a5e4a3cdf3ed0f75834f4 100644 (file)
@@ -16,9 +16,10 @@ pub struct Model {
     pub created: DateTimeWithTimeZone,
 }
 
-#[derive(EnumIter, DeriveActiveEnum, Clone, Debug, PartialEq)]
+#[derive(EnumIter, DeriveActiveEnum, Clone, Debug, PartialEq, Default)]
 #[sea_orm(rs_type = "i32", db_type = "Integer")]
 pub enum NoteType {
+    #[default]
     PlainTextNote = 0,
     MDTextNote = 1,
 }