fix bugs and add copy function to db classes

This commit is contained in:
Chris Cromer 2019-07-19 19:38:25 -04:00
parent d6ebf12814
commit 84c077f941
Signed by: cromer
GPG Key ID: 39CC813FF3C8708A
4 changed files with 54 additions and 8 deletions

View File

@ -10,6 +10,12 @@ namespace Colegio {
this.nombre = nombre; this.nombre = nombre;
this.profesor = profesor; 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
}
} }
} }
} }

View File

@ -8,6 +8,11 @@ namespace Colegio {
this.id_bloque = id_bloque; this.id_bloque = id_bloque;
this.descripcion = descripcion; this.descripcion = descripcion;
} }
public void copy (Bloque bloque) {
this.id_bloque = bloque.id_bloque;
this.descripcion = bloque.descripcion;
}
} }
} }
} }

View File

@ -10,6 +10,14 @@ namespace Colegio {
this.bloque = bloque; this.bloque = bloque;
this.sala = sala; 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);
}
} }
} }
} }

View File

@ -23,7 +23,7 @@ namespace Colegio {
public class HorarioEditor : Gtk.ApplicationWindow { public class HorarioEditor : Gtk.ApplicationWindow {
private Connection conn; private Connection conn;
private Horario horario; private Horario horario;
private bool new_horario = false; private Horario? old_horario = null;
private List<Asignatura> asignaturas; private List<Asignatura> asignaturas;
private Gtk.ListStore asignatura_list_store; private Gtk.ListStore asignatura_list_store;
@ -62,6 +62,10 @@ namespace Colegio {
Object (application: application); Object (application: application);
this.conn = conn; this.conn = conn;
this.horario = horario; this.horario = horario;
if (this.horario != null) {
this.old_horario = new Horario ();
this.old_horario.copy (horario);
}
} }
[GtkCallback] [GtkCallback]
@ -72,7 +76,7 @@ namespace Colegio {
else if (button == save) { else if (button == save) {
update_instance (); update_instance ();
if (validate_data ()) { if (validate_data ()) {
if (new_horario) { if (old_horario == null) {
try { try {
var res = conn.db.exec (" var res = conn.db.exec ("
INSERT INTO horario INSERT INTO horario
@ -108,9 +112,11 @@ VALUES
try { try {
var res = conn.db.exec (" var res = conn.db.exec ("
UPDATE horario SET UPDATE horario SET
id_asignatura = " + horario.asignatura.id_asignatura.to_string () + ",
id_bloque = " + horario.bloque.id_bloque.to_string () + ",
sala = '" + conn.escape (horario.sala) + "' sala = '" + conn.escape (horario.sala) + "'
WHERE id_asignatura = " + horario.asignatura.id_asignatura.to_string () + " AND WHERE id_asignatura = " + old_horario.asignatura.id_asignatura.to_string () + " AND
id_bloque = " + horario.bloque.id_bloque.to_string () id_bloque = " + old_horario.bloque.id_bloque.to_string ()
); );
if (res.get_status () != ExecStatus.COMMAND_OK) { if (res.get_status () != ExecStatus.COMMAND_OK) {
#if DEBUG #if DEBUG
@ -167,7 +173,7 @@ FROM horario
int n = res.get_n_tuples (); int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
try { 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_asignatura") == horario.asignatura.id_asignatura &&
wra.get_int_n (i, "id_bloque") == horario.bloque.id_bloque) { wra.get_int_n (i, "id_bloque") == horario.bloque.id_bloque) {
var msg = new Gtk.MessageDialog (this, var msg = new Gtk.MessageDialog (this,
@ -196,6 +202,30 @@ FROM horario
return true; 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 () { private void update_instance () {
horario.sala = sala.get_text ().strip (); horario.sala = sala.get_text ().strip ();
@ -306,11 +336,8 @@ FROM bloque
if (horario != null) { if (horario != null) {
sala.set_text (horario.sala); sala.set_text (horario.sala);
asignatura.set_sensitive (false);
bloque.set_sensitive (false);
} }
else { else {
new_horario = true;
horario = new Horario (); horario = new Horario ();
horario.asignatura = new Asignatura (); horario.asignatura = new Asignatura ();
horario.bloque = new Bloque (); horario.bloque = new Bloque ();