Implemented getSystem method end-to-end

This commit is contained in:
Jim Martens 2020-07-26 19:05:05 +02:00
parent 41d67bf5d6
commit 8968ecc536
3 changed files with 51 additions and 89 deletions

View File

@ -2,23 +2,30 @@ package de.twomartens.oparlservice.entity.internal;
import lombok.*; import lombok.*;
import javax.persistence.*;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
@Getter @Getter
@Setter
@ToString @ToString
@EqualsAndHashCode @EqualsAndHashCode
@RequiredArgsConstructor @AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "systems")
public class System { public class System {
private final int id; @Id
private final String license; @GeneratedValue(strategy = GenerationType.IDENTITY)
private final String name; private long id;
private final String contactEmail; private String license;
private final String contactName; private String name;
private final String website; private String contactEmail;
private final String vendor; private String contactName;
private final String product; private String website;
private final String web; private String vendor;
private final ZonedDateTime created; private String product;
private final ZonedDateTime modified; private String web;
private final boolean deleted; private ZonedDateTime created;
private ZonedDateTime modified;
private boolean deleted;
} }

View File

@ -1,65 +1,9 @@
package de.twomartens.oparlservice.repository; package de.twomartens.oparlservice.repository;
import de.twomartens.oparlservice.entity.internal.System; import de.twomartens.oparlservice.entity.internal.System;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository @Repository
public class SystemRepository implements CrudRepository<System, Integer> { public interface SystemRepository extends JpaRepository<System, Integer> {
@Override
public <S extends System> S save(S entity) {
return null;
}
@Override
public <S extends System> Iterable<S> saveAll(Iterable<S> entities) {
return null;
}
@Override
public Optional<System> findById(Integer integer) {
return Optional.empty();
}
@Override
public boolean existsById(Integer integer) {
return false;
}
@Override
public Iterable<System> findAll() {
return null;
}
@Override
public Iterable<System> findAllById(Iterable<Integer> integers) {
return null;
}
@Override
public long count() {
return 0;
}
@Override
public void deleteById(Integer integer) {
}
@Override
public void delete(System entity) {
}
@Override
public void deleteAll(Iterable<? extends System> entities) {
}
@Override
public void deleteAll() {
}
} }

View File

@ -1,14 +1,14 @@
package de.twomartens.oparlservice.service; package de.twomartens.oparlservice.service;
import de.twomartens.oparlservice.configs.OParlServiceProperties; import de.twomartens.oparlservice.configs.OParlServiceProperties;
import de.twomartens.oparlservice.entity.dto.*;
import de.twomartens.oparlservice.entity.dto.System; import de.twomartens.oparlservice.entity.dto.System;
import de.twomartens.oparlservice.entity.dto.*;
import de.twomartens.oparlservice.repository.SystemRepository; import de.twomartens.oparlservice.repository.SystemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
@ -18,8 +18,8 @@ public class OParlService {
private final OParlServiceProperties properties; private final OParlServiceProperties properties;
private final SystemRepository systemRepository; private final SystemRepository systemRepository;
public OParlService(OParlServiceProperties properties, @Autowired
SystemRepository systemRepository) { public OParlService(OParlServiceProperties properties, SystemRepository systemRepository) {
this.properties = properties; this.properties = properties;
this.systemRepository = systemRepository; this.systemRepository = systemRepository;
} }
@ -122,27 +122,38 @@ public class OParlService {
public System getSystem() { public System getSystem() {
Iterable<de.twomartens.oparlservice.entity.internal.System> systems = systemRepository.findAll(); Iterable<de.twomartens.oparlservice.entity.internal.System> systems = systemRepository.findAll();
de.twomartens.oparlservice.entity.internal.System firstNotDeletedSystem = StreamSupport Optional<de.twomartens.oparlservice.entity.internal.System> firstNotDeletedSystem = StreamSupport
.stream(systems.spliterator(), false) .stream(systems.spliterator(), false)
.filter(system -> !system.isDeleted()) .filter(system -> !system.isDeleted())
.findFirst().orElseThrow(); .findFirst();
de.twomartens.oparlservice.entity.internal.System system;
if (firstNotDeletedSystem.isEmpty()) {
de.twomartens.oparlservice.entity.internal.System newSystem = new de.twomartens.oparlservice.entity.internal.System();
newSystem.setCreated(ZonedDateTime.now());
newSystem.setModified(ZonedDateTime.now());
newSystem.setDeleted(false);
system = systemRepository.saveAndFlush(newSystem);
} else {
system = firstNotDeletedSystem.get();
}
return System.builder() return System.builder()
.id(String.valueOf(firstNotDeletedSystem.getId())) .id(String.valueOf(system.getId()))
.type("https://schema.oparl.org/1.1/Schema") .type("https://schema.oparl.org/1.1/Schema")
.created(firstNotDeletedSystem.getCreated()) .created(system.getCreated())
.modified(firstNotDeletedSystem.getModified()) .modified(system.getModified())
.oparlVersion("https://schema.oparl.org/1.1/") .oparlVersion("https://schema.oparl.org/1.1/")
.body(properties.getUrl() + "/bodies") .body(properties.getUrl() + "/bodies")
.license(firstNotDeletedSystem.getLicense()) .license(system.getLicense())
.name(firstNotDeletedSystem.getName()) .name(system.getName())
.contactEmail(firstNotDeletedSystem.getContactEmail()) .contactEmail(system.getContactEmail())
.contactName(firstNotDeletedSystem.getContactName()) .contactName(system.getContactName())
.deleted(firstNotDeletedSystem.isDeleted()) .deleted(system.isDeleted())
.product(firstNotDeletedSystem.getProduct()) .product(system.getProduct())
.vendor(firstNotDeletedSystem.getVendor()) .vendor(system.getVendor())
.web(firstNotDeletedSystem.getWeb()) .web(system.getWeb())
.website(firstNotDeletedSystem.getWebsite()) .website(system.getWebsite())
.otherOparlVersions(Collections.emptyList()) .otherOparlVersions(Collections.emptyList())
.build(); .build();
} }