validity checks

This commit is contained in:
Chris Cromer 2019-01-20 11:25:55 -03:00
parent 7e7b09cef1
commit fece34f7dc
Signed by: cromer
GPG Key ID: 39CC813FF3C8708A
2 changed files with 205 additions and 41 deletions

View File

@ -141,6 +141,152 @@ namespace Sernatur {
} }
} }
/**
* Validate the tour data before trying to insert it into the database
* @return Returns true if the data is valid
*/
private bool validate_place_data () {
if (lugar.nombre_lugar.strip () == "") {
var msg = new Gtk.MessageDialog (this,
Gtk.DialogFlags.MODAL,
Gtk.MessageType.ERROR,
Gtk.ButtonsType.CLOSE,
_ ("Error: Place name cannot be left blank!"));
msg.response.connect ((response_id) => {
msg.destroy ();
});
msg.set_title (_ ("Error"));
msg.run ();
return false;
}
bool list_success = true;
try {
List<Lugar> list = Lugar.get_all_lugares (conn);
list.foreach ((entry) => {
if (lugar.nombre_lugar.down () == entry.nombre_lugar.down () && lugar.id_lugar != entry.id_lugar) {
var msg = new Gtk.MessageDialog (this,
Gtk.DialogFlags.MODAL,
Gtk.MessageType.ERROR,
Gtk.ButtonsType.CLOSE,
_ ("Error: A place named \"%s\" already exists!"), entry.nombre_lugar);
msg.response.connect ((response_id) => {
msg.destroy ();
});
msg.set_title (_ ("Error"));
msg.run ();
list_success = false;
}
});
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
return false;
#endif
}
return list_success;
}
/**
* Validate the city data before trying to insert it into the database
* @param ciudad The city to validate
* @return Returns true if the data is valid
*/
private bool validate_city_data (Ciudad ciudad) {
if (ciudad.nombre_ciudad.strip () == "") {
var msg = new Gtk.MessageDialog (this,
Gtk.DialogFlags.MODAL,
Gtk.MessageType.ERROR,
Gtk.ButtonsType.CLOSE,
_ ("Error: City name cannot be left blank!"));
msg.response.connect ((response_id) => {
msg.destroy ();
});
msg.set_title (_ ("Error"));
msg.run ();
return false;
}
bool list_success = true;
try {
List<Ciudad> list = Ciudad.get_all_ciudades (conn);
list.foreach ((entry) => {
if (ciudad.nombre_ciudad.down () == entry.nombre_ciudad.down ()) {
var msg = new Gtk.MessageDialog (this,
Gtk.DialogFlags.MODAL,
Gtk.MessageType.ERROR,
Gtk.ButtonsType.CLOSE,
_ ("Error: A city named \"%s\" already exists!"), entry.nombre_ciudad);
msg.response.connect ((response_id) => {
msg.destroy ();
});
msg.set_title (_ ("Error"));
msg.run ();
list_success = false;
}
});
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
return false;
#endif
}
return list_success;
}
/**
* Validate the region data before trying to insert it into the database
* @param region The region to validate
* @return Returns true if the data is valid
*/
private bool validate_region_data (Region region) {
if (region.nombre_region.strip () == "") {
var msg = new Gtk.MessageDialog (this,
Gtk.DialogFlags.MODAL,
Gtk.MessageType.ERROR,
Gtk.ButtonsType.CLOSE,
_ ("Error: Region name cannot be left blank!"));
msg.response.connect ((response_id) => {
msg.destroy ();
});
msg.set_title (_ ("Error"));
msg.run ();
return false;
}
bool list_success = true;
try {
List<Region> list = Region.get_all_regiones (conn);
list.foreach ((entry) => {
if (region.nombre_region.down () == entry.nombre_region.down ()) {
var msg = new Gtk.MessageDialog (this,
Gtk.DialogFlags.MODAL,
Gtk.MessageType.ERROR,
Gtk.ButtonsType.CLOSE,
_ ("Error: A region named \"%s\" already exists!"), entry.nombre_region);
msg.response.connect ((response_id) => {
msg.destroy ();
});
msg.set_title (_ ("Error"));
msg.run ();
list_success = false;
}
});
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
return false;
#endif
}
return list_success;
}
/** /**
* Reset the city dropdown * Reset the city dropdown
*/ */
@ -194,38 +340,40 @@ namespace Sernatur {
[GtkCallback] [GtkCallback]
public void on_clicked_button (Gtk.Button button) { public void on_clicked_button (Gtk.Button button) {
if (button == save) { if (button == save) {
if (lugar.id_lugar == 0) { if (update_place_instance () && validate_place_data ()) {
update_place_instance (); if (lugar.id_lugar == 0) {
try { update_place_instance ();
Lugar.insert_place (conn, lugar); try {
Lugar.insert_place (conn, lugar);
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
finally {
save_place (); // Signal the parent to update itself
this.close ();
}
} }
catch (Error e) { else {
#if DEBUG update_place_instance ();
error (e.message); try {
#else Lugar.update_place (conn, lugar);
warning (e.message); }
#endif catch (Error e) {
} #if DEBUG
finally { error (e.message);
save_place (); // Signal the parent to update itself #else
this.close (); warning (e.message);
} #endif
} }
else { finally {
update_place_instance (); save_place (); // Signal the parent to update itself
try { this.close ();
Lugar.update_place (conn, lugar); }
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
finally {
save_place (); // Signal the parent to update itself
this.close ();
} }
} }
} }
@ -237,14 +385,20 @@ namespace Sernatur {
/** /**
* Update the the place object with new info from the editor * Update the the place object with new info from the editor
*/ */
private void update_place_instance () { private bool update_place_instance () {
lugar.nombre_lugar = place_name.get_text (); lugar.nombre_lugar = place_name.get_text ();
lugar.valor_entrada = (uint) int.parse (ticket_price.get_text ()); lugar.valor_entrada = (uint) int.parse (ticket_price.get_text ());
lugar.nivel = (uint) difficulty.get_value_as_int (); lugar.nivel = (uint) difficulty.get_value_as_int ();
if (region.get_active () == -1) { if (region.get_active () == -1) {
Region new_region = new Region (0, region.get_active_text ()); Region new_region = new Region (0, region.get_active_text ());
try { try {
new_region.id_region = Region.insert_region (conn, new_region); if (validate_region_data (new_region)) {
new_region.id_region = Region.insert_region (conn, new_region);
return update_place_instance_city (new_region);
}
else {
return false;
}
} }
catch (Error e) { catch (Error e) {
#if DEBUG #if DEBUG
@ -253,9 +407,6 @@ namespace Sernatur {
warning (e.message); warning (e.message);
#endif #endif
} }
finally {
update_place_instance_city (new_region);
}
} }
else { else {
Region new_region; Region new_region;
@ -268,21 +419,27 @@ namespace Sernatur {
else { else {
new_region = new Region (); new_region = new Region ();
} }
update_place_instance_city (new_region); return update_place_instance_city (new_region);
} }
return true;
} }
/** /**
* This method updates the city part of the lugar instance * This method updates the city part of the lugar instance
* @param new_region The region to insert into the city object * @param new_region The region to insert into the city object
*/ */
private void update_place_instance_city (Region new_region) { private bool update_place_instance_city (Region new_region) {
Ciudad ciudad; Ciudad ciudad;
if (city.get_active () == -1) { if (city.get_active () == -1) {
ciudad = new Ciudad (0, city.get_active_text ()); ciudad = new Ciudad (0, city.get_active_text ());
ciudad.region = new_region; ciudad.region = new_region;
try { try {
ciudad.id_ciudad = Ciudad.insert_city (conn, ciudad); if (validate_city_data (ciudad)) {
ciudad.id_ciudad = Ciudad.insert_city (conn, ciudad);
}
else {
return false;
}
} }
catch (Error e) { catch (Error e) {
#if DEBUG #if DEBUG
@ -305,6 +462,7 @@ namespace Sernatur {
lugar.ciudad = ciudad; lugar.ciudad = ciudad;
} }
} }
return true;
} }
/** /**

View File

@ -437,7 +437,10 @@ namespace Sernatur {
try { try {
if (validate_region_data (new_region)) { if (validate_region_data (new_region)) {
new_region.id_region = Region.insert_region (conn, new_region); new_region.id_region = Region.insert_region (conn, new_region);
update_tour_instance_city (new_region); return update_tour_instance_city (new_region);
}
else {
return false;
} }
} }
catch (Error e) { catch (Error e) {
@ -459,7 +462,7 @@ namespace Sernatur {
else { else {
new_region = new Region (); new_region = new Region ();
} }
update_tour_instance_city (new_region); return update_tour_instance_city (new_region);
} }
return true; return true;
} }
@ -477,6 +480,9 @@ namespace Sernatur {
if (validate_city_data (ciudad)) { if (validate_city_data (ciudad)) {
ciudad.id_ciudad = Ciudad.insert_city (conn, ciudad); ciudad.id_ciudad = Ciudad.insert_city (conn, ciudad);
} }
else {
return false;
}
} }
catch (Error e) { catch (Error e) {
#if DEBUG #if DEBUG