1
1
package fi.hsl.jore4.hastus.api
2
2
3
+ import com.fasterxml.jackson.databind.ObjectMapper
3
4
import fi.hsl.jore4.hastus.Constants.MIME_TYPE_CSV
4
5
import fi.hsl.jore4.hastus.service.importing.ImportService
5
6
import mu.KotlinLogging
6
- import org.springframework.http.HttpStatus
7
+ import org.springframework.http.HttpHeaders
8
+ import org.springframework.http.MediaType
7
9
import org.springframework.http.ResponseEntity
8
10
import org.springframework.web.bind.annotation.ExceptionHandler
9
11
import org.springframework.web.bind.annotation.PostMapping
@@ -12,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestHeader
12
14
import org.springframework.web.bind.annotation.RequestMapping
13
15
import org.springframework.web.bind.annotation.RestController
14
16
import org.springframework.web.server.ResponseStatusException
17
+ import java.io.Serializable
15
18
import java.util.UUID
16
19
import kotlin.time.ExperimentalTime
17
20
import kotlin.time.measureTimedValue
@@ -22,22 +25,23 @@ private val LOGGER = KotlinLogging.logger {}
22
25
@RestController
23
26
@RequestMapping(" import" )
24
27
class ImportController (
25
- private val importService : ImportService
28
+ private val importService : ImportService ,
29
+ private val objectMapper : ObjectMapper
26
30
) {
27
31
28
32
data class ImportTimetablesSuccessResult (
29
33
val vehicleScheduleFrameId : UUID ?
30
- )
34
+ ) : Serializable
31
35
32
36
data class ImportTimetablesFailureResult (
33
37
val reason : String?
34
- )
38
+ ) : Serializable
35
39
36
40
@PostMapping(" " , consumes = [MIME_TYPE_CSV ])
37
41
fun importCsvFile (
38
42
@RequestBody csv : String ,
39
43
@RequestHeader headers : Map <String , String >
40
- ): ImportTimetablesSuccessResult {
44
+ ): ResponseEntity < String > {
41
45
val (nullableVehicleScheduleFrameId, elapsed) = measureTimedValue {
42
46
LOGGER .info(" Starting to import timetables from CSV file..." )
43
47
@@ -55,26 +59,36 @@ class ImportController(
55
59
56
60
LOGGER .info { " CSV import processing completed in $elapsed " }
57
61
58
- return ImportTimetablesSuccessResult (nullableVehicleScheduleFrameId)
62
+ return ResponseEntity .ok()
63
+ .header(HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE )
64
+ .body(
65
+ serialise(
66
+ ImportTimetablesSuccessResult (nullableVehicleScheduleFrameId)
67
+ )
68
+ )
59
69
}
60
70
61
71
@ExceptionHandler
62
- fun handleExportException (ex : Exception ): ResponseEntity <ImportTimetablesFailureResult > {
72
+ fun handleExportException (ex : Exception ): ResponseEntity <String > {
63
73
return when (ex) {
64
74
is ResponseStatusException -> {
65
75
ResponseEntity
66
76
.status(ex.statusCode)
67
- .body(ImportTimetablesFailureResult (ex.reason))
77
+ .header(HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE )
78
+ .body(serialise(ImportTimetablesFailureResult (ex.reason)))
68
79
}
69
80
70
81
else -> {
71
82
LOGGER .error { " Exception during import request:$ex " }
72
83
LOGGER .error(ex.stackTraceToString())
73
84
74
85
ResponseEntity
75
- .status(HttpStatus .INTERNAL_SERVER_ERROR )
76
- .body(ImportTimetablesFailureResult (ex.message))
86
+ .internalServerError()
87
+ .header(HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE )
88
+ .body(serialise(ImportTimetablesFailureResult (ex.message)))
77
89
}
78
90
}
79
91
}
92
+
93
+ private fun serialise (obj : Serializable ): String = objectMapper.writeValueAsString(obj)
80
94
}
0 commit comments