Skip to content

Commit

Permalink
[ML4SE-267] Fixed getUserId.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikrise2 committed Dec 19, 2023
1 parent ce22d0c commit b6db66d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ class User(id: EntityID<Int>) : Entity<Int>(id) {

object Users : IntIdTable() {
val name = text("name")
val email = text("email").uniqueIndex()
val email = text("email")

init {
uniqueIndex(name, email)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class UserRequestTest {
fun singleCreationTest() = testApplicationRouted {
val id = client.createUserRequest("example", "[email protected]")
.apply {
assertEquals(HttpStatusCode.Created, status)
assertEquals(HttpStatusCode.OK, status)
}.body<Int>()
transaction {
val user = User.findById(id)
Expand All @@ -30,11 +30,11 @@ class UserRequestTest {
fun multiCreationTest() = testApplicationRouted {
val id1 = client.createUserRequest("example1", "[email protected]")
.apply {
assertEquals(HttpStatusCode.Created, status)
assertEquals(HttpStatusCode.OK, status)
}.body<Int>()
val id2 = client.createUserRequest("example2", "[email protected]")
.apply {
assertEquals(HttpStatusCode.Created, status)
assertEquals(HttpStatusCode.OK, status)
}.body<Int>()
transaction {
assertNotNull(User.findById(id1))
Expand All @@ -48,19 +48,19 @@ class UserRequestTest {
val size = transaction { User.all().count() }
client.createUserRequest("example", "[email protected]")
.apply {
assertEquals(HttpStatusCode.Created, status)
assertEquals(HttpStatusCode.OK, status)
}
transaction {
assertEquals(size, User.all().count())
}
}

@Test
fun `creation with same email test`() = testApplicationRouted {
fun `creation with same email and name test`() = testApplicationRouted {
client.createUserRequest("example", "[email protected]")
val size = transaction { User.all().count() }
client.createUserRequest("another", "[email protected]").apply {
assertEquals(HttpStatusCode.BadRequest, status)
client.createUserRequest("example", "[email protected]").apply {
assertEquals(HttpStatusCode.OK, status)
}
transaction {
assertEquals(size, User.all().count())
Expand Down

0 comments on commit b6db66d

Please sign in to comment.