-
Notifications
You must be signed in to change notification settings - Fork 34
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
6 changed files
with
112 additions
and
3 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.github.nomisrev.repo | ||
|
||
import io.github.nomisrev.sqldelight.TagsQueries | ||
|
||
interface TagPersistence { | ||
/** List all tags * */ | ||
suspend fun selectTags(): List<String> | ||
} | ||
|
||
fun tagPersistence(tags: TagsQueries) = | ||
object : TagPersistence { | ||
override suspend fun selectTags() = tags.selectTags().executeAsList() | ||
} |
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,24 @@ | ||
@file:Suppress("MatchingDeclarationName") | ||
|
||
package io.github.nomisrev.routes | ||
|
||
import io.github.nomisrev.repo.TagPersistence | ||
import io.ktor.server.application.Application | ||
import io.ktor.server.application.call | ||
import io.ktor.server.response.respond | ||
import io.ktor.server.routing.get | ||
import io.ktor.server.routing.route | ||
import io.ktor.server.routing.routing | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable data class TagsResponse(val tags: List<String>) | ||
|
||
fun Application.tagRoutes(tagPersistence: TagPersistence) = routing { | ||
route("/tags") { | ||
/* Registration: GET /api/tags */ | ||
get { | ||
val tags = tagPersistence.selectTags() | ||
call.respond(TagsResponse(tags)) | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.github.nomisrev.routes | ||
|
||
import arrow.core.flatMap | ||
import io.github.nefilim.kjwt.JWSHMAC512Algorithm | ||
import io.github.nefilim.kjwt.JWT | ||
import io.github.nomisrev.repo.UserId | ||
import io.github.nomisrev.service.CreateArticle | ||
import io.github.nomisrev.service.RegisterUser | ||
import io.github.nomisrev.withServer | ||
import io.kotest.assertions.arrow.core.shouldBeRight | ||
import io.kotest.assertions.arrow.core.shouldBeSome | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.collections.shouldHaveSize | ||
import io.kotest.matchers.shouldBe | ||
import io.ktor.client.call.body | ||
import io.ktor.client.request.get | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.http.contentType | ||
|
||
class TagRouteSpec : | ||
StringSpec({ | ||
// User | ||
val validUsername = "username2" | ||
val validEmail = "[email protected]" | ||
val validPw = "123456789" | ||
// Article | ||
val validTags = setOf("arrow", "ktor", "kotlin", "sqldelight") | ||
val validTitle = "Fake Article Arrow " | ||
val validDescription = "This is a fake article description." | ||
val validBody = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." | ||
|
||
"Check for empty list retrieval" { | ||
withServer { | ||
val response = get("/tags") { contentType(ContentType.Application.Json) } | ||
|
||
response.status shouldBe HttpStatusCode.OK | ||
response.body<TagsResponse>().tags.toSet() shouldBe validTags | ||
} | ||
} | ||
|
||
"Can get all tags" { | ||
withServer { dependencies -> | ||
val userId = | ||
dependencies.userService | ||
.register(RegisterUser(validUsername, validEmail, validPw)) | ||
.flatMap { JWT.decodeT(it.value, JWSHMAC512Algorithm) } | ||
.map { it.claimValueAsLong("id").shouldBeSome() } | ||
.shouldBeRight() | ||
|
||
dependencies.articleService | ||
.createArticle( | ||
CreateArticle(UserId(userId), validTitle, validDescription, validBody, validTags) | ||
) | ||
.shouldBeRight() | ||
|
||
val response = get("/tags") { contentType(ContentType.Application.Json) } | ||
|
||
response.status shouldBe HttpStatusCode.OK | ||
response.body<TagsResponse>().tags shouldHaveSize 4 | ||
} | ||
} | ||
}) |