From b6db66dcf6690136800829b0608ccd80f6ca474d Mon Sep 17 00:00:00 2001 From: mikrise2 Date: Tue, 19 Dec 2023 08:03:14 +0100 Subject: [PATCH] [ML4SE-267] Fixed getUserId. --- .../ui/main/panel/panelStates/DefaultStates.kt | 9 ++++++++- .../research/tasktracker/database/models/User.kt | 6 +++++- .../tasktracker/plugins/requests/UserRequests.kt | 15 +++++---------- .../tasktracker/requests/UserRequestTest.kt | 14 +++++++------- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/ui/main/panel/panelStates/DefaultStates.kt b/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/ui/main/panel/panelStates/DefaultStates.kt index 8590edfb..cfe1baa8 100644 --- a/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/ui/main/panel/panelStates/DefaultStates.kt +++ b/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/ui/main/panel/panelStates/DefaultStates.kt @@ -1,6 +1,7 @@ package org.jetbrains.research.tasktracker.ui.main.panel.panelStates import com.intellij.openapi.application.ApplicationManager +import com.intellij.util.alsoIfNull import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch @@ -44,7 +45,13 @@ fun Panel.welcomePage() { loadBasePage(MainPageTemplate.loadCurrentTemplate(), "ui.button.next", false) setNextAction { GlobalPluginStorage.agreementChecker?.let { - GlobalPluginStorage.userId = IdRequests.getUserId(it.name, it.email) + GlobalPluginStorage.userId = IdRequests.getUserId(it.name, it.email).alsoIfNull { + notifyError( + project, + "Connection problems with the server, either you entered an incorrect email or name." + ) + return@setNextAction + } } GlobalPluginStorage.currentResearchId = IdRequests.getResearchId() TaskTrackerPlugin.initializationHandler.setupEnvironment(project) diff --git a/ij-server/src/main/kotlin/org/jetbrains/research/tasktracker/database/models/User.kt b/ij-server/src/main/kotlin/org/jetbrains/research/tasktracker/database/models/User.kt index efbb4b99..db44596c 100644 --- a/ij-server/src/main/kotlin/org/jetbrains/research/tasktracker/database/models/User.kt +++ b/ij-server/src/main/kotlin/org/jetbrains/research/tasktracker/database/models/User.kt @@ -15,5 +15,9 @@ class User(id: EntityID) : Entity(id) { object Users : IntIdTable() { val name = text("name") - val email = text("email").uniqueIndex() + val email = text("email") + + init { + uniqueIndex(name, email) + } } diff --git a/ij-server/src/main/kotlin/org/jetbrains/research/tasktracker/plugins/requests/UserRequests.kt b/ij-server/src/main/kotlin/org/jetbrains/research/tasktracker/plugins/requests/UserRequests.kt index 90a57282..d5d4f02f 100644 --- a/ij-server/src/main/kotlin/org/jetbrains/research/tasktracker/plugins/requests/UserRequests.kt +++ b/ij-server/src/main/kotlin/org/jetbrains/research/tasktracker/plugins/requests/UserRequests.kt @@ -7,7 +7,6 @@ import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* import io.ktor.server.util.* -import org.jetbrains.exposed.exceptions.ExposedSQLException import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.research.tasktracker.database.models.User @@ -24,7 +23,7 @@ fun Routing.createUser() { } call.respondText( userId.toString(), - status = HttpStatusCode.Created + status = HttpStatusCode.OK ) } catch (e: IllegalArgumentException) { call.respond(HttpStatusCode.BadRequest, e.localizedMessage) @@ -38,14 +37,10 @@ fun Routing.createUser() { fun getUserId(name: String, email: String): Int { val user = User.find { (Users.name eq name) and (Users.email eq email) } if (user.empty()) { - try { - return User.new { - this.name = name - this.email = email - }.id.value - } catch (e: ExposedSQLException) { - throw IllegalArgumentException("user with name `$name` or with email `$email` already exists") - } + return User.new { + this.name = name + this.email = email + }.id.value } return user.first().id.value } diff --git a/ij-server/src/test/kotlin/org/jetbrains/research/tasktracker/requests/UserRequestTest.kt b/ij-server/src/test/kotlin/org/jetbrains/research/tasktracker/requests/UserRequestTest.kt index 6747160b..891fd54c 100644 --- a/ij-server/src/test/kotlin/org/jetbrains/research/tasktracker/requests/UserRequestTest.kt +++ b/ij-server/src/test/kotlin/org/jetbrains/research/tasktracker/requests/UserRequestTest.kt @@ -16,7 +16,7 @@ class UserRequestTest { fun singleCreationTest() = testApplicationRouted { val id = client.createUserRequest("example", "example@example.example") .apply { - assertEquals(HttpStatusCode.Created, status) + assertEquals(HttpStatusCode.OK, status) }.body() transaction { val user = User.findById(id) @@ -30,11 +30,11 @@ class UserRequestTest { fun multiCreationTest() = testApplicationRouted { val id1 = client.createUserRequest("example1", "example1@example.example") .apply { - assertEquals(HttpStatusCode.Created, status) + assertEquals(HttpStatusCode.OK, status) }.body() val id2 = client.createUserRequest("example2", "example2@example.example") .apply { - assertEquals(HttpStatusCode.Created, status) + assertEquals(HttpStatusCode.OK, status) }.body() transaction { assertNotNull(User.findById(id1)) @@ -48,7 +48,7 @@ class UserRequestTest { val size = transaction { User.all().count() } client.createUserRequest("example", "example@example.example") .apply { - assertEquals(HttpStatusCode.Created, status) + assertEquals(HttpStatusCode.OK, status) } transaction { assertEquals(size, User.all().count()) @@ -56,11 +56,11 @@ class UserRequestTest { } @Test - fun `creation with same email test`() = testApplicationRouted { + fun `creation with same email and name test`() = testApplicationRouted { client.createUserRequest("example", "example@example.example") val size = transaction { User.all().count() } - client.createUserRequest("another", "example@example.example").apply { - assertEquals(HttpStatusCode.BadRequest, status) + client.createUserRequest("example", "example@example.example").apply { + assertEquals(HttpStatusCode.OK, status) } transaction { assertEquals(size, User.all().count())