diff --git a/src-tauri/src/db/create.sql b/src-tauri/src/db/create.sql index 9666e55c..5fff7cc0 100644 --- a/src-tauri/src/db/create.sql +++ b/src-tauri/src/db/create.sql @@ -43,4 +43,8 @@ CREATE TABLE Games ( FOREIGN KEY(SiteID) REFERENCES Sites, FOREIGN KEY(WhiteID) REFERENCES Players, FOREIGN KEY(BlackID) REFERENCES Players -); \ No newline at end of file +); + +INSERT INTO Players (ID, Name, Elo) VALUES (0, 'Unknown', NULL); +INSERT INTO Events (ID, Name) VALUES (0, 'Unknown'); +INSERT INTO Sites (ID, Name) VALUES (0, 'Unknown'); \ No newline at end of file diff --git a/src-tauri/src/db/mod.rs b/src-tauri/src/db/mod.rs index 1d7761dd..f98983db 100644 --- a/src-tauri/src/db/mod.rs +++ b/src-tauri/src/db/mod.rs @@ -196,35 +196,28 @@ impl TempGame { pub fn insert_to_db(&self, db: &mut SqliteConnection) -> Result<(), diesel::result::Error> { let pawn_home = get_pawn_home(self.position.board()); - let white; - let black; - let site_id; - let event_id; - - if let Some(name) = &self.white_name { - white = create_player(db, name)?; + let white_id = if let Some(name) = &self.white_name { + create_player(db, name)?.id } else { - white = Player::default(); - } - if let Some(name) = &self.black_name { - black = create_player(db, name)?; + 0 + }; + let black_id = if let Some(name) = &self.black_name { + create_player(db, name)?.id } else { - black = Player::default(); - } + 0 + }; - if let Some(name) = &self.event_name { - let event = create_event(db, name)?; - event_id = event.id; + let event_id = if let Some(name) = &self.event_name { + create_event(db, name)?.id } else { - event_id = 1; - } + 0 + }; - if let Some(name) = &self.site_name { - let site = create_site(db, name)?; - site_id = site.id; + let site_id = if let Some(name) = &self.site_name { + create_site(db, name)?.id } else { - site_id = 1; - } + 0 + }; let ply_count = (self.moves.len()) as i32; let final_material = get_material_count(self.position.board()); @@ -232,8 +225,8 @@ impl TempGame { let minimal_black_material = self.material_count.black.min(final_material.black) as i32; let new_game = NewGame { - white_id: Some(white.id), - black_id: Some(black.id), + white_id, + black_id, ply_count, eco: self.eco.as_deref(), round: self.round.as_deref(), diff --git a/src-tauri/src/db/models.rs b/src-tauri/src/db/models.rs index f88d3689..f72552df 100644 --- a/src-tauri/src/db/models.rs +++ b/src-tauri/src/db/models.rs @@ -67,9 +67,9 @@ pub struct NewGame<'a> { pub date: Option<&'a str>, pub time: Option<&'a str>, pub round: Option<&'a str>, - pub white_id: Option, + pub white_id: i32, pub white_elo: Option, - pub black_id: Option, + pub black_id: i32, pub black_elo: Option, pub white_material: i32, pub black_material: i32,