Skip to content

Commit

Permalink
Select Credential, Credential Detail loading
Browse files Browse the repository at this point in the history
  • Loading branch information
SAUL committed Nov 21, 2024
1 parent cde6010 commit b1ca7d1
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 50 deletions.
17 changes: 12 additions & 5 deletions src/main/kotlin/core/DatabaseFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import org.flywaydb.core.Flyway
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.DatabaseConfig
import org.jetbrains.exposed.sql.Slf4jSqlDebugLogger

object DatabaseFactory {

Expand All @@ -24,12 +26,17 @@ object DatabaseFactory {
val dataSource = HikariDataSource(hikariConfig)

Flyway.configure()
.dataSource(dataSource)
.locations("classpath:db/migration")
.load()
.migrate()
.dataSource(dataSource)
.locations("classpath:db/migration")
.load()
.migrate()

val exposedDbConfig = DatabaseConfig {
sqlLogger = Slf4jSqlDebugLogger
keepLoadedReferencesOutOfTransaction = true
}

return Database.connect(dataSource)
return Database.connect(datasource = dataSource, databaseConfig = exposedDbConfig)
}

}
2 changes: 1 addition & 1 deletion src/main/kotlin/core/configs/DatabaseConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ data class DatabaseConfig(
val maximumPoolSize: Int,
val isAutoCommit: Boolean,
val isReadOnly: Boolean,
val transactionIsolation: String
val transactionIsolation: String,
)
9 changes: 9 additions & 0 deletions src/main/kotlin/core/models/UiModel.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package core.models

import androidx.compose.ui.graphics.Color
import repository.creditcard.CreditCard
import repository.password.Password
import java.util.UUID

enum class NotificationType(val color: Color) {
ERROR(Color.Red),
Expand All @@ -9,11 +12,17 @@ enum class NotificationType(val color: Color) {
}

data class CredentialDisplay(
val id: UUID,
val title: String,
val description: String,
val favorite: Boolean
)

data class SelectedCredential(
val password: Password?,
val creditCard: CreditCard?
)

enum class CredentialSort(val value: String) {
NAME("Name"),
FAVORITE("Favorite"),
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/repository/creditcard/CreditCardRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import core.models.Result
import core.models.criteria.CredentialSearchCriteria
import core.models.dto.CreditCardDto
import repository.creditcard.projection.CreditCardSummary
import java.util.*

interface CreditCardRepository {

suspend fun findById(id: UUID): Result<CreditCard>

suspend fun findSummaries(searchCriteria: CredentialSearchCriteria): Result<List<CreditCardSummary>>

suspend fun save(creditCardDto: CreditCardDto): Result<Boolean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import core.models.Result
import core.models.criteria.CredentialSearchCriteria
import core.models.dto.CreditCardDto
import kotlinx.coroutines.delay
import org.jetbrains.exposed.dao.load
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.Expression
import org.jetbrains.exposed.sql.SortOrder
Expand All @@ -16,12 +17,28 @@ import repository.creditcard.CreditCard
import repository.creditcard.CreditCardRepository
import repository.creditcard.CreditCardTable
import repository.creditcard.projection.CreditCardSummary
import java.util.*

class CreditCardRepositoryImpl(
private val db: Database,
private val logger: Logger
) : CreditCardRepository {

override suspend fun findById(id: UUID): Result<CreditCard> {
return try {
return transaction(db) {
CreditCard.findById(id)?.load(CreditCard::owner, CreditCard::user)?.apply {
owner
user
}

}?.let { Result.Success(it) } ?: Result.Error("Password not found")
} catch (e: Exception) {
logger.error(e.message, e)
Result.Error(DatabaseError.fromException(e).extractMessage())
}
}

override suspend fun findSummaries(searchCriteria: CredentialSearchCriteria): Result<List<CreditCardSummary>> {
delay(550)
return try {
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/repository/password/PasswordRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import core.models.Result
import core.models.criteria.CredentialSearchCriteria
import core.models.dto.PasswordDto
import repository.password.projection.PasswordSummary
import java.util.UUID

interface PasswordRepository {

suspend fun findById(id: UUID): Result<Password>

suspend fun findSummaries(searchCriteria: CredentialSearchCriteria): Result<List<PasswordSummary>>

suspend fun save(password: PasswordDto): Result<Boolean>
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/repository/password/impl/PasswordRepositoryImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ class PasswordRepositoryImpl(
private val logger: Logger
) : PasswordRepository {

override suspend fun findById(id: UUID): Result<Password> {
return try {
return transaction(db) {
Password.findById(id)
}?.let { Result.Success(it) } ?: Result.Error("Password not found")
} catch (e: Exception) {
logger.error(e.message, e)
Result.Error(DatabaseError.fromException(e).extractMessage())
}
}

override suspend fun findSummaries(searchCriteria: CredentialSearchCriteria): Result<List<PasswordSummary>> {
delay(550)
return try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import ui.components.UnderLineTextFiled
import viewmodel.SecVaultScreenModel

@Composable
fun PasswordCredentialForm() {
var userName by remember { mutableStateOf("") }
fun PasswordCredentialForm(screenModel: SecVaultScreenModel) {
val selectedCredential by screenModel.selectedCredential.collectAsState()

var userName by remember(selectedCredential) {
mutableStateOf(selectedCredential.password?.username ?: "")
}

var password by remember(selectedCredential) {
mutableStateOf(selectedCredential.password?.password ?: "")
}

var email by remember(selectedCredential) {
mutableStateOf(selectedCredential.password?.email ?: "")
}

var website by remember(selectedCredential) {
mutableStateOf(selectedCredential.password?.website ?: "")
}

Column() {
Row(modifier = Modifier.weight(1f)) {
Expand All @@ -23,8 +40,8 @@ fun PasswordCredentialForm() {

Row(modifier = Modifier.weight(1f)) {
UnderLineTextFiled(
field = userName,
onFieldChange = { userName = it },
field = password,
onFieldChange = { password = it },
label = "Password",
modifier = Modifier.fillMaxWidth(),
isPassword = true
Expand All @@ -33,17 +50,17 @@ fun PasswordCredentialForm() {

Row(modifier = Modifier.weight(1f)) {
UnderLineTextFiled(
field = userName,
onFieldChange = { userName = it },
field = email,
onFieldChange = { email = it },
label = "Email",
modifier = Modifier.fillMaxWidth()
)
}

Row(modifier = Modifier.weight(1f)) {
UnderLineTextFiled(
field = userName,
onFieldChange = { userName = it },
field = website,
onFieldChange = { website = it },
label = "Website",
modifier = Modifier.fillMaxWidth()
)
Expand All @@ -53,68 +70,96 @@ fun PasswordCredentialForm() {
}

@Composable
fun CreditCardCredentialForm() {
var userName by remember { mutableStateOf("") }
fun CreditCardCredentialForm(screenModel: SecVaultScreenModel) {
val selectedCredential by screenModel.selectedCredential.collectAsState()

var bankName by remember(selectedCredential) {
mutableStateOf(selectedCredential.creditCard?.name ?: "")
}

var cardOwner by remember(selectedCredential) {
mutableStateOf(selectedCredential.creditCard?.owner?.userName ?: "")
}

var cardNumber by remember(selectedCredential) {
mutableStateOf(selectedCredential.creditCard?.number ?: "")
}

var cvc by remember(selectedCredential) {
mutableStateOf(selectedCredential.creditCard?.cvc ?: "")
}

var pin by remember(selectedCredential) {
mutableStateOf(selectedCredential.creditCard?.pin ?: "")
}

var expiryDate by remember(selectedCredential) {
mutableStateOf(selectedCredential.creditCard?.expiryDate ?: "")
}

var notes by remember(selectedCredential) {
mutableStateOf(selectedCredential.creditCard?.notes ?: "")
}

Column() {
Row(modifier = Modifier.weight(1f)) {
UnderLineTextFiled(
field = userName,
onFieldChange = { userName = it },
field = bankName,
onFieldChange = { bankName = it },
label = "Bank Name",
modifier = Modifier.fillMaxWidth()
)
}

Row(modifier = Modifier.weight(1f)) {
UnderLineTextFiled(
field = userName,
onFieldChange = { userName = it },
field = cardOwner,
onFieldChange = { cardOwner = it },
label = "Card Owner",
modifier = Modifier.fillMaxWidth()
)
}

Row(modifier = Modifier.weight(1f)) {
UnderLineTextFiled(
field = userName,
onFieldChange = { userName = it },
field = cardNumber,
onFieldChange = { cardNumber = it },
label = "Card Number",
modifier = Modifier.fillMaxWidth()
)
}

Row(modifier = Modifier.weight(1f)) {
UnderLineTextFiled(
field = userName,
onFieldChange = { userName = it },
field = cvc.toString(),
onFieldChange = { cvc = it },
label = "CVC",
modifier = Modifier.fillMaxWidth()
)
}

Row(modifier = Modifier.weight(1f)) {
UnderLineTextFiled(
field = userName,
onFieldChange = { userName = it },
field = pin.toString(),
onFieldChange = { pin = it },
label = "Pin",
modifier = Modifier.fillMaxWidth()
)
}

Row(modifier = Modifier.weight(1f)) {
UnderLineTextFiled(
field = userName,
onFieldChange = { userName = it },
field = expiryDate,
onFieldChange = { expiryDate = it },
label = "Expiry Date",
modifier = Modifier.fillMaxWidth()
)
}

Row(modifier = Modifier.weight(1f)) {
UnderLineTextFiled(
field = userName,
onFieldChange = { userName = it },
field = notes,
onFieldChange = { notes = it },
label = "Notes",
modifier = Modifier.fillMaxWidth(),
singleLine = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ fun PasswordForm(screenModel: SecVaultScreenModel) {
Row(modifier = Modifier.weight(9f)) {
when (menuItem.value) {
DefaultMenuItem.PASSWORDS -> {
PasswordCredentialForm()
PasswordCredentialForm(screenModel)
}

DefaultMenuItem.CREDIT_CARD -> {
CreditCardCredentialForm()
CreditCardCredentialForm(screenModel)
}

DefaultMenuItem.NOTES -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import core.models.CredentialDisplay
import repository.password.projection.PasswordSummary
import ui.theme.Font
import ui.theme.PasswordColors
import java.util.*

@Composable
fun PasswordItem(credentialDisplay: CredentialDisplay) {
fun PasswordItem(credentialDisplay: CredentialDisplay, onClick: (id: UUID) -> Unit = {}) {
val interactionSource = remember { MutableInteractionSource() }
val isHovered by interactionSource.collectIsHoveredAsState()

Expand All @@ -37,7 +37,7 @@ fun PasswordItem(credentialDisplay: CredentialDisplay) {
shape = RoundedCornerShape(6.dp)
)
.padding(PaddingValues(start = 5.dp, end = 5.dp))
.clickable(onClick = {}, indication = null, interactionSource = interactionSource)
.clickable(onClick = { onClick(credentialDisplay.id) }, indication = null, interactionSource = interactionSource)
.hoverable(interactionSource),
verticalAlignment = Alignment.CenterVertically
)
Expand Down
Loading

0 comments on commit b1ca7d1

Please sign in to comment.