Passed
Push — master ( 16ccd1...8623b8 )
by Filip
02:49
created

getServices(Environment)   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
1
package com.hltech.judged.server.interfaces.rest.environment;
2
3
import com.hltech.judged.server.domain.JudgeDApplicationService;
4
import com.hltech.judged.server.domain.environment.Environment;
5
import com.hltech.judged.server.domain.environment.EnvironmentRepository;
6
import com.hltech.judged.server.domain.ServiceId;
7
import io.swagger.annotations.ApiOperation;
8
import io.swagger.annotations.ApiResponse;
9
import io.swagger.annotations.ApiResponses;
10
import lombok.RequiredArgsConstructor;
11
import org.springframework.http.MediaType;
12
import org.springframework.web.bind.annotation.GetMapping;
13
import org.springframework.web.bind.annotation.PathVariable;
14
import org.springframework.web.bind.annotation.PutMapping;
15
import org.springframework.web.bind.annotation.RequestBody;
16
import org.springframework.web.bind.annotation.RequestHeader;
17
import org.springframework.web.bind.annotation.RestController;
18
19
import java.util.ArrayList;
20
import java.util.List;
21
import java.util.Set;
22
23
import static com.hltech.judged.server.domain.environment.Environment.DEFAULT_NAMESPACE;
24
import static java.util.stream.Collectors.toList;
25
import static java.util.stream.Collectors.toSet;
26
27
@RestController
28
@RequiredArgsConstructor
29
public class EnvironmentController {
30
31
    private final JudgeDApplicationService judgeD;
32
    private final EnvironmentRepository environmentRepository;
33
34
    @GetMapping(value = "environments", produces = MediaType.APPLICATION_JSON_VALUE)
35
    @ApiOperation(value = "Get names of all persisted environments", nickname = "Get environments")
36
    @ApiResponses(value = {
37
        @ApiResponse(code = 200, message = "Success", response = String.class, responseContainer = "list"),
38
        @ApiResponse(code = 500, message = "Failure")})
39
    public Set<String> getEnvironmentNames() {
40
        return environmentRepository.getNames();
41
    }
42
43
    @GetMapping(value = "environments/{name}", produces = MediaType.APPLICATION_JSON_VALUE)
44
    @ApiOperation(value = "Get services from the environment", nickname = "Get Services")
45
    @ApiResponses(value = {
46
        @ApiResponse(code = 200, message = "Success", response = ServiceDto.class, responseContainer = "list"),
47
        @ApiResponse(code = 500, message = "Failure")})
48
    public List<ServiceDto> getEnvironment(@PathVariable("name") String name) {
49
        return environmentRepository.find(name)
50
            .map(this::getServices)
51
            .orElse(new ArrayList<>());
52
    }
53
54
    private List<ServiceDto> getServices(Environment env) {
55
        return env.getAllServices().stream()
56
            .map(sv -> new ServiceDto(sv.getName(), sv.getVersion()))
57
            .collect(toList());
58
    }
59
60
    @PutMapping(
61
        value = "environments/{name}",
62
        consumes = MediaType.APPLICATION_JSON_VALUE)
63
    @ApiOperation(value = "Update the environment", nickname = "update environment")
64
    @ApiResponses(value = {
65
        @ApiResponse(code = 200, message = "Success"),
66
        @ApiResponse(code = 500, message = "Failure")})
67
    public void overwriteEnvironment(
68
        @PathVariable("name") String name,
69
        @RequestHeader(value = "X-JUDGE-D-AGENT-SPACE", defaultValue = DEFAULT_NAMESPACE, required = false) String agentSpace,
70
        @RequestBody Set<ServiceForm> serviceForms
71
    ) {
72
73
        Set<ServiceId> serviceIds = serviceForms.stream()
74
            .map(sf -> new ServiceId(sf.getName(), sf.getVersion()))
75
            .collect(toSet());
76
77
        judgeD.overwriteEnvironment(name, agentSpace, serviceIds);
78
    }
79
}
80