add delete option and error handling to tours

This commit is contained in:
2019-01-17 09:59:06 -03:00
parent c659ab03e0
commit 6cb6283af0
4 changed files with 88 additions and 10 deletions

View File

@@ -39,7 +39,11 @@ namespace LibSernatur {
/**
* 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
}
}
/**
* 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,30 +21,37 @@ namespace LibSernatur {
public enum Type {
SELECT_ALL_TOURS,
INSERT_TOUR,
UPDATE_TOUR
UPDATE_TOUR,
DELETE_TOUR
}
public static string get_query (Type type, T? t) {
if (type == Type.SELECT_ALL_TOURS) {
return "
switch (type) {
case SELECT_ALL_TOURS:
return "
SELECT T.id_tour, T.nombre_tour, T.costo_indiv, T.costo_grupal, T.minima_personas,
C.id_ciudad, C.nombre_ciudad,
R.id_region, R.nombre_region
FROM tour T
JOIN ciudad C ON (T.id_ciudad = C.id_ciudad)
JOIN region R ON (C.id_region = R.id_region)";
}
else if (type == Type.UPDATE_TOUR) {
Tour tour = (Tour) t;
return "
case UPDATE_TOUR:
Tour tour = (Tour) t;
return "
UPDATE tour SET
nombre_tour = '" + tour.nombre_tour + "',
costo_indiv = " + tour.costo_indiv.to_string () + ",
costo_grupal = " + tour.costo_grupal.to_string () + ",
minima_personas = " + tour.minima_personas.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 "";
}
}
}