1
|
|
|
package com.hltech.judged.server.interfaces.rest.interrelationship; |
2
|
|
|
|
3
|
|
|
import com.hltech.judged.server.domain.ServiceId; |
4
|
|
|
import com.hltech.judged.server.interfaces.rest.contracts.ServiceContractsDto; |
5
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContracts; |
6
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContractsRepository; |
7
|
|
|
import com.hltech.judged.server.domain.environment.EnvironmentRepository; |
8
|
|
|
import com.hltech.judged.server.interfaces.rest.contracts.ContractsMapper; |
9
|
|
|
import io.swagger.annotations.ApiOperation; |
10
|
|
|
import io.swagger.annotations.ApiResponse; |
11
|
|
|
import io.swagger.annotations.ApiResponses; |
12
|
|
|
import lombok.RequiredArgsConstructor; |
13
|
|
|
import org.springframework.http.MediaType; |
14
|
|
|
import org.springframework.web.bind.annotation.CrossOrigin; |
15
|
|
|
import org.springframework.web.bind.annotation.GetMapping; |
16
|
|
|
import org.springframework.web.bind.annotation.PathVariable; |
17
|
|
|
import org.springframework.web.bind.annotation.RestController; |
18
|
|
|
|
19
|
|
|
import java.util.ArrayList; |
20
|
|
|
import java.util.Set; |
21
|
|
|
import java.util.stream.Collectors; |
22
|
|
|
|
23
|
|
|
@RestController |
24
|
|
|
@RequiredArgsConstructor |
25
|
|
|
public class InterrelationshipController { |
26
|
|
|
|
27
|
|
|
private final EnvironmentRepository environmentRepository; |
28
|
|
|
private final ServiceContractsRepository serviceContractsRepository; |
29
|
|
|
private final ContractsMapper contractsMapper; |
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 = 500, message = "Failure") |
37
|
|
|
}) |
38
|
|
|
public InterrelationshipDto getInterrelationship (@PathVariable("environment") String env) { |
39
|
|
|
|
40
|
|
|
Set<ServiceContractsDto> serviceContractsSet = environmentRepository.get(env).getAllServices().stream() |
41
|
|
|
.map(this::getServiceContracts) |
42
|
|
|
.map(contractsMapper::toDto) |
43
|
|
|
.collect(Collectors.toSet()); |
44
|
|
|
|
45
|
|
|
return new InterrelationshipDto(env, serviceContractsSet); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private ServiceContracts getServiceContracts(ServiceId serviceId) { |
49
|
|
|
return serviceContractsRepository.findOne(new ServiceId(serviceId.getName(), serviceId.getVersion())) |
50
|
|
|
.orElseGet(() -> |
51
|
|
|
new ServiceContracts( |
52
|
|
|
new ServiceId(serviceId.getName(), serviceId.getVersion()), |
53
|
|
|
new ArrayList<>(), |
54
|
|
|
new ArrayList<>())); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|