From 7c3eac8446687bab39210e1cee7f3505a554bf61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Dyda?= Date: Tue, 6 Dec 2022 20:35:40 +0100 Subject: [PATCH] Demo application changes to ensure it is working. --- .../pl/edu/amu/demo/life/dto/PolicyDTO.java | 1 + .../java/pl/edu/amu/demo/life/jpa/Policy.java | 6 +- .../life/repository/PersonRepository.java | 9 +++ .../life/repository/PolicyRepository.java | 3 + .../amu/demo/life/service/PolicyService.java | 74 ++++++++++++++++++- .../demo/life/web/rest/PolicyController.java | 23 ++++-- .../src/main/resources/application.properties | 3 +- 7 files changed, 108 insertions(+), 11 deletions(-) create mode 100644 demo/05-06/src/main/java/pl/edu/amu/demo/life/repository/PersonRepository.java diff --git a/demo/05-06/src/main/java/pl/edu/amu/demo/life/dto/PolicyDTO.java b/demo/05-06/src/main/java/pl/edu/amu/demo/life/dto/PolicyDTO.java index d7d537b..55ccebc 100644 --- a/demo/05-06/src/main/java/pl/edu/amu/demo/life/dto/PolicyDTO.java +++ b/demo/05-06/src/main/java/pl/edu/amu/demo/life/dto/PolicyDTO.java @@ -2,6 +2,7 @@ package pl.edu.amu.demo.life.dto; import lombok.*; import lombok.experimental.FieldDefaults; +import pl.edu.amu.demo.life.jpa.Person; import pl.edu.amu.demo.life.jpa.Policy; import pl.edu.amu.demo.life.jpa.PolicyStatus; diff --git a/demo/05-06/src/main/java/pl/edu/amu/demo/life/jpa/Policy.java b/demo/05-06/src/main/java/pl/edu/amu/demo/life/jpa/Policy.java index 0e65696..081b384 100644 --- a/demo/05-06/src/main/java/pl/edu/amu/demo/life/jpa/Policy.java +++ b/demo/05-06/src/main/java/pl/edu/amu/demo/life/jpa/Policy.java @@ -22,16 +22,16 @@ public class Policy { @Column(name = "policy_number", unique = true, nullable = false, updatable = false) private String number; - @Column(name = "policy_insured_sum", nullable = false) + @Column(name = "policy_insured_sum", nullable = false, updatable = false) private BigDecimal insuredSum; - @Column(name = "policy_start_date", nullable = false) + @Column(name = "policy_start_date", nullable = false, updatable = false) private LocalDate startDate; @Column(name = "policy_end_date", nullable = false) private LocalDate endDate; - @ManyToOne(cascade = CascadeType.ALL, optional = false) + @ManyToOne(cascade = CascadeType.ALL) private Person insuredPerson; @Column(name = "policy_status", nullable = false) diff --git a/demo/05-06/src/main/java/pl/edu/amu/demo/life/repository/PersonRepository.java b/demo/05-06/src/main/java/pl/edu/amu/demo/life/repository/PersonRepository.java new file mode 100644 index 0000000..feff43a --- /dev/null +++ b/demo/05-06/src/main/java/pl/edu/amu/demo/life/repository/PersonRepository.java @@ -0,0 +1,9 @@ +package pl.edu.amu.demo.life.repository; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; +import pl.edu.amu.demo.life.jpa.Person; + +@Repository +public interface PersonRepository extends CrudRepository { +} diff --git a/demo/05-06/src/main/java/pl/edu/amu/demo/life/repository/PolicyRepository.java b/demo/05-06/src/main/java/pl/edu/amu/demo/life/repository/PolicyRepository.java index 9523c07..25171f2 100644 --- a/demo/05-06/src/main/java/pl/edu/amu/demo/life/repository/PolicyRepository.java +++ b/demo/05-06/src/main/java/pl/edu/amu/demo/life/repository/PolicyRepository.java @@ -6,4 +6,7 @@ import pl.edu.amu.demo.life.jpa.Policy; @Repository public interface PolicyRepository extends CrudRepository { + + Iterable findAllByInsuredPersonId(Long insuredPersonId); + } diff --git a/demo/05-06/src/main/java/pl/edu/amu/demo/life/service/PolicyService.java b/demo/05-06/src/main/java/pl/edu/amu/demo/life/service/PolicyService.java index 55375f1..d0d3b9e 100644 --- a/demo/05-06/src/main/java/pl/edu/amu/demo/life/service/PolicyService.java +++ b/demo/05-06/src/main/java/pl/edu/amu/demo/life/service/PolicyService.java @@ -3,12 +3,19 @@ package pl.edu.amu.demo.life.service; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import pl.edu.amu.demo.life.dto.PersonDTO; import pl.edu.amu.demo.life.dto.PolicyDTO; +import pl.edu.amu.demo.life.jpa.Person; +import pl.edu.amu.demo.life.jpa.Policy; +import pl.edu.amu.demo.life.repository.PersonRepository; import pl.edu.amu.demo.life.repository.PolicyRepository; +import java.time.LocalDate; import java.util.List; import java.util.stream.StreamSupport; +import static java.util.Objects.isNull; +import static java.util.Objects.nonNull; import static java.util.stream.Collectors.toList; @Service @@ -16,10 +23,12 @@ import static java.util.stream.Collectors.toList; public class PolicyService { private final PolicyRepository policyRepository; + private final PersonRepository personRepository; @Autowired - public PolicyService(PolicyRepository policyRepository) { + public PolicyService(PolicyRepository policyRepository, PersonRepository personRepository) { this.policyRepository = policyRepository; + this.personRepository = personRepository; } public long countPolicies() { @@ -40,7 +49,68 @@ public class PolicyService { } public void savePolicy(PolicyDTO policyDTO) { - policyRepository.save(policyDTO.toPolicy()); + Policy policyToAdd = policyDTO.toPolicy(); + if (nonNull(policyToAdd.getId())) { + log.error("Attempt to add policy with id {}", policyToAdd.getId()); + throw new IllegalStateException("New policies must not have an id!"); + } + if (nonNull(policyToAdd.getInsuredPerson().getId())) { + Person person = findPerson(policyToAdd.getInsuredPerson().getId()); + policyToAdd.setInsuredPerson(person); + } + policyRepository.save(policyToAdd); + } + + private Person findPerson(Long personId) { + return personRepository.findById(personId) + .orElseThrow(() -> new IllegalStateException(String.format("Person id not found: %d", personId))); + } + + public void updatePolicy(Long policyId, PolicyDTO policyDTO) { + Policy policy = policyRepository.findById(policyId) + .orElseThrow(() -> new IllegalStateException(String.format("Policy not found: %d", policyId))); + Policy updated = policyDTO.toPolicy(); + if (nonNull(updated.getInsuredSum()) && !policy.getInsuredSum().equals(updated.getInsuredSum())) { + policy.setInsuredSum(updated.getInsuredSum()); + } + if (nonNull(updated.getEndDate()) && !policy.getEndDate().equals(updated.getEndDate())) { + if (!updated.getEndDate().isAfter(policy.getStartDate())) { + throw new IllegalStateException("End date must be after start date!"); + } + policy.setEndDate(updated.getEndDate()); + } + if (nonNull(updated.getStatus()) && !policy.getStatus().equals(updated.getStatus())) { + policy.setStatus(updated.getStatus()); + policy.setStatusChangeDate(LocalDate.now()); + } + policyRepository.save(policy); + } + + public void updatePerson(PersonDTO personDTO) { + if (isNull(personDTO.getId())) { + throw new IllegalStateException("Unable to update person without an id."); + } + var person = findPerson(personDTO.getId()); + if (nonNull(personDTO.getDisplayName()) && !personDTO.getDisplayName().equals(person.getDisplayName())) { + person.setDisplayName(personDTO.getDisplayName()); + } + if (nonNull(personDTO.getFirstName()) && !personDTO.getDisplayName().equals(person.getFirstName())) { + person.setFirstName(personDTO.getFirstName()); + } + if (nonNull(personDTO.getLastName()) && !personDTO.getLastName().equals(person.getLastName())) { + person.setLastName(personDTO.getLastName()); + } + personRepository.save(person); + } + + public void deletePerson(Long personId) { + var personPolicies = policyRepository.findAllByInsuredPersonId(personId); + StreamSupport.stream(personPolicies.spliterator(), false) + .forEach(policy -> { + policy.setInsuredPerson(null); + policyRepository.save(policy); + }); + personRepository.deleteById(personId); } } diff --git a/demo/05-06/src/main/java/pl/edu/amu/demo/life/web/rest/PolicyController.java b/demo/05-06/src/main/java/pl/edu/amu/demo/life/web/rest/PolicyController.java index 3efeb40..88bcf2e 100644 --- a/demo/05-06/src/main/java/pl/edu/amu/demo/life/web/rest/PolicyController.java +++ b/demo/05-06/src/main/java/pl/edu/amu/demo/life/web/rest/PolicyController.java @@ -1,10 +1,8 @@ package pl.edu.amu.demo.life.web.rest; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; +import pl.edu.amu.demo.life.dto.PersonDTO; import pl.edu.amu.demo.life.dto.PolicyDTO; import pl.edu.amu.demo.life.service.PolicyService; @@ -33,8 +31,23 @@ public class PolicyController { } @PutMapping(path = "policies/add") - public void putNewPolicy(@RequestParam PolicyDTO policyDTO) { + public void putNewPolicy(@RequestBody PolicyDTO policyDTO) { service.savePolicy(policyDTO); } + @PostMapping(path = "policies/update/{policyId}") + public void updatePolicy(@PathVariable Long policyId, @RequestBody PolicyDTO policyDTO) { + service.updatePolicy(policyId, policyDTO); + } + + @PostMapping(path = "person/update") + public void updatePerson(@RequestBody PersonDTO personDTO) { + service.updatePerson(personDTO); + } + + @DeleteMapping(path = "person/delete/{personId}") + public void deletePerson(@PathVariable Long personId) { + service.deletePerson(personId); + } + } diff --git a/demo/05-06/src/main/resources/application.properties b/demo/05-06/src/main/resources/application.properties index 8b13789..16fac67 100644 --- a/demo/05-06/src/main/resources/application.properties +++ b/demo/05-06/src/main/resources/application.properties @@ -1 +1,2 @@ - +spring.devtools.livereload.enabled=true +spring.devtools.restart.pollInterval=10s