Passed
Push — master ( 60fd5a...4bedd1 )
by Tomasz
04:04
created

findOne(ServiceVersion)   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
dl 0
loc 5
rs 10
1
package dev.hltech.dredd.domain.contracts;
2
3
import dev.hltech.dredd.domain.ServiceVersion;
4
import org.springframework.beans.factory.annotation.Autowired;
5
import org.springframework.stereotype.Component;
6
7
import javax.persistence.EntityManager;
8
import java.util.List;
9
import java.util.Optional;
10
11
import static java.util.Optional.ofNullable;
12
13
@Component
14
public class JpaServiceContractsRepository implements ServiceContractsRepository {
15
16
    private final EntityManager entityManager;
17
18
    @Autowired
19
    public JpaServiceContractsRepository(EntityManager entityManager) {
20
        this.entityManager = entityManager;
21
    }
22
23
    @Override
24
    public ServiceContracts persist(ServiceContracts serviceContracts) {
25
        return entityManager.merge(serviceContracts);
26
    }
27
28
    @Override
29
    public Optional<ServiceContracts> findOne(ServiceVersion serviceVersion) {
30
        return ofNullable(entityManager.find(
31
            ServiceContracts.class,
32
            new ServiceVersion(serviceVersion.getName(), serviceVersion.getVersion())
33
        ));
34
    }
35
36
    @Override
37
    public String getService(String name) {
38
        return entityManager
39
            .createQuery("select distinct o.id.name from " + ServiceContracts.class.getName() + " o where o.id.name = :name", String.class)
40
            .setParameter("name", name)
41
            .getSingleResult();
42
    }
43
44
    @Override
45
    public List<ServiceContracts> findAllByServiceName(String name) {
46
        return entityManager
47
            .createQuery("select o from " + ServiceContracts.class.getName() + " o where o.id.name = :name", ServiceContracts.class)
48
            .setParameter("name", name)
49
            .getResultList();
50
    }
51
52
    @Override
53
    public List<String> getServiceNames() {
54
        return entityManager
55
            .createQuery("select distinct o.id.name from " + ServiceContracts.class.getName() + " o", String.class)
56
            .getResultList();
57
    }
58
59
}
60