From 84c077f941a5659324ac40709c17925099488a4a Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Fri, 19 Jul 2019 19:38:25 -0400 Subject: [PATCH] fix bugs and add copy function to db classes --- src/db/asignatura.vala | 6 ++++++ src/db/bloque.vala | 5 +++++ src/db/horario.vala | 8 ++++++++ src/horario_editor.vala | 43 +++++++++++++++++++++++++++++++++-------- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/db/asignatura.vala b/src/db/asignatura.vala index c01de71..ae975c4 100644 --- a/src/db/asignatura.vala +++ b/src/db/asignatura.vala @@ -10,6 +10,12 @@ namespace Colegio { this.nombre = nombre; this.profesor = profesor; } + + public void copy (Asignatura asignatura) { + this.id_asignatura = asignatura.id_asignatura; + this.nombre = asignatura.nombre; + this.profesor = new Profesor (); // Don't copy profesor + } } } } diff --git a/src/db/bloque.vala b/src/db/bloque.vala index 513446a..ea3caf9 100644 --- a/src/db/bloque.vala +++ b/src/db/bloque.vala @@ -8,6 +8,11 @@ namespace Colegio { this.id_bloque = id_bloque; this.descripcion = descripcion; } + + public void copy (Bloque bloque) { + this.id_bloque = bloque.id_bloque; + this.descripcion = bloque.descripcion; + } } } } diff --git a/src/db/horario.vala b/src/db/horario.vala index e522797..d0c6d90 100644 --- a/src/db/horario.vala +++ b/src/db/horario.vala @@ -10,6 +10,14 @@ namespace Colegio { this.bloque = bloque; this.sala = sala; } + + public void copy (Horario horario) { + this.sala = horario.sala; + this.asignatura = new Asignatura (); + this.asignatura.copy (horario.asignatura); + this.bloque = new Bloque (); + this.bloque.copy (horario.bloque); + } } } } diff --git a/src/horario_editor.vala b/src/horario_editor.vala index 4edb7dd..ba80e6b 100644 --- a/src/horario_editor.vala +++ b/src/horario_editor.vala @@ -23,7 +23,7 @@ namespace Colegio { public class HorarioEditor : Gtk.ApplicationWindow { private Connection conn; private Horario horario; - private bool new_horario = false; + private Horario? old_horario = null; private List asignaturas; private Gtk.ListStore asignatura_list_store; @@ -62,6 +62,10 @@ namespace Colegio { Object (application: application); this.conn = conn; this.horario = horario; + if (this.horario != null) { + this.old_horario = new Horario (); + this.old_horario.copy (horario); + } } [GtkCallback] @@ -72,7 +76,7 @@ namespace Colegio { else if (button == save) { update_instance (); if (validate_data ()) { - if (new_horario) { + if (old_horario == null) { try { var res = conn.db.exec (" INSERT INTO horario @@ -108,9 +112,11 @@ VALUES try { var res = conn.db.exec (" UPDATE horario SET + id_asignatura = " + horario.asignatura.id_asignatura.to_string () + ", + id_bloque = " + horario.bloque.id_bloque.to_string () + ", sala = '" + conn.escape (horario.sala) + "' -WHERE id_asignatura = " + horario.asignatura.id_asignatura.to_string () + " AND -id_bloque = " + horario.bloque.id_bloque.to_string () +WHERE id_asignatura = " + old_horario.asignatura.id_asignatura.to_string () + " AND +id_bloque = " + old_horario.bloque.id_bloque.to_string () ); if (res.get_status () != ExecStatus.COMMAND_OK) { #if DEBUG @@ -167,7 +173,7 @@ FROM horario int n = res.get_n_tuples (); for (int i = 0; i < n; i++) { try { - if (new_horario && + if (check_old_horario (wra, i) && wra.get_int_n (i, "id_asignatura") == horario.asignatura.id_asignatura && wra.get_int_n (i, "id_bloque") == horario.bloque.id_bloque) { var msg = new Gtk.MessageDialog (this, @@ -196,6 +202,30 @@ FROM horario return true; } + private bool check_old_horario (ResultWrapper wra, int i) { + try { + if (old_horario == null) { + // This is a new horario + return true; + } + else if (old_horario.asignatura.id_asignatura != wra.get_int_n (i, "id_asignatura") || + old_horario.bloque.id_bloque != wra.get_int_n (i, "id_bloque")) { + return true; + } + else { + return false; + } + } + catch (Error e) { + #if DEBUG + error (e.message); + #else + warning (e.message); + return false; + #endif + } + } + private void update_instance () { horario.sala = sala.get_text ().strip (); @@ -306,11 +336,8 @@ FROM bloque if (horario != null) { sala.set_text (horario.sala); - asignatura.set_sensitive (false); - bloque.set_sensitive (false); } else { - new_horario = true; horario = new Horario (); horario.asignatura = new Asignatura (); horario.bloque = new Bloque ();