|
1
|
|
|
package com.hltech.judged.server.infrastructure.persistence.environment; |
|
2
|
|
|
|
|
3
|
|
|
import com.hltech.judged.server.domain.ServiceVersion; |
|
4
|
|
|
import com.hltech.judged.server.domain.SpaceServiceVersion; |
|
5
|
|
|
import com.hltech.judged.server.domain.environment.Environment; |
|
6
|
|
|
import com.hltech.judged.server.domain.environment.EnvironmentRepository; |
|
7
|
|
|
import lombok.RequiredArgsConstructor; |
|
8
|
|
|
import org.springframework.stereotype.Repository; |
|
9
|
|
|
|
|
10
|
|
|
import java.util.Set; |
|
11
|
|
|
import java.util.stream.Collectors; |
|
12
|
|
|
|
|
13
|
|
|
@Repository |
|
14
|
|
|
@RequiredArgsConstructor |
|
15
|
|
|
public class JPAEnvironmentRepository implements EnvironmentRepository { |
|
16
|
|
|
|
|
17
|
|
|
private final SpringDataEnvironmentRepository springDataEnvironmentRepository; |
|
18
|
|
|
|
|
19
|
|
|
@Override |
|
20
|
|
|
public Environment persist(Environment environment) { |
|
21
|
|
|
EnvironmentTuple environmentTuple = toEnvironmentTuple(environment); |
|
22
|
|
|
return toEnvironment(springDataEnvironmentRepository.saveAndFlush(environmentTuple)); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
@Override |
|
26
|
|
|
public Set<String> getNames() { |
|
27
|
|
|
return springDataEnvironmentRepository.getNames(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
@Override |
|
31
|
|
|
public Environment get(String name) { |
|
32
|
|
|
return springDataEnvironmentRepository.findById(name) |
|
33
|
|
|
.map(this::toEnvironment) |
|
34
|
|
|
.orElse(Environment.empty(name)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private EnvironmentTuple toEnvironmentTuple(Environment environment) { |
|
38
|
|
|
return new EnvironmentTuple(environment.getName(), getSpaceServiceVersions(environment)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
private Set<SpaceServiceVersion> getSpaceServiceVersions(Environment environment) { |
|
42
|
|
|
return environment.getSpaceNames().stream() |
|
43
|
|
|
.flatMap(space -> environment.getServices(space).stream() |
|
44
|
|
|
.map(serviceVersion -> new SpaceServiceVersion(space, serviceVersion.getName(), serviceVersion.getVersion()))) |
|
45
|
|
|
.collect(Collectors.toUnmodifiableSet()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private Environment toEnvironment(EnvironmentTuple environmentTuple) { |
|
49
|
|
|
Environment.EnvironmentBuilder environmentBuilder = Environment.builder(environmentTuple.getName()); |
|
50
|
|
|
|
|
51
|
|
|
environmentTuple.getSpaceServiceVersions().forEach(spaceServiceVersion -> |
|
52
|
|
|
environmentBuilder.withServiceVersion( |
|
53
|
|
|
spaceServiceVersion.getSpace(), |
|
54
|
|
|
new ServiceVersion(spaceServiceVersion.getName(), spaceServiceVersion.getVersion()) |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
return environmentBuilder.build(); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|