-
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
Showing
9 changed files
with
215 additions
and
2 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
botalka/src/main/kotlin/ru/itmo/lms/botalka/api/http/endpoint/UserHttpApi.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,31 @@ | ||
package ru.itmo.lms.botalka.api.http.endpoint | ||
|
||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.RestController | ||
import ru.itmo.lms.botalka.api.http.UserDraftMessage | ||
import ru.itmo.lms.botalka.api.http.UserMessage | ||
import ru.itmo.lms.botalka.api.http.apis.UserApi | ||
import ru.itmo.lms.botalka.api.http.message.toMessage | ||
import ru.itmo.lms.botalka.api.http.message.toModel | ||
import ru.itmo.lms.botalka.domain.model.User | ||
import ru.itmo.lms.botalka.logic.UserService | ||
|
||
@RestController | ||
class UserHttpApi( | ||
@Autowired private val userService: UserService, | ||
) : UserApi { | ||
override suspend fun userIdGet(id: Int): ResponseEntity<UserMessage> { | ||
val userId = User.Id(id) | ||
val user = userService.getById(userId) | ||
return ResponseEntity.ok(user.toMessage()) | ||
} | ||
|
||
override suspend fun userPost( | ||
userDraftMessage: UserDraftMessage, | ||
): ResponseEntity<UserMessage> { | ||
val draft = userDraftMessage.toModel() | ||
val user = userService.create(draft) | ||
return ResponseEntity.ok(user.toMessage()) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
botalka/src/main/kotlin/ru/itmo/lms/botalka/api/http/message/UserMapping.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,29 @@ | ||
package ru.itmo.lms.botalka.api.http.message | ||
|
||
import ru.itmo.lms.botalka.api.http.UserDraftMessage | ||
import ru.itmo.lms.botalka.api.http.UserMessage | ||
import ru.itmo.lms.botalka.api.http.UserRoleMessage | ||
import ru.itmo.lms.botalka.domain.model.User | ||
|
||
fun User.Role.toMessage(): UserRoleMessage = | ||
when (this) { | ||
User.Role.TEACHER -> UserRoleMessage.TEACHER | ||
User.Role.STUDENT -> UserRoleMessage.STUDENT | ||
} | ||
|
||
fun User.toMessage(): UserMessage = | ||
UserMessage( | ||
id = this.id.number, | ||
alias = this.alias.text, | ||
roles = this.roles.map { it.toMessage() }, | ||
) | ||
|
||
fun User.Draft.toMessage(): UserDraftMessage = | ||
UserDraftMessage( | ||
alias = this.alias.text, | ||
) | ||
|
||
fun UserDraftMessage.toModel(): User.Draft = | ||
User.Draft( | ||
alias = User.Alias(this.alias), | ||
) |
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
botalka/src/main/kotlin/ru/itmo/lms/botalka/logic/UserService.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 ru.itmo.lms.botalka.logic | ||
|
||
import ru.itmo.lms.botalka.domain.model.User | ||
|
||
interface UserService { | ||
suspend fun getById(id: User.Id): User | ||
suspend fun create(user: User.Draft): User | ||
suspend fun promote(id: User.Id, role: User.Role) | ||
} |
22 changes: 22 additions & 0 deletions
22
botalka/src/main/kotlin/ru/itmo/lms/botalka/logic/basic/BasicUserService.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,22 @@ | ||
package ru.itmo.lms.botalka.logic.basic | ||
|
||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.stereotype.Service | ||
import ru.itmo.lms.botalka.domain.model.User | ||
import ru.itmo.lms.botalka.logic.UserService | ||
import ru.itmo.lms.botalka.storage.UserStorage | ||
|
||
@Service | ||
class BasicUserService( | ||
@Autowired private val storage: UserStorage, | ||
) : UserService { | ||
override suspend fun getById(id: User.Id): User = | ||
storage.getById(id) | ||
|
||
override suspend fun create(user: User.Draft): User = | ||
storage.create(user) | ||
|
||
override suspend fun promote(id: User.Id, role: User.Role) { | ||
TODO("Not yet implemented") | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
botalka/src/main/kotlin/ru/itmo/lms/botalka/storage/UserStorage.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,8 @@ | ||
package ru.itmo.lms.botalka.storage | ||
|
||
import ru.itmo.lms.botalka.domain.model.User | ||
|
||
interface UserStorage { | ||
suspend fun getById(id: User.Id): User | ||
suspend fun create(user: User.Draft): User | ||
} |
32 changes: 32 additions & 0 deletions
32
botalka/src/main/kotlin/ru/itmo/lms/botalka/storage/jooq/JooqUserStorage.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,32 @@ | ||
package ru.itmo.lms.botalka.storage.jooq | ||
|
||
import kotlinx.coroutines.reactor.awaitSingle | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.stereotype.Repository | ||
import ru.itmo.lms.botalka.domain.model.User | ||
import ru.itmo.lms.botalka.storage.UserStorage | ||
import ru.itmo.lms.botalka.storage.jooq.entity.toModel | ||
import ru.itmo.lms.botalka.storage.jooq.tables.references.USER | ||
|
||
@Repository | ||
class JooqUserStorage( | ||
@Autowired private val database: JooqDatabase, | ||
) : UserStorage { | ||
override suspend fun getById(id: User.Id): User = | ||
database.execute | ||
.selectFrom(USER) | ||
.where(USER.ID.equal(id.number)) | ||
.toMono() | ||
.map { it.toModel() } | ||
.awaitSingle() | ||
|
||
override suspend fun create(user: User.Draft): User = | ||
database.execute | ||
.insertInto(USER, USER.ALIAS) | ||
.values(user.alias.text) | ||
.returningResult(USER.fields().asList()) | ||
.coerce(USER) | ||
.toMono() | ||
.map { it.toModel() } | ||
.awaitSingle() | ||
} |
16 changes: 16 additions & 0 deletions
16
botalka/src/main/kotlin/ru/itmo/lms/botalka/storage/jooq/entity/UserMapping.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,16 @@ | ||
package ru.itmo.lms.botalka.storage.jooq.entity | ||
|
||
import ru.itmo.lms.botalka.domain.model.User | ||
import ru.itmo.lms.botalka.storage.jooq.tables.records.UserRecord | ||
|
||
fun UserRecord.toModel(): User = | ||
User( | ||
id = User.Id(this.id!!), | ||
alias = User.Alias(this.alias), | ||
roles = emptySet(), | ||
) | ||
|
||
fun User.Draft.toRecord(): UserRecord = | ||
UserRecord( | ||
alias = this.alias.text, | ||
) |
62 changes: 62 additions & 0 deletions
62
botalka/src/test/kotlin/ru/itmo/lms/botalka/api/http/endpoint/UserApiTest.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,62 @@ | ||
package ru.itmo.lms.botalka.api.http.endpoint | ||
|
||
import io.kotest.matchers.collections.shouldBeUnique | ||
import io.kotest.matchers.collections.shouldContainExactly | ||
import io.kotest.matchers.collections.shouldHaveSize | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import ru.itmo.lms.botalka.BotalkaTestSuite | ||
import ru.itmo.lms.botalka.api.http.apis.UserApi | ||
import ru.itmo.lms.botalka.api.http.message.toMessage | ||
import ru.itmo.lms.botalka.domain.model.User | ||
|
||
@SpringBootTest | ||
class UserApiTest( | ||
@Autowired private val api: UserApi, | ||
) : BotalkaTestSuite() { | ||
private val drafts = listOf( | ||
User.Draft(User.Alias("admin")), | ||
User.Draft(User.Alias("vityaman")), | ||
User.Draft(User.Alias("dima")), | ||
User.Draft(User.Alias("tester")), | ||
) | ||
|
||
@Test | ||
fun createAndGetUser() { | ||
val draftToResultList = runBlocking { | ||
drafts.map { draft -> | ||
val message = draft.toMessage() | ||
val result = async { api.userPost(message).body } | ||
Pair(draft, result) | ||
}.map { (draft, result) -> | ||
Pair(draft, result.await() ?: throw NullPointerException()) | ||
} | ||
} | ||
|
||
draftToResultList.forEach { (l, r) -> | ||
l.alias.text shouldBe r.alias | ||
r.roles shouldHaveSize 0 | ||
} | ||
|
||
val results = draftToResultList.map { it.second } | ||
|
||
val ids = results.map { it.id } | ||
ids.shouldBeUnique() | ||
|
||
runBlocking { | ||
results.forEach { expected -> | ||
launch { | ||
val actual = api.userIdGet(expected.id).body!! | ||
expected.id shouldBe actual.id | ||
expected.alias shouldBe actual.alias | ||
expected.roles shouldContainExactly actual.roles | ||
} | ||
} | ||
} | ||
} | ||
} |