Initial version of demo code; Work In Progress.

This commit is contained in:
Paweł Dyda 2022-12-03 20:45:48 +01:00
parent 6596d78718
commit f8f9ce08ea
7 changed files with 230 additions and 2 deletions

View File

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

View File

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

View File

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

View File

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

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.Policy;
@Repository
public interface PolicyRepository extends CrudRepository<Policy, Long> {
}

View File

@ -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<PolicyDTO> getAllPolicies() {
return StreamSupport.stream(policyRepository.findAll().spliterator(), false)
.map(PolicyDTO::fromPolicy)
.collect(toList());
}
public void savePolicy(PolicyDTO policyDTO) {
policyRepository.save(policyDTO.toPolicy());
}
}

View File

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