forked from maiora/backend-api
0002343-endpoint-tessere #1
@ -3,6 +3,7 @@ package eu.maiora.db
|
||||
import com.fasterxml.jackson.databind.deser.impl.CreatorCandidate.Param
|
||||
import eu.maiora.model.Accounts
|
||||
import eu.maiora.model.Parametri
|
||||
import eu.maiora.model.Tessere
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import org.jetbrains.exposed.dao.IntEntity
|
||||
import org.jetbrains.exposed.dao.IntEntityClass
|
||||
@ -27,6 +28,17 @@ object ParametriTable : IdTable<Int>("parametri"){
|
||||
override val primaryKey = PrimaryKey(id)
|
||||
}
|
||||
|
||||
object TessereTable : IdTable<Int>("tessere"){
|
||||
override val id = integer("id").entityId()
|
||||
val idUtente = integer("id_utente")
|
||||
val codiceFiscale = varchar("codice_fiscale", 255)
|
||||
val numero = varchar("numero", 255)
|
||||
val saldo = double("saldo")
|
||||
val punti = integer("punti")
|
||||
|
||||
override val primaryKey = PrimaryKey(id)
|
||||
}
|
||||
|
||||
class AccountsDAO(id: EntityID<Int>) :IntEntity(id) {
|
||||
companion object : IntEntityClass<AccountsDAO>(AccountsTable)
|
||||
|
||||
@ -41,6 +53,16 @@ class ParametriDAO(id: EntityID<Int>) :IntEntity(id) {
|
||||
var valore by ParametriTable.valore
|
||||
}
|
||||
|
||||
class TessereDao(id: EntityID<Int>) :IntEntity(id) {
|
||||
companion object : IntEntityClass<TessereDao>(TessereTable)
|
||||
|
||||
var idUtente by TessereTable.idUtente
|
||||
var codiceFiscale by TessereTable.codiceFiscale
|
||||
var numero by TessereTable.numero
|
||||
var saldo by TessereTable.saldo
|
||||
var punti by TessereTable.punti
|
||||
}
|
||||
|
||||
|
||||
fun accountsDaoToModel(dao: AccountsDAO) = Accounts(
|
||||
dao.id.value,
|
||||
@ -54,6 +76,15 @@ fun parametriDaoToModel(dao: ParametriDAO) = Parametri(
|
||||
dao.valore
|
||||
)
|
||||
|
||||
fun tessereDaoToModel(dao: TessereDao) = Tessere(
|
||||
dao.id.value,
|
||||
dao.idUtente,
|
||||
dao.codiceFiscale,
|
||||
dao.numero,
|
||||
dao.saldo,
|
||||
dao.punti
|
||||
)
|
||||
|
||||
|
||||
suspend fun <T> suspendTransaction(block: Transaction.() -> T): T =
|
||||
newSuspendedTransaction(Dispatchers.IO, statement = block)
|
13
src/main/kotlin/eu/maiora/model/Tessere.kt
Normal file
13
src/main/kotlin/eu/maiora/model/Tessere.kt
Normal file
@ -0,0 +1,13 @@
|
||||
package eu.maiora.model
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class Tessere(
|
||||
val id: Int,
|
||||
val idUtente : Int,
|
||||
val codiceFiscale : String,
|
||||
val numero : String,
|
||||
val saldo : Double,
|
||||
val punti : Int
|
||||
)
|
5
src/main/kotlin/eu/maiora/model/TessereRepository.kt
Normal file
5
src/main/kotlin/eu/maiora/model/TessereRepository.kt
Normal file
@ -0,0 +1,5 @@
|
||||
package eu.maiora.model
|
||||
|
||||
interface TessereRepository {
|
||||
suspend fun tesseraByCodiceFiscale(cf : String): Tessere?
|
||||
}
|
13
src/main/kotlin/eu/maiora/model/TessereRepositoryImpl.kt
Normal file
13
src/main/kotlin/eu/maiora/model/TessereRepositoryImpl.kt
Normal file
@ -0,0 +1,13 @@
|
||||
package eu.maiora.model
|
||||
|
||||
import eu.maiora.db.*
|
||||
|
||||
class TessereRepositoryImpl : TessereRepository {
|
||||
override suspend fun tesseraByCodiceFiscale(cf: String): Tessere? = suspendTransaction {
|
||||
// Cerca tessere in base al codice fiscale
|
||||
TessereDao.find { TessereTable.codiceFiscale eq cf }
|
||||
.singleOrNull() // Restituisce un singolo risultato o null se non trovato
|
||||
?.let { tessereDaoToModel(it) } // Converte il DAO in un oggetto Accounts
|
||||
|
||||
}
|
||||
}
|
48
src/main/kotlin/eu/maiora/routes/Tessere.kt
Normal file
48
src/main/kotlin/eu/maiora/routes/Tessere.kt
Normal file
@ -0,0 +1,48 @@
|
||||
package eu.maiora.routes
|
||||
|
||||
import eu.maiora.model.ParametriRepositoryImpl
|
||||
import eu.maiora.model.TessereRepositoryImpl
|
||||
import io.jsonwebtoken.Jwts
|
||||
import io.jsonwebtoken.SignatureAlgorithm
|
||||
import io.jsonwebtoken.security.Keys
|
||||
import io.ktor.client.statement.*
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.util.*
|
||||
|
||||
|
||||
fun Route.tessere(tessereRepository: TessereRepositoryImpl, parametriRepository: ParametriRepositoryImpl){
|
||||
route("/tessere"){
|
||||
get("{cf}"){
|
||||
//verifica JWT
|
||||
val parametro = parametriRepository.parametroByChiave("jwt_secret")
|
||||
if(parametro != null){
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Ottieni il codice fiscale dal percorso
|
||||
val cf = call.parameters["cf"]
|
||||
|
||||
if (cf == null) {
|
||||
call.respondText("Codice fiscale non valido", status = HttpStatusCode.BadRequest)
|
||||
return@get
|
||||
}
|
||||
|
||||
// Cerca la tessera per codice fiscale
|
||||
val tessera = tessereRepository.tesseraByCodiceFiscale(cf)
|
||||
|
||||
if (tessera != null) {
|
||||
call.respond(tessera)
|
||||
} else {
|
||||
call.respondText("Tessera non trovata", status = HttpStatusCode.NotFound)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user