sernatur/lib/db/tour.vala

73 lines
2.1 KiB
Vala

namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Tour : GLib.Object {
public uint id_tour { get; set; default = 0; }
public string nombre_tour { get; set; default = ""; }
public uint costo_indiv { get; set; default = 0; }
public uint costo_grupal { get; set; default = 0; }
public uint minima_personas { get; set; default = 0; }
public Ciudad ciudad { get; set; default = null; }
public Tour (uint id_tour = 0, string nombre_tour = "", uint costo_indiv = 0, uint costo_grupal = 0, uint minima_personas = 0, Ciudad? ciudad = null) {
this.id_tour = id_tour;
this.nombre_tour = nombre_tour;
this.costo_indiv = costo_indiv;
this.costo_grupal = costo_grupal;
this.minima_personas = minima_personas;
this.ciudad = ciudad;
}
public static Tour[]? get_all_tours(Database conn) {
var res = conn.exec ("
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)
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Tour[] tours = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var tour = new Tour (wra.get_int_n (i, "id_tour"),
wra.get_string_n (i, "nombre_tour"),
wra.get_int_n (i, "costo_indiv"),
wra.get_int_n (i, "costo_grupal"),
wra.get_int_n (i, "minima_personas"),
new Ciudad (wra.get_int_n (i, "id_ciudad"),
wra.get_string_n (i, "nombre_ciudad"),
new Region (wra.get_int_n (i, "id_region"),
wra.get_string_n (i, "nombre_region")
)
)
);
tours += tour;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return tours;
}
}
}
}