map(Space)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
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 com.hltech.judged.server.domain.environment.Space;
8
import io.swagger.annotations.ApiOperation;
9
import io.swagger.annotations.ApiResponse;
10
import io.swagger.annotations.ApiResponses;
11
import lombok.RequiredArgsConstructor;
12
import org.springframework.http.MediaType;
13
import org.springframework.http.ResponseEntity;
14
import org.springframework.web.bind.annotation.GetMapping;
15
import org.springframework.web.bind.annotation.PathVariable;
16
import org.springframework.web.bind.annotation.PutMapping;
17
import org.springframework.web.bind.annotation.RequestBody;
18
import org.springframework.web.bind.annotation.RequestHeader;
19
import org.springframework.web.bind.annotation.RestController;
20
21
import java.util.Set;
22
import java.util.stream.Collectors;
23
24
import static com.hltech.judged.server.domain.environment.Environment.DEFAULT_NAMESPACE;
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 = EnvironmentDto.SpaceDto.ServiceDto.class, responseContainer = "list"),
47
        @ApiResponse(code = 404, message = "Not found"),
48
        @ApiResponse(code = 500, message = "Failure")})
49
    public ResponseEntity<EnvironmentDto> getEnvironment(@PathVariable("name") String name) {
50
        return environmentRepository.find(name)
51
            .map(this::map)
52
            .map(ResponseEntity::ok)
53
            .orElse(ResponseEntity.notFound().build());
54
    }
55
56
    @PutMapping(
57
        value = "environments/{name}",
58
        consumes = MediaType.APPLICATION_JSON_VALUE)
59
    @ApiOperation(value = "Update the environment", nickname = "update environment")
60
    @ApiResponses(value = {
61
        @ApiResponse(code = 200, message = "Success"),
62
        @ApiResponse(code = 500, message = "Failure")})
63
    public void overwriteEnvironment(
64
        @PathVariable("name") String name,
65
        @RequestHeader(value = "X-JUDGE-D-AGENT-SPACE", defaultValue = DEFAULT_NAMESPACE, required = false) String agentSpace,
66
        @RequestBody Set<ServiceForm> serviceForms
67
    ) {
68
69
        Set<ServiceId> serviceIds = serviceForms.stream()
70
            .map(sf -> new ServiceId(sf.getName(), sf.getVersion()))
71
            .collect(toSet());
72
73
        judgeD.overwriteEnvironment(name, agentSpace, serviceIds);
74
    }
75
76
    private EnvironmentDto map(Environment env) {
77
        return new EnvironmentDto(
78
            env.getName(),
79
            env.getSpaces().stream()
80
                .map(this::map)
81
                .collect(Collectors.toSet())
82
        );
83
    }
84
85
    private EnvironmentDto.SpaceDto map(Space space) {
86
        return new EnvironmentDto.SpaceDto(
87
            space.getName(),
88
            space.getServiceIds().stream()
89
                .map(this::map)
90
                .collect(Collectors.toSet())
91
        );
92
    }
93
94
    private EnvironmentDto.SpaceDto.ServiceDto map(ServiceId service) {
95
        return new EnvironmentDto.SpaceDto.ServiceDto(
96
            service.getName(),
97
            service.getVersion()
98
        );
99
    }
100
}
101