Add some resources

This commit is contained in:
2025-01-11 18:31:50 +01:00
parent 06803b48a1
commit eaeae11e97
10 changed files with 132 additions and 4 deletions

View File

@@ -20,11 +20,13 @@ import service.DictTypeService
import service.DictTypeServiceImpl
import service.DictionaryServiceImpl
import service.PronunciationServiceImpl
import service.LanguageServiceImpl
import service.TranslationServiceImpl
import web.dictType
import web.dictionary
import web.pronunciation
import web.language
import web.translation
fun Application.module() {
install(DefaultHeaders)
@@ -44,12 +46,16 @@ fun Application.module() {
val dictTypeService = DictTypeServiceImpl()
val dictionaryService = DictionaryServiceImpl()
val pronunciationService = PronunciationServiceImpl()
val languageService = LanguageServiceImpl()
val translationService = TranslationServiceImpl()
routing {
index()
dictType(dictTypeService)
dictionary(dictionaryService)
pronunciation(pronunciationService)
language(languageService)
translation(translationService)
}
}

View File

@@ -1,11 +1,10 @@
package dao
import models.Translation
import org.jetbrains.exposed.dao.*
import org.jetbrains.exposed.dao.id.EntityID
import tables.Pronunciations
import models.Pronunciation
import tables.PronunciationTypes
class PronunciationDao(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<PronunciationDao>(Pronunciations)

View File

View File

@@ -11,4 +11,17 @@ class TranslationDao(id: EntityID<Int>) : IntEntity(id) {
var langName1 by Translations.langName1
var langName2 by Translations.langName2
var direction by Translations.direction
var dictionary by DictionaryDao referencedOn Translations.dictionaryId
var lang1 by LanguageDao referencedOn Translations.lang1Id
var lang2 by LanguageDao referencedOn Translations.lang2Id
fun toModel() : Translation = Translation(
id = id.value,
dictionaryId = dictionary.id.value,
lang1Id = lang1.id.value,
lang2Id = lang2.id.value,
lang1Name = langName1,
lang2Name = langName2,
direction = direction
)
}

View File

@@ -0,0 +1,9 @@
package service
import dao.LanguageDao
import models.Language
interface LanguageService {
fun getLanguage(id: Int): Language?
fun getAllLanguages(): List<Language>
}

View File

@@ -0,0 +1,17 @@
package service
import dao.LanguageDao
import org.jetbrains.exposed.sql.transactions.transaction
import service.DatabaseFactory.dbExecId
import models.Language
class LanguageServiceImpl : LanguageService {
override fun getLanguage(id: Int): Language? = dbExecId(1) {
LanguageDao.findById(id)?.toModel()
}
override fun getAllLanguages(): List<Language> = dbExecId(1) {
LanguageDao.all().map { it.toModel() }
}
}

View File

@@ -0,0 +1,9 @@
package service
import dao.TranslationDao
import models.Translation
interface TranslationService {
fun getTranslation(id: Int): Translation?
fun getAllTranslations(): List<Translation>
}

View File

@@ -0,0 +1,17 @@
package service
import dao.TranslationDao
import org.jetbrains.exposed.sql.transactions.transaction
import service.DatabaseFactory.dbExecId
import models.Translation
class TranslationServiceImpl : TranslationService {
override fun getTranslation(id: Int): Translation? = dbExecId(1) {
TranslationDao.findById(id)?.toModel()
}
override fun getAllTranslations(): List<Translation> = dbExecId(1) {
TranslationDao.all().map { it.toModel() }
}
}

View File

@@ -0,0 +1,29 @@
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.LanguageServiceImpl
import service.LanguageService
fun Route.language(LanguageService: LanguageServiceImpl) {
route("/language") {
get {
call.respond(LanguageService.getAllLanguages())
}
get("/{id}") {
val id = call.parameters["id"]?.toInt() ?: throw IllegalStateException("Must provide id")
val language = LanguageService.getLanguage(id)
if (language == null) call.respond(HttpStatusCode.NotFound)
else call.respond(language)
}
}
}

View File

@@ -0,0 +1,29 @@
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.TranslationServiceImpl
import service.TranslationService
fun Route.translation(TranslationService: TranslationServiceImpl) {
route("/translation") {
get {
call.respond(TranslationService.getAllTranslations())
}
get("/{id}") {
val id = call.parameters["id"]?.toInt() ?: throw IllegalStateException("Must provide id")
val Translation = TranslationService.getTranslation(id)
if (Translation == null) call.respond(HttpStatusCode.NotFound)
else call.respond(Translation)
}
}
}