From 3b9d691d5771946b9569f3f192187a86ab171492 Mon Sep 17 00:00:00 2001 From: evgnep Date: Sun, 5 May 2024 14:17:24 +0300 Subject: [PATCH] M4l3 mappers (#16) (#16) * m4l3 - Models & Mappers (cherry picked from commit f8fdcf4670c0dea16524f06b3083a55408aa198b) Co-authored-by: Sergey Okatov --- .github/workflows/{test.yml => test.yml_} | 0 .../build.gradle.kts | 14 ++ .../src/main/kotlin/MappersV1FromTransport.kt | 133 ++++++++++++++++++ .../src/main/kotlin/MappersV1ToTransport.kt | 113 +++++++++++++++ .../kotlin/exceptions/UnknownRequestClass.kt | 3 + .../src/test/kotlin/MapperTest.kt | 70 +++++++++ .../build.gradle.kts | 2 + .../kotlin/mappers/MappersV2FromTransport.kt | 130 +++++++++++++++++ .../kotlin/mappers/MappersV2ToTransport.kt | 114 +++++++++++++++ .../kotlin/mappers/MapperCreateTest.kt | 70 +++++++++ .../kotlin/mappers/MapperDeleteTest.kt | 71 ++++++++++ .../commonTest/kotlin/mappers/MapperTest.kt | 70 +++++++++ .../kotlin/mappers/MapperUpdateTest.kt | 78 ++++++++++ .../ok-marketplace-common/README.md | 6 + .../ok-marketplace-common/build.gradle.kts | 24 ++++ .../src/commonMain/kotlin/Constants.kt | 7 + .../src/commonMain/kotlin/MkplContext.kt | 23 +++ .../kotlin/exceptions/UnknownMkplCommand.kt | 6 + .../src/commonMain/kotlin/models/MkplAd.kt | 20 +++ .../commonMain/kotlin/models/MkplAdFilter.kt | 7 + .../src/commonMain/kotlin/models/MkplAdId.kt | 12 ++ .../commonMain/kotlin/models/MkplAdLock.kt | 12 ++ .../kotlin/models/MkplAdPermissionClient.kt | 10 ++ .../commonMain/kotlin/models/MkplCommand.kt | 11 ++ .../commonMain/kotlin/models/MkplDealSide.kt | 7 + .../src/commonMain/kotlin/models/MkplError.kt | 9 ++ .../commonMain/kotlin/models/MkplProductId.kt | 12 ++ .../commonMain/kotlin/models/MkplRequestId.kt | 12 ++ .../src/commonMain/kotlin/models/MkplState.kt | 8 ++ .../commonMain/kotlin/models/MkplUserId.kt | 12 ++ .../kotlin/models/MkplVisibility.kt | 8 ++ .../commonMain/kotlin/models/MkplWorkMode.kt | 7 + .../src/commonMain/kotlin/stubs/MkplStubs.kt | 14 ++ ok-marketplace-be/settings.gradle.kts | 4 + 34 files changed, 1099 insertions(+) rename .github/workflows/{test.yml => test.yml_} (100%) create mode 100644 ok-marketplace-be/ok-marketplace-api-v1-mappers/build.gradle.kts create mode 100644 ok-marketplace-be/ok-marketplace-api-v1-mappers/src/main/kotlin/MappersV1FromTransport.kt create mode 100644 ok-marketplace-be/ok-marketplace-api-v1-mappers/src/main/kotlin/MappersV1ToTransport.kt create mode 100644 ok-marketplace-be/ok-marketplace-api-v1-mappers/src/main/kotlin/exceptions/UnknownRequestClass.kt create mode 100644 ok-marketplace-be/ok-marketplace-api-v1-mappers/src/test/kotlin/MapperTest.kt create mode 100644 ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonMain/kotlin/mappers/MappersV2FromTransport.kt create mode 100644 ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonMain/kotlin/mappers/MappersV2ToTransport.kt create mode 100644 ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperCreateTest.kt create mode 100644 ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperDeleteTest.kt create mode 100644 ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperTest.kt create mode 100644 ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperUpdateTest.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/README.md create mode 100644 ok-marketplace-be/ok-marketplace-common/build.gradle.kts create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/Constants.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/MkplContext.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/exceptions/UnknownMkplCommand.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAd.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAdFilter.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAdId.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAdLock.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAdPermissionClient.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplCommand.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplDealSide.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplError.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplProductId.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplRequestId.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplState.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplUserId.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplVisibility.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplWorkMode.kt create mode 100644 ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/stubs/MkplStubs.kt diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml_ similarity index 100% rename from .github/workflows/test.yml rename to .github/workflows/test.yml_ diff --git a/ok-marketplace-be/ok-marketplace-api-v1-mappers/build.gradle.kts b/ok-marketplace-be/ok-marketplace-api-v1-mappers/build.gradle.kts new file mode 100644 index 0000000..48423eb --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-api-v1-mappers/build.gradle.kts @@ -0,0 +1,14 @@ +plugins { + id("build-jvm") +} + +group = rootProject.group +version = rootProject.version + +dependencies { + implementation(kotlin("stdlib")) + implementation(project(":ok-marketplace-api-v1-jackson")) + implementation(project(":ok-marketplace-common")) + + testImplementation(kotlin("test-junit")) +} diff --git a/ok-marketplace-be/ok-marketplace-api-v1-mappers/src/main/kotlin/MappersV1FromTransport.kt b/ok-marketplace-be/ok-marketplace-api-v1-mappers/src/main/kotlin/MappersV1FromTransport.kt new file mode 100644 index 0000000..35d28bc --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-api-v1-mappers/src/main/kotlin/MappersV1FromTransport.kt @@ -0,0 +1,133 @@ +package ru.otus.otuskotlin.marketplace.mappers.v1 + +import ru.otus.otuskotlin.marketplace.api.v1.models.* +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.* +import ru.otus.otuskotlin.marketplace.common.models.MkplWorkMode +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import ru.otus.otuskotlin.marketplace.mappers.v1.exceptions.UnknownRequestClass + +fun MkplContext.fromTransport(request: IRequest) = when (request) { + is AdCreateRequest -> fromTransport(request) + is AdReadRequest -> fromTransport(request) + is AdUpdateRequest -> fromTransport(request) + is AdDeleteRequest -> fromTransport(request) + is AdSearchRequest -> fromTransport(request) + is AdOffersRequest -> fromTransport(request) + else -> throw UnknownRequestClass(request.javaClass) +} + +private fun String?.toAdId() = this?.let { MkplAdId(it) } ?: MkplAdId.NONE +private fun String?.toAdWithId() = MkplAd(id = this.toAdId()) +private fun String?.toAdLock() = this?.let { MkplAdLock(it) } ?: MkplAdLock.NONE + +private fun AdDebug?.transportToWorkMode(): MkplWorkMode = when (this?.mode) { + AdRequestDebugMode.PROD -> MkplWorkMode.PROD + AdRequestDebugMode.TEST -> MkplWorkMode.TEST + AdRequestDebugMode.STUB -> MkplWorkMode.STUB + null -> MkplWorkMode.PROD +} + +private fun AdDebug?.transportToStubCase(): MkplStubs = when (this?.stub) { + AdRequestDebugStubs.SUCCESS -> MkplStubs.SUCCESS + AdRequestDebugStubs.NOT_FOUND -> MkplStubs.NOT_FOUND + AdRequestDebugStubs.BAD_ID -> MkplStubs.BAD_ID + AdRequestDebugStubs.BAD_TITLE -> MkplStubs.BAD_TITLE + AdRequestDebugStubs.BAD_DESCRIPTION -> MkplStubs.BAD_DESCRIPTION + AdRequestDebugStubs.BAD_VISIBILITY -> MkplStubs.BAD_VISIBILITY + AdRequestDebugStubs.CANNOT_DELETE -> MkplStubs.CANNOT_DELETE + AdRequestDebugStubs.BAD_SEARCH_STRING -> MkplStubs.BAD_SEARCH_STRING + null -> MkplStubs.NONE +} + +fun MkplContext.fromTransport(request: AdCreateRequest) { + command = MkplCommand.CREATE + adRequest = request.ad?.toInternal() ?: MkplAd() + workMode = request.debug.transportToWorkMode() + stubCase = request.debug.transportToStubCase() +} + +fun MkplContext.fromTransport(request: AdReadRequest) { + command = MkplCommand.READ + adRequest = request.ad.toInternal() + workMode = request.debug.transportToWorkMode() + stubCase = request.debug.transportToStubCase() +} + +private fun AdReadObject?.toInternal(): MkplAd = if (this != null) { + MkplAd(id = id.toAdId()) +} else { + MkplAd() +} + + +fun MkplContext.fromTransport(request: AdUpdateRequest) { + command = MkplCommand.UPDATE + adRequest = request.ad?.toInternal() ?: MkplAd() + workMode = request.debug.transportToWorkMode() + stubCase = request.debug.transportToStubCase() +} + +fun MkplContext.fromTransport(request: AdDeleteRequest) { + command = MkplCommand.DELETE + adRequest = request.ad.toInternal() + workMode = request.debug.transportToWorkMode() + stubCase = request.debug.transportToStubCase() +} + +private fun AdDeleteObject?.toInternal(): MkplAd = if (this != null) { + MkplAd( + id = id.toAdId(), + lock = lock.toAdLock(), + ) +} else { + MkplAd() +} + +fun MkplContext.fromTransport(request: AdSearchRequest) { + command = MkplCommand.SEARCH + adFilterRequest = request.adFilter.toInternal() + workMode = request.debug.transportToWorkMode() + stubCase = request.debug.transportToStubCase() +} + +fun MkplContext.fromTransport(request: AdOffersRequest) { + command = MkplCommand.OFFERS + adRequest = request.ad?.id.toAdWithId() + workMode = request.debug.transportToWorkMode() + stubCase = request.debug.transportToStubCase() +} + +private fun AdSearchFilter?.toInternal(): MkplAdFilter = MkplAdFilter( + searchString = this?.searchString ?: "" +) + +private fun AdCreateObject.toInternal(): MkplAd = MkplAd( + title = this.title ?: "", + description = this.description ?: "", + adType = this.adType.fromTransport(), + visibility = this.visibility.fromTransport(), +) + +private fun AdUpdateObject.toInternal(): MkplAd = MkplAd( + id = this.id.toAdId(), + title = this.title ?: "", + description = this.description ?: "", + adType = this.adType.fromTransport(), + visibility = this.visibility.fromTransport(), + lock = lock.toAdLock(), +) + +private fun AdVisibility?.fromTransport(): MkplVisibility = when (this) { + AdVisibility.PUBLIC -> MkplVisibility.VISIBLE_PUBLIC + AdVisibility.OWNER_ONLY -> MkplVisibility.VISIBLE_TO_OWNER + AdVisibility.REGISTERED_ONLY -> MkplVisibility.VISIBLE_TO_GROUP + null -> MkplVisibility.NONE +} + +private fun DealSide?.fromTransport(): MkplDealSide = when (this) { + DealSide.DEMAND -> MkplDealSide.DEMAND + DealSide.SUPPLY -> MkplDealSide.SUPPLY + null -> MkplDealSide.NONE +} + diff --git a/ok-marketplace-be/ok-marketplace-api-v1-mappers/src/main/kotlin/MappersV1ToTransport.kt b/ok-marketplace-be/ok-marketplace-api-v1-mappers/src/main/kotlin/MappersV1ToTransport.kt new file mode 100644 index 0000000..efac0b0 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-api-v1-mappers/src/main/kotlin/MappersV1ToTransport.kt @@ -0,0 +1,113 @@ +package ru.otus.otuskotlin.marketplace.mappers.v1 + +import ru.otus.otuskotlin.marketplace.api.v1.models.* +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.exceptions.UnknownMkplCommand +import ru.otus.otuskotlin.marketplace.common.models.* + +fun MkplContext.toTransportAd(): IResponse = when (val cmd = command) { + MkplCommand.CREATE -> toTransportCreate() + MkplCommand.READ -> toTransportRead() + MkplCommand.UPDATE -> toTransportUpdate() + MkplCommand.DELETE -> toTransportDelete() + MkplCommand.SEARCH -> toTransportSearch() + MkplCommand.OFFERS -> toTransportOffers() + MkplCommand.NONE -> throw UnknownMkplCommand(cmd) +} + +fun MkplContext.toTransportCreate() = AdCreateResponse( + result = state.toResult(), + errors = errors.toTransportErrors(), + ad = adResponse.toTransportAd() +) + +fun MkplContext.toTransportRead() = AdReadResponse( + result = state.toResult(), + errors = errors.toTransportErrors(), + ad = adResponse.toTransportAd() +) + +fun MkplContext.toTransportUpdate() = AdUpdateResponse( + result = state.toResult(), + errors = errors.toTransportErrors(), + ad = adResponse.toTransportAd() +) + +fun MkplContext.toTransportDelete() = AdDeleteResponse( + result = state.toResult(), + errors = errors.toTransportErrors(), + ad = adResponse.toTransportAd() +) + +fun MkplContext.toTransportSearch() = AdSearchResponse( + result = state.toResult(), + errors = errors.toTransportErrors(), + ads = adsResponse.toTransportAd() +) + +fun MkplContext.toTransportOffers() = AdOffersResponse( + result = state.toResult(), + errors = errors.toTransportErrors(), + ads = adsResponse.toTransportAd() +) + +fun List.toTransportAd(): List? = this + .map { it.toTransportAd() } + .toList() + .takeIf { it.isNotEmpty() } + +private fun MkplAd.toTransportAd(): AdResponseObject = AdResponseObject( + id = id.takeIf { it != MkplAdId.NONE }?.asString(), + title = title.takeIf { it.isNotBlank() }, + description = description.takeIf { it.isNotBlank() }, + ownerId = ownerId.takeIf { it != MkplUserId.NONE }?.asString(), + adType = adType.toTransportAd(), + visibility = visibility.toTransportAd(), + permissions = permissionsClient.toTransportAd(), +) + +private fun Set.toTransportAd(): Set? = this + .map { it.toTransportAd() } + .toSet() + .takeIf { it.isNotEmpty() } + +private fun MkplAdPermissionClient.toTransportAd() = when (this) { + MkplAdPermissionClient.READ -> AdPermissions.READ + MkplAdPermissionClient.UPDATE -> AdPermissions.UPDATE + MkplAdPermissionClient.MAKE_VISIBLE_OWNER -> AdPermissions.MAKE_VISIBLE_OWN + MkplAdPermissionClient.MAKE_VISIBLE_GROUP -> AdPermissions.MAKE_VISIBLE_GROUP + MkplAdPermissionClient.MAKE_VISIBLE_PUBLIC -> AdPermissions.MAKE_VISIBLE_PUBLIC + MkplAdPermissionClient.DELETE -> AdPermissions.DELETE +} + +private fun MkplVisibility.toTransportAd(): AdVisibility? = when (this) { + MkplVisibility.VISIBLE_PUBLIC -> AdVisibility.PUBLIC + MkplVisibility.VISIBLE_TO_GROUP -> AdVisibility.REGISTERED_ONLY + MkplVisibility.VISIBLE_TO_OWNER -> AdVisibility.OWNER_ONLY + MkplVisibility.NONE -> null +} + +private fun MkplDealSide.toTransportAd(): DealSide? = when (this) { + MkplDealSide.DEMAND -> DealSide.DEMAND + MkplDealSide.SUPPLY -> DealSide.SUPPLY + MkplDealSide.NONE -> null +} + +private fun List.toTransportErrors(): List? = this + .map { it.toTransportAd() } + .toList() + .takeIf { it.isNotEmpty() } + +private fun MkplError.toTransportAd() = Error( + code = code.takeIf { it.isNotBlank() }, + group = group.takeIf { it.isNotBlank() }, + field = field.takeIf { it.isNotBlank() }, + message = message.takeIf { it.isNotBlank() }, +) + +private fun MkplState.toResult(): ResponseResult? = when (this) { + MkplState.RUNNING -> ResponseResult.SUCCESS + MkplState.FAILING -> ResponseResult.ERROR + MkplState.FINISHING -> ResponseResult.SUCCESS + MkplState.NONE -> null +} diff --git a/ok-marketplace-be/ok-marketplace-api-v1-mappers/src/main/kotlin/exceptions/UnknownRequestClass.kt b/ok-marketplace-be/ok-marketplace-api-v1-mappers/src/main/kotlin/exceptions/UnknownRequestClass.kt new file mode 100644 index 0000000..af7834a --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-api-v1-mappers/src/main/kotlin/exceptions/UnknownRequestClass.kt @@ -0,0 +1,3 @@ +package ru.otus.otuskotlin.marketplace.mappers.v1.exceptions + +class UnknownRequestClass(clazz: Class<*>) : RuntimeException("Class $clazz cannot be mapped to MkplContext") diff --git a/ok-marketplace-be/ok-marketplace-api-v1-mappers/src/test/kotlin/MapperTest.kt b/ok-marketplace-be/ok-marketplace-api-v1-mappers/src/test/kotlin/MapperTest.kt new file mode 100644 index 0000000..8f40d25 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-api-v1-mappers/src/test/kotlin/MapperTest.kt @@ -0,0 +1,70 @@ +package ru.otus.otuskotlin.marketplace.mappers.v1 + +import org.junit.Test +import ru.otus.otuskotlin.marketplace.api.v1.models.* +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.* +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import kotlin.test.assertEquals + +class MapperTest { + @Test + fun fromTransport() { + val req = AdCreateRequest( + debug = AdDebug( + mode = AdRequestDebugMode.STUB, + stub = AdRequestDebugStubs.SUCCESS, + ), + ad = AdCreateObject( + title = "title", + description = "desc", + adType = DealSide.DEMAND, + visibility = AdVisibility.PUBLIC, + ), + ) + + val context = MkplContext() + context.fromTransport(req) + + assertEquals(MkplStubs.SUCCESS, context.stubCase) + assertEquals(MkplWorkMode.STUB, context.workMode) + assertEquals("title", context.adRequest.title) + assertEquals(MkplVisibility.VISIBLE_PUBLIC, context.adRequest.visibility) + assertEquals(MkplDealSide.DEMAND, context.adRequest.adType) + } + + @Test + fun toTransport() { + val context = MkplContext( + requestId = MkplRequestId("1234"), + command = MkplCommand.CREATE, + adResponse = MkplAd( + title = "title", + description = "desc", + adType = MkplDealSide.DEMAND, + visibility = MkplVisibility.VISIBLE_PUBLIC, + ), + errors = mutableListOf( + MkplError( + code = "err", + group = "request", + field = "title", + message = "wrong title", + ) + ), + state = MkplState.RUNNING, + ) + + val req = context.toTransportAd() as AdCreateResponse + + assertEquals("title", req.ad?.title) + assertEquals("desc", req.ad?.description) + assertEquals(AdVisibility.PUBLIC, req.ad?.visibility) + assertEquals(DealSide.DEMAND, req.ad?.adType) + assertEquals(1, req.errors?.size) + assertEquals("err", req.errors?.firstOrNull()?.code) + assertEquals("request", req.errors?.firstOrNull()?.group) + assertEquals("title", req.errors?.firstOrNull()?.field) + assertEquals("wrong title", req.errors?.firstOrNull()?.message) + } +} diff --git a/ok-marketplace-be/ok-marketplace-api-v2-kmp/build.gradle.kts b/ok-marketplace-be/ok-marketplace-api-v2-kmp/build.gradle.kts index 7766775..fc19299 100644 --- a/ok-marketplace-be/ok-marketplace-api-v2-kmp/build.gradle.kts +++ b/ok-marketplace-be/ok-marketplace-api-v2-kmp/build.gradle.kts @@ -21,6 +21,8 @@ kotlin { implementation(libs.kotlinx.serialization.core) implementation(libs.kotlinx.serialization.json) + + implementation(project(":ok-marketplace-common")) } } val commonTest by getting { diff --git a/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonMain/kotlin/mappers/MappersV2FromTransport.kt b/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonMain/kotlin/mappers/MappersV2FromTransport.kt new file mode 100644 index 0000000..aaffafe --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonMain/kotlin/mappers/MappersV2FromTransport.kt @@ -0,0 +1,130 @@ +package ru.otus.otuskotlin.marketplace.api.v2.mappers + +import ru.otus.otuskotlin.marketplace.api.v2.models.* +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.* +import ru.otus.otuskotlin.marketplace.common.models.MkplWorkMode +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs + +fun MkplContext.fromTransport(request: IRequest) = when (request) { + is AdCreateRequest -> fromTransport(request) + is AdReadRequest -> fromTransport(request) + is AdUpdateRequest -> fromTransport(request) + is AdDeleteRequest -> fromTransport(request) + is AdSearchRequest -> fromTransport(request) + is AdOffersRequest -> fromTransport(request) +} + +private fun String?.toAdId() = this?.let { MkplAdId(it) } ?: MkplAdId.NONE +private fun String?.toAdLock() = this?.let { MkplAdLock(it) } ?: MkplAdLock.NONE +private fun AdReadObject?.toInternal() = if (this != null) { + MkplAd(id = id.toAdId()) +} else { + MkplAd() +} +private fun String?.toProductId() = this?.let { MkplProductId(it) } ?: MkplProductId.NONE + +private fun AdDebug?.transportToWorkMode(): MkplWorkMode = when (this?.mode) { + AdRequestDebugMode.PROD -> MkplWorkMode.PROD + AdRequestDebugMode.TEST -> MkplWorkMode.TEST + AdRequestDebugMode.STUB -> MkplWorkMode.STUB + null -> MkplWorkMode.PROD +} + +private fun AdDebug?.transportToStubCase(): MkplStubs = when (this?.stub) { + AdRequestDebugStubs.SUCCESS -> MkplStubs.SUCCESS + AdRequestDebugStubs.NOT_FOUND -> MkplStubs.NOT_FOUND + AdRequestDebugStubs.BAD_ID -> MkplStubs.BAD_ID + AdRequestDebugStubs.BAD_TITLE -> MkplStubs.BAD_TITLE + AdRequestDebugStubs.BAD_DESCRIPTION -> MkplStubs.BAD_DESCRIPTION + AdRequestDebugStubs.BAD_VISIBILITY -> MkplStubs.BAD_VISIBILITY + AdRequestDebugStubs.CANNOT_DELETE -> MkplStubs.CANNOT_DELETE + AdRequestDebugStubs.BAD_SEARCH_STRING -> MkplStubs.BAD_SEARCH_STRING + null -> MkplStubs.NONE +} + +fun MkplContext.fromTransport(request: AdCreateRequest) { + command = MkplCommand.CREATE + adRequest = request.ad?.toInternal() ?: MkplAd() + workMode = request.debug.transportToWorkMode() + stubCase = request.debug.transportToStubCase() +} + +fun MkplContext.fromTransport(request: AdReadRequest) { + command = MkplCommand.READ + adRequest = request.ad.toInternal() + workMode = request.debug.transportToWorkMode() + stubCase = request.debug.transportToStubCase() +} + +fun MkplContext.fromTransport(request: AdUpdateRequest) { + command = MkplCommand.UPDATE + adRequest = request.ad?.toInternal() ?: MkplAd() + workMode = request.debug.transportToWorkMode() + stubCase = request.debug.transportToStubCase() +} + +fun MkplContext.fromTransport(request: AdDeleteRequest) { + command = MkplCommand.DELETE + adRequest = request.ad.toInternal() + workMode = request.debug.transportToWorkMode() + stubCase = request.debug.transportToStubCase() +} + +private fun AdDeleteObject?.toInternal(): MkplAd = if (this != null) { + MkplAd( + id = id.toAdId(), + lock = lock.toAdLock(), + ) +} else { + MkplAd() +} + +fun MkplContext.fromTransport(request: AdSearchRequest) { + command = MkplCommand.SEARCH + adFilterRequest = request.adFilter.toInternal() + workMode = request.debug.transportToWorkMode() + stubCase = request.debug.transportToStubCase() +} + +fun MkplContext.fromTransport(request: AdOffersRequest) { + command = MkplCommand.OFFERS + adRequest = request.ad.toInternal() + workMode = request.debug.transportToWorkMode() + stubCase = request.debug.transportToStubCase() +} + +private fun AdSearchFilter?.toInternal(): MkplAdFilter = MkplAdFilter( + searchString = this?.searchString ?: "" +) + +private fun AdCreateObject.toInternal(): MkplAd = MkplAd( + title = this.title ?: "", + description = this.description ?: "", + adType = this.adType.fromTransport(), + visibility = this.visibility.fromTransport(), + productId = this.productId.toProductId(), +) + +private fun AdUpdateObject.toInternal(): MkplAd = MkplAd( + id = this.id.toAdId(), + title = this.title ?: "", + description = this.description ?: "", + adType = this.adType.fromTransport(), + visibility = this.visibility.fromTransport(), + productId = this.productId.toProductId(), + lock = this.lock.toAdLock(), +) + +private fun AdVisibility?.fromTransport(): MkplVisibility = when (this) { + AdVisibility.PUBLIC -> MkplVisibility.VISIBLE_PUBLIC + AdVisibility.OWNER_ONLY -> MkplVisibility.VISIBLE_TO_OWNER + AdVisibility.REGISTERED_ONLY -> MkplVisibility.VISIBLE_TO_GROUP + null -> MkplVisibility.NONE +} + +private fun DealSide?.fromTransport(): MkplDealSide = when (this) { + DealSide.DEMAND -> MkplDealSide.DEMAND + DealSide.SUPPLY -> MkplDealSide.SUPPLY + null -> MkplDealSide.NONE +} diff --git a/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonMain/kotlin/mappers/MappersV2ToTransport.kt b/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonMain/kotlin/mappers/MappersV2ToTransport.kt new file mode 100644 index 0000000..c80d4d8 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonMain/kotlin/mappers/MappersV2ToTransport.kt @@ -0,0 +1,114 @@ +package ru.otus.otuskotlin.marketplace.api.v2.mappers + +import ru.otus.otuskotlin.marketplace.common.exceptions.UnknownMkplCommand +import ru.otus.otuskotlin.marketplace.api.v2.models.* +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.* + +fun MkplContext.toTransportAd(): IResponse = when (val cmd = command) { + MkplCommand.CREATE -> toTransportCreate() + MkplCommand.READ -> toTransportRead() + MkplCommand.UPDATE -> toTransportUpdate() + MkplCommand.DELETE -> toTransportDelete() + MkplCommand.SEARCH -> toTransportSearch() + MkplCommand.OFFERS -> toTransportOffers() + MkplCommand.NONE -> throw UnknownMkplCommand(cmd) +} + +fun MkplContext.toTransportCreate() = AdCreateResponse( + result = state.toResult(), + errors = errors.toTransportErrors(), + ad = adResponse.toTransportAd() +) + +fun MkplContext.toTransportRead() = AdReadResponse( + result = state.toResult(), + errors = errors.toTransportErrors(), + ad = adResponse.toTransportAd() +) + +fun MkplContext.toTransportUpdate() = AdUpdateResponse( + result = state.toResult(), + errors = errors.toTransportErrors(), + ad = adResponse.toTransportAd() +) + +fun MkplContext.toTransportDelete() = AdDeleteResponse( + result = state.toResult(), + errors = errors.toTransportErrors(), + ad = adResponse.toTransportAd() +) + +fun MkplContext.toTransportSearch() = AdSearchResponse( + result = state.toResult(), + errors = errors.toTransportErrors(), + ads = adsResponse.toTransportAd() +) + +fun MkplContext.toTransportOffers() = AdOffersResponse( + result = state.toResult(), + errors = errors.toTransportErrors(), + ads = adsResponse.toTransportAd() +) + +fun List.toTransportAd(): List? = this + .map { it.toTransportAd() } + .toList() + .takeIf { it.isNotEmpty() } + +private fun MkplAd.toTransportAd(): AdResponseObject = AdResponseObject( + id = id.takeIf { it != MkplAdId.NONE }?.asString(), + title = title.takeIf { it.isNotBlank() }, + description = description.takeIf { it.isNotBlank() }, + ownerId = ownerId.takeIf { it != MkplUserId.NONE }?.asString(), + adType = adType.toTransportAd(), + visibility = visibility.toTransportAd(), + permissions = permissionsClient.toTransportAd(), + productId = productId.takeIf { it != MkplProductId.NONE }?.asString(), + lock = lock.takeIf { it != MkplAdLock.NONE }?.asString() +) + +private fun Set.toTransportAd(): Set? = this + .map { it.toTransportAd() } + .toSet() + .takeIf { it.isNotEmpty() } + +private fun MkplAdPermissionClient.toTransportAd() = when (this) { + MkplAdPermissionClient.READ -> AdPermissions.READ + MkplAdPermissionClient.UPDATE -> AdPermissions.UPDATE + MkplAdPermissionClient.MAKE_VISIBLE_OWNER -> AdPermissions.MAKE_VISIBLE_OWN + MkplAdPermissionClient.MAKE_VISIBLE_GROUP -> AdPermissions.MAKE_VISIBLE_GROUP + MkplAdPermissionClient.MAKE_VISIBLE_PUBLIC -> AdPermissions.MAKE_VISIBLE_PUBLIC + MkplAdPermissionClient.DELETE -> AdPermissions.DELETE +} + +private fun MkplVisibility.toTransportAd(): AdVisibility? = when (this) { + MkplVisibility.VISIBLE_PUBLIC -> AdVisibility.PUBLIC + MkplVisibility.VISIBLE_TO_GROUP -> AdVisibility.REGISTERED_ONLY + MkplVisibility.VISIBLE_TO_OWNER -> AdVisibility.OWNER_ONLY + MkplVisibility.NONE -> null +} + +private fun MkplDealSide.toTransportAd(): DealSide? = when (this) { + MkplDealSide.DEMAND -> DealSide.DEMAND + MkplDealSide.SUPPLY -> DealSide.SUPPLY + MkplDealSide.NONE -> null +} + +private fun List.toTransportErrors(): List? = this + .map { it.toTransportAd() } + .toList() + .takeIf { it.isNotEmpty() } + +private fun MkplError.toTransportAd() = Error( + code = code.takeIf { it.isNotBlank() }, + group = group.takeIf { it.isNotBlank() }, + field = field.takeIf { it.isNotBlank() }, + message = message.takeIf { it.isNotBlank() }, +) + +private fun MkplState.toResult(): ResponseResult? = when (this) { + MkplState.RUNNING, MkplState.FINISHING -> ResponseResult.SUCCESS + MkplState.FAILING -> ResponseResult.ERROR + MkplState.NONE -> null +} diff --git a/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperCreateTest.kt b/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperCreateTest.kt new file mode 100644 index 0000000..63fad80 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperCreateTest.kt @@ -0,0 +1,70 @@ +package ru.otus.otuskotlin.marketplace.api.v2.mappers + +import ru.otus.otuskotlin.marketplace.api.v2.models.* +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.* +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import kotlin.test.Test +import kotlin.test.assertEquals + +class MapperCreateTest { + @Test + fun fromTransport() { + val req = AdCreateRequest( + debug = AdDebug( + mode = AdRequestDebugMode.STUB, + stub = AdRequestDebugStubs.SUCCESS, + ), + ad = AdCreateObject( + title = "title", + description = "desc", + adType = DealSide.DEMAND, + visibility = AdVisibility.PUBLIC, + ), + ) + + val context = MkplContext() + context.fromTransport(req) + + assertEquals(MkplStubs.SUCCESS, context.stubCase) + assertEquals(MkplWorkMode.STUB, context.workMode) + assertEquals("title", context.adRequest.title) + assertEquals(MkplVisibility.VISIBLE_PUBLIC, context.adRequest.visibility) + assertEquals(MkplDealSide.DEMAND, context.adRequest.adType) + } + + @Test + fun toTransport() { + val context = MkplContext( + requestId = MkplRequestId("1234"), + command = MkplCommand.CREATE, + adResponse = MkplAd( + title = "title", + description = "desc", + adType = MkplDealSide.DEMAND, + visibility = MkplVisibility.VISIBLE_PUBLIC, + ), + errors = mutableListOf( + MkplError( + code = "err", + group = "request", + field = "title", + message = "wrong title", + ) + ), + state = MkplState.RUNNING, + ) + + val req = context.toTransportAd() as AdCreateResponse + + assertEquals("title", req.ad?.title) + assertEquals("desc", req.ad?.description) + assertEquals(AdVisibility.PUBLIC, req.ad?.visibility) + assertEquals(DealSide.DEMAND, req.ad?.adType) + assertEquals(1, req.errors?.size) + assertEquals("err", req.errors?.firstOrNull()?.code) + assertEquals("request", req.errors?.firstOrNull()?.group) + assertEquals("title", req.errors?.firstOrNull()?.field) + assertEquals("wrong title", req.errors?.firstOrNull()?.message) + } +} diff --git a/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperDeleteTest.kt b/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperDeleteTest.kt new file mode 100644 index 0000000..16ac235 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperDeleteTest.kt @@ -0,0 +1,71 @@ +package ru.otus.otuskotlin.marketplace.api.v2.mappers + +import ru.otus.otuskotlin.marketplace.api.v2.models.* +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.* +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import kotlin.test.assertEquals +import kotlin.test.Test + +class MapperDeleteTest { + @Test + fun fromTransport() { + val req = AdDeleteRequest( + debug = AdDebug( + mode = AdRequestDebugMode.STUB, + stub = AdRequestDebugStubs.SUCCESS, + ), + ad = AdDeleteObject( + id = "12345", + lock = "456789", + ), + ) + + val context = MkplContext() + context.fromTransport(req) + + assertEquals(MkplStubs.SUCCESS, context.stubCase) + assertEquals(MkplWorkMode.STUB, context.workMode) + assertEquals("12345", context.adRequest.id.asString()) + assertEquals("456789", context.adRequest.lock.asString()) + } + + @Test + fun toTransport() { + val context = MkplContext( + requestId = MkplRequestId("1234"), + command = MkplCommand.DELETE, + adResponse = MkplAd( + id = MkplAdId("12345"), + title = "title", + description = "desc", + adType = MkplDealSide.DEMAND, + visibility = MkplVisibility.VISIBLE_PUBLIC, + lock = MkplAdLock("456789"), + ), + errors = mutableListOf( + MkplError( + code = "err", + group = "request", + field = "title", + message = "wrong title", + ) + ), + state = MkplState.RUNNING, + ) + + val req = context.toTransportAd() as AdDeleteResponse + + assertEquals("12345", req.ad?.id) + assertEquals("456789", req.ad?.lock) + assertEquals("title", req.ad?.title) + assertEquals("desc", req.ad?.description) + assertEquals(AdVisibility.PUBLIC, req.ad?.visibility) + assertEquals(DealSide.DEMAND, req.ad?.adType) + assertEquals(1, req.errors?.size) + assertEquals("err", req.errors?.firstOrNull()?.code) + assertEquals("request", req.errors?.firstOrNull()?.group) + assertEquals("title", req.errors?.firstOrNull()?.field) + assertEquals("wrong title", req.errors?.firstOrNull()?.message) + } +} diff --git a/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperTest.kt b/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperTest.kt new file mode 100644 index 0000000..064265f --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperTest.kt @@ -0,0 +1,70 @@ +package ru.otus.otuskotlin.marketplace.api.v2.mappers + +import ru.otus.otuskotlin.marketplace.api.v2.models.* +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.* +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import kotlin.test.Test +import kotlin.test.assertEquals + +class MapperTest { + @Test + fun fromTransport() { + val req = AdCreateRequest( + debug = AdDebug( + mode = AdRequestDebugMode.STUB, + stub = AdRequestDebugStubs.SUCCESS, + ), + ad = AdCreateObject( + title = "title", + description = "desc", + adType = DealSide.DEMAND, + visibility = AdVisibility.PUBLIC, + ), + ) + + val context = MkplContext() + context.fromTransport(req) + + assertEquals(MkplStubs.SUCCESS, context.stubCase) + assertEquals(MkplWorkMode.STUB, context.workMode) + assertEquals("title", context.adRequest.title) + assertEquals(MkplVisibility.VISIBLE_PUBLIC, context.adRequest.visibility) + assertEquals(MkplDealSide.DEMAND, context.adRequest.adType) + } + + @Test + fun toTransport() { + val context = MkplContext( + requestId = MkplRequestId("1234"), + command = MkplCommand.CREATE, + adResponse = MkplAd( + title = "title", + description = "desc", + adType = MkplDealSide.DEMAND, + visibility = MkplVisibility.VISIBLE_PUBLIC, + ), + errors = mutableListOf( + MkplError( + code = "err", + group = "request", + field = "title", + message = "wrong title", + ) + ), + state = MkplState.RUNNING, + ) + + val req = context.toTransportAd() as AdCreateResponse + + assertEquals("title", req.ad?.title) + assertEquals("desc", req.ad?.description) + assertEquals(AdVisibility.PUBLIC, req.ad?.visibility) + assertEquals(DealSide.DEMAND, req.ad?.adType) + assertEquals(1, req.errors?.size) + assertEquals("err", req.errors?.firstOrNull()?.code) + assertEquals("request", req.errors?.firstOrNull()?.group) + assertEquals("title", req.errors?.firstOrNull()?.field) + assertEquals("wrong title", req.errors?.firstOrNull()?.message) + } +} diff --git a/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperUpdateTest.kt b/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperUpdateTest.kt new file mode 100644 index 0000000..7a0ea6e --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonTest/kotlin/mappers/MapperUpdateTest.kt @@ -0,0 +1,78 @@ +package ru.otus.otuskotlin.marketplace.api.v2.mappers + +import ru.otus.otuskotlin.marketplace.api.v2.models.* +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.* +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import kotlin.test.assertEquals +import kotlin.test.Test + +class MapperUpdateTest { + @Test + fun fromTransport() { + val req = AdUpdateRequest( + debug = AdDebug( + mode = AdRequestDebugMode.STUB, + stub = AdRequestDebugStubs.SUCCESS, + ), + ad = AdUpdateObject( + id = "12345", + title = "title", + description = "desc", + adType = DealSide.DEMAND, + visibility = AdVisibility.PUBLIC, + lock = "456789", + ), + ) + + val context = MkplContext() + context.fromTransport(req) + + assertEquals(MkplStubs.SUCCESS, context.stubCase) + assertEquals(MkplWorkMode.STUB, context.workMode) + assertEquals("12345", context.adRequest.id.asString()) + assertEquals("456789", context.adRequest.lock.asString()) + assertEquals("title", context.adRequest.title) + assertEquals(MkplVisibility.VISIBLE_PUBLIC, context.adRequest.visibility) + assertEquals(MkplDealSide.DEMAND, context.adRequest.adType) + } + + @Test + fun toTransport() { + val context = MkplContext( + requestId = MkplRequestId("1234"), + command = MkplCommand.UPDATE, + adResponse = MkplAd( + id = MkplAdId("12345"), + title = "title", + description = "desc", + adType = MkplDealSide.DEMAND, + visibility = MkplVisibility.VISIBLE_PUBLIC, + lock = MkplAdLock("456789"), + ), + errors = mutableListOf( + MkplError( + code = "err", + group = "request", + field = "title", + message = "wrong title", + ) + ), + state = MkplState.RUNNING, + ) + + val req = context.toTransportAd() as AdUpdateResponse + + assertEquals("12345", req.ad?.id) + assertEquals("456789", req.ad?.lock) + assertEquals("title", req.ad?.title) + assertEquals("desc", req.ad?.description) + assertEquals(AdVisibility.PUBLIC, req.ad?.visibility) + assertEquals(DealSide.DEMAND, req.ad?.adType) + assertEquals(1, req.errors?.size) + assertEquals("err", req.errors?.firstOrNull()?.code) + assertEquals("request", req.errors?.firstOrNull()?.group) + assertEquals("title", req.errors?.firstOrNull()?.field) + assertEquals("wrong title", req.errors?.firstOrNull()?.message) + } +} diff --git a/ok-marketplace-be/ok-marketplace-common/README.md b/ok-marketplace-be/ok-marketplace-common/README.md new file mode 100644 index 0000000..754b061 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/README.md @@ -0,0 +1,6 @@ +# Модуль `ok-marketplace-common` + +Модуль содержит только объекты, которые используются всеми остальными модулями проекта. **Запрещено** сюда добавлять +зависимости, которые характерны для какой-то специфичной реализации (Postgres, WebsocketSession). Следует снизить +количество зависимостей для этого модуля. Допускается добавлять зависимости на базовые библиотеки типа kotlinx.datetime, +kotlinx.coroutines и подобные. diff --git a/ok-marketplace-be/ok-marketplace-common/build.gradle.kts b/ok-marketplace-be/ok-marketplace-common/build.gradle.kts new file mode 100644 index 0000000..aff06b2 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/build.gradle.kts @@ -0,0 +1,24 @@ +plugins { + id("build-kmp") +} + +group = rootProject.group +version = rootProject.version + +kotlin { + sourceSets { + val commonMain by getting { + dependencies { + implementation(kotlin("stdlib-common")) + + api(libs.kotlinx.datetime) + } + } + val commonTest by getting { + dependencies { + implementation(kotlin("test-common")) + implementation(kotlin("test-annotations-common")) + } + } + } +} diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/Constants.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/Constants.kt new file mode 100644 index 0000000..828e00f --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/Constants.kt @@ -0,0 +1,7 @@ +package ru.otus.otuskotlin.marketplace.common + +import kotlinx.datetime.Instant + +private val INSTANT_NONE = Instant.fromEpochMilliseconds(Long.MIN_VALUE) +val Instant.Companion.NONE + get() = INSTANT_NONE diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/MkplContext.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/MkplContext.kt new file mode 100644 index 0000000..7e9dcf7 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/MkplContext.kt @@ -0,0 +1,23 @@ +package ru.otus.otuskotlin.marketplace.common + +import kotlinx.datetime.Instant +import ru.otus.otuskotlin.marketplace.common.models.* +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs + +data class MkplContext( + var command: MkplCommand = MkplCommand.NONE, + var state: MkplState = MkplState.NONE, + val errors: MutableList = mutableListOf(), + + var workMode: MkplWorkMode = MkplWorkMode.PROD, + var stubCase: MkplStubs = MkplStubs.NONE, + + var requestId: MkplRequestId = MkplRequestId.NONE, + var timeStart: Instant = Instant.NONE, + var adRequest: MkplAd = MkplAd(), + var adFilterRequest: MkplAdFilter = MkplAdFilter(), + + var adResponse: MkplAd = MkplAd(), + var adsResponse: MutableList = mutableListOf(), + + ) diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/exceptions/UnknownMkplCommand.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/exceptions/UnknownMkplCommand.kt new file mode 100644 index 0000000..46081c2 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/exceptions/UnknownMkplCommand.kt @@ -0,0 +1,6 @@ +package ru.otus.otuskotlin.marketplace.common.exceptions + +import ru.otus.otuskotlin.marketplace.common.models.MkplCommand + + +class UnknownMkplCommand(command: MkplCommand) : Throwable("Wrong command $command at mapping toTransport stage") diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAd.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAd.kt new file mode 100644 index 0000000..995fe45 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAd.kt @@ -0,0 +1,20 @@ +package ru.otus.otuskotlin.marketplace.common.models + +data class MkplAd( + var id: MkplAdId = MkplAdId.NONE, + var title: String = "", + var description: String = "", + var ownerId: MkplUserId = MkplUserId.NONE, + var adType: MkplDealSide = MkplDealSide.NONE, + var visibility: MkplVisibility = MkplVisibility.NONE, + var productId: MkplProductId = MkplProductId.NONE, + var lock: MkplAdLock = MkplAdLock.NONE, + val permissionsClient: MutableSet = mutableSetOf() +) { + fun isEmpty() = this == NONE + + companion object { + private val NONE = MkplAd() + } + +} diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAdFilter.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAdFilter.kt new file mode 100644 index 0000000..b7352b4 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAdFilter.kt @@ -0,0 +1,7 @@ +package ru.otus.otuskotlin.marketplace.common.models + +data class MkplAdFilter( + var searchString: String = "", + var ownerId: MkplUserId = MkplUserId.NONE, + var dealSide: MkplDealSide = MkplDealSide.NONE, +) diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAdId.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAdId.kt new file mode 100644 index 0000000..66691d6 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAdId.kt @@ -0,0 +1,12 @@ +package ru.otus.otuskotlin.marketplace.common.models + +import kotlin.jvm.JvmInline + +@JvmInline +value class MkplAdId(private val id: String) { + fun asString() = id + + companion object { + val NONE = MkplAdId("") + } +} diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAdLock.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAdLock.kt new file mode 100644 index 0000000..6e3f8fa --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAdLock.kt @@ -0,0 +1,12 @@ +package ru.otus.otuskotlin.marketplace.common.models + +import kotlin.jvm.JvmInline + +@JvmInline +value class MkplAdLock(private val id: String) { + fun asString() = id + + companion object { + val NONE = MkplAdLock("") + } +} diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAdPermissionClient.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAdPermissionClient.kt new file mode 100644 index 0000000..b911e54 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplAdPermissionClient.kt @@ -0,0 +1,10 @@ +package ru.otus.otuskotlin.marketplace.common.models + +enum class MkplAdPermissionClient { + READ, + UPDATE, + DELETE, + MAKE_VISIBLE_PUBLIC, + MAKE_VISIBLE_GROUP, + MAKE_VISIBLE_OWNER, +} diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplCommand.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplCommand.kt new file mode 100644 index 0000000..a20254a --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplCommand.kt @@ -0,0 +1,11 @@ +package ru.otus.otuskotlin.marketplace.common.models + +enum class MkplCommand { + NONE, + CREATE, + READ, + UPDATE, + DELETE, + SEARCH, + OFFERS, +} diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplDealSide.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplDealSide.kt new file mode 100644 index 0000000..7940274 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplDealSide.kt @@ -0,0 +1,7 @@ +package ru.otus.otuskotlin.marketplace.common.models + +enum class MkplDealSide { + NONE, + DEMAND, + SUPPLY, +} diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplError.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplError.kt new file mode 100644 index 0000000..a153ec0 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplError.kt @@ -0,0 +1,9 @@ +package ru.otus.otuskotlin.marketplace.common.models + +data class MkplError( + val code: String = "", + val group: String = "", + val field: String = "", + val message: String = "", + val exception: Throwable? = null, +) diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplProductId.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplProductId.kt new file mode 100644 index 0000000..c11b1d0 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplProductId.kt @@ -0,0 +1,12 @@ +package ru.otus.otuskotlin.marketplace.common.models + +import kotlin.jvm.JvmInline + +@JvmInline +value class MkplProductId(private val id: String) { + fun asString() = id + + companion object { + val NONE = MkplProductId("") + } +} diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplRequestId.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplRequestId.kt new file mode 100644 index 0000000..5a7c613 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplRequestId.kt @@ -0,0 +1,12 @@ +package ru.otus.otuskotlin.marketplace.common.models + +import kotlin.jvm.JvmInline + +@JvmInline +value class MkplRequestId(private val id: String) { + fun asString() = id + + companion object { + val NONE = MkplRequestId("") + } +} diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplState.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplState.kt new file mode 100644 index 0000000..a14fa04 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplState.kt @@ -0,0 +1,8 @@ +package ru.otus.otuskotlin.marketplace.common.models + +enum class MkplState { + NONE, + RUNNING, + FAILING, + FINISHING, +} diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplUserId.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplUserId.kt new file mode 100644 index 0000000..214891d --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplUserId.kt @@ -0,0 +1,12 @@ +package ru.otus.otuskotlin.marketplace.common.models + +import kotlin.jvm.JvmInline + +@JvmInline +value class MkplUserId(private val id: String) { + fun asString() = id + + companion object { + val NONE = MkplUserId("") + } +} diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplVisibility.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplVisibility.kt new file mode 100644 index 0000000..d774d4f --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplVisibility.kt @@ -0,0 +1,8 @@ +package ru.otus.otuskotlin.marketplace.common.models + +enum class MkplVisibility { + NONE, + VISIBLE_TO_OWNER, + VISIBLE_TO_GROUP, + VISIBLE_PUBLIC, +} diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplWorkMode.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplWorkMode.kt new file mode 100644 index 0000000..3be586d --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/models/MkplWorkMode.kt @@ -0,0 +1,7 @@ +package ru.otus.otuskotlin.marketplace.common.models + +enum class MkplWorkMode { + PROD, + TEST, + STUB, +} diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/stubs/MkplStubs.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/stubs/MkplStubs.kt new file mode 100644 index 0000000..cef9895 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/stubs/MkplStubs.kt @@ -0,0 +1,14 @@ +package ru.otus.otuskotlin.marketplace.common.stubs + +enum class MkplStubs { + NONE, + SUCCESS, + NOT_FOUND, + BAD_ID, + BAD_TITLE, + BAD_DESCRIPTION, + BAD_VISIBILITY, + CANNOT_DELETE, + BAD_SEARCH_STRING, + DB_ERROR, +} diff --git a/ok-marketplace-be/settings.gradle.kts b/ok-marketplace-be/settings.gradle.kts index c347ff9..338621f 100644 --- a/ok-marketplace-be/settings.gradle.kts +++ b/ok-marketplace-be/settings.gradle.kts @@ -30,4 +30,8 @@ enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") //include(":ok-marketplace-tmp") include(":ok-marketplace-api-v1-jackson") +include(":ok-marketplace-api-v1-mappers") include(":ok-marketplace-api-v2-kmp") + +include(":ok-marketplace-common") +