Rework TermDao

This commit is contained in:
2025-01-12 19:44:57 +01:00
parent ef5df17f58
commit c0f6a22582
6 changed files with 10 additions and 67 deletions

View File

@@ -19,6 +19,7 @@ class TermDao(id: EntityID<Int>) : IntEntity(id) {
var member by Terms.member
var order2 by Terms.order2
var flags by Terms.flags
var pronunciations by PronunciationDao via TermsPronunciations
fun toModel(): Term = Term(
id = id.value,
@@ -41,6 +42,6 @@ class TermDao(id: EntityID<Int>) : IntEntity(id) {
member = member,
order2 = order2,
flags = flags,
pronunciation = null
pronunciations = pronunciations.map { it.toModel() }
)
}

View File

@@ -29,5 +29,5 @@ data class TermFull(
val member: String?,
val order2: Int?,
val flags: IntArray?,
val pronunciation: Pronunciation?
val pronunciations: List<Pronunciation>?
)

View File

@@ -3,9 +3,11 @@ package service
import dao.TermDao
import models.Term
import models.TermFull
import models.Pronunciation
interface TermService {
fun getTerm(id: Int): Term?
fun getFullTerm(id: Int): TermFull?
fun getAllTerm(): List<Term>
fun getPronunciationsForTerm(id: Int): List<Pronunciation>?
}

View File

@@ -5,6 +5,7 @@ import org.jetbrains.exposed.sql.transactions.transaction
import service.DatabaseFactory.dbExecId
import models.Term
import models.TermFull
import models.Pronunciation
class TermServiceImpl : TermService {
override fun getTerm(id: Int): Term? = dbExecId(1) {
@@ -19,4 +20,7 @@ class TermServiceImpl : TermService {
TermDao.findById(id)?.toFullModel()
}
override fun getPronunciationsForTerm(id: Int): List<Pronunciation>? = dbExecId(1) {
null
}
}

View File

@@ -27,7 +27,7 @@ fun Route.term(TermService: TermServiceImpl) {
get("/{id}/full") {
val id = call.parameters["id"]?.toInt() ?: throw IllegalStateException("Must provide id")
val term = TermService.getFullTerm(id)
val pron = Pro
if (term == null) call.respond(HttpStatusCode.NotFound)
else call.respond(term)
}

View File

@@ -1,64 +0,0 @@
package web
import io.ktor.http.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.websocket.*
import io.ktor.websocket.*
import model.NewWidget
import service.WidgetService
fun Route.widget(widgetService: WidgetService) {
route("/widgets") {
get {
call.respond(widgetService.getAllWidgets())
}
get("/{id}") {
val id = call.parameters["id"]?.toInt() ?: throw IllegalStateException("Must provide id")
val widget = widgetService.getWidget(id)
if (widget == null) call.respond(HttpStatusCode.NotFound)
else call.respond(widget)
}
post {
val widget = call.receive<NewWidget>()
call.respond(HttpStatusCode.Created, widgetService.addWidget(widget))
}
put {
val widget = call.receive<NewWidget>()
val updated = widgetService.updateWidget(widget)
if (updated == null) call.respond(HttpStatusCode.NotFound)
else call.respond(HttpStatusCode.OK, updated)
}
delete("/{id}") {
val id = call.parameters["id"]?.toInt() ?: throw IllegalStateException("Must provide id")
val removed = widgetService.deleteWidget(id)
if (removed) call.respond(HttpStatusCode.OK)
else call.respond(HttpStatusCode.NotFound)
}
}
webSocket("/updates") {
try {
widgetService.addChangeListener(this.hashCode()) {
sendSerialized(it)
}
for (frame in incoming) {
if (frame.frameType == FrameType.CLOSE) {
break
} else if (frame is Frame.Text) {
call.application.environment.log.info("Received websocket message: {}", frame.readText())
}
}
} finally {
widgetService.removeChangeListener(this.hashCode())
}
}
}