mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-16 00:59:02 +00:00
docs: data transfer object + refactor
This commit is contained in:
@@ -67,15 +67,15 @@ public class App {
|
||||
|
||||
var customerResource = new CustomerResource(customers);
|
||||
|
||||
LOGGER.info("All customers:-");
|
||||
var allCustomers = customerResource.getAllCustomers();
|
||||
LOGGER.info("All customers:");
|
||||
var allCustomers = customerResource.customers();
|
||||
printCustomerDetails(allCustomers);
|
||||
|
||||
LOGGER.info("----------------------------------------------------------");
|
||||
|
||||
LOGGER.info("Deleting customer with id {1}");
|
||||
customerResource.delete(customerOne.getId());
|
||||
allCustomers = customerResource.getAllCustomers();
|
||||
customerResource.delete(customerOne.id());
|
||||
allCustomers = customerResource.customers();
|
||||
printCustomerDetails(allCustomers);
|
||||
|
||||
LOGGER.info("----------------------------------------------------------");
|
||||
@@ -83,7 +83,7 @@ public class App {
|
||||
LOGGER.info("Adding customer three}");
|
||||
var customerThree = new CustomerDto("3", "Lynda", "Blair");
|
||||
customerResource.save(customerThree);
|
||||
allCustomers = customerResource.getAllCustomers();
|
||||
allCustomers = customerResource.customers();
|
||||
printCustomerDetails(allCustomers);
|
||||
|
||||
// Example 2: Product DTO
|
||||
@@ -131,10 +131,10 @@ public class App {
|
||||
productResource.save(createProductRequestDto);
|
||||
LOGGER.info(
|
||||
"####### List of products after adding PS5: {}",
|
||||
Arrays.toString(productResource.getProducts().toArray()));
|
||||
Arrays.toString(productResource.products().toArray()));
|
||||
}
|
||||
|
||||
private static void printCustomerDetails(List<CustomerDto> allCustomers) {
|
||||
allCustomers.forEach(customer -> LOGGER.info(customer.getFirstName()));
|
||||
allCustomers.forEach(customer -> LOGGER.info(customer.firstName()));
|
||||
}
|
||||
}
|
||||
|
||||
+1
-8
@@ -33,11 +33,4 @@ import lombok.RequiredArgsConstructor;
|
||||
*
|
||||
* <p>Dto will not have any business logic in it.
|
||||
*/
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class CustomerDto {
|
||||
private final String id;
|
||||
private final String firstName;
|
||||
private final String lastName;
|
||||
|
||||
}
|
||||
public record CustomerDto(String id, String firstName, String lastName) {}
|
||||
|
||||
+4
-15
@@ -25,25 +25,14 @@
|
||||
package com.iluwatar.datatransfer.customer;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* The resource class which serves customer information. This class act as server in the demo. Which
|
||||
* has all customer details.
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class CustomerResource {
|
||||
private final List<CustomerDto> customers;
|
||||
|
||||
/**
|
||||
* Get all customers.
|
||||
*
|
||||
* @return : all customers in list.
|
||||
*/
|
||||
public List<CustomerDto> getAllCustomers() {
|
||||
return customers;
|
||||
}
|
||||
|
||||
public record CustomerResource(List<CustomerDto> customers) {
|
||||
/**
|
||||
* Save new customer.
|
||||
*
|
||||
@@ -59,6 +48,6 @@ public class CustomerResource {
|
||||
* @param customerId delete customer with id {@code customerId}
|
||||
*/
|
||||
public void delete(String customerId) {
|
||||
customers.removeIf(customer -> customer.getId().equals(customerId));
|
||||
customers.removeIf(customer -> customer.id().equals(customerId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+19
-21
@@ -30,16 +30,13 @@ import java.util.List;
|
||||
* The resource class which serves product information. This class act as server in the demo. Which
|
||||
* has all product details.
|
||||
*/
|
||||
public class ProductResource {
|
||||
private final List<Product> products;
|
||||
|
||||
public record ProductResource(List<Product> products) {
|
||||
/**
|
||||
* Initialise resource with existing products.
|
||||
*
|
||||
* @param products initialize resource with existing products. Act as database.
|
||||
*/
|
||||
public ProductResource(final List<Product> products) {
|
||||
this.products = products;
|
||||
public ProductResource {
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,11 +46,11 @@ public class ProductResource {
|
||||
*/
|
||||
public List<ProductDto.Response.Private> getAllProductsForAdmin() {
|
||||
return products
|
||||
.stream()
|
||||
.map(p -> new ProductDto.Response.Private().setId(p.getId()).setName(p.getName())
|
||||
.setCost(p.getCost())
|
||||
.setPrice(p.getPrice()))
|
||||
.toList();
|
||||
.stream()
|
||||
.map(p -> new ProductDto.Response.Private().setId(p.getId()).setName(p.getName())
|
||||
.setCost(p.getCost())
|
||||
.setPrice(p.getPrice()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,10 +60,10 @@ public class ProductResource {
|
||||
*/
|
||||
public List<ProductDto.Response.Public> getAllProductsForCustomer() {
|
||||
return products
|
||||
.stream()
|
||||
.map(p -> new ProductDto.Response.Public().setId(p.getId()).setName(p.getName())
|
||||
.setPrice(p.getPrice()))
|
||||
.toList();
|
||||
.stream()
|
||||
.map(p -> new ProductDto.Response.Public().setId(p.getId()).setName(p.getName())
|
||||
.setPrice(p.getPrice()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,12 +73,12 @@ public class ProductResource {
|
||||
*/
|
||||
public void save(ProductDto.Request.Create createProductDto) {
|
||||
products.add(Product.builder()
|
||||
.id((long) (products.size() + 1))
|
||||
.name(createProductDto.getName())
|
||||
.supplier(createProductDto.getSupplier())
|
||||
.price(createProductDto.getPrice())
|
||||
.cost(createProductDto.getCost())
|
||||
.build());
|
||||
.id((long) (products.size() + 1))
|
||||
.name(createProductDto.getName())
|
||||
.supplier(createProductDto.getSupplier())
|
||||
.price(createProductDto.getPrice())
|
||||
.cost(createProductDto.getCost())
|
||||
.build());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,7 +86,8 @@ public class ProductResource {
|
||||
*
|
||||
* @return : all the products entity that stored in the products list
|
||||
*/
|
||||
public List<Product> getProducts() {
|
||||
@Override
|
||||
public List<Product> products() {
|
||||
return products;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user