Passed
Push — master ( f84632...bfa934 )
by Tomasz
03:08
created

EnvironmentController(EnvironmentRepository)   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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