Simplified + refactored

This commit is contained in:
Michele Guarnaccia 2020-05-16 17:56:00 +02:00
parent 4f7b0d6a64
commit 9ea4e4f04e
4 changed files with 19 additions and 36 deletions

View File

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

View File

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

View File

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

View File

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