Simplified + refactored
This commit is contained in:
parent
4f7b0d6a64
commit
9ea4e4f04e
|
@ -34,22 +34,18 @@ public class Application {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void basicLoadAndSave() {
|
private void basicLoadAndSave() {
|
||||||
Customer myCustomer = service.addCustomer("John", "Doe");
|
Customer myCustomer = service.addCustomer("John");
|
||||||
logger.info("Insert -- " + myCustomer.toString());
|
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());
|
logger.info("Update -- " + myCustomer.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void basicLoadAndSaveWithMapper() {
|
private void basicLoadAndSaveWithMapper() {
|
||||||
CustomerDto dto = new CustomerDto();
|
CustomerDto dto = new CustomerDto(null);
|
||||||
dto.firstName = "Johnny";
|
dto.name = "Johnny";
|
||||||
dto.lastName = "Doe";
|
|
||||||
Customer entity = service.addCustomer(dto);
|
Customer entity = service.addCustomer(dto);
|
||||||
logger.info("Insert -- " + entity.toString());
|
logger.info("Insert -- " + entity.toString());
|
||||||
CustomerDto dto2 = new CustomerDto();
|
CustomerDto dto2 = new CustomerDto(entity.id);
|
||||||
dto2.id = entity.id;
|
|
||||||
dto2.address = "Mountain View";
|
|
||||||
dto2.email = "doe@mail.com";
|
|
||||||
dto2.phone = "+44";
|
dto2.phone = "+44";
|
||||||
entity = service.updateCustomer(dto2);
|
entity = service.updateCustomer(dto2);
|
||||||
logger.info("Update -- " + entity.toString());
|
logger.info("Update -- " + entity.toString());
|
||||||
|
|
|
@ -10,10 +10,7 @@ public class Customer {
|
||||||
|
|
||||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
public long id;
|
public long id;
|
||||||
public String firstName;
|
public String name;
|
||||||
public String lastName;
|
|
||||||
public String address;
|
|
||||||
public String email;
|
|
||||||
public String phone;
|
public String phone;
|
||||||
//...
|
//...
|
||||||
public String phone99;
|
public String phone99;
|
||||||
|
@ -21,7 +18,7 @@ public class Customer {
|
||||||
public Customer() {}
|
public Customer() {}
|
||||||
|
|
||||||
@Override public String toString() {
|
@Override public String toString() {
|
||||||
return String.format("Customer %s %s, Address: %s, Email: %s, Phone: %s",
|
return String.format("Customer %s, Phone: %s",
|
||||||
this.firstName, this.lastName, this.address, this.email, this.phone);
|
this.name, this.phone);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,33 +1,26 @@
|
||||||
package com.baeldung.model;
|
package com.baeldung.model;
|
||||||
|
|
||||||
public class CustomerDto {
|
public class CustomerDto {
|
||||||
public long id;
|
private long id;
|
||||||
public String firstName;
|
public String name;
|
||||||
public String lastName;
|
|
||||||
public String address;
|
|
||||||
public String email;
|
|
||||||
public String phone;
|
public String phone;
|
||||||
//...
|
//...
|
||||||
public String phone99;
|
private String phone99;
|
||||||
|
|
||||||
public CustomerDto() {}
|
public CustomerDto(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
public CustomerDto(Customer c) {
|
public CustomerDto(Customer c) {
|
||||||
this.id = c.id;
|
this.id = c.id;
|
||||||
this.firstName = c.firstName;
|
this.name = c.name;
|
||||||
this.lastName = c.lastName;
|
|
||||||
this.address = c.address;
|
|
||||||
this.email = c.email;
|
|
||||||
this.phone = c.phone;
|
this.phone = c.phone;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Customer convertToEntity() {
|
public Customer convertToEntity() {
|
||||||
Customer c = new Customer();
|
Customer c = new Customer();
|
||||||
c.id = id;
|
c.id = id;
|
||||||
c.firstName = firstName;
|
c.name = name;
|
||||||
c.lastName = lastName;
|
|
||||||
c.address = address;
|
|
||||||
c.email = email;
|
|
||||||
c.phone = phone;
|
c.phone = phone;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,18 +16,15 @@ public class CustomerService {
|
||||||
@Autowired CustomerRepository repo;
|
@Autowired CustomerRepository repo;
|
||||||
@Autowired CustomerMapper mapper;
|
@Autowired CustomerMapper mapper;
|
||||||
|
|
||||||
public Customer addCustomer(String firstName, String lastName) {
|
public Customer addCustomer(String name) {
|
||||||
Customer myCustomer = new Customer();
|
Customer myCustomer = new Customer();
|
||||||
myCustomer.firstName = firstName;
|
myCustomer.name = name;
|
||||||
myCustomer.lastName = lastName;
|
|
||||||
repo.save(myCustomer);
|
repo.save(myCustomer);
|
||||||
return 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);
|
Customer myCustomer = repo.findById(id);
|
||||||
myCustomer.address = address;
|
|
||||||
myCustomer.email = email;
|
|
||||||
myCustomer.phone = phone;
|
myCustomer.phone = phone;
|
||||||
repo.save(myCustomer);
|
repo.save(myCustomer);
|
||||||
return myCustomer;
|
return myCustomer;
|
||||||
|
|
Loading…
Reference in New Issue