fixed code formatting issue

This commit is contained in:
akeshri 2020-07-05 21:33:50 +05:30
parent 18894f5c03
commit b59f4d32b4
9 changed files with 193 additions and 191 deletions

View File

@ -9,10 +9,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
*/ */
@SpringBootApplication @SpringBootApplication
public class App public class App {
{ public static void main(String[] args) {
public static void main( String[] args )
{
SpringApplication.run(App.class, args); SpringApplication.run(App.class, args);
} }
} }

View File

@ -20,29 +20,31 @@ import com.fasterxml.jackson.dataformat.csv.CsvSchema;
* *
*/ */
@SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class,WebMvcAutoConfiguration.class}) @SpringBootApplication(exclude = { EmbeddedServletContainerAutoConfiguration.class, WebMvcAutoConfiguration.class })
public class ConsoleApp implements CommandLineRunner { public class ConsoleApp implements CommandLineRunner {
@Autowired @Autowired
private ProductService productService; private ProductService productService;
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(ConsoleApp.class, args); SpringApplication.run(ConsoleApp.class, args);
} }
@Override @Override
public void run(String... args) throws Exception { public void run(String... args) throws Exception {
String filePath = ""; String filePath = "";
if (args != null && args.length == 2 && "Product".equalsIgnoreCase(args[0]) if (args != null && args.length == 2 && "Product".equalsIgnoreCase(args[0]) && (filePath = args[1]).length() > 0) {
&& (filePath = args[1]).length() > 0) { File sourceFile = new File(filePath);
File sourceFile = new File(filePath); if (sourceFile.exists()) {
if (sourceFile.exists()) { CsvMapper mapper = new CsvMapper();
CsvMapper mapper = new CsvMapper(); List<Product> products = mapper.readerFor(Product.class)
List<Product> products = mapper.readerFor(Product.class).with(CsvSchema.emptySchema().withHeader()) .with(CsvSchema.emptySchema()
.<Product>readValues(sourceFile).readAll(); .withHeader())
productService.saveAll(products); .<Product> readValues(sourceFile)
} .readAll();
productService.saveAll(products);
}
} }
} }
} }

View File

@ -23,30 +23,33 @@ import com.baeldung.hexagonal.architecture.service.ProductService;
@RequestMapping("api/v1/product") @RequestMapping("api/v1/product")
public class ProductController { public class ProductController {
@Autowired @Autowired
private ProductService productService; private ProductService productService;
@RequestMapping(value = "/all", method = RequestMethod.GET) @RequestMapping(value = "/all", method = RequestMethod.GET)
public List<ProductDto> list() { public List<ProductDto> list() {
return productService.findAll().stream().map(p -> new ProductDto(p)).collect(Collectors.toList()); return productService.findAll()
} .stream()
.map(p -> new ProductDto(p))
.collect(Collectors.toList());
}
@RequestMapping(value = "/{productId}", method = RequestMethod.GET) @RequestMapping(value = "/{productId}", method = RequestMethod.GET)
public ProductDto get(@PathVariable long productId) { public ProductDto get(@PathVariable long productId) {
Product p = productService.findById(productId); Product p = productService.findById(productId);
return p != null ? new ProductDto(p) : null; return p != null ? new ProductDto(p) : null;
} }
@RequestMapping(value = "/add", method = RequestMethod.POST) @RequestMapping(value = "/add", method = RequestMethod.POST)
public ProductDto create(@RequestBody ProductDto product) { public ProductDto create(@RequestBody ProductDto product) {
Product p = new Product(); Product p = new Product();
p.setDescription(product.getDescription()); p.setDescription(product.getDescription());
p.setName(product.getName()); p.setName(product.getName());
p.setPrice(product.getPrice()); p.setPrice(product.getPrice());
p.setQuantity(product.getQuantity()); p.setQuantity(product.getQuantity());
Long id = productService.create(p); Long id = productService.create(p);
product.setId(id); product.setId(id);
return product; return product;
} }
} }

View File

@ -1,71 +1,72 @@
package com.baeldung.hexagonal.architecture.dtos; package com.baeldung.hexagonal.architecture.dtos;
import com.baeldung.hexagonal.architecture.model.Product; import com.baeldung.hexagonal.architecture.model.Product;
/** /**
* @author AshwiniKeshri * @author AshwiniKeshri
* *
*/ */
public class ProductDto { public class ProductDto {
private Long id; private Long id;
private String name; private String name;
private Long quantity; private Long quantity;
private Double price; private Double price;
private String description; private String description;
public ProductDto() {} public ProductDto() {
}
public ProductDto(Product product) { public ProductDto(Product product) {
this.description = product.getDescription(); this.description = product.getDescription();
this.id = product.getId(); this.id = product.getId();
this.name = product.getName(); this.name = product.getName();
this.price = product.getPrice(); this.price = product.getPrice();
this.quantity = product.getQuantity(); this.quantity = product.getQuantity();
} }
public Long getId() { public Long getId() {
return id; return id;
} }
public void setId(Long id) { public void setId(Long id) {
this.id = id; this.id = id;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public Long getQuantity() { public Long getQuantity() {
return quantity; return quantity;
} }
public void setQuantity(Long quantity) { public void setQuantity(Long quantity) {
this.quantity = quantity; this.quantity = quantity;
} }
public Double getPrice() { public Double getPrice() {
return price; return price;
} }
public void setPrice(Double price) { public void setPrice(Double price) {
this.price = price; this.price = price;
} }
public String getDescription() { public String getDescription() {
return description; return description;
} }
public void setDescription(String description) {
this.description = description;
}
public void setDescription(String description) {
this.description = description;
}
} }

View File

@ -19,69 +19,67 @@ import javax.persistence.Table;
@Entity @Entity
@Table(name = "PRODUCT") @Table(name = "PRODUCT")
public class Product implements Serializable{ public class Product implements Serializable {
/** /**
* *
*/ */
private static final long serialVersionUID = 4000353732860709995L; private static final long serialVersionUID = 4000353732860709995L;
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
@Column(name="NAME") @Column(name = "NAME")
private String name; private String name;
@Column(name = "QUANTITY") @Column(name = "QUANTITY")
private Long quantity; private Long quantity;
@Column(name = "PRICE") @Column(name = "PRICE")
private Double price; private Double price;
@Column(name = "DESCRIPTION") @Column(name = "DESCRIPTION")
private String description; private String description;
public Long getId() { public Long getId() {
return id; return id;
} }
public void setId(Long id) { public void setId(Long id) {
this.id = id; this.id = id;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public Long getQuantity() { public Long getQuantity() {
return quantity; return quantity;
} }
public void setQuantity(Long quantity) { public void setQuantity(Long quantity) {
this.quantity = quantity > 0 ? quantity : 0; this.quantity = quantity > 0 ? quantity : 0;
} }
public Double getPrice() { public Double getPrice() {
return price; return price;
} }
public void setPrice(Double price) { public void setPrice(Double price) {
this.price = price == null ? 0.0 : price; this.price = price == null ? 0.0 : price;
} }
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
} }

View File

@ -9,6 +9,6 @@ import com.baeldung.hexagonal.architecture.model.Product;
* *
*/ */
public interface ProductRepository extends JpaRepository<Product,Long>{ public interface ProductRepository extends JpaRepository<Product, Long> {
} }

View File

@ -11,11 +11,11 @@ import com.baeldung.hexagonal.architecture.model.Product;
public interface ProductService { public interface ProductService {
List<Product> findAll(); List<Product> findAll();
Product findById(long id); Product findById(long id);
Long create(Product product); Long create(Product product);
void saveAll(List<Product> products); void saveAll(List<Product> products);
} }

View File

@ -18,27 +18,27 @@ import com.baeldung.hexagonal.architecture.repository.ProductRepository;
@Service @Service
public class ProductServiceImpl implements ProductService { public class ProductServiceImpl implements ProductService {
private Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class); private Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);
@Autowired @Autowired
private ProductRepository productRepository; private ProductRepository productRepository;
public List<Product> findAll() { public List<Product> findAll() {
return productRepository.findAll(); return productRepository.findAll();
} }
public Product findById(long id) { public Product findById(long id) {
return productRepository.findOne(id); return productRepository.findOne(id);
} }
public Long create(Product product) { public Long create(Product product) {
product = productRepository.saveAndFlush(product); product = productRepository.saveAndFlush(product);
return product.getId(); return product.getId();
} }
@Override @Override
public void saveAll(List<Product> products) { public void saveAll(List<Product> products) {
productRepository.save(products); productRepository.save(products);
} }
} }

View File

@ -26,18 +26,18 @@ import com.baeldung.hexagonal.architecture.service.ProductService;
@ActiveProfiles(value = "test") @ActiveProfiles(value = "test")
public class ProductServiceTest { public class ProductServiceTest {
@Autowired @Autowired
private ProductService productService; private ProductService productService;
@Test @Test
public void testCreateProduct() { public void testCreateProduct() {
Product product = new Product(); Product product = new Product();
product.setDescription("test product"); product.setDescription("test product");
product.setName("Product1"); product.setName("Product1");
product.setPrice(10.0); product.setPrice(10.0);
product.setQuantity(100l); product.setQuantity(100l);
Long id = productService.create(product); Long id = productService.create(product);
Assert.assertTrue(id >0); Assert.assertTrue(id > 0);
} }
} }