forked from maiora/backend-api
Compare commits
2 Commits
80aa4afd57
...
ab56a26f57
Author | SHA1 | Date | |
---|---|---|---|
ab56a26f57 | |||
230c529d40 |
@ -1,5 +1,6 @@
|
||||
package eu.maiora.model
|
||||
|
||||
interface MovimentiRepository {
|
||||
suspend fun movimentiByIdTessera(idTessera : Long): List<Movimenti>?
|
||||
suspend fun movimentiByIdTessera(idTessera : Long, limit : Long, offset : Long, order : String): List<Movimenti>?
|
||||
suspend fun countMovimentiByIdTessera(idTessera : Long): Int
|
||||
}
|
@ -4,12 +4,32 @@ import eu.maiora.db.*
|
||||
import org.jetbrains.exposed.sql.SortOrder
|
||||
|
||||
class MovimentiRepositoryImpl : MovimentiRepository {
|
||||
override suspend fun movimentiByIdTessera(idTessera : Long): List<Movimenti> = suspendTransaction {
|
||||
override suspend fun movimentiByIdTessera(idTessera : Long,
|
||||
limit : Long,
|
||||
offset : Long,
|
||||
order : String): List<Movimenti> = 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()
|
||||
|
||||
}
|
||||
}
|
@ -2,5 +2,6 @@ package eu.maiora.model
|
||||
|
||||
interface ViewPrenotazioniPastiRepository {
|
||||
suspend fun prenotazioniPastiById(id : Long): ViewPrenotazioniPasti
|
||||
suspend fun prenotazioniPastiByIdTessera(idTessera : Long): List<ViewPrenotazioniPasti>
|
||||
suspend fun prenotazioniPastiByIdTessera(idTessera : Long, limit : Long, offset : Long, order : String): List<ViewPrenotazioniPasti>
|
||||
suspend fun countPrenotazioniPastiByIdTessera(idTessera : Long): Int
|
||||
}
|
@ -13,12 +13,27 @@ class ViewPrenotazioniPastiRepositoryImpl : ViewPrenotazioniPastiRepository {
|
||||
.let { viewPrenotazioniPastiDaoToModel(it) } // Converte il DAO in un oggetto ViewPrenotazioniPasti
|
||||
}
|
||||
|
||||
override suspend fun prenotazioniPastiByIdTessera(idTessera : Long): List<ViewPrenotazioniPasti> = suspendTransaction {
|
||||
override suspend fun prenotazioniPastiByIdTessera(idTessera : Long, limit : Long, offset : Long, order : String): List<ViewPrenotazioniPasti> = 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()
|
||||
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
@ -34,3 +47,6 @@ fun Route.movimenti(movimentiRepository: MovimentiRepositoryImpl){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class ListaMovimenti (val totalRecords : Int, val listaMovimenti: List<Movimenti>)
|
@ -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{
|
||||
@ -108,3 +125,6 @@ fun Route.prenotazioni(prenotazioniPastiRepository: PrenotazioniPastiRepository,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class ListaPrenotazioni (val totalRecords : Int, val listaPrenotazioni: List<ViewPrenotazioniPasti>)
|
Loading…
Reference in New Issue
Block a user