Demo application changes to ensure it is working.

This commit is contained in:
Paweł Dyda 2022-12-06 20:35:40 +01:00
parent 05af4cba6d
commit 7c3eac8446
7 changed files with 108 additions and 11 deletions

View File

@ -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;

View File

@ -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)

View File

@ -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<Person, Long> {
}

View File

@ -6,4 +6,7 @@ import pl.edu.amu.demo.life.jpa.Policy;
@Repository
public interface PolicyRepository extends CrudRepository<Policy, Long> {
Iterable<Policy> findAllByInsuredPersonId(Long insuredPersonId);
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -1 +1,2 @@
spring.devtools.livereload.enabled=true
spring.devtools.restart.pollInterval=10s