-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
SAUL
committed
Nov 20, 2024
1 parent
58f567e
commit 7f95027
Showing
21 changed files
with
440 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/core/models/criteria/CredentialSearchCriteria.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package core.models.criteria | ||
|
||
import core.models.CredentialSort | ||
import java.util.* | ||
|
||
data class CredentialSearchCriteria( | ||
val userId: UUID?, | ||
val sort: CredentialSort | ||
) |
9 changes: 0 additions & 9 deletions
9
src/main/kotlin/core/models/criteria/PasswordSearchCriteria.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package core.models.dto | ||
|
||
import repository.user.User | ||
|
||
data class CreditCardDto( | ||
var user: User, | ||
var name: String, | ||
var owner: User, | ||
var number: String, | ||
var cvc: Int?, | ||
var pin: Int?, | ||
var expiryDate: String, | ||
var notes: String? | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/kotlin/repository/creditcard/CreditCardExposedEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package repository.creditcard | ||
|
||
import org.jetbrains.exposed.dao.UUIDEntity | ||
import org.jetbrains.exposed.dao.UUIDEntityClass | ||
import org.jetbrains.exposed.dao.id.EntityID | ||
import repository.AuditableTable | ||
import repository.user.User | ||
import repository.user.UsersTable | ||
import java.util.* | ||
|
||
object CreditCardTable : AuditableTable(name = "credit_cards") { | ||
val user = reference("user_id", UsersTable) | ||
val name = varchar("name", length = 255) | ||
val owner = reference("owner_id", UsersTable) | ||
val number = varchar("number", length = 255) | ||
val cvc = integer("cvc").nullable() | ||
val pin = integer("pin").nullable() | ||
val expiryDate = varchar("expiry_date", length = 5) | ||
val notes = text("notes").nullable() | ||
val favorite = bool("favorite").default(false) | ||
val deleted = bool("deleted").default(false) | ||
} | ||
|
||
class CreditCard(id: EntityID<UUID>) : UUIDEntity(id) { | ||
companion object : UUIDEntityClass<CreditCard>(CreditCardTable) | ||
|
||
var user by User referencedOn CreditCardTable.user | ||
var name by CreditCardTable.name | ||
var owner by User referencedOn CreditCardTable.owner | ||
var number by CreditCardTable.number | ||
var cvc by CreditCardTable.cvc | ||
var pin by CreditCardTable.pin | ||
var expiryDate by CreditCardTable.expiryDate | ||
var notes by CreditCardTable.notes | ||
var favorite by CreditCardTable.favorite | ||
var deleted by CreditCardTable.deleted | ||
|
||
var creationDateTime by CreditCardTable.creationDateTime | ||
var createdBy by CreditCardTable.createdBy | ||
var lastUpdateDateTime by CreditCardTable.lastUpdateDateTime | ||
var lastUpdatedBy by CreditCardTable.lastUpdatedBy | ||
var version by CreditCardTable.version | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/repository/creditcard/CreditCardRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package repository.creditcard | ||
|
||
import core.models.Result | ||
import core.models.criteria.CredentialSearchCriteria | ||
import core.models.dto.CreditCardDto | ||
import repository.creditcard.projection.CreditCardSummary | ||
|
||
interface CreditCardRepository { | ||
|
||
suspend fun findSummaries(searchCriteria: CredentialSearchCriteria): Result<List<CreditCardSummary>> | ||
|
||
suspend fun save(creditCardDto: CreditCardDto): Result<Boolean> | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
src/main/kotlin/repository/creditcard/impl/CreditCardRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package repository.creditcard.impl | ||
|
||
import core.models.CredentialSort | ||
import core.models.Result | ||
import core.models.criteria.CredentialSearchCriteria | ||
import core.models.dto.CreditCardDto | ||
import kotlinx.coroutines.delay | ||
import org.jetbrains.exposed.sql.Database | ||
import org.jetbrains.exposed.sql.Expression | ||
import org.jetbrains.exposed.sql.SortOrder | ||
import org.jetbrains.exposed.sql.andWhere | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import org.slf4j.Logger | ||
import repository.common.errors.DatabaseError | ||
import repository.creditcard.CreditCard | ||
import repository.creditcard.CreditCardRepository | ||
import repository.creditcard.CreditCardTable | ||
import repository.creditcard.projection.CreditCardSummary | ||
|
||
class CreditCardRepositoryImpl( | ||
private val db: Database, | ||
private val logger: Logger | ||
) : CreditCardRepository { | ||
|
||
override suspend fun findSummaries(searchCriteria: CredentialSearchCriteria): Result<List<CreditCardSummary>> { | ||
delay(550) | ||
return try { | ||
return transaction(db) { | ||
val query = CreditCardTable.select( | ||
listOf( | ||
CreditCardTable.id, | ||
CreditCardTable.name, | ||
CreditCardTable.number, | ||
CreditCardTable.favorite | ||
) | ||
) | ||
|
||
searchCriteria.userId?.let { | ||
query.andWhere { CreditCardTable.user eq it } | ||
} | ||
|
||
query.orderBy(toSort(searchCriteria.sort), toOrder(searchCriteria.sort)).map { resultRow -> | ||
CreditCardSummary( | ||
id = resultRow[CreditCardTable.id].value, | ||
name = resultRow[CreditCardTable.name], | ||
number = resultRow[CreditCardTable.number], | ||
favorite = resultRow[CreditCardTable.favorite] | ||
) | ||
} | ||
}.let { Result.Success(it) } | ||
} catch (e: Exception) { | ||
logger.error(e.message, e) | ||
Result.Error(DatabaseError.fromException(e).extractMessage()) | ||
} | ||
} | ||
|
||
override suspend fun save(creditCardDto: CreditCardDto): Result<Boolean> { | ||
return try { | ||
return transaction(db) { | ||
CreditCard.new { | ||
this.user = creditCardDto.user | ||
this.expiryDate = creditCardDto.expiryDate | ||
this.number = creditCardDto.number | ||
this.cvc = creditCardDto.cvc | ||
this.pin = creditCardDto.pin | ||
this.notes = creditCardDto.notes | ||
this.owner = creditCardDto.owner | ||
this.name = creditCardDto.name | ||
this.createdBy = creditCardDto.user.userName | ||
this.lastUpdatedBy = creditCardDto.user.userName | ||
} | ||
}.let { | ||
Result.Success(true) | ||
} | ||
} catch (e: Exception) { | ||
logger.error(e.message, e) | ||
Result.Error(DatabaseError.fromException(e).extractMessage()) | ||
} | ||
} | ||
|
||
private fun toSort(sort: CredentialSort): Expression<*> { | ||
return when (sort) { | ||
CredentialSort.NAME -> CreditCardTable.name | ||
CredentialSort.CREATED -> CreditCardTable.createdBy | ||
CredentialSort.FAVORITE -> CreditCardTable.favorite | ||
} | ||
} | ||
|
||
private fun toOrder(sort: CredentialSort): SortOrder { | ||
return when (sort) { | ||
CredentialSort.NAME -> SortOrder.ASC | ||
CredentialSort.CREATED -> SortOrder.ASC | ||
CredentialSort.FAVORITE -> SortOrder.DESC | ||
} | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/repository/creditcard/projection/CreditCardSummary.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package repository.creditcard.projection | ||
|
||
import java.util.* | ||
|
||
data class CreditCardSummary( | ||
val id: UUID, | ||
val name: String, | ||
val number: String, | ||
val favorite: Boolean | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.