Skip to content

Commit

Permalink
Work on repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstaeding committed Aug 15, 2023
1 parent 51afbff commit b96550f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
package guestbook.http

import io.ktor.http.HttpStatusCode
import io.ktor.resources.Resource
import io.ktor.server.application.call
import io.ktor.server.resources.get
import io.ktor.server.response.respond
import io.ktor.server.routing.Route
import io.ktor.server.routing.route
import org.sourcegrade.kontour.DomainEntity
import org.sourcegrade.kontour.Repository
import java.util.UUID

fun Route.registerGuestbookRoutes() {
route("/api/guestbook") {

inline fun <reified E : DomainEntity> Route.registerRepositoryGet(
name: String,
repository: Repository<E>,
) {
route("/api/$name") {
get<ById> { (sid) ->
val id = UUID.fromString(sid)
val item = repository.getById(id)
if (item == null) {
call.respond(HttpStatusCode.NotFound, "Item with id $id not found in repository $name")
} else {
call.respond(HttpStatusCode.OK, item)
}
}
}
}

@PublishedApi
@Resource("{id}")
internal data class ById(val id: String)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ClientRepository<E : DomainEntity, C : Creates<E>>(
val httpClient: HttpClient,
) : Repository<E, C> {
override suspend fun countAll(): Long {
TODO("Not yet implemented")

}

override suspend fun deleteById(id: UUID): Boolean {
Expand All @@ -21,6 +21,10 @@ class ClientRepository<E : DomainEntity, C : Creates<E>>(
TODO("Not yet implemented")
}

override suspend fun getById(id: UUID): E? {

}

override suspend fun create(item: C): E {
TODO("Not yet implemented")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package guestbook.web.http
import io.ktor.resources.Resource

@Resource("/api/v1/guestbook")
class GuestbookRoute {
class RepositoryRoute {
@Resource("{id}")
class ById(val id: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.sourcegrade.kontour.Pagination
import org.sourcegrade.kontour.UUID

object GuestbookRepositoryImpl : GuestbookRepository {
Expand All @@ -36,6 +37,10 @@ object GuestbookRepositoryImpl : GuestbookRepository {
}
}

override suspend fun paginate(): Pagination<Guestbook> {
TODO("Not yet implemented")
}

override suspend fun getById(id: UUID): Guestbook? {
return newSuspendedTransaction {
createJoin()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package guestbook.domain

import org.sourcegrade.kontour.Repository
import org.sourcegrade.kontour.MutableRepository

interface GuestbookRepository : Repository<Guestbook, Guestbook.CreateDto> {
interface GuestbookRepository : MutableRepository<Guestbook, Guestbook.CreateDto> {
suspend fun findByName(name: String): Guestbook?
}

0 comments on commit b96550f

Please sign in to comment.