From 230c529d40702aa793e56502774dd4cc354e4356 Mon Sep 17 00:00:00 2001 From: francescods Date: Tue, 29 Jul 2025 15:40:48 +0200 Subject: [PATCH] aggiunta parametri per paginazione query per recupero del numero di record totali in base alla tessera indicata nei parametri --- .../eu/maiora/model/MovimentiRepository.kt | 3 ++- .../maiora/model/MovimentiRepositoryImpl.kt | 24 +++++++++++++++-- .../model/ViewPrenotazioniPastiRepository.kt | 3 ++- .../ViewPrenotazioniPastiRepositoryImpl.kt | 19 ++++++++++++-- src/main/kotlin/eu/maiora/routes/Movimenti.kt | 22 +++++++++++++--- .../kotlin/eu/maiora/routes/Prenotazioni.kt | 26 ++++++++++++++++--- 6 files changed, 85 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/eu/maiora/model/MovimentiRepository.kt b/src/main/kotlin/eu/maiora/model/MovimentiRepository.kt index 706231c..a15e3ca 100644 --- a/src/main/kotlin/eu/maiora/model/MovimentiRepository.kt +++ b/src/main/kotlin/eu/maiora/model/MovimentiRepository.kt @@ -1,5 +1,6 @@ package eu.maiora.model interface MovimentiRepository { - suspend fun movimentiByIdTessera(idTessera : Long): List? + suspend fun movimentiByIdTessera(idTessera : Long, limit : Long, offset : Long, order : String): List? + suspend fun countMovimentiByIdTessera(idTessera : Long): Int } \ No newline at end of file diff --git a/src/main/kotlin/eu/maiora/model/MovimentiRepositoryImpl.kt b/src/main/kotlin/eu/maiora/model/MovimentiRepositoryImpl.kt index 4e02e58..f7c5850 100644 --- a/src/main/kotlin/eu/maiora/model/MovimentiRepositoryImpl.kt +++ b/src/main/kotlin/eu/maiora/model/MovimentiRepositoryImpl.kt @@ -4,12 +4,32 @@ import eu.maiora.db.* import org.jetbrains.exposed.sql.SortOrder class MovimentiRepositoryImpl : MovimentiRepository { - override suspend fun movimentiByIdTessera(idTessera : Long): List = suspendTransaction { + override suspend fun movimentiByIdTessera(idTessera : Long, + limit : Long, + offset : Long, + order : String): List = suspendTransaction { + + val sortOrder = when (order) { + "asc" -> SortOrder.ASC + "desc" -> SortOrder.DESC + else -> SortOrder.DESC // Default a DESC in caso di valore invalido + } + // Cerca la lista di movimenti MovimentiDao.find { MovimentiTable.idTessera eq idTessera } - .orderBy(MovimentiTable.dataMovimento to SortOrder.DESC) + .orderBy(MovimentiTable.dataMovimento to sortOrder) + .limit(limit.toInt()) + .offset(offset) .toList() // Restituisce la lista dei movimenti .map { movimentiDaoToModel(it) } // Converte il DAO in un oggetto Movimenti } + + override suspend fun countMovimentiByIdTessera(idTessera : Long): Int = suspendTransaction { + // Conta il numero totale di movimenti della tessera + MovimentiDao.find { MovimentiTable.idTessera eq idTessera } + .toList() // Restituisce la lista dei movimenti + .count() + + } } \ No newline at end of file diff --git a/src/main/kotlin/eu/maiora/model/ViewPrenotazioniPastiRepository.kt b/src/main/kotlin/eu/maiora/model/ViewPrenotazioniPastiRepository.kt index 96347be..c7136af 100644 --- a/src/main/kotlin/eu/maiora/model/ViewPrenotazioniPastiRepository.kt +++ b/src/main/kotlin/eu/maiora/model/ViewPrenotazioniPastiRepository.kt @@ -2,5 +2,6 @@ package eu.maiora.model interface ViewPrenotazioniPastiRepository { suspend fun prenotazioniPastiById(id : Long): ViewPrenotazioniPasti - suspend fun prenotazioniPastiByIdTessera(idTessera : Long): List + suspend fun prenotazioniPastiByIdTessera(idTessera : Long, limit : Long, offset : Long, order : String): List + suspend fun countPrenotazioniPastiByIdTessera(idTessera : Long): Int } \ No newline at end of file diff --git a/src/main/kotlin/eu/maiora/model/ViewPrenotazioniPastiRepositoryImpl.kt b/src/main/kotlin/eu/maiora/model/ViewPrenotazioniPastiRepositoryImpl.kt index 843f388..244fde8 100644 --- a/src/main/kotlin/eu/maiora/model/ViewPrenotazioniPastiRepositoryImpl.kt +++ b/src/main/kotlin/eu/maiora/model/ViewPrenotazioniPastiRepositoryImpl.kt @@ -13,12 +13,27 @@ class ViewPrenotazioniPastiRepositoryImpl : ViewPrenotazioniPastiRepository { .let { viewPrenotazioniPastiDaoToModel(it) } // Converte il DAO in un oggetto ViewPrenotazioniPasti } - override suspend fun prenotazioniPastiByIdTessera(idTessera : Long): List = suspendTransaction { + override suspend fun prenotazioniPastiByIdTessera(idTessera : Long, limit : Long, offset : Long, order : String): List = suspendTransaction { + val sortOrder = when (order) { + "asc" -> SortOrder.ASC + "desc" -> SortOrder.DESC + else -> SortOrder.DESC // Default a DESC in caso di valore invalido + } // Cerca la lista di prenotazioni ViewPrenotazioniPastiDao.find { ViewPrenotazioniPastiTable.idTessera eq idTessera } - .orderBy(ViewPrenotazioniPastiTable.giorno to SortOrder.DESC) + .orderBy(ViewPrenotazioniPastiTable.giorno to sortOrder) + .limit(limit.toInt()) + .offset(offset) .toList() // Restituisce la lista delle prenotazioni .map { viewPrenotazioniPastiDaoToModel(it) } // Converte il DAO in un oggetto Movimenti } + + override suspend fun countPrenotazioniPastiByIdTessera(idTessera : Long): Int = suspendTransaction { + // Conta il numero totale di prenotazioni della tessera + ViewPrenotazioniPastiDao.find { ViewPrenotazioniPastiTable.idTessera eq idTessera } + .toList() // Restituisce la lista delle prenotazioni + .count() + + } } \ No newline at end of file diff --git a/src/main/kotlin/eu/maiora/routes/Movimenti.kt b/src/main/kotlin/eu/maiora/routes/Movimenti.kt index 0a6d75d..3b059bb 100644 --- a/src/main/kotlin/eu/maiora/routes/Movimenti.kt +++ b/src/main/kotlin/eu/maiora/routes/Movimenti.kt @@ -1,11 +1,13 @@ package eu.maiora.routes +import eu.maiora.model.Movimenti import eu.maiora.model.MovimentiRepositoryImpl import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.auth.* import io.ktor.server.response.* import io.ktor.server.routing.* +import kotlinx.serialization.Serializable fun Route.movimenti(movimentiRepository: MovimentiRepositoryImpl){ @@ -14,18 +16,29 @@ fun Route.movimenti(movimentiRepository: MovimentiRepositoryImpl){ get("{idTessera}"){ // Ottieni l'id della tessera dal percorso val idTessera = call.parameters["idTessera"] + var limit = call.parameters["limit"] + var offset = call.parameters["offset"] + var order = call.parameters["order"] if (idTessera == null) { call.respondText("ID tessera non valido", status = HttpStatusCode.BadRequest) return@get } + if(limit?.toIntOrNull() == null) + limit = "20"; + if(offset?.toIntOrNull() == null) + offset = "0"; + if(order == null) + order = "desc" + // Conta il numero di movimenti della tessera + val totalRecords = movimentiRepository.countMovimentiByIdTessera(idTessera.toLong()) // Cerca la tessera per codice fiscale - val listaMovimenti = movimentiRepository.movimentiByIdTessera(idTessera.toLong()) + val listaMovimenti = movimentiRepository.movimentiByIdTessera(idTessera.toLong(), limit.toLong(), offset.toLong(), order) if (listaMovimenti != null) { - call.respond(listaMovimenti) + call.respond(ListaMovimenti(totalRecords, listaMovimenti)) } else { call.respondText("Movimenti non trovati", status = HttpStatusCode.NotFound) } @@ -33,4 +46,7 @@ fun Route.movimenti(movimentiRepository: MovimentiRepositoryImpl){ } } -} \ No newline at end of file +} + +@Serializable +data class ListaMovimenti (val totalRecords : Int, val listaMovimenti: List) \ No newline at end of file diff --git a/src/main/kotlin/eu/maiora/routes/Prenotazioni.kt b/src/main/kotlin/eu/maiora/routes/Prenotazioni.kt index 73d79f6..f29d0c2 100644 --- a/src/main/kotlin/eu/maiora/routes/Prenotazioni.kt +++ b/src/main/kotlin/eu/maiora/routes/Prenotazioni.kt @@ -7,6 +7,7 @@ import io.ktor.server.auth.* import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* +import kotlinx.serialization.Serializable fun Route.prenotazioni(prenotazioniPastiRepository: PrenotazioniPastiRepository, @@ -18,14 +19,30 @@ fun Route.prenotazioni(prenotazioniPastiRepository: PrenotazioniPastiRepository, get("{idTessera}"){ // Ottieni l'id della tessera dal percorso val idTessera = call.parameters["idTessera"] + var limit = call.parameters["limit"] + var offset = call.parameters["offset"] + var order = call.parameters["order"] if (idTessera == null) { call.respondText("ID tessera non valido", status = HttpStatusCode.BadRequest) return@get } + if(limit?.toIntOrNull() == null) + limit = "20"; + if(offset?.toIntOrNull() == null) + offset = "0"; + if(order == null) + order = "desc" + + // Conta il numero di prenotazioni per la tessera indicata + val totalRecords = viewPrenotazioniPastiRepositoryImpl.countPrenotazioniPastiByIdTessera(idTessera.toLong()) + // Cerca le prenotazioni per la tessera indicata - val listaPrenotazioniPasti = viewPrenotazioniPastiRepositoryImpl.prenotazioniPastiByIdTessera(idTessera.toLong()) + val listaPrenotazioniPasti = viewPrenotazioniPastiRepositoryImpl.prenotazioniPastiByIdTessera(idTessera.toLong(), + limit.toLong(), + offset.toLong(), + order) listaPrenotazioniPasti.forEach{ el -> el.listaProdotti = @@ -33,7 +50,7 @@ fun Route.prenotazioni(prenotazioniPastiRepository: PrenotazioniPastiRepository, } - call.respond(listaPrenotazioniPasti) + call.respond(ListaPrenotazioni(totalRecords, listaPrenotazioniPasti)) } post(){ try{ @@ -107,4 +124,7 @@ fun Route.prenotazioni(prenotazioniPastiRepository: PrenotazioniPastiRepository, } } -} \ No newline at end of file +} + +@Serializable +data class ListaPrenotazioni (val totalRecords : Int, val listaPrenotazioni: List) \ No newline at end of file -- 2.27.0