Skip to content

Commit

Permalink
Merge pull request #30 from crowdproj/wip-2
Browse files Browse the repository at this point in the history
refactoring of data layer
  • Loading branch information
sszuev authored Apr 7, 2024
2 parents 71acb1f + 701091c commit c4907e0
Show file tree
Hide file tree
Showing 125 changed files with 2,431 additions and 3,143 deletions.
12 changes: 6 additions & 6 deletions app-ktor/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.gitlab.sszuev.flashcards.api.cardApiV1
import com.gitlab.sszuev.flashcards.api.dictionaryApiV1
import com.gitlab.sszuev.flashcards.config.ContextConfig
import com.gitlab.sszuev.flashcards.config.KeycloakConfig
import com.gitlab.sszuev.flashcards.config.RepositoriesConfig
import com.gitlab.sszuev.flashcards.config.RunConfig
import com.gitlab.sszuev.flashcards.config.TutorConfig
import com.gitlab.sszuev.flashcards.logslib.ExtLogger
Expand Down Expand Up @@ -81,13 +80,14 @@ fun main(args: Array<String>) = io.ktor.server.jetty.EngineMain.main(args)
@KtorExperimentalLocationsAPI
@Suppress("unused")
fun Application.module(
repositoriesConfig: RepositoriesConfig = RepositoriesConfig(),
keycloakConfig: KeycloakConfig = KeycloakConfig(environment.config),
runConfig: RunConfig = RunConfig(environment.config),
tutorConfig: TutorConfig = TutorConfig(environment.config),
) {
logger.info(printGeneralSettings(runConfig, keycloakConfig, tutorConfig))

val repositories = appRepositories()

val port = environment.config.property("ktor.deployment.port").getString()

val keycloakProvider = OAuthServerSettings.OAuth2ServerSettings(
Expand Down Expand Up @@ -174,12 +174,12 @@ fun Application.module(
authenticate("auth-jwt") {
this@authenticate.cardApiV1(
service = cardService,
repositories = repositoriesConfig.cardRepositories,
repositories = repositories,
contextConfig = contextConfig,
)
this@authenticate.dictionaryApiV1(
service = dictionaryService,
repositories = repositoriesConfig.dictionaryRepositories,
repositories = repositories,
contextConfig = contextConfig,
)
}
Expand All @@ -203,12 +203,12 @@ fun Application.module(
} else {
cardApiV1(
service = cardService,
repositories = repositoriesConfig.cardRepositories,
repositories = repositories,
contextConfig = contextConfig,
)
dictionaryApiV1(
service = dictionaryService,
repositories = repositoriesConfig.dictionaryRepositories,
repositories = repositories,
contextConfig = contextConfig,
)
get("/") {
Expand Down
17 changes: 17 additions & 0 deletions app-ktor/src/main/kotlin/Repositories.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.gitlab.sszuev.flashcards

import com.gitlab.sszuev.flashcards.dbmem.MemDbCardRepository
import com.gitlab.sszuev.flashcards.dbmem.MemDbDictionaryRepository
import com.gitlab.sszuev.flashcards.dbpg.PgDbCardRepository
import com.gitlab.sszuev.flashcards.dbpg.PgDbDictionaryRepository
import com.gitlab.sszuev.flashcards.speaker.createDirectTTSResourceRepository
import com.gitlab.sszuev.flashcards.speaker.rabbitmq.RMQTTSResourceRepository

fun appRepositories() = AppRepositories(
prodTTSClientRepository = RMQTTSResourceRepository(),
testTTSClientRepository = createDirectTTSResourceRepository(),
prodCardRepository = PgDbCardRepository(),
testCardRepository = MemDbCardRepository(),
prodDictionaryRepository = PgDbDictionaryRepository(),
testDictionaryRepository = MemDbDictionaryRepository(),
)
7 changes: 3 additions & 4 deletions app-ktor/src/main/kotlin/api/Api.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.gitlab.sszuev.flashcards.api

import com.gitlab.sszuev.flashcards.CardRepositories
import com.gitlab.sszuev.flashcards.DictionaryRepositories
import com.gitlab.sszuev.flashcards.AppRepositories
import com.gitlab.sszuev.flashcards.api.controllers.cards
import com.gitlab.sszuev.flashcards.api.controllers.dictionaries
import com.gitlab.sszuev.flashcards.api.controllers.sounds
Expand All @@ -13,7 +12,7 @@ import io.ktor.server.routing.route

internal fun Route.cardApiV1(
service: CardService,
repositories: CardRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig,
) {
route("v1/api") {
Expand All @@ -24,7 +23,7 @@ internal fun Route.cardApiV1(

internal fun Route.dictionaryApiV1(
service: DictionaryService,
repositories: DictionaryRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig,
) {
route("v1/api") {
Expand Down
22 changes: 11 additions & 11 deletions app-ktor/src/main/kotlin/api/controllers/CardController.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.gitlab.sszuev.flashcards.api.controllers

import com.gitlab.sszuev.flashcards.AppRepositories
import com.gitlab.sszuev.flashcards.CardContext
import com.gitlab.sszuev.flashcards.CardRepositories
import com.gitlab.sszuev.flashcards.api.services.CardService
import com.gitlab.sszuev.flashcards.api.v1.models.BaseRequest
import com.gitlab.sszuev.flashcards.api.v1.models.CreateCardRequest
Expand All @@ -26,7 +26,7 @@ private val logger: ExtLogger = logger("com.gitlab.sszuev.flashcards.api.control

suspend fun ApplicationCall.getResource(
service: CardService,
repositories: CardRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig
) {
execute<GetAudioRequest>(CardOperation.GET_RESOURCE, repositories, logger, contextConfig) {
Expand All @@ -36,7 +36,7 @@ suspend fun ApplicationCall.getResource(

suspend fun ApplicationCall.createCard(
service: CardService,
repositories: CardRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig
) {
execute<CreateCardRequest>(CardOperation.CREATE_CARD, repositories, logger, contextConfig) {
Expand All @@ -46,7 +46,7 @@ suspend fun ApplicationCall.createCard(

suspend fun ApplicationCall.updateCard(
service: CardService,
repositories: CardRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig
) {
execute<UpdateCardRequest>(CardOperation.UPDATE_CARD, repositories, logger, contextConfig) {
Expand All @@ -56,7 +56,7 @@ suspend fun ApplicationCall.updateCard(

suspend fun ApplicationCall.searchCards(
service: CardService,
repositories: CardRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig
) {
execute<SearchCardsRequest>(CardOperation.SEARCH_CARDS, repositories, logger, contextConfig) {
Expand All @@ -66,7 +66,7 @@ suspend fun ApplicationCall.searchCards(

suspend fun ApplicationCall.getAllCards(
service: CardService,
repositories: CardRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig
) {
execute<GetAllCardsRequest>(CardOperation.GET_ALL_CARDS, repositories, logger, contextConfig) {
Expand All @@ -76,7 +76,7 @@ suspend fun ApplicationCall.getAllCards(

suspend fun ApplicationCall.getCard(
service: CardService,
repositories: CardRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig
) {
execute<GetCardRequest>(CardOperation.GET_CARD, repositories, logger, contextConfig) {
Expand All @@ -86,7 +86,7 @@ suspend fun ApplicationCall.getCard(

suspend fun ApplicationCall.learnCard(
service: CardService,
repositories: CardRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig
) {
execute<LearnCardsRequest>(CardOperation.LEARN_CARDS, repositories, logger, contextConfig) {
Expand All @@ -96,7 +96,7 @@ suspend fun ApplicationCall.learnCard(

suspend fun ApplicationCall.resetCard(
service: CardService,
repositories: CardRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig
) {
execute<ResetCardRequest>(CardOperation.RESET_CARD, repositories, logger, contextConfig) {
Expand All @@ -106,7 +106,7 @@ suspend fun ApplicationCall.resetCard(

suspend fun ApplicationCall.deleteCard(
service: CardService,
repositories: CardRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig
) {
execute<DeleteCardRequest>(CardOperation.DELETE_CARD, repositories, logger, contextConfig) {
Expand All @@ -116,7 +116,7 @@ suspend fun ApplicationCall.deleteCard(

private suspend inline fun <reified R : BaseRequest> ApplicationCall.execute(
operation: CardOperation,
repositories: CardRepositories,
repositories: AppRepositories,
logger: ExtLogger,
contextConfig: ContextConfig,
noinline exec: suspend CardContext.() -> Unit,
Expand Down
14 changes: 7 additions & 7 deletions app-ktor/src/main/kotlin/api/controllers/DictionaryController.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.gitlab.sszuev.flashcards.api.controllers

import com.gitlab.sszuev.flashcards.AppRepositories
import com.gitlab.sszuev.flashcards.DictionaryContext
import com.gitlab.sszuev.flashcards.DictionaryRepositories
import com.gitlab.sszuev.flashcards.api.services.DictionaryService
import com.gitlab.sszuev.flashcards.api.v1.models.BaseRequest
import com.gitlab.sszuev.flashcards.api.v1.models.CreateDictionaryRequest
Expand All @@ -22,7 +22,7 @@ private val logger: ExtLogger = logger("com.gitlab.sszuev.flashcards.api.control

suspend fun ApplicationCall.getAllDictionaries(
service: DictionaryService,
repositories: DictionaryRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig
) {
execute<GetAllDictionariesRequest>(
Expand All @@ -37,7 +37,7 @@ suspend fun ApplicationCall.getAllDictionaries(

suspend fun ApplicationCall.createDictionary(
service: DictionaryService,
repositories: DictionaryRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig
) {
execute<CreateDictionaryRequest>(
Expand All @@ -52,7 +52,7 @@ suspend fun ApplicationCall.createDictionary(

suspend fun ApplicationCall.deleteDictionary(
service: DictionaryService,
repositories: DictionaryRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig
) {
execute<DeleteDictionaryRequest>(
Expand All @@ -67,7 +67,7 @@ suspend fun ApplicationCall.deleteDictionary(

suspend fun ApplicationCall.downloadDictionary(
service: DictionaryService,
repositories: DictionaryRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig
) {
execute<DownloadDictionaryRequest>(
Expand All @@ -82,7 +82,7 @@ suspend fun ApplicationCall.downloadDictionary(

suspend fun ApplicationCall.uploadDictionary(
service: DictionaryService,
repositories: DictionaryRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig
) {
execute<UploadDictionaryRequest>(
Expand All @@ -97,7 +97,7 @@ suspend fun ApplicationCall.uploadDictionary(

private suspend inline fun <reified R : BaseRequest> ApplicationCall.execute(
operation: DictionaryOperation,
repositories: DictionaryRepositories,
repositories: AppRepositories,
logger: ExtLogger,
contextConfig: ContextConfig,
noinline exec: suspend DictionaryContext.() -> Unit,
Expand Down
9 changes: 4 additions & 5 deletions app-ktor/src/main/kotlin/api/controllers/Rest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.gitlab.sszuev.flashcards.api.controllers

import com.gitlab.sszuev.flashcards.CardRepositories
import com.gitlab.sszuev.flashcards.DictionaryRepositories
import com.gitlab.sszuev.flashcards.AppRepositories
import com.gitlab.sszuev.flashcards.api.services.CardService
import com.gitlab.sszuev.flashcards.api.services.DictionaryService
import com.gitlab.sszuev.flashcards.config.ContextConfig
Expand All @@ -12,7 +11,7 @@ import io.ktor.server.routing.route

fun Route.cards(
service: CardService,
repositories: CardRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig,
) {
route("cards") {
Expand Down Expand Up @@ -45,7 +44,7 @@ fun Route.cards(

fun Route.sounds(
service: CardService,
repositories: CardRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig,
) {
route("sounds") {
Expand All @@ -57,7 +56,7 @@ fun Route.sounds(

fun Route.dictionaries(
service: DictionaryService,
repositories: DictionaryRepositories,
repositories: AppRepositories,
contextConfig: ContextConfig,
) {
route("dictionaries") {
Expand Down
50 changes: 0 additions & 50 deletions app-ktor/src/main/kotlin/config/RepositoriesConfig.kt

This file was deleted.

4 changes: 2 additions & 2 deletions app-ktor/src/main/resources/data/dictionaries.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
id,name,user_id,source_lang,target_lang,details,changed_at
1,Irregular Verbs,42,en,ru,{},2022-12-26T16:04:14
2,Weather,42,en,ru,{},2022-12-26T16:04:14
1,Irregular Verbs,c9a414f5-3f75-4494-b664-f4c8b33ff4e6,en,ru,{},2022-12-26T16:04:14
2,Weather,c9a414f5-3f75-4494-b664-f4c8b33ff4e6,en,ru,{},2022-12-26T16:04:14
2 changes: 0 additions & 2 deletions app-ktor/src/main/resources/data/users.csv

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.gitlab.sszuev.flashcards.api.controllers

import com.gitlab.sszuev.flashcards.AppRepositories
import com.gitlab.sszuev.flashcards.CardContext
import com.gitlab.sszuev.flashcards.CardRepositories
import com.gitlab.sszuev.flashcards.api.services.CardService
import com.gitlab.sszuev.flashcards.api.v1.models.BaseRequest
import com.gitlab.sszuev.flashcards.api.v1.models.BaseResponse
Expand Down Expand Up @@ -112,7 +112,7 @@ internal class CardControllerMockkTest {
coEvery {
service.serviceMethod(any())
} throws TestException(msg)
val repositories = mockk<CardRepositories>()
val repositories = mockk<AppRepositories>()

val tutorConfig = mockk<TutorConfig>(relaxed = true)
val runConfig = mockk<RunConfig>(relaxed = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ internal class CardControllerRunTest {
"spell of cold weather"
), res.card!!.words!!.single().examples?.map { it.example }
)
Assertions.assertNull(res.card!!.words!!.single().sound)
Assertions.assertEquals("en:weather", res.card!!.words!!.single().sound)
Assertions.assertEquals("en:weather", res.card!!.sound)
Assertions.assertNull(res.card!!.answered)
Assertions.assertEquals(emptyMap<String, Long>(), res.card!!.details)
}
Expand Down
Loading

0 comments on commit c4907e0

Please sign in to comment.