Completed
Push — master ( 8f8478...3a1842 )
by Tomasz
03:30
created

overwriteEnvironment(String,List)   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.95
cc 1
1
package dev.hltech.dredd.interfaces.rest.environment;
2
3
import dev.hltech.dredd.domain.environment.Environment;
4
import dev.hltech.dredd.domain.environment.EnvironmentAggregate;
5
import dev.hltech.dredd.domain.environment.EnvironmentRepository;
6
import dev.hltech.dredd.domain.environment.Service;
7
import io.swagger.annotations.ApiOperation;
8
import io.swagger.annotations.ApiResponse;
9
import io.swagger.annotations.ApiResponses;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.http.MediaType;
12
import org.springframework.web.bind.annotation.*;
13
14
import java.util.List;
15
import java.util.Set;
16
import java.util.stream.Collectors;
17
18
import static java.util.stream.Collectors.toList;
19
20
@RestController
21
public class EnvironmentController {
22
23
    private final Environment environment;
24
    private final EnvironmentRepository environmentRepository;
25
26
    @Autowired
27
    public EnvironmentController(Environment environment, EnvironmentRepository environmentRepository) {
28
        this.environment = environment;
29
        this.environmentRepository = environmentRepository;
30
    }
31
32
    @GetMapping(value = "environments", produces = MediaType.APPLICATION_JSON_VALUE)
33
    @ApiOperation(value = "Get names of all persisted environments", nickname = "Get environments")
34
    @ApiResponses(value = {
35
        @ApiResponse(code = 200, message = "Success", response = String.class, responseContainer = "list"),
36
        @ApiResponse(code = 500, message = "Failure")})
37
    public Set<String> getEnvironmentNames() {
38
        return environmentRepository.getNames();
39
    }
40
41
    @GetMapping(value = "environments/{name}", produces = MediaType.APPLICATION_JSON_VALUE)
42
    @ApiOperation(value = "Get services from the environment", nickname = "Get Services")
43
    @ApiResponses(value = {
44
        @ApiResponse(code = 200, message = "Success", response = ServiceDto.class, responseContainer = "list"),
45
        @ApiResponse(code = 500, message = "Failure")})
46
    public List<ServiceDto> getEnvironment(@PathVariable("name")String name) {
47
        return environmentRepository.get(name).getAllServices()
48
            .stream()
49
            .map(sv -> new ServiceDto(sv.getName(), sv.getVersion()))
50
            .collect(toList());
51
    }
52
53
    @PutMapping(value="environments/{name}")
54
    @ApiOperation(value = "Update the environment", nickname = "update environment")
55
    @ApiResponses(value = {
56
        @ApiResponse(code = 200, message = "Success"),
57
        @ApiResponse(code = 500, message = "Failure")})
58
    public void overwriteEnvironment(@PathVariable("name")String name, @RequestBody List<ServiceForm> services){
59
        EnvironmentAggregate.EnvironmentAggregateBuilder builder = EnvironmentAggregate.builder(name);
60
        services.stream().forEach(sf -> builder.withServiceVersion(sf.getName(), sf.getVersion()));
61
        environmentRepository.persist(builder.build());
62
    }
63
64
65
    @GetMapping(value = "environment/services", produces = MediaType.APPLICATION_JSON_VALUE)
66
    @ApiOperation(value = "Get services from the environment", nickname = "Get Services")
67
    @ApiResponses(value = {
68
        @ApiResponse(code = 200, message = "Success", response = ServiceDto.class, responseContainer = "list"),
69
        @ApiResponse(code = 500, message = "Failure")})
70
    public List<ServiceDto> getServices() {
71
        return environment.getAllServices()
72
            .stream()
73
            .map(EnvironmentController::toDto)
74
            .collect(Collectors.toList());
75
    }
76
77
    private static ServiceDto toDto(Service service) {
78
        return ServiceDto.builder()
79
            .name(service.getName())
80
            .version(service.getVersion())
81
            .build();
82
    }
83
84
}
85