1
|
|
|
package com.hltech.judged.server.interfaces.rest.interrelationship; |
2
|
|
|
|
3
|
|
|
import com.hltech.judged.server.domain.environment.Service; |
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.domain.ServiceVersion; |
9
|
|
|
import com.hltech.judged.server.interfaces.rest.contracts.ContractsMapper; |
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
|
|
|
private final ContractsMapper contractsMapper; |
31
|
|
|
|
32
|
|
|
@CrossOrigin |
33
|
|
|
@GetMapping(value = "/interrelationship/{environment}", produces = MediaType.APPLICATION_JSON_VALUE) |
34
|
|
|
@ApiOperation(value = "Get interrelationship between services in given environment", nickname = "Validate against environment") |
35
|
|
|
@ApiResponses(value = { |
36
|
|
|
@ApiResponse(code = 200, message = "Success", response = InterrelationshipDto.class), |
37
|
|
|
@ApiResponse(code = 500, message = "Failure") |
38
|
|
|
}) |
39
|
|
|
public InterrelationshipDto getInterrelationship (@PathVariable("environment") String env) { |
40
|
|
|
|
41
|
|
|
Set<ServiceContractsDto> serviceContractsSet = environmentRepository.get(env).getAllServices().stream() |
42
|
|
|
.map(this::getServiceContracts) |
43
|
|
|
.map(contractsMapper::toDto) |
44
|
|
|
.collect(Collectors.toSet()); |
45
|
|
|
|
46
|
|
|
return new InterrelationshipDto(env, serviceContractsSet); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private ServiceContracts getServiceContracts(Service service) { |
50
|
|
|
return serviceContractsRepository.findOne(new ServiceVersion(service.getName(), service.getVersion())) |
51
|
|
|
.orElseGet(() -> |
52
|
|
|
new ServiceContracts( |
53
|
|
|
new ServiceVersion(service.getName(), service.getVersion()), |
54
|
|
|
new ArrayList<>(), |
55
|
|
|
new ArrayList<>())); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|