From 9ea4e4f04ef90d3267f0c2ab7dd6f8431f6a03e1 Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Sat, 16 May 2020 17:56:00 +0200 Subject: [PATCH] Simplified + refactored --- .../main/java/com/baeldung/Application.java | 14 ++++------- .../java/com/baeldung/model/Customer.java | 9 +++----- .../java/com/baeldung/model/CustomerDto.java | 23 +++++++------------ .../com/baeldung/service/CustomerService.java | 9 +++----- 4 files changed, 19 insertions(+), 36 deletions(-) diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java index 64d0a5e6c6..34e86fe135 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java @@ -34,22 +34,18 @@ public class Application { } private void basicLoadAndSave() { - Customer myCustomer = service.addCustomer("John", "Doe"); + Customer myCustomer = service.addCustomer("John"); logger.info("Insert -- " + myCustomer.toString()); - myCustomer = service.updateCustomer(myCustomer.id, "john@doe.com", "+00", "Route 66"); + myCustomer = service.updateCustomer(myCustomer.id, "+00"); logger.info("Update -- " + myCustomer.toString()); } private void basicLoadAndSaveWithMapper() { - CustomerDto dto = new CustomerDto(); - dto.firstName = "Johnny"; - dto.lastName = "Doe"; + CustomerDto dto = new CustomerDto(null); + dto.name = "Johnny"; Customer entity = service.addCustomer(dto); logger.info("Insert -- " + entity.toString()); - CustomerDto dto2 = new CustomerDto(); - dto2.id = entity.id; - dto2.address = "Mountain View"; - dto2.email = "doe@mail.com"; + CustomerDto dto2 = new CustomerDto(entity.id); dto2.phone = "+44"; entity = service.updateCustomer(dto2); logger.info("Update -- " + entity.toString()); diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java index 91808e7971..28c5d5c76c 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java @@ -10,10 +10,7 @@ public class Customer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public long id; - public String firstName; - public String lastName; - public String address; - public String email; + public String name; public String phone; //... public String phone99; @@ -21,7 +18,7 @@ public class Customer { public Customer() {} @Override public String toString() { - return String.format("Customer %s %s, Address: %s, Email: %s, Phone: %s", - this.firstName, this.lastName, this.address, this.email, this.phone); + return String.format("Customer %s, Phone: %s", + this.name, this.phone); } } diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java index 8f7803fc32..c4601d5b81 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java @@ -1,33 +1,26 @@ package com.baeldung.model; public class CustomerDto { - public long id; - public String firstName; - public String lastName; - public String address; - public String email; + private long id; + public String name; public String phone; //... - public String phone99; + private String phone99; - public CustomerDto() {} + public CustomerDto(long id) { + this.id = id; + } public CustomerDto(Customer c) { this.id = c.id; - this.firstName = c.firstName; - this.lastName = c.lastName; - this.address = c.address; - this.email = c.email; + this.name = c.name; this.phone = c.phone; } public Customer convertToEntity() { Customer c = new Customer(); c.id = id; - c.firstName = firstName; - c.lastName = lastName; - c.address = address; - c.email = email; + c.name = name; c.phone = phone; return c; } diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java index 56afc9e80d..3101cd8ece 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java @@ -16,18 +16,15 @@ public class CustomerService { @Autowired CustomerRepository repo; @Autowired CustomerMapper mapper; - public Customer addCustomer(String firstName, String lastName) { + public Customer addCustomer(String name) { Customer myCustomer = new Customer(); - myCustomer.firstName = firstName; - myCustomer.lastName = lastName; + myCustomer.name = name; repo.save(myCustomer); return myCustomer; } - public Customer updateCustomer(long id, String email, String phone, String address) { + public Customer updateCustomer(long id, String phone) { Customer myCustomer = repo.findById(id); - myCustomer.address = address; - myCustomer.email = email; myCustomer.phone = phone; repo.save(myCustomer); return myCustomer;