Add some other resources
This commit is contained in:
@@ -22,11 +22,14 @@ import service.DictionaryServiceImpl
|
||||
import service.PronunciationServiceImpl
|
||||
import service.LanguageServiceImpl
|
||||
import service.TranslationServiceImpl
|
||||
import service.TermServiceImpl
|
||||
|
||||
import web.dictType
|
||||
import web.dictionary
|
||||
import web.pronunciation
|
||||
import web.language
|
||||
import web.translation
|
||||
import web.term
|
||||
|
||||
fun Application.module() {
|
||||
install(DefaultHeaders)
|
||||
@@ -48,6 +51,7 @@ fun Application.module() {
|
||||
val pronunciationService = PronunciationServiceImpl()
|
||||
val languageService = LanguageServiceImpl()
|
||||
val translationService = TranslationServiceImpl()
|
||||
val termService = TermServiceImpl()
|
||||
|
||||
routing {
|
||||
index()
|
||||
@@ -56,6 +60,7 @@ fun Application.module() {
|
||||
pronunciation(pronunciationService)
|
||||
language(languageService)
|
||||
translation(translationService)
|
||||
term(termService)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,9 +3,14 @@ package dao
|
||||
import org.jetbrains.exposed.dao.*
|
||||
import org.jetbrains.exposed.dao.id.EntityID
|
||||
import tables.Suffixes
|
||||
import models.Suffix
|
||||
|
||||
class SuffixDao(id: EntityID<Int>) : IntEntity(id) {
|
||||
companion object : IntEntityClass<SuffixDao>(Suffixes)
|
||||
|
||||
var text by Suffixes.text
|
||||
fun toModel(): Suffix = Suffix(
|
||||
id = id.value,
|
||||
text = text
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package dao
|
||||
|
||||
import dao.SuffixDao.Companion.referrersOn
|
||||
import models.Language
|
||||
import org.jetbrains.exposed.dao.*
|
||||
import org.jetbrains.exposed.dao.id.EntityID
|
||||
import tables.*
|
||||
import models.Term
|
||||
import models.TermFull
|
||||
|
||||
class TermDao(id: EntityID<Int>) : IntEntity(id) {
|
||||
companion object : IntEntityClass<TermDao>(Terms)
|
||||
|
||||
var string1 by Terms.string1
|
||||
var string2 by Terms.string2
|
||||
var suffix1 by SuffixDao optionalReferencedOn Terms.suffix1
|
||||
var suffix2 by SuffixDao optionalReferencedOn Terms.suffix2
|
||||
var type by DictTypeDao optionalReferencedOn Terms.type
|
||||
var member by Terms.member
|
||||
var order2 by Terms.order2
|
||||
var flags by Terms.flags
|
||||
|
||||
fun toModel(): Term = Term(
|
||||
id = id.value,
|
||||
string1 = string1,
|
||||
string2 = string2,
|
||||
typeId = type?.id?.value,
|
||||
suffix1Id = suffix1?.id?.value,
|
||||
suffix2Id = suffix2?.id?.value,
|
||||
member = member,
|
||||
order2 = order2,
|
||||
flags = flags
|
||||
)
|
||||
fun toFullModel(): TermFull = TermFull(
|
||||
id = id.value,
|
||||
string1 = string1,
|
||||
string2 = string2,
|
||||
type = type?.toModel(),
|
||||
suffix1 = suffix1?.toModel(),
|
||||
suffix2 = suffix2?.toModel(),
|
||||
member = member,
|
||||
order2 = order2,
|
||||
flags = flags,
|
||||
pronunciation = null
|
||||
)
|
||||
}
|
||||
@@ -8,13 +8,26 @@ import org.jetbrains.exposed.sql.Table
|
||||
@Serializable
|
||||
data class Term(
|
||||
val id: Int,
|
||||
val dictionaryId: Int,
|
||||
val string1: String,
|
||||
val string2: String,
|
||||
val typeId: Int,
|
||||
val suffix1Id: Int,
|
||||
val suffix2Id: Int,
|
||||
val typeId: Int?,
|
||||
val suffix1Id: Int?,
|
||||
val suffix2Id: Int?,
|
||||
val member: String?,
|
||||
val order2: Int,
|
||||
val flags: JsonObject?
|
||||
val order2: Int?,
|
||||
val flags: IntArray?
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class TermFull(
|
||||
val id: Int,
|
||||
val string1: String,
|
||||
val string2: String,
|
||||
val type: DictType?,
|
||||
val suffix1: Suffix?,
|
||||
val suffix2: Suffix?,
|
||||
val member: String?,
|
||||
val order2: Int?,
|
||||
val flags: IntArray?,
|
||||
val pronunciation: Pronunciation?
|
||||
)
|
||||
11
src/main/kotlin/service/TermService.kt
Normal file
11
src/main/kotlin/service/TermService.kt
Normal file
@@ -0,0 +1,11 @@
|
||||
package service
|
||||
|
||||
import dao.TermDao
|
||||
import models.Term
|
||||
import models.TermFull
|
||||
|
||||
interface TermService {
|
||||
fun getTerm(id: Int): Term?
|
||||
fun getFullTerm(id: Int): TermFull?
|
||||
fun getAllTerm(): List<Term>
|
||||
}
|
||||
22
src/main/kotlin/service/TermServiceImpl.kt
Normal file
22
src/main/kotlin/service/TermServiceImpl.kt
Normal file
@@ -0,0 +1,22 @@
|
||||
package service
|
||||
|
||||
import dao.TermDao
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import service.DatabaseFactory.dbExecId
|
||||
import models.Term
|
||||
import models.TermFull
|
||||
|
||||
class TermServiceImpl : TermService {
|
||||
override fun getTerm(id: Int): Term? = dbExecId(1) {
|
||||
TermDao.findById(id)?.toModel()
|
||||
}
|
||||
|
||||
override fun getAllTerm(): List<Term> = dbExecId(1) {
|
||||
TermDao.all().map { it.toModel() }
|
||||
}
|
||||
|
||||
override fun getFullTerm(id: Int): TermFull? = dbExecId(1) {
|
||||
TermDao.findById(id)?.toFullModel()
|
||||
}
|
||||
|
||||
}
|
||||
37
src/main/kotlin/web/TermResource.kt
Normal file
37
src/main/kotlin/web/TermResource.kt
Normal file
@@ -0,0 +1,37 @@
|
||||
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 service.TermServiceImpl
|
||||
import service.TermService
|
||||
|
||||
fun Route.term(TermService: TermServiceImpl) {
|
||||
|
||||
route("/term") {
|
||||
|
||||
get {
|
||||
call.respond(TermService.getAllTerm())
|
||||
}
|
||||
|
||||
get("/{id}") {
|
||||
val id = call.parameters["id"]?.toInt() ?: throw IllegalStateException("Must provide id")
|
||||
val term = TermService.getTerm(id)
|
||||
if (term == null) call.respond(HttpStatusCode.NotFound)
|
||||
else call.respond(term)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user