-
Notifications
You must be signed in to change notification settings - Fork 15
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
José Carlos Montañez
authored and
José Carlos Montañez
committed
May 7, 2024
1 parent
1aef223
commit 47a8421
Showing
10 changed files
with
467 additions
and
73 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
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
23 changes: 23 additions & 0 deletions
23
.../src/main/kotlin/com/xebia/functional/xef/server/http/routes/ApplicationCallExtensions.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,23 @@ | ||
package com.xebia.functional.xef.server.http.routes | ||
|
||
import arrow.core.raise.catch | ||
import com.xebia.functional.xef.server.models.Token | ||
import com.xebia.functional.xef.server.models.exceptions.XefExceptions | ||
import io.ktor.server.application.* | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.request.* | ||
import kotlinx.serialization.json.Json | ||
|
||
fun ApplicationCall.getToken(): Token = | ||
principal<UserIdPrincipal>()?.name?.let { Token(it) } | ||
?: throw XefExceptions.AuthorizationException("No token found") | ||
|
||
fun ApplicationCall.getId(): Int = getInt("id") | ||
|
||
fun ApplicationCall.getInt(field: String): Int = | ||
this.parameters[field]?.toInt() ?: throw XefExceptions.ValidationException("Invalid $field") | ||
|
||
suspend inline fun <reified T> ApplicationCall.decodeFromStringRequest(): T = | ||
catch({ Json.decodeFromString<T>(this.receive<String>()) }) { | ||
throw XefExceptions.ValidationException("Invalid ${T::class.simpleName}") | ||
} |
127 changes: 108 additions & 19 deletions
127
server/src/main/kotlin/com/xebia/functional/xef/server/http/routes/OrganizationRoutes.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 |
---|---|---|
@@ -1,53 +1,142 @@ | ||
package com.xebia.functional.xef.server.http.routes | ||
|
||
import com.xebia.functional.xef.server.models.OrganizationFullResponse | ||
import com.xebia.functional.xef.server.models.OrganizationRequest | ||
import com.xebia.functional.xef.server.models.OrganizationUpdateRequest | ||
import com.xebia.functional.xef.server.models.UserResponse | ||
import com.xebia.functional.xef.server.services.OrganizationRepositoryService | ||
import guru.zoroark.tegral.openapi.dsl.schema | ||
import guru.zoroark.tegral.openapi.ktor.resources.* | ||
import io.ktor.http.* | ||
import io.ktor.resources.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import kotlinx.serialization.json.Json | ||
import io.ktor.server.routing.Routing | ||
import io.swagger.v3.oas.models.media.StringSchema | ||
|
||
fun Routing.organizationRoutes(orgRepositoryService: OrganizationRepositoryService) { | ||
authenticate("auth-bearer") { | ||
get("/v1/settings/org") { | ||
getD<OrganizationRoutes> { | ||
val token = call.getToken() | ||
val response = orgRepositoryService.getOrganizations(token) | ||
call.respond(response) | ||
} | ||
get("/v1/settings/org/{id}") { | ||
postD<OrganizationRoutes> { | ||
val request = call.decodeFromStringRequest<OrganizationRequest>() | ||
val token = call.getToken() | ||
val id = call.getId() | ||
val response = orgRepositoryService.getOrganization(token, id) | ||
call.respond(response) | ||
val response = orgRepositoryService.createOrganization(request, token) | ||
call.respond(status = HttpStatusCode.Created, response) | ||
} | ||
get("/v1/settings/org/{id}/users") { | ||
getD<OrganizationDetailsRoutes> { | ||
val token = call.getToken() | ||
val id = call.getId() | ||
val response = orgRepositoryService.getUsersInOrganization(token, id) | ||
val response = orgRepositoryService.getOrganization(token, id) | ||
call.respond(response) | ||
} | ||
post("/v1/settings/org") { | ||
val request = Json.decodeFromString<OrganizationRequest>(call.receive<String>()) | ||
val token = call.getToken() | ||
val response = orgRepositoryService.createOrganization(request, token) | ||
call.respond(status = HttpStatusCode.Created, response) | ||
} | ||
put("/v1/settings/org/{id}") { | ||
val request = Json.decodeFromString<OrganizationUpdateRequest>(call.receive<String>()) | ||
putD<OrganizationDetailsRoutes> { | ||
val request = call.decodeFromStringRequest<OrganizationUpdateRequest>() | ||
val token = call.getToken() | ||
val id = call.getId() | ||
val response = orgRepositoryService.updateOrganization(token, request, id) | ||
call.respond(status = HttpStatusCode.NoContent, response) | ||
} | ||
delete("/v1/settings/org/{id}") { | ||
deleteD<OrganizationDetailsRoutes> { | ||
val token = call.getToken() | ||
val id = call.getId() | ||
val response = orgRepositoryService.deleteOrganization(token, id) | ||
call.respond(status = HttpStatusCode.NoContent, response) | ||
} | ||
getD<OrganizationUsersRoutes> { | ||
val token = call.getToken() | ||
val id = call.getId() | ||
val response = orgRepositoryService.getUsersInOrganization(token, id) | ||
call.respond(response) | ||
} | ||
} | ||
} | ||
|
||
@Resource("/v1/settings/org") | ||
class OrganizationRoutes { | ||
companion object : | ||
ResourceDescription by describeResource({ | ||
get { | ||
description = "Get all organizations" | ||
tags += "Organization" | ||
HttpStatusCode.OK.value response | ||
{ | ||
description = "List of organizations" | ||
json { schema<List<OrganizationFullResponse>>() } | ||
} | ||
} | ||
post { | ||
description = "Create an organization" | ||
tags += "Organization" | ||
body { | ||
description = "The organization to create" | ||
required = true | ||
json { schema<OrganizationRequest>() } | ||
} | ||
} | ||
}) | ||
} | ||
|
||
@Resource("/v1/settings/org/{id}") | ||
class OrganizationDetailsRoutes { | ||
companion object : | ||
ResourceDescription by describeResource({ | ||
"id" pathParameter | ||
{ | ||
description = "Organization ID" | ||
required = true | ||
schema = StringSchema() | ||
example = "org_123" | ||
} | ||
get { | ||
description = "Get organization details" | ||
tags += "Organization" | ||
HttpStatusCode.OK.value response | ||
{ | ||
description = "Organization details" | ||
json { schema<OrganizationFullResponse>() } | ||
} | ||
} | ||
put { | ||
description = "Update organization details" | ||
tags += "Organization" | ||
body { | ||
description = "The organization details to update" | ||
required = true | ||
json { schema<OrganizationUpdateRequest>() } | ||
} | ||
HttpStatusCode.NoContent.value response { description = "Organization updated" } | ||
} | ||
delete { | ||
description = "Delete organization" | ||
tags += "Organization" | ||
} | ||
}) | ||
} | ||
|
||
@Resource("/v1/settings/org/{id}/users") | ||
class OrganizationUsersRoutes { | ||
companion object : | ||
ResourceDescription by describeResource({ | ||
"id" pathParameter | ||
{ | ||
description = "Organization ID" | ||
required = true | ||
schema = StringSchema() | ||
example = "org_123" | ||
} | ||
get { | ||
description = "Get users in organization" | ||
tags += "Organization" | ||
HttpStatusCode.OK.value response | ||
{ | ||
description = "List of users in organization" | ||
json { schema<List<UserResponse>>() } | ||
} | ||
} | ||
}) | ||
} |
Oops, something went wrong.