forked from maiora/backend-api
Compare commits
No commits in common. "efe1e332294133371373adac5102b0961698e8d1" and "fe3011a9fcf8e2ed99e5dcf3b85f0a0fecadd968" have entirely different histories.
efe1e33229
...
fe3011a9fc
@ -3,12 +3,9 @@ package eu.maiora.db
|
|||||||
import com.fasterxml.jackson.databind.deser.impl.CreatorCandidate.Param
|
import com.fasterxml.jackson.databind.deser.impl.CreatorCandidate.Param
|
||||||
import eu.maiora.model.Accounts
|
import eu.maiora.model.Accounts
|
||||||
import eu.maiora.model.Parametri
|
import eu.maiora.model.Parametri
|
||||||
import eu.maiora.model.ViewEcommerceArticoli
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import org.jetbrains.exposed.dao.IntEntity
|
import org.jetbrains.exposed.dao.IntEntity
|
||||||
import org.jetbrains.exposed.dao.IntEntityClass
|
import org.jetbrains.exposed.dao.IntEntityClass
|
||||||
import org.jetbrains.exposed.dao.LongEntity
|
|
||||||
import org.jetbrains.exposed.dao.LongEntityClass
|
|
||||||
import org.jetbrains.exposed.dao.id.EntityID
|
import org.jetbrains.exposed.dao.id.EntityID
|
||||||
import org.jetbrains.exposed.dao.id.IdTable
|
import org.jetbrains.exposed.dao.id.IdTable
|
||||||
import org.jetbrains.exposed.sql.Transaction
|
import org.jetbrains.exposed.sql.Transaction
|
||||||
@ -30,12 +27,6 @@ object ParametriTable : IdTable<Int>("parametri"){
|
|||||||
override val primaryKey = PrimaryKey(id)
|
override val primaryKey = PrimaryKey(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
object ViewEcommerceArticoliTable : IdTable<Long>("view_ecommerce_articoli"){
|
|
||||||
override val id = long("id").entityId()
|
|
||||||
val codice = varchar("codice", 20)
|
|
||||||
val descrizione = varchar("descrizione", 255)
|
|
||||||
}
|
|
||||||
|
|
||||||
class AccountsDAO(id: EntityID<Int>) :IntEntity(id) {
|
class AccountsDAO(id: EntityID<Int>) :IntEntity(id) {
|
||||||
companion object : IntEntityClass<AccountsDAO>(AccountsTable)
|
companion object : IntEntityClass<AccountsDAO>(AccountsTable)
|
||||||
|
|
||||||
@ -50,13 +41,6 @@ class ParametriDAO(id: EntityID<Int>) :IntEntity(id) {
|
|||||||
var valore by ParametriTable.valore
|
var valore by ParametriTable.valore
|
||||||
}
|
}
|
||||||
|
|
||||||
class ViewEcommerceArticoliDAO(id:EntityID<Long>) : LongEntity(id){
|
|
||||||
companion object : LongEntityClass<ViewEcommerceArticoliDAO>(ViewEcommerceArticoliTable)
|
|
||||||
|
|
||||||
var codice by ViewEcommerceArticoliTable.codice
|
|
||||||
var descrizione by ViewEcommerceArticoliTable.descrizione
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fun accountsDaoToModel(dao: AccountsDAO) = Accounts(
|
fun accountsDaoToModel(dao: AccountsDAO) = Accounts(
|
||||||
dao.id.value,
|
dao.id.value,
|
||||||
@ -70,12 +54,6 @@ fun parametriDaoToModel(dao: ParametriDAO) = Parametri(
|
|||||||
dao.valore
|
dao.valore
|
||||||
)
|
)
|
||||||
|
|
||||||
fun viewEcommerceArticoliDaoToModel(dao: ViewEcommerceArticoliDAO) = ViewEcommerceArticoli(
|
|
||||||
dao.id.value,
|
|
||||||
dao.codice,
|
|
||||||
dao.descrizione
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
suspend fun <T> suspendTransaction(block: Transaction.() -> T): T =
|
suspend fun <T> suspendTransaction(block: Transaction.() -> T): T =
|
||||||
newSuspendedTransaction(Dispatchers.IO, statement = block)
|
newSuspendedTransaction(Dispatchers.IO, statement = block)
|
||||||
@ -1,10 +0,0 @@
|
|||||||
package eu.maiora.model
|
|
||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class ViewEcommerceArticoli(
|
|
||||||
val id : Long,
|
|
||||||
val codice : String,
|
|
||||||
val descrizione : String
|
|
||||||
)
|
|
||||||
@ -1,6 +0,0 @@
|
|||||||
package eu.maiora.model
|
|
||||||
|
|
||||||
interface ViewEcommerceArticoliRepository {
|
|
||||||
suspend fun getListaArticoli(): List<ViewEcommerceArticoli>
|
|
||||||
suspend fun getArticoloById(idArticolo : Long): ViewEcommerceArticoli?
|
|
||||||
}
|
|
||||||
@ -1,22 +0,0 @@
|
|||||||
package eu.maiora.model
|
|
||||||
|
|
||||||
import eu.maiora.db.*
|
|
||||||
import eu.maiora.loadConfig
|
|
||||||
import org.jetbrains.exposed.sql.LongColumnType
|
|
||||||
|
|
||||||
class ViewEcommerceArticoliRepositoryImpl : ViewEcommerceArticoliRepository {
|
|
||||||
override suspend fun getListaArticoli(): List<ViewEcommerceArticoli> = suspendTransaction {
|
|
||||||
// Cerca la lista di articoli
|
|
||||||
ViewEcommerceArticoliDAO.all()
|
|
||||||
.toList()
|
|
||||||
.map { viewEcommerceArticoliDaoToModel(it) } // Converte il DAO in un oggetto ViewEcommerceArticoli
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getArticoloById(idArticolo : Long): ViewEcommerceArticoli? = suspendTransaction {
|
|
||||||
// Cerca un articolo dato il suo id
|
|
||||||
ViewEcommerceArticoliDAO.find{ ViewEcommerceArticoliTable.id eq idArticolo }
|
|
||||||
.singleOrNull() // Restituisce un singolo risultato o null se non trovato
|
|
||||||
?.let { viewEcommerceArticoliDaoToModel(it) } // Converte il DAO in un oggetto ViewEcommerceArticoli
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,9 +1,11 @@
|
|||||||
package eu.maiora.plugins
|
package eu.maiora.plugins
|
||||||
|
|
||||||
import eu.maiora.model.*
|
import eu.maiora.model.AccountsRepositoryImpl
|
||||||
|
import eu.maiora.model.ViewEcommercePrezSconDispRepositoryImpl
|
||||||
|
import eu.maiora.model.ViewEcommerceValoreUsatoRepository
|
||||||
|
import eu.maiora.model.ViewEcommerceValoreUsatoRepositoryImpl
|
||||||
import eu.maiora.routes.auth
|
import eu.maiora.routes.auth
|
||||||
import eu.maiora.routes.prezziScontiMagazzino
|
import eu.maiora.routes.prezziScontiMagazzino
|
||||||
import eu.maiora.routes.viewEcommerceArticoli
|
|
||||||
import io.ktor.server.application.*
|
import io.ktor.server.application.*
|
||||||
import io.ktor.server.response.*
|
import io.ktor.server.response.*
|
||||||
import io.ktor.server.routing.*
|
import io.ktor.server.routing.*
|
||||||
@ -16,6 +18,5 @@ fun Application.configureRouting() {
|
|||||||
|
|
||||||
auth(AccountsRepositoryImpl())
|
auth(AccountsRepositoryImpl())
|
||||||
prezziScontiMagazzino(ViewEcommerceValoreUsatoRepositoryImpl(), ViewEcommercePrezSconDispRepositoryImpl())
|
prezziScontiMagazzino(ViewEcommerceValoreUsatoRepositoryImpl(), ViewEcommercePrezSconDispRepositoryImpl())
|
||||||
viewEcommerceArticoli(ViewEcommerceArticoliRepositoryImpl())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,42 +0,0 @@
|
|||||||
package eu.maiora.routes
|
|
||||||
|
|
||||||
import eu.maiora.model.ViewEcommerceArticoliRepository
|
|
||||||
import eu.maiora.model.ViewEcommercePrezSconDisp
|
|
||||||
import eu.maiora.model.ViewEcommercePrezSconDispRepository
|
|
||||||
import eu.maiora.model.ViewEcommerceValoreUsatoRepository
|
|
||||||
import io.ktor.http.*
|
|
||||||
import io.ktor.server.application.*
|
|
||||||
import io.ktor.server.auth.*
|
|
||||||
import io.ktor.server.response.*
|
|
||||||
import io.ktor.server.routing.*
|
|
||||||
|
|
||||||
fun Route.viewEcommerceArticoli(viewEcommerceArticoliRepository: ViewEcommerceArticoliRepository){
|
|
||||||
route("/api/articoli"){
|
|
||||||
authenticate("auth-jwt") {
|
|
||||||
get("{id?}"){
|
|
||||||
// Ottieni l'id articolo dal percorso
|
|
||||||
val idArticolo = call.parameters["id"]
|
|
||||||
|
|
||||||
if(idArticolo != null){
|
|
||||||
val id = idArticolo.toLongOrNull()
|
|
||||||
if(id == null) {
|
|
||||||
call.respondText("ID articolo non valido", status = HttpStatusCode.BadRequest)
|
|
||||||
return@get
|
|
||||||
}
|
|
||||||
//restituisco il singolo articolo se disponibile
|
|
||||||
val articolo = viewEcommerceArticoliRepository.getArticoloById(idArticolo.toLong())
|
|
||||||
if(articolo == null){
|
|
||||||
call.respondText("Articolo non esistente", status = HttpStatusCode.NotFound)
|
|
||||||
return@get
|
|
||||||
}
|
|
||||||
call.respond(articolo)
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
//restituisco la lista articoli completa
|
|
||||||
val articoli = viewEcommerceArticoliRepository.getListaArticoli()
|
|
||||||
call.respond(articoli)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user