-
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
1 parent
c35b1e6
commit 3e434c0
Showing
7 changed files
with
365 additions
and
5 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
25 changes: 25 additions & 0 deletions
25
backend/src/main/kotlin/ch/sbb/backend/application/rest/ServicePointDto.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,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, | ||
) |
91 changes: 91 additions & 0 deletions
91
backend/src/main/kotlin/ch/sbb/backend/application/rest/ServicePointsController.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,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() | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
backend/src/main/kotlin/ch/sbb/backend/domain/servicepoints/InMemoryServicePointsService.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,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) | ||
} | ||
} | ||
} | ||
|
||
|
7 changes: 7 additions & 0 deletions
7
backend/src/main/kotlin/ch/sbb/backend/domain/servicepoints/ServicePoint.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,7 @@ | ||
package ch.sbb.backend.domain.servicepoints | ||
|
||
data class ServicePoint( | ||
val uic: Int, | ||
val designation: String, | ||
val abbreviation: String | ||
) |
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
Oops, something went wrong.