1
0

patch-with-getReference, no need for fetch anymore

This commit is contained in:
Marc O. Sandlus
2022-09-26 12:40:59 +02:00
committed by Michael Hoennig
parent d13f0cbcdf
commit 89415067ef
5 changed files with 36 additions and 46 deletions

View File

@ -12,6 +12,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import javax.persistence.EntityManager;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.UUID;
@ -35,6 +36,9 @@ public class HsOfficePartnerController implements HsOfficePartnersApi {
@Autowired
private HsOfficeContactRepository contactRepo;
@Autowired
private EntityManager em;
@Override
@Transactional(readOnly = true)
public ResponseEntity<List<HsOfficePartnerResource>> listPartners(
@ -124,7 +128,7 @@ public class HsOfficePartnerController implements HsOfficePartnersApi {
final var current = partnerRepo.findByUuid(partnerUuid).orElseThrow();
new HsOfficePartnerEntityPatcher(current, contactRepo::findByUuid, personRepo::findByUuid).apply(body);
new HsOfficePartnerEntityPatcher(em, current, contactRepo::findByUuid, personRepo::findByUuid).apply(body);
final var saved = partnerRepo.save(current);
final var mapped = map(saved, HsOfficePartnerResource.class);

View File

@ -6,22 +6,26 @@ import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactEntity;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePartnerPatchResource;
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntity;
import javax.persistence.EntityManager;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Supplier;
class HsOfficePartnerEntityPatcher implements EntityPatcher<HsOfficePartnerPatchResource> {
class HsOfficePartnerEntityPatcher implements EntityPatcher<HsOfficePartnerPatchResource> {
private final EntityManager em;
private final HsOfficePartnerEntity entity;
private final Function<UUID, Optional<HsOfficeContactEntity>> fetchContact;
private final Function<UUID, Optional<HsOfficePersonEntity>> fetchPerson;
HsOfficePartnerEntityPatcher(
final EntityManager em,
final HsOfficePartnerEntity entity,
final Function<UUID, Optional<HsOfficeContactEntity>> fetchContact,
final Function<UUID, Optional<HsOfficePersonEntity>> fetchPerson) {
this.em = em;
this.entity = entity;
this.fetchContact = fetchContact;
this.fetchPerson = fetchPerson;
@ -31,13 +35,11 @@ class HsOfficePartnerEntityPatcher implements EntityPatcher<HsOfficePartnerPatch
public void apply(final HsOfficePartnerPatchResource resource) {
OptionalFromJson.of(resource.getContactUuid()).ifPresent(newValue -> {
verifyNotNull(newValue, "contact");
entity.setContact(fetchContact.apply(newValue)
.orElseThrow(noSuchElementException("contact", newValue)));
entity.setContact(em.getReference(HsOfficeContactEntity.class, newValue));
});
OptionalFromJson.of(resource.getPersonUuid()).ifPresent(newValue -> {
verifyNotNull(newValue, "person");
entity.setPerson(fetchPerson.apply(newValue)
.orElseThrow(noSuchElementException("person", newValue)));
entity.setPerson(em.getReference(HsOfficePersonEntity.class, newValue));
});
OptionalFromJson.of(resource.getRegistrationOffice()).ifPresent(entity::setRegistrationOffice);
OptionalFromJson.of(resource.getRegistrationNumber()).ifPresent(entity::setRegistrationNumber);
@ -46,10 +48,6 @@ class HsOfficePartnerEntityPatcher implements EntityPatcher<HsOfficePartnerPatch
OptionalFromJson.of(resource.getDateOfDeath()).ifPresent(entity::setDateOfDeath);
}
private Supplier<RuntimeException> noSuchElementException(final String propertyName, final UUID newValue) {
return () -> new NoSuchElementException("cannot find '" + propertyName + "' uuid " + newValue);
}
private void verifyNotNull(final UUID newValue, final String propertyName) {
if (newValue == null) {
throw new IllegalArgumentException("property '" + propertyName + "' must not be null");