-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReportCalculationController.kt
92 lines (80 loc) · 3.55 KB
/
ReportCalculationController.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.aamdigital.aambackendservice.reporting.reportcalculation.controller
import com.aamdigital.aambackendservice.domain.DomainReference
import com.aamdigital.aambackendservice.error.InternalServerException
import com.aamdigital.aambackendservice.error.NotFoundException
import com.aamdigital.aambackendservice.reporting.domain.ReportCalculation
import com.aamdigital.aambackendservice.reporting.domain.ReportData
import com.aamdigital.aambackendservice.reporting.report.core.ReportingStorage
import com.aamdigital.aambackendservice.reporting.reportcalculation.core.CreateReportCalculationRequest
import com.aamdigital.aambackendservice.reporting.reportcalculation.core.CreateReportCalculationResult
import com.aamdigital.aambackendservice.reporting.reportcalculation.core.CreateReportCalculationUseCase
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Mono
@RestController
@RequestMapping("/v1/reporting/report-calculation")
@Validated
class ReportCalculationController(
private val reportingStorage: ReportingStorage,
private val createReportCalculationUseCase: CreateReportCalculationUseCase
) {
@PostMapping("/report/{reportId}")
fun startCalculation(
@PathVariable reportId: String
): Mono<DomainReference> {
return reportingStorage.fetchReport(DomainReference(id = reportId))
.flatMap { reportOptional ->
val report = reportOptional.orElseThrow {
NotFoundException()
}
createReportCalculationUseCase.startReportCalculation(
CreateReportCalculationRequest(
report = DomainReference(report.id)
)
).handle { result, sink ->
when (result) {
is CreateReportCalculationResult.Failure -> {
sink.error(InternalServerException())
}
is CreateReportCalculationResult.Success -> sink.next(result.calculation)
}
}
}
}
@GetMapping("/report/{reportId}")
fun fetchReportCalculations(
@PathVariable reportId: String
): Mono<List<ReportCalculation>> {
return reportingStorage.fetchCalculations(DomainReference(id = reportId))
}
@GetMapping("/{calculationId}")
fun fetchReportCalculation(
@PathVariable calculationId: String
): Mono<ReportCalculation> {
return reportingStorage.fetchCalculation(DomainReference(id = calculationId))
.map { calculationOptional ->
val calculation = calculationOptional.orElseThrow {
NotFoundException()
}
// TODO Auth check
calculation
}
}
@GetMapping("/{calculationId}/data")
fun fetchReportCalculationData(
@PathVariable calculationId: String
): Mono<ReportData> {
return reportingStorage.fetchData(DomainReference(id = calculationId))
.map { calculationOptional ->
val calculation = calculationOptional.orElseThrow {
NotFoundException()
}
// TODO Auth check
calculation
}
}
}