diff --git a/demo/05-06/src/main/java/pl/edu/amu/demo/life/dto/PersonDTO.java b/demo/05-06/src/main/java/pl/edu/amu/demo/life/dto/PersonDTO.java new file mode 100644 index 0000000..86c0f30 --- /dev/null +++ b/demo/05-06/src/main/java/pl/edu/amu/demo/life/dto/PersonDTO.java @@ -0,0 +1,36 @@ +package pl.edu.amu.demo.life.dto; + +import lombok.*; +import lombok.experimental.FieldDefaults; +import pl.edu.amu.demo.life.jpa.Person; + +@FieldDefaults(level = AccessLevel.PRIVATE) +@AllArgsConstructor +@NoArgsConstructor +@Setter +@Getter +@EqualsAndHashCode +@ToString +public class PersonDTO { + + Long id; + String displayName; + String firstName; + String lastName; + Long clientId; + + public Person toPerson() { + return new Person(id, displayName, firstName, lastName, clientId); + } + + public static PersonDTO fromPerson(Person person) { + return new PersonDTO( + person.getId(), + person.getDisplayName(), + person.getFirstName(), + person.getLastName(), + person.getClientId() + ); + } + +} 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 new file mode 100644 index 0000000..d7d537b --- /dev/null +++ b/demo/05-06/src/main/java/pl/edu/amu/demo/life/dto/PolicyDTO.java @@ -0,0 +1,49 @@ +package pl.edu.amu.demo.life.dto; + +import lombok.*; +import lombok.experimental.FieldDefaults; +import pl.edu.amu.demo.life.jpa.Policy; +import pl.edu.amu.demo.life.jpa.PolicyStatus; + +import java.math.BigDecimal; +import java.time.LocalDate; + +@FieldDefaults(level = AccessLevel.PRIVATE) +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode +@ToString +public class PolicyDTO { + + Long id; + String number; + BigDecimal insuredSum; + LocalDate startDate; + LocalDate endDate; + PersonDTO insuredPerson; + String status; + LocalDate statusChangeDate; + + public Policy toPolicy() { + var policyStatus = PolicyStatus.valueOf(status); + return new Policy( + id, number, insuredSum, startDate, endDate, insuredPerson.toPerson(), policyStatus, statusChangeDate + ); + } + + public static PolicyDTO fromPolicy(Policy policy) { + return new PolicyDTO( + policy.getId(), + policy.getNumber(), + policy.getInsuredSum(), + policy.getStartDate(), + policy.getEndDate(), + PersonDTO.fromPerson(policy.getInsuredPerson()), + policy.getStatus().name(), + policy.getStatusChangeDate() + ); + } + +} diff --git a/demo/05-06/src/main/java/pl/edu/amu/demo/life/jpa/Person.java b/demo/05-06/src/main/java/pl/edu/amu/demo/life/jpa/Person.java index d6d49b9..c0c7573 100644 --- a/demo/05-06/src/main/java/pl/edu/amu/demo/life/jpa/Person.java +++ b/demo/05-06/src/main/java/pl/edu/amu/demo/life/jpa/Person.java @@ -1,7 +1,6 @@ package pl.edu.amu.demo.life.jpa; -import lombok.Getter; -import lombok.Setter; +import lombok.*; import javax.persistence.Column; import javax.persistence.Entity; @@ -12,6 +11,9 @@ import javax.persistence.Id; @Entity @Getter @Setter +@NoArgsConstructor +@AllArgsConstructor +@ToString public class Person { @Id @@ -28,4 +30,7 @@ public class Person { @Column(name = "person_last_name") private String lastName; + @Column(name = "person_client_id", unique = true, nullable = false) + private Long clientId; + } 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 new file mode 100644 index 0000000..0e65696 --- /dev/null +++ b/demo/05-06/src/main/java/pl/edu/amu/demo/life/jpa/Policy.java @@ -0,0 +1,43 @@ +package pl.edu.amu.demo.life.jpa; + +import lombok.*; + +import javax.persistence.*; +import java.math.BigDecimal; +import java.time.LocalDate; + +@Entity +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@ToString +public class Policy { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "policy_id") + private Long id; + + @Column(name = "policy_number", unique = true, nullable = false, updatable = false) + private String number; + + @Column(name = "policy_insured_sum", nullable = false) + private BigDecimal insuredSum; + + @Column(name = "policy_start_date", nullable = false) + private LocalDate startDate; + + @Column(name = "policy_end_date", nullable = false) + private LocalDate endDate; + + @ManyToOne(cascade = CascadeType.ALL, optional = false) + private Person insuredPerson; + + @Column(name = "policy_status", nullable = false) + private PolicyStatus status; + + @Column(name = "policy") + private LocalDate statusChangeDate; + +} 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 new file mode 100644 index 0000000..9523c07 --- /dev/null +++ b/demo/05-06/src/main/java/pl/edu/amu/demo/life/repository/PolicyRepository.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.Policy; + +@Repository +public interface PolicyRepository extends CrudRepository { +} 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 new file mode 100644 index 0000000..55375f1 --- /dev/null +++ b/demo/05-06/src/main/java/pl/edu/amu/demo/life/service/PolicyService.java @@ -0,0 +1,46 @@ +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.PolicyDTO; +import pl.edu.amu.demo.life.repository.PolicyRepository; + +import java.util.List; +import java.util.stream.StreamSupport; + +import static java.util.stream.Collectors.toList; + +@Service +@Slf4j +public class PolicyService { + + private final PolicyRepository policyRepository; + + @Autowired + public PolicyService(PolicyRepository policyRepository) { + this.policyRepository = policyRepository; + } + + public long countPolicies() { + log.info("Repository class: {}", policyRepository.getClass().getCanonicalName()); + var policies = policyRepository.findAll(); + var policiesCount = StreamSupport.stream(policies.spliterator(), false).count(); + log.info("{} policies found in the repository.", policiesCount); + return policiesCount; + } + + // Don't do that!!! + // NEVER, EVER YOU SHOULD QUERY FOR ALL!!!1 + // Just an example! + public List getAllPolicies() { + return StreamSupport.stream(policyRepository.findAll().spliterator(), false) + .map(PolicyDTO::fromPolicy) + .collect(toList()); + } + + public void savePolicy(PolicyDTO policyDTO) { + policyRepository.save(policyDTO.toPolicy()); + } + +} 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 new file mode 100644 index 0000000..3efeb40 --- /dev/null +++ b/demo/05-06/src/main/java/pl/edu/amu/demo/life/web/rest/PolicyController.java @@ -0,0 +1,40 @@ +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 pl.edu.amu.demo.life.dto.PolicyDTO; +import pl.edu.amu.demo.life.service.PolicyService; + +import java.util.List; + +@RestController +public class PolicyController { + + final PolicyService service; + + @Autowired + public PolicyController(PolicyService service) { + this.service = service; + } + + @GetMapping(path = "/policies/count") + public long getCount() { + return service.countPolicies(); + } + + @GetMapping(path = "policies/list") + public List getAllPolicies() { + // AGAIN, DO NOT DO THAT IN REAL LIFE!!! + // YOU ALWAYS NEED WHERE CLAUSE WHILE QUERYING A DATABASE!!! + return service.getAllPolicies(); + } + + @PutMapping(path = "policies/add") + public void putNewPolicy(@RequestParam PolicyDTO policyDTO) { + service.savePolicy(policyDTO); + } + +}