getTimesOfSession method
Método para obtener los tiempos de una sesión específica con opción de búsqueda por comentario o tiempo.
Este método consulta la base de datos y devuelve una lista de todos los tiempos registrados para una sesión específica. Se puede filtrar la búsqueda por comentarios o por tiempo. También se puede ordenar los resultados por fecha o tiempo según los parámetros proporcionados.
Parámetros:
idSession
: ID de la sesión de la que se desean obtener los tiempos.comment
(String?, opcional): Si se proporciona, la búsqueda filtrará los resultados que contengan este comentario en el campocomments
.time
(String?, opcional): Si se proporciona, la búsqueda filtrará los resultados cuyos valores entimeInSeconds
comiencen con este valor.dateAsc
(bool?, opcional): Si se proporciona, determina el orden de los resultados por fecha:true
para ascendente,false
para descendente.timeAsc
(bool?, opcional): Si se proporciona, determina el orden de los resultados por tiempo:true
para ascendente,false
para descendente.
Retorna:
Future<List<TimeTraining>>
: Lista de objetos TimeTraining con los tiempos registrados que cumplen los criterios de búsqueda. Si no se encuentran resultados, devuelve una lista vacía.
Implementation
Future<List<TimeTraining>> getTimesOfSession(int? idSession,
[String? comment, String? time, bool? dateAsc, bool? timeAsc]) async {
final db = await DatabaseHelper.database;
try {
// CONSULTA PARA OBTENER TODOS LOS TIEMPOS DE UNA SESION
final List<Map<String, dynamic>> result;
String orderBy = ""; // VARIABLE PARA ALMACENAR ORDEN
// DETERMINAR ORDENACION
if (dateAsc != null) {
orderBy = "registrationDate ${dateAsc ? 'ASC' : 'DESC'}";
} else if (timeAsc != null) {
orderBy = timeAsc ? "timeInSeconds ASC" : "timeInSeconds DESC";
}
if (comment != null) {
// SI EL USUARIO HA INTRODUCIDO UN COMENTARIO, SE REALIZA LA BUSQUEDA POR COMENTARIOS
// SE USA LIKE CON '%' PARA ENCONTRAR CUALQUIER COINCIDENCIA QUE CONTENGA LA PALABRA
result = await db.query('timeTraining',
where: 'idSession = ? AND comments LIKE ?',
whereArgs: [idSession, '%$comment%'],
orderBy: orderBy.isNotEmpty ? orderBy : null);
} else if (time != null) {
// SI EL USUARIO HA INTRODUCIDO UN TIEMPO, SE BUSCA POR EL TIEMPO
// SE USA LIKE CON '%' AL FINAL PARA ENCONTRAR LOS REGISTROS CUYO TIEMPO
// COMIENCE CON EL VALOR INTRODUCIDO
result = await db.query(
'timeTraining',
where: "idSession = ? AND timeInSeconds LIKE ?",
whereArgs: [idSession, '$time%'],
orderBy: orderBy.isNotEmpty ? orderBy : null,
);
} else {
// SI NO SE HA INTRODUCIDO NI TIEMPO NI COMENTARIO, SE FILTRA POR LA SESION
result = await db.query('timeTraining',
where: 'idSession = ?',
whereArgs: [idSession],
orderBy: orderBy.isNotEmpty ? orderBy : null);
}
if (result.isNotEmpty) {
// MAPEAR LOS RESULTADOS A UNA LISTA DE OBJETOS TimeTraining
return result.map((map) {
return TimeTraining(
idTimeTraining: map['idTimeTraining'] as int,
idSession: map['idSession'] as int,
scramble: map['scramble'] as String,
timeInSeconds: map['timeInSeconds'] as double,
comments: map['comments'] as String?,
penalty: map['penalty'] as String,
registrationDate: map['registrationDate'] as String,
);
}).toList();
} else {
//DatabaseHelper.logger
// .w("No se encontraron tiempos para la sesion con ID: $idSession.");
return []; // DEVOLVER UNA LISTA VACIA SI NO HAY RESULTADOS
} // VERIFICAR SI HAY RESULTADOS
} catch (e) {
// SI HAY UN ERROR, MMUESTRA UN MENSAJE
DatabaseHelper.logger.e("Error al obtener los tiempos de la sesion: $e");
return []; // DEVOLVER UNA LISTA VACIA EN CASO DE ERROR
}
}