add delete option and error handling to tours

This commit is contained in:
Chris Cromer 2019-01-17 09:59:06 -03:00
parent c659ab03e0
commit 6cb6283af0
Signed by: cromer
GPG Key ID: 39CC813FF3C8708A
4 changed files with 88 additions and 10 deletions

View File

@ -39,7 +39,11 @@ namespace LibSernatur {
/** /**
* Invalid value * Invalid value
*/ */
INVALID_VALUE INVALID_VALUE,
/**
* Could not be modified or deleted because of a constraint
*/
FOREIGN_KEY_CONSTAINT
} }
/** /**

View File

@ -129,6 +129,29 @@ namespace LibSernatur {
#endif #endif
} }
} }
/**
* Delete a tour in the database
* @param conn The database connection
* @param tour The tour to update
* @throws DBError Thrown if the data in the object is invalid
*/
public static void delete_tour (Database conn, Tour tour) throws DBError {
if (tour.id_tour == 0) {
throw new DBError.INVALID_VALUE (dgettext (null, "The id of the tour is invalid!"));
}
var res = conn.exec (Query.get_query (Query.Type.DELETE_TOUR, tour));
if (res.get_status () != ExecStatus.COMMAND_OK) {
if (res.get_error_field (FieldCode.SQLSTATE) == "23503") {
throw new DBError.FOREIGN_KEY_CONSTAINT (res.get_error_field (FieldCode.MESSAGE_PRIMARY));
}
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
#endif
}
}
} }
} }
} }

View File

@ -21,11 +21,13 @@ namespace LibSernatur {
public enum Type { public enum Type {
SELECT_ALL_TOURS, SELECT_ALL_TOURS,
INSERT_TOUR, INSERT_TOUR,
UPDATE_TOUR UPDATE_TOUR,
DELETE_TOUR
} }
public static string get_query (Type type, T? t) { public static string get_query (Type type, T? t) {
if (type == Type.SELECT_ALL_TOURS) { switch (type) {
case SELECT_ALL_TOURS:
return " return "
SELECT T.id_tour, T.nombre_tour, T.costo_indiv, T.costo_grupal, T.minima_personas, SELECT T.id_tour, T.nombre_tour, T.costo_indiv, T.costo_grupal, T.minima_personas,
C.id_ciudad, C.nombre_ciudad, C.id_ciudad, C.nombre_ciudad,
@ -33,8 +35,7 @@ R.id_region, R.nombre_region
FROM tour T FROM tour T
JOIN ciudad C ON (T.id_ciudad = C.id_ciudad) JOIN ciudad C ON (T.id_ciudad = C.id_ciudad)
JOIN region R ON (C.id_region = R.id_region)"; JOIN region R ON (C.id_region = R.id_region)";
} case UPDATE_TOUR:
else if (type == Type.UPDATE_TOUR) {
Tour tour = (Tour) t; Tour tour = (Tour) t;
return " return "
UPDATE tour SET UPDATE tour SET
@ -43,9 +44,15 @@ UPDATE tour SET
costo_grupal = " + tour.costo_grupal.to_string () + ", costo_grupal = " + tour.costo_grupal.to_string () + ",
minima_personas = " + tour.minima_personas.to_string () + " minima_personas = " + tour.minima_personas.to_string () + "
WHERE id_tour = " + tour.id_tour.to_string (); WHERE id_tour = " + tour.id_tour.to_string ();
} case DELETE_TOUR:
Tour tour = (Tour) t;
return "
DELETE FROM tour
WHERE id_tour = " + tour.id_tour.to_string ();
default:
return ""; return "";
} }
} }
} }
} }
}

View File

@ -174,7 +174,51 @@ namespace Sernatur {
} }
} }
else if (button == delete_tour) { else if (button == delete_tour) {
print ("Delete tour clicked\n"); var msg = new Gtk.MessageDialog (this,
Gtk.DialogFlags.MODAL,
Gtk.MessageType.ERROR,
Gtk.ButtonsType.YES_NO,
dgettext(null, "Are you sure you wish to delete this tour?"));
msg.response.connect ((response_id) => {
switch (response_id) {
case Gtk.ResponseType.YES:
try {
Gtk.TreeModel model;
Gtk.TreeIter iter;
Tour tour;
if (selection.get_selected (out model, out iter)) {
model.get (iter,
Column.TOUR, out tour);
Tour.delete_tour (conn.db, tour);
}
}
catch (DBError e) {
if (e.code == 1) {
var msg2 = new Gtk.MessageDialog (this,
Gtk.DialogFlags.MODAL,
Gtk.MessageType.ERROR,
Gtk.ButtonsType.CLOSE,
dgettext(null, "Error: Could not delete tour because there are still associated arrival and departure dates and times!"));
msg2.response.connect ((response_id) => {
msg2.destroy ();
});
msg2.set_title (dgettext (null, "Error"));
msg2.show ();
}
else {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
break;
}
msg.destroy ();
});
msg.set_title (dgettext (null, "Error"));
msg.show ();
} }
else if (button == close_tour) { else if (button == close_tour) {
this.close (); this.close ();