Skip to content

Commit

Permalink
This diff introduces several improvements to the Kotlin code, primari…
Browse files Browse the repository at this point in the history
…ly focused on SQL query handling and test cleanup. Let's break down the changes:

**1. Removal of Redundant Test Code:**

The deleted lines in `ServiceTests.kt` suggest a redundant test case for signup functionality. The removed code was likely a duplicate of the existing parameterized test, which already covers the same scenario. This simplification reduces code duplication and improves maintainability.

**2. Consistent use of `trimIndent`:**

The most significant change is the consistent application of `.trimIndent()` to SQL queries across `UserActivationDao.kt`, `UserDao.kt`.  `trimIndent()` removes leading whitespace from each line of a multiline string, making the SQL queries cleaner, more readable, and less prone to errors caused by unexpected whitespace. This also improves the presentation of the queries in logs and debugging output.  Crucially, this prevents SQL syntax errors when previously leading whitespace may have been unintentionally included in the executed query string.

**3. Improved SQL Query Formatting in `UserDao.kt`:**

The `INSERT` statement in `UserDao.kt` has been reformatted for better readability. While functionally equivalent to the previous version, the new formatting aligns the values with their corresponding fields, enhancing clarity.

**4. `UserActivationDao.kt` - Updating Activation Status:**

A new SQL query `UPDATE_ACTIVATION_BY_KEY` has been added to `UserActivationDao.kt`. This query allows updating the activation status and timestamp directly in the database when a user activates their account.  This likely replaces a less efficient approach, potentially involving retrieving the activation record, modifying it, and then saving it back. This change streamlines the activation process.

**5. Summary:**

These changes collectively improve the code's readability, maintainability, and efficiency.  The consistent use of `trimIndent()` is a significant improvement, minimizing the risk of SQL errors due to whitespace, and improving the readability of SQL queries within the code. The addition of `UPDATE_ACTIVATION_BY_KEY` optimizes the user activation workflow.  Removing the redundant test code streamlines the test suite.
  • Loading branch information
cheroliv committed Dec 10, 2024
1 parent 5630710 commit d1a5755
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
27 changes: 20 additions & 7 deletions api/src/main/kotlin/users/UserDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,16 @@ object UserDao {

@Suppress("SqlDialectInspection")
const val INSERT = """
insert into "$TABLE_NAME" (
"$LOGIN_FIELD", "$EMAIL_FIELD",
"$PASSWORD_FIELD", $LANG_KEY_FIELD,
$VERSION_FIELD
) values ( :$LOGIN_ATTR, :$EMAIL_ATTR, :$PASSWORD_ATTR, :$LANG_KEY_ATTR, :$VERSION_ATTR);"""
insert into "$TABLE_NAME" (
"$LOGIN_FIELD", "$EMAIL_FIELD",
"$PASSWORD_FIELD", $LANG_KEY_FIELD,
$VERSION_FIELD
) values (
:$LOGIN_ATTR,
:$EMAIL_ATTR,
:$PASSWORD_ATTR,
:$LANG_KEY_ATTR,
:$VERSION_ATTR);"""

const val FIND_USER_BY_LOGIN = """
SELECT u.$ID_FIELD
Expand Down Expand Up @@ -206,13 +211,14 @@ object UserDao {
RoleDao.Relations.SQL_SCRIPT,
UserRoleDao.Relations.SQL_SCRIPT,
UserActivationDao.Relations.SQL_SCRIPT,
).joinToString("")
).joinToString("", transform = String::trimIndent)
.trimMargin()

}

object Dao {
suspend fun ApplicationContext.countUsers(): Int = COUNT
.trimIndent()
.let(getBean<DatabaseClient>()::sql)
.fetch()
.awaitSingle()
Expand All @@ -224,6 +230,7 @@ object UserDao {
@Throws(EmptyResultDataAccessException::class)
suspend fun Pair<User, ApplicationContext>.save(): Either<Throwable, UUID> = try {
INSERT
.trimIndent()
.run(second.getBean<R2dbcEntityTemplate>().databaseClient::sql)
.bind(LOGIN_ATTR, first.login)
.bind(EMAIL_ATTR, first.email)
Expand Down Expand Up @@ -255,6 +262,7 @@ object UserDao {
): Either<Throwable, User> = when (T::class) {
User::class -> try {
FIND_USER_BY_LOGIN_OR_EMAIL
.trimIndent()
.run(getBean<DatabaseClient>()::sql)
.bind(EMAIL_ATTR, emailOrLogin)
.bind(LOGIN_ATTR, emailOrLogin)
Expand Down Expand Up @@ -286,7 +294,8 @@ object UserDao {
id: UUID
): Either<Throwable, User> = when (T::class) {
User::class -> try {
FIND_USER_BY_ID.trimIndent()
FIND_USER_BY_ID
.trimIndent()
.run(getBean<DatabaseClient>()::sql)
.bind(EMAIL_ATTR, id)
.bind(LOGIN_ATTR, id)
Expand Down Expand Up @@ -325,6 +334,7 @@ object UserDao {
.left()

FIND_USER_WITH_AUTHS_BY_EMAILOGIN
.trimIndent()
.run(getBean<DatabaseClient>()::sql)
.bind(EMAILORLOGIN, emailOrLogin)
.fetch()
Expand Down Expand Up @@ -363,6 +373,7 @@ object UserDao {
User::class -> {
try {
FIND_USER_BY_LOGIN
.trimIndent()
.run(getBean<DatabaseClient>()::sql)
.bind(LOGIN_ATTR, login)
.fetch()
Expand All @@ -382,6 +393,7 @@ object UserDao {
User::class -> {
try {
FIND_USER_BY_EMAIL
.trimIndent()
.run(getBean<DatabaseClient>()::sql)
.bind(EMAIL_ATTR, email)
.fetch()
Expand Down Expand Up @@ -428,6 +440,7 @@ object UserDao {
suspend fun Pair<Signup, ApplicationContext>.signupAvailability()
: Either<Throwable, Triple<Boolean/*OK*/, Boolean/*email*/, Boolean/*login*/>> = try {
SELECT_SIGNUP_AVAILABILITY
.trimIndent()
.run(second.getBean<R2dbcEntityTemplate>().databaseClient::sql)
.bind(LOGIN_ATTR, first.login)
.bind(EMAIL_ATTR, first.email)
Expand Down
12 changes: 11 additions & 1 deletion api/src/main/kotlin/users/signup/UserActivationDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,31 @@ object UserActivationDao {
CREATE INDEX IF NOT EXISTS idx_user_activation_creation_date
ON $TABLE_NAME ($CREATED_DATE_FIELD);
"""
const val COUNT = "SELECT COUNT(*) FROM $TABLE_NAME;"

const val INSERT = """
INSERT INTO $TABLE_NAME (
$ID_FIELD, $ACTIVATION_KEY_FIELD, $CREATED_DATE_FIELD, $ACTIVATION_DATE_FIELD)
VALUES (:$ID_ATTR, :$ACTIVATION_KEY_ATTR, :$CREATED_DATE_ATTR, :$ACTIVATION_DATE_ATTR);
"""

const val FIND_BY_ACTIVATION_KEY = """
SELECT * FROM "$TABLE_NAME" as ua
WHERE ua."$ACTIVATION_KEY_FIELD" = :$ACTIVATION_KEY_ATTR;
"""
const val COUNT = "SELECT COUNT(*) FROM $TABLE_NAME;"

const val UPDATE_ACTIVATION_BY_KEY = """
UPDATE "$TABLE_NAME"
SET activated = true,
activation_date = NOW()
WHERE "$ACTIVATION_KEY_FIELD" = :$ACTIVATION_KEY_ATTR
"""
}

object Dao {

suspend fun ApplicationContext.countUserActivation() = COUNT
.trimIndent()
.let(getBean<DatabaseClient>()::sql)
.fetch()
.awaitSingle()
Expand Down Expand Up @@ -115,6 +124,7 @@ object UserActivationDao {
suspend fun ApplicationContext.findUserActivationByKey(key: String)
: Either<Throwable, UserActivation> = try {
FIND_BY_ACTIVATION_KEY
.trimIndent()
.run(getBean<R2dbcEntityTemplate>().databaseClient::sql)
.bind(ACTIVATION_KEY_ATTR, key)
.fetch()
Expand Down
11 changes: 0 additions & 11 deletions api/src/test/kotlin/users/ServiceTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,6 @@ class ServiceTests {
assertEquals(second + 1, context.countUserAuthority())
assertEquals(third + 1, context.countUserActivation())
}

// val countUserBefore = context.countUsers()
// assertEquals(0, countUserBefore)
// val countUserAuthBefore = context.countUserAuthority()
// assertEquals(0, countUserAuthBefore)
// val countUserActivationBefore = context.countUserActivation()
// assertEquals(0, countUserActivationBefore)
// context.getBean<SignupService>().signup(this)
// assertEquals(countUserBefore + 1, context.countUsers())
// assertEquals(countUserAuthBefore + 1, context.countUserAuthority())
// assertEquals(countUserActivationBefore + 1, context.countUserActivation())
}
}

Expand Down

0 comments on commit d1a5755

Please sign in to comment.