Skip to content

Commit

Permalink
feat: service points api (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
mghilardelli authored Nov 5, 2024
1 parent c35b1e6 commit 3e434c0
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package ch.sbb.backend.application.rest

import ch.sbb.backend.domain.logging.MultitenantLogService
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Schema
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.validation.annotation.Validated
Expand All @@ -18,11 +20,25 @@ class LoggingController(private val multitenantLogService: MultitenantLogService

@Operation(summary = "Log messages from clients")
@ApiResponse(responseCode = "200", description = "Logs successfully saved")
@ApiResponse(responseCode = "400", description = "Invalid input")
@ApiResponse(
responseCode = "400", description = "Invalid input", content = [
Content(
mediaType = "application/json",
schema = Schema(ref = "#/components/schemas/ErrorResponse")
)
]
)
@ApiResponse(responseCode = "401", description = "Unauthorized")
@ApiResponse(responseCode = "500", description = "Internal server error")
@PostMapping("/logs")
fun logs( @RequestBody logs: List<LogEntryRequest>) {
@ApiResponse(
responseCode = "500", description = "Internal server error", content = [
Content(
mediaType = "application/json",
schema = Schema(ref = "#/components/schemas/ErrorResponse")
)
]
)
@PostMapping("/logs", consumes = ["application/json"])
fun logs(@RequestBody logs: List<LogEntryRequest>) {
multitenantLogService.logs(logs)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ch.sbb.backend.application.rest

import io.swagger.v3.oas.annotations.media.Schema

@Schema(name = "ServicePoint")
data class ServicePointDto(

@Schema(
description = "UIC-Code, combination of uicCountryCode and numberShort. Size: 7",
example = "8518771"
)
val uic: Int,

@Schema(
description = "Official designation of a location",
example = "Biel/Bienne Bözingenfeld/Champ"
)
val designation: String,

@Schema(
description = "Location abbreviation",
example = "BIBD"
)
val abbreviation: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package ch.sbb.backend.application.rest

import ch.sbb.backend.domain.servicepoints.InMemoryServicePointsService
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Schema
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.http.MediaType.APPLICATION_JSON_VALUE
import org.springframework.http.ResponseEntity
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*

@Validated
@RestController
@RequestMapping("api/v1/service-points")
@Tag(name = "Service Points", description = "API for service points")
class ServicePointsController(private val servicePointsService: InMemoryServicePointsService) {

@Operation(summary = "Update service points")
@ApiResponse(responseCode = "200", description = "Service points successfully updated")
@ApiResponse(
responseCode = "400", description = "Invalid input", content = [
Content(
mediaType = APPLICATION_JSON_VALUE,
schema = Schema(ref = "#/components/schemas/ErrorResponse")
)
]
)
@ApiResponse(responseCode = "401", description = "Unauthorized")
@ApiResponse(
responseCode = "500", description = "Internal server error", content = [
Content(
mediaType = "application/json",
schema = Schema(ref = "#/components/schemas/ErrorResponse")
)
]
)
@PutMapping(consumes = ["application/json"])
fun updateServicePoints(@RequestBody servicePoints: List<ServicePointDto>) {
servicePointsService.update(servicePoints)
}

@Operation(summary = "Get all service points")
@ApiResponse(responseCode = "200", description = "Service points successfully retrieved")
@ApiResponse(
responseCode = "401",
description = "Unauthorized",
content = [Content(schema = Schema(hidden = true))]
)
@ApiResponse(
responseCode = "500", description = "Internal server error", content = [
Content(
mediaType = "application/json",
schema = io.swagger.v3.oas.annotations.media.Schema(ref = "#/components/schemas/ErrorResponse")
)
]
)
@ResponseBody
@GetMapping(produces = [APPLICATION_JSON_VALUE])
fun getAllServicePoints(): ResponseEntity<List<ServicePointDto>> {
return ResponseEntity.ok(servicePointsService.getAll())
}

@Operation(summary = "Get service point by UIC")
@ApiResponse(responseCode = "200", description = "Service point successfully retrieved")
@ApiResponse(
responseCode = "401",
description = "Unauthorized",
content = [Content(schema = Schema(hidden = true))]
)
@ApiResponse(
responseCode = "404",
description = "Service point not found",
content = [Content(schema = Schema(hidden = true))]
)
@ApiResponse(
responseCode = "500", description = "Internal server error", content = [
Content(
mediaType = "application/json",
schema = Schema(ref = "#/components/schemas/ErrorResponse")
)
]
)
@ResponseBody
@GetMapping("/{uic}", produces = [APPLICATION_JSON_VALUE])
fun getServicePoint(@PathVariable uic: Int): ResponseEntity<ServicePointDto> {
return servicePointsService.getByUic(uic)?.let { ResponseEntity.ok(it) }
?: ResponseEntity.notFound().build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ch.sbb.backend.domain.servicepoints

import ch.sbb.backend.application.rest.ServicePointDto
import org.springframework.stereotype.Service

@Service
class InMemoryServicePointsService {
private val servicePoints = HashMap<Int, ServicePoint>()

fun update(servicePoints: List<ServicePointDto>) {
servicePoints.forEach {
this.servicePoints[it.uic] = ServicePoint(it.uic, it.designation, it.abbreviation)
}
}

fun getAll(): List<ServicePointDto> {
return servicePoints.map {
ServicePointDto(
it.key,
it.value.designation,
it.value.abbreviation
)
}
}

fun getByUic(uic: Int): ServicePointDto? {
return servicePoints[uic]?.let {
ServicePointDto(it.uic, it.designation, it.abbreviation)
}
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ch.sbb.backend.domain.servicepoints

data class ServicePoint(
val uic: Int,
val designation: String,
val abbreviation: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import io.swagger.v3.oas.models.Components
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.info.Contact
import io.swagger.v3.oas.models.info.Info
import io.swagger.v3.oas.models.media.DateTimeSchema
import io.swagger.v3.oas.models.media.NumberSchema
import io.swagger.v3.oas.models.media.Schema
import io.swagger.v3.oas.models.media.StringSchema
import io.swagger.v3.oas.models.security.*
import org.apache.commons.lang3.StringUtils
import org.springframework.beans.factory.annotation.Value
Expand All @@ -29,7 +33,18 @@ class OpenApiConfig {
@Bean
fun openAPIConfiguration(): OpenAPI {
return OpenAPI()
.components(Components().addSecuritySchemes(OAUTH_2, addOAuthSecurityScheme()))
.components(
Components()
.addSecuritySchemes(OAUTH_2, addOAuthSecurityScheme())
.addSchemas(
"ErrorResponse",
Schema<Map<String, Any>>()
.addProperty("timestamp", DateTimeSchema())
.addProperty("status", NumberSchema())
.addProperty("error", StringSchema())
.addProperty("path", StringSchema())
)
)
.security(listOf(SecurityRequirement().addList(OAUTH_2)))
.info(apiInfo())
}
Expand Down
Loading

0 comments on commit 3e434c0

Please sign in to comment.