|
1
|
|
|
package com.hltech.judged.server.interfaces.rest.interrelationship; |
|
2
|
|
|
|
|
3
|
|
|
import com.hltech.judged.server.domain.ServiceId; |
|
4
|
|
|
import com.hltech.judged.server.domain.environment.Environment; |
|
5
|
|
|
import com.hltech.judged.server.interfaces.rest.ResourceNotFoundException; |
|
6
|
|
|
import com.hltech.judged.server.interfaces.rest.contracts.ServiceContractsDto; |
|
7
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContracts; |
|
8
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContractsRepository; |
|
9
|
|
|
import com.hltech.judged.server.domain.environment.EnvironmentRepository; |
|
10
|
|
|
import io.swagger.annotations.ApiOperation; |
|
11
|
|
|
import io.swagger.annotations.ApiResponse; |
|
12
|
|
|
import io.swagger.annotations.ApiResponses; |
|
13
|
|
|
import lombok.RequiredArgsConstructor; |
|
14
|
|
|
import org.springframework.http.MediaType; |
|
15
|
|
|
import org.springframework.web.bind.annotation.CrossOrigin; |
|
16
|
|
|
import org.springframework.web.bind.annotation.GetMapping; |
|
17
|
|
|
import org.springframework.web.bind.annotation.PathVariable; |
|
18
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
19
|
|
|
|
|
20
|
|
|
import java.util.ArrayList; |
|
21
|
|
|
import java.util.Set; |
|
22
|
|
|
import java.util.stream.Collectors; |
|
23
|
|
|
|
|
24
|
|
|
@RestController |
|
25
|
|
|
@RequiredArgsConstructor |
|
26
|
|
|
public class InterrelationshipController { |
|
27
|
|
|
|
|
28
|
|
|
private final EnvironmentRepository environmentRepository; |
|
29
|
|
|
private final ServiceContractsRepository serviceContractsRepository; |
|
30
|
|
|
|
|
31
|
|
|
@CrossOrigin |
|
32
|
|
|
@GetMapping(value = "/interrelationship/{environment}", produces = MediaType.APPLICATION_JSON_VALUE) |
|
33
|
|
|
@ApiOperation(value = "Get interrelationship between services in given environment", nickname = "Validate against environment") |
|
34
|
|
|
@ApiResponses(value = { |
|
35
|
|
|
@ApiResponse(code = 200, message = "Success", response = InterrelationshipDto.class), |
|
36
|
|
|
@ApiResponse(code = 404, message = "Not found"), |
|
37
|
|
|
@ApiResponse(code = 500, message = "Failure") |
|
38
|
|
|
}) |
|
39
|
|
|
public InterrelationshipDto getInterrelationship(@PathVariable("environment") String env) { |
|
40
|
|
|
Set<ServiceContractsDto> serviceContractsSet = getServicesIds(env).stream() |
|
41
|
|
|
.map(this::getServiceContracts) |
|
42
|
|
|
.map(ServiceContractsDto::fromDomain) |
|
43
|
|
|
.collect(Collectors.toSet()); |
|
44
|
|
|
|
|
45
|
|
|
return new InterrelationshipDto(env, serviceContractsSet); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private ServiceContracts getServiceContracts(ServiceId serviceId) { |
|
49
|
|
|
return serviceContractsRepository.findOne(serviceId) |
|
50
|
|
|
.orElseGet(() -> ServiceContracts.withEmptyContracts(serviceId)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private Set<ServiceId> getServicesIds(String environmentName) { |
|
54
|
|
|
return this.environmentRepository.find(environmentName) |
|
55
|
|
|
.map(Environment::getAllServices) |
|
56
|
|
|
.orElseThrow(ResourceNotFoundException::new); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|