From 630fe01faac92b13c0f2c05e8f38f4a0e972777d Mon Sep 17 00:00:00 2001 From: Harold Jose Date: Tue, 13 Jun 2023 16:57:26 +0100 Subject: [PATCH] #4 - US01 implemented --- .../AccountMongoDbDataApiDataSource.kt | 16 ++- .../FamilyListMongoDbDataApiDataSource.kt | 15 ++- .../mongoDb/IMongoDbDataApiDataSource.kt | 7 +- .../mongoDb/MongoDbDataApiDataSource.kt | 38 +++--- .../mongoDb/response/IMongoDbResponseDto.kt | 9 +- .../dataLayer/dto/AccountDto.kt | 5 +- .../familyList/FamilyListRepository.kt | 10 +- .../familyList/FamilyListRepository_Old.kt | 126 ------------------ .../domainLayer/extensions/AccountMapper.kt | 30 +++++ ...lyListDtoMapper.kt => FamilyListMapper.kt} | 9 ++ .../extensions/FamilyListModelMapper.kt | 13 -- .../domainLayer/models/AccountModel.kt | 26 ++++ .../account/CreateAnonymousAccountUseCase.kt | 18 +++ .../account/GetAccountByUuidUseCase.kt | 17 +++ .../usecases/uuid/GetOrCreateUuidUseCase.kt | 25 ++++ 15 files changed, 188 insertions(+), 176 deletions(-) delete mode 100644 shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/repositories/familyList/FamilyListRepository_Old.kt create mode 100644 shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/extensions/AccountMapper.kt rename shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/extensions/{FamilyListDtoMapper.kt => FamilyListMapper.kt} (66%) delete mode 100644 shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/extensions/FamilyListModelMapper.kt create mode 100644 shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/models/AccountModel.kt create mode 100644 shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/usecases/account/CreateAnonymousAccountUseCase.kt create mode 100644 shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/usecases/account/GetAccountByUuidUseCase.kt create mode 100644 shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/usecases/uuid/GetOrCreateUuidUseCase.kt diff --git a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/account/AccountMongoDbDataApiDataSource.kt b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/account/AccountMongoDbDataApiDataSource.kt index b7c8f40..79b9df7 100644 --- a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/account/AccountMongoDbDataApiDataSource.kt +++ b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/account/AccountMongoDbDataApiDataSource.kt @@ -1,11 +1,25 @@ package dev.haroldjose.familysharedlist.dataLayer.datasource.remote.account +import dev.haroldjose.familysharedlist.dataLayer.datasource.mongoDb.request.FilterByUuidDto +import dev.haroldjose.familysharedlist.dataLayer.datasource.mongoDb.request.MongoDbRequestDocumentDto +import dev.haroldjose.familysharedlist.dataLayer.datasource.mongoDb.request.MongoDbRequestFilterUpdateDto import dev.haroldjose.familysharedlist.dataLayer.datasource.remote.mongoDb.MongoDbDataApiDataSource import dev.haroldjose.familysharedlist.dataLayer.datasource.remote.mongoDb.resources.MongoDbResources +import dev.haroldjose.familysharedlist.dataLayer.datasource.remote.mongoDb.response.MongoDbFindAllResponseDto +import dev.haroldjose.familysharedlist.dataLayer.datasource.remote.mongoDb.response.MongoDbFindByUuidResponseDto import dev.haroldjose.familysharedlist.dataLayer.dto.AccountDto +import dev.haroldjose.familysharedlist.dataLayer.dto.FamilyListDto +import kotlinx.serialization.modules.SerializersModule +import kotlinx.serialization.modules.contextual internal class AccountMongoDbDataApiDataSource: MongoDbDataApiDataSource( dataSource = MongoDbResources.DataSource.CLUSTER0.value, database = MongoDbResources.Database.ACCOUNT.value, - collection = MongoDbResources.Collection.ACCOUNT.value + collection = MongoDbResources.Collection.ACCOUNT.value, + serializers = SerializersModule { + contextual(MongoDbFindAllResponseDto.serializer(AccountDto.serializer())) + contextual(MongoDbFindByUuidResponseDto.serializer(AccountDto.serializer())) + contextual(MongoDbRequestDocumentDto.serializer(AccountDto.serializer())) + contextual(MongoDbRequestFilterUpdateDto.serializer(FilterByUuidDto.serializer(), AccountDto.serializer())) + } ), IAccountMongoDbDataApiDataSource \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/familiList/FamilyListMongoDbDataApiDataSource.kt b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/familiList/FamilyListMongoDbDataApiDataSource.kt index d74bb7d..de1a036 100644 --- a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/familiList/FamilyListMongoDbDataApiDataSource.kt +++ b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/familiList/FamilyListMongoDbDataApiDataSource.kt @@ -1,12 +1,25 @@ package dev.haroldjose.familysharedlist.dataLayer.datasource.remote.familiList +import dev.haroldjose.familysharedlist.dataLayer.datasource.mongoDb.request.FilterByUuidDto +import dev.haroldjose.familysharedlist.dataLayer.datasource.mongoDb.request.MongoDbRequestDocumentDto +import dev.haroldjose.familysharedlist.dataLayer.datasource.mongoDb.request.MongoDbRequestFilterUpdateDto import dev.haroldjose.familysharedlist.dataLayer.datasource.remote.mongoDb.MongoDbDataApiDataSource import dev.haroldjose.familysharedlist.dataLayer.datasource.remote.mongoDb.resources.MongoDbResources +import dev.haroldjose.familysharedlist.dataLayer.datasource.remote.mongoDb.response.MongoDbFindAllResponseDto +import dev.haroldjose.familysharedlist.dataLayer.datasource.remote.mongoDb.response.MongoDbFindByUuidResponseDto import dev.haroldjose.familysharedlist.dataLayer.dto.FamilyListDto +import kotlinx.serialization.modules.SerializersModule +import kotlinx.serialization.modules.contextual internal class FamilyListMongoDbDataApiDataSource: MongoDbDataApiDataSource( dataSource = MongoDbResources.DataSource.CLUSTER0.value, database = MongoDbResources.Database.FAMILYSHAREDLIST.value, - collection = MongoDbResources.Collection.SHOPPINGLIST.value + collection = MongoDbResources.Collection.SHOPPINGLIST.value, + serializers = SerializersModule { + contextual(MongoDbFindAllResponseDto.serializer(FamilyListDto.serializer())) + contextual(MongoDbFindByUuidResponseDto.serializer(FamilyListDto.serializer())) + contextual(MongoDbRequestDocumentDto.serializer(FamilyListDto.serializer())) + contextual(MongoDbRequestFilterUpdateDto.serializer(FilterByUuidDto.serializer(), FamilyListDto.serializer())) + } ), IFamilyListMongoDbDataApiDataSource diff --git a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/mongoDb/IMongoDbDataApiDataSource.kt b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/mongoDb/IMongoDbDataApiDataSource.kt index 42cf288..a89f41a 100644 --- a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/mongoDb/IMongoDbDataApiDataSource.kt +++ b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/mongoDb/IMongoDbDataApiDataSource.kt @@ -1,17 +1,18 @@ package dev.haroldjose.familysharedlist.dataLayer.datasource.remote.mongoDb import dev.haroldjose.familysharedlist.dataLayer.datasource.mongoDb.IMongoDbBaseDto -import io.ktor.client.statement.HttpResponse +import kotlinx.serialization.modules.SerializersModule interface IMongoDbDataApiDataSource { val dataSource: String val database: String val collection: String + val serializers: SerializersModule suspend fun insert(item: T) - suspend fun findAll(): HttpResponse - suspend fun findBy(uuid: String): HttpResponse + suspend fun findAll(): List + suspend fun findBy(uuid: String): T? suspend fun update(item: T) suspend fun delete(uuid: String) } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/mongoDb/MongoDbDataApiDataSource.kt b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/mongoDb/MongoDbDataApiDataSource.kt index e2a4cfe..70586bf 100644 --- a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/mongoDb/MongoDbDataApiDataSource.kt +++ b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/mongoDb/MongoDbDataApiDataSource.kt @@ -7,7 +7,10 @@ import dev.haroldjose.familysharedlist.dataLayer.datasource.mongoDb.request.Mong import dev.haroldjose.familysharedlist.dataLayer.datasource.mongoDb.request.MongoDbRequestDto import dev.haroldjose.familysharedlist.dataLayer.datasource.mongoDb.request.MongoDbRequestFilterDto import dev.haroldjose.familysharedlist.dataLayer.datasource.mongoDb.request.MongoDbRequestFilterUpdateDto -import dev.haroldjose.familysharedlist.dataLayer.datasource.remote.mongoDb.response.MongoDbResponseDto +import dev.haroldjose.familysharedlist.dataLayer.datasource.remote.mongoDb.response.MongoDbFindAllResponseDto +import dev.haroldjose.familysharedlist.dataLayer.datasource.remote.mongoDb.response.MongoDbFindByUuidResponseDto +import dev.haroldjose.familysharedlist.dataLayer.dto.AccountDto +import dev.haroldjose.familysharedlist.dataLayer.dto.FamilyListDto import io.ktor.client.HttpClient import io.ktor.client.call.body import io.ktor.client.plugins.contentnegotiation.ContentNegotiation @@ -20,12 +23,14 @@ import io.ktor.http.ContentType import io.ktor.http.contentType import io.ktor.serialization.kotlinx.json.json import kotlinx.serialization.json.Json +import kotlinx.serialization.modules.SerializersModule +import kotlinx.serialization.modules.contextual open class MongoDbDataApiDataSource( override val dataSource: String, override val database: String, override val collection: String, -) : IMongoDbDataApiDataSource { + override val serializers: SerializersModule) : IMongoDbDataApiDataSource { //TODO: handle Error in all request private val apiUrl = BuildKonfig.apiUrl @@ -51,6 +56,7 @@ open class MongoDbDataApiDataSource( json(Json { encodeDefaults = true ignoreUnknownKeys = true + serializersModule = serializers }) } } @@ -76,23 +82,21 @@ open class MongoDbDataApiDataSource( } } - override suspend fun findAll(): HttpResponse { + override suspend fun findAll(): List { - val httpResponse = client.post(Resources.FIND_ONE.value) { + val httpResponse = client.post(Resources.FIND.value) { setBody(getDefaultRequestDto()) } - //TODO: serialize here with generics or inject an serializer function to return List - //This code does not work if (httpResponse.status.value in 200..299) { - //val mongoDbResponseDto = httpResponse.body>() - //val test = mongoDbResponseDto.documents + val mongoDbFindAllResponseDto = httpResponse.body>() + return mongoDbFindAllResponseDto.documents } - return httpResponse + return arrayListOf() } - override suspend fun findBy(uuid: String): HttpResponse { + override suspend fun findBy(uuid: String): T? { val bodyRequest = MongoDbRequestFilterDto( default = getDefaultRequestDto(), @@ -101,18 +105,16 @@ open class MongoDbDataApiDataSource( ) ) - val httpResponse = client.post(Resources.FIND.value) { + val httpResponse = client.post(Resources.FIND_ONE.value) { setBody(bodyRequest) } - //TODO: serialize here with generics or inject an serializer function to return List - //This code does not work - //if (httpResponse.status.value in 200..299) { - // val mongoDbResponseDto = httpResponse.body() - // return mongoDbResponseDto.documents - //} + if (httpResponse.status.value in 200..299) { + val mongoDbFindAllResponseDto = httpResponse.body>() + return mongoDbFindAllResponseDto.document + } - return httpResponse + return null } override suspend fun update(item: T) { diff --git a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/mongoDb/response/IMongoDbResponseDto.kt b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/mongoDb/response/IMongoDbResponseDto.kt index a8c157f..0ec483f 100644 --- a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/mongoDb/response/IMongoDbResponseDto.kt +++ b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/datasource/remote/mongoDb/response/IMongoDbResponseDto.kt @@ -3,6 +3,11 @@ package dev.haroldjose.familysharedlist.dataLayer.datasource.remote.mongoDb.resp import kotlinx.serialization.Serializable @Serializable -data class MongoDbResponseDto( - val documents: T +data class MongoDbFindAllResponseDto( + val documents: List +) + +@Serializable +data class MongoDbFindByUuidResponseDto( + val document: T ) \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/dto/AccountDto.kt b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/dto/AccountDto.kt index 8e74ef8..9390243 100644 --- a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/dto/AccountDto.kt +++ b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/dto/AccountDto.kt @@ -7,12 +7,11 @@ import kotlinx.serialization.Serializable @Serializable data class AccountDto( - override val uuid: String, + override val uuid: String = "", val createdDate: Instant = Clock.System.now(), val updatedDate: Instant = Clock.System.now(), - val name: String = "Guest", + val name: String = "", val email: String = "", - val password: String = "", val myAccountIsSharedWith: List = arrayListOf(), var accountsSharedWithMe: List = arrayListOf(), ) : IMongoDbBaseDto \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/repositories/familyList/FamilyListRepository.kt b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/repositories/familyList/FamilyListRepository.kt index e4ca0ec..7d795cf 100644 --- a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/repositories/familyList/FamilyListRepository.kt +++ b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/repositories/familyList/FamilyListRepository.kt @@ -14,15 +14,7 @@ internal class FamilyListRepository() : IFamilyListRepository { } override suspend fun findAll(): List { - - val httpResponse = remoteDataSource.findAll() - - //if (httpResponse.status.value in 200..299) { - // val responseDto = httpResponse.body() - // return responseDto.documents - //} - - return arrayListOf() + return remoteDataSource.findAll() } override suspend fun update(item: FamilyListDto) { diff --git a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/repositories/familyList/FamilyListRepository_Old.kt b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/repositories/familyList/FamilyListRepository_Old.kt deleted file mode 100644 index 28e1117..0000000 --- a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/dataLayer/repositories/familyList/FamilyListRepository_Old.kt +++ /dev/null @@ -1,126 +0,0 @@ -package dev.haroldjose.familysharedlist.dataLayer.repositories.familyList -/* -import dev.haroldjose.familysharedlist.BuildKonfig -import dev.haroldjose.familysharedlist.dataLayer.datasource.mongoDb.request.FilterByUuidDto -import dev.haroldjose.familysharedlist.dataLayer.datasource.mongoDb.request.MongoDbRequestDocumentDto -import dev.haroldjose.familysharedlist.dataLayer.datasource.mongoDb.request.MongoDbRequestDto -import dev.haroldjose.familysharedlist.dataLayer.datasource.mongoDb.request.MongoDbRequestFilterDto -import dev.haroldjose.familysharedlist.dataLayer.datasource.mongoDb.request.MongoDbRequestFilterUpdateDto -import dev.haroldjose.familysharedlist.dataLayer.datasource.remote.mongoDb.response.MongoDbResponseDto -import dev.haroldjose.familysharedlist.dataLayer.dto.FamilyListDto -import io.ktor.client.HttpClient -import io.ktor.client.call.body -import io.ktor.client.plugins.contentnegotiation.ContentNegotiation -import io.ktor.client.plugins.defaultRequest -import io.ktor.client.request.headers -import io.ktor.client.request.post -import io.ktor.client.request.setBody -import io.ktor.http.ContentType -import io.ktor.http.contentType -import io.ktor.serialization.kotlinx.json.json -import kotlinx.serialization.json.Json - - -internal class FamilyListRepository_Old: IFamilyListRepository { - - //TODO: handle Error in all request - private val apiUrl = BuildKonfig.apiUrl - private val apiKey = BuildKonfig.apiKey - private enum class Resources(val value: String) { - FIND("find"), - INSERT_ONE("insertOne"), - UPDATE_ONE("updateOne"), - DELETE_ONE("deleteOne") - } - - //TODO: implement DI - private val client = HttpClient() { - defaultRequest { - url(apiUrl) - contentType(ContentType.Application.Json) - headers { - append("api-key", apiKey) - } - } - install(ContentNegotiation) { - json(Json { - encodeDefaults = true - ignoreUnknownKeys = true - }) - } - } - - private fun getDefaultRequestDto(): MongoDbRequestDto { - //TODO: config build config to PROD, DEV and QA - var enviroment = "PROD" - var collection = "shoppingList" - var database = "FamilySharedListDB_$enviroment" - val dataSource = "Cluster0" - //FIXME: harold - //if (isDebug) { - // collection = "devCollection" - // database = "devDatabase" - //} - return MongoDbRequestDto( - collection = collection, - database = database, - dataSource = dataSource - ) - } - - override suspend fun insert(item: FamilyListDto) { - - val bodyRequest = MongoDbRequestDocumentDto( - default = getDefaultRequestDto(), - document = item - ) - - client.post(Resources.INSERT_ONE.value) { - setBody(bodyRequest) - } - } - - override suspend fun findAll(): List { - - val httpResponse = client.post(Resources.FIND.value) { - setBody(getDefaultRequestDto()) - } - - if (httpResponse.status.value in 200..299) { - val mongoDbResponseDto = httpResponse.body>>() - return mongoDbResponseDto.documents - } - - return arrayListOf() - } - - override suspend fun update(item: FamilyListDto) { - - val bodyRequest = MongoDbRequestFilterUpdateDto( - default = getDefaultRequestDto(), - filter = FilterByUuidDto( - uuid = item.uuid - ), - update = item - ) - - client.post(Resources.UPDATE_ONE.value) { - setBody(bodyRequest) - } - } - - override suspend fun delete(uuid: String) { - - val bodyRequest = MongoDbRequestFilterDto( - default = getDefaultRequestDto(), - filter = FilterByUuidDto( - uuid = uuid - ) - ) - - client.post(Resources.DELETE_ONE.value) { - setBody(bodyRequest) - } - } -} - */ \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/extensions/AccountMapper.kt b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/extensions/AccountMapper.kt new file mode 100644 index 0000000..345314f --- /dev/null +++ b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/extensions/AccountMapper.kt @@ -0,0 +1,30 @@ +package dev.haroldjose.familysharedlist.domainLayer.extensions + + +import dev.haroldjose.familysharedlist.dataLayer.dto.AccountDto +import dev.haroldjose.familysharedlist.domainLayer.models.AccountModel + +internal fun AccountDto.toModel(): AccountModel { + return AccountModel( + uuid = this.uuid, + createdDate = this.createdDate, + updatedDate = this.updatedDate, + name = this.name, + email = this.email, + myAccountIsSharedWith = this.myAccountIsSharedWith, + accountsSharedWithMe = this.accountsSharedWithMe + ) +} + +internal fun AccountModel.toDto(): AccountDto { + return AccountDto( + uuid = this.uuid, + createdDate = this.createdDate, + updatedDate = this.updatedDate, + name = this.name, + email = this.email, + myAccountIsSharedWith = this.myAccountIsSharedWith, + accountsSharedWithMe = this.accountsSharedWithMe + ) +} + diff --git a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/extensions/FamilyListDtoMapper.kt b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/extensions/FamilyListMapper.kt similarity index 66% rename from shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/extensions/FamilyListDtoMapper.kt rename to shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/extensions/FamilyListMapper.kt index f3ab7a6..f393f90 100644 --- a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/extensions/FamilyListDtoMapper.kt +++ b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/extensions/FamilyListMapper.kt @@ -10,4 +10,13 @@ internal fun FamilyListDto.toModel(): FamilyListModel { isCompleted = this.isCompleted, quantity = this.quantity ) +} + +internal fun FamilyListModel.toDto(): FamilyListDto { + return FamilyListDto( + uuid = this.uuid, + name = this.name, + isCompleted = this.isCompleted, + quantity = this.quantity + ) } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/extensions/FamilyListModelMapper.kt b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/extensions/FamilyListModelMapper.kt deleted file mode 100644 index b0dadb1..0000000 --- a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/extensions/FamilyListModelMapper.kt +++ /dev/null @@ -1,13 +0,0 @@ -package dev.haroldjose.familysharedlist.domainLayer.extensions - -import dev.haroldjose.familysharedlist.domainLayer.models.FamilyListModel -import dev.haroldjose.familysharedlist.dataLayer.dto.FamilyListDto - -internal fun FamilyListModel.toDto(): FamilyListDto { - return FamilyListDto( - uuid = this.uuid, - name = this.name, - isCompleted = this.isCompleted, - quantity = this.quantity - ) -} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/models/AccountModel.kt b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/models/AccountModel.kt new file mode 100644 index 0000000..18eea14 --- /dev/null +++ b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/models/AccountModel.kt @@ -0,0 +1,26 @@ +package dev.haroldjose.familysharedlist.domainLayer.models + +import generateUUID +import kotlinx.datetime.Clock +import kotlinx.datetime.Instant + +data class AccountModel( + var uuid: String, + var createdDate: Instant = Clock.System.now(), + var updatedDate: Instant = Clock.System.now(), + var name: String = "", + var email: String = "", + var myAccountIsSharedWith: List = arrayListOf(), + var accountsSharedWithMe: List = arrayListOf(), +) { + init { + this.uuid = if (uuid.isEmpty()) generateUUID() else uuid + this.name = name + this.createdDate = createdDate + this.updatedDate = updatedDate + this.name = name + this.email = email + this.myAccountIsSharedWith = myAccountIsSharedWith + this.accountsSharedWithMe = accountsSharedWithMe + } +} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/usecases/account/CreateAnonymousAccountUseCase.kt b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/usecases/account/CreateAnonymousAccountUseCase.kt new file mode 100644 index 0000000..590fd65 --- /dev/null +++ b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/usecases/account/CreateAnonymousAccountUseCase.kt @@ -0,0 +1,18 @@ +package dev.haroldjose.familysharedlist.domainLayer.usecases.account + +import com.rickclephas.kmp.nativecoroutines.NativeCoroutines +import dev.haroldjose.familysharedlist.dataLayer.repositories.account.IAccountRepository +import dev.haroldjose.familysharedlist.domainLayer.extensions.toDto +import dev.haroldjose.familysharedlist.domainLayer.models.AccountModel + +class CreateAnonymousAccountUseCase( + private val accountRepository: IAccountRepository +) { + + @NativeCoroutines + suspend fun execute(uuid: String) { + + val accountModel = AccountModel(uuid = uuid) + return accountRepository.insert(accountModel.toDto()) + } +} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/usecases/account/GetAccountByUuidUseCase.kt b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/usecases/account/GetAccountByUuidUseCase.kt new file mode 100644 index 0000000..c206e5b --- /dev/null +++ b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/usecases/account/GetAccountByUuidUseCase.kt @@ -0,0 +1,17 @@ +package dev.haroldjose.familysharedlist.domainLayer.usecases.account + +import com.rickclephas.kmp.nativecoroutines.NativeCoroutines +import dev.haroldjose.familysharedlist.dataLayer.repositories.account.IAccountRepository +import dev.haroldjose.familysharedlist.domainLayer.extensions.toModel +import dev.haroldjose.familysharedlist.domainLayer.models.AccountModel + +class GetAccountByUuidUseCase( + private val accountRepository: IAccountRepository +) { + + @NativeCoroutines + suspend fun execute(uuid: String): AccountModel? { + + return accountRepository.findBy(uuid = uuid)?.toModel() + } +} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/usecases/uuid/GetOrCreateUuidUseCase.kt b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/usecases/uuid/GetOrCreateUuidUseCase.kt new file mode 100644 index 0000000..6168a89 --- /dev/null +++ b/shared/src/commonMain/kotlin/dev/haroldjose/familysharedlist/domainLayer/usecases/uuid/GetOrCreateUuidUseCase.kt @@ -0,0 +1,25 @@ +package dev.haroldjose.familysharedlist.domainLayer.usecases.uuid + +import com.rickclephas.kmp.nativecoroutines.NativeCoroutines +import dev.haroldjose.familysharedlist.dataLayer.repositories.account.IAccountRepository +import dev.haroldjose.familysharedlist.domainLayer.extensions.toDto +import dev.haroldjose.familysharedlist.domainLayer.models.AccountModel + +class GetOrCreateUuidUseCase( + private val accountRepository: IAccountRepository +) { + + @NativeCoroutines + suspend fun execute(): String { + + //TODO: try to get local UUID + + //TODO: Generate new UUID for this device + + //TODO: Save UUID on localSimpleStorage + + //TODO: return the UUID + + return "" + } +} \ No newline at end of file