From 9d664418f18639b5f5455af73d52be6a639c3fd6 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Fri, 6 Nov 2015 16:02:39 -0500 Subject: [PATCH 01/10] Added Index Page and controller --- .../controllers/IndexController.java | 16 ++++++++++++++++ src/main/resources/templates/index.html | 13 +++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 src/main/java/guru/springframework/controllers/IndexController.java create mode 100644 src/main/resources/templates/index.html diff --git a/src/main/java/guru/springframework/controllers/IndexController.java b/src/main/java/guru/springframework/controllers/IndexController.java new file mode 100644 index 00000000..d8cdf576 --- /dev/null +++ b/src/main/java/guru/springframework/controllers/IndexController.java @@ -0,0 +1,16 @@ +package guru.springframework.controllers; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + * Created by jt on 11/6/15. + */ +@Controller +public class IndexController { + + @RequestMapping("/") + public String index(){ + return "index"; + } +} diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 00000000..f5bc0f9f --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,13 @@ + + + + + Spring Core Online Tutorial + + + +

Hello World

+ +

This is my Thymeleaf index page. This is my changed.

+ + \ No newline at end of file From eecfc023334ad012543d08e22cd168720acceb0e Mon Sep 17 00:00:00 2001 From: John Thompson Date: Fri, 6 Nov 2015 16:10:03 -0500 Subject: [PATCH 02/10] Adding .mvn to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 200bfbe2..a9e7e666 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,7 @@ Thumbs.db .project .classpath .idea +.mvn *.iml atlassian-ide-plugin.xml target From d806ca99a63066d13016f8de436765f3ed7101c6 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Fri, 6 Nov 2015 16:12:38 -0500 Subject: [PATCH 03/10] Added Index Page and controller --- src/main/resources/templates/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index f5bc0f9f..4115bf52 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -1,5 +1,5 @@ - + Spring Core Online Tutorial From ab332736c3284f4aa83b400cb0cf172dd30046d6 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Fri, 6 Nov 2015 16:43:09 -0500 Subject: [PATCH 04/10] Added Web Jars --- pom.xml | 12 ++++++++++++ src/main/resources/static/css/spring-core.css | 0 src/main/resources/templates/index.html | 18 ++++++++++++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/static/css/spring-core.css diff --git a/pom.xml b/pom.xml index f138e8df..563ac6ee 100644 --- a/pom.xml +++ b/pom.xml @@ -32,6 +32,18 @@ org.springframework.boot spring-boot-starter-web + + + + org.webjars + bootstrap + 3.3.5 + + + org.webjars + jquery + 2.1.4 + org.springframework.boot diff --git a/src/main/resources/static/css/spring-core.css b/src/main/resources/static/css/spring-core.css new file mode 100644 index 00000000..e69de29b diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 4115bf52..a2ccb97f 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -4,10 +4,24 @@ Spring Core Online Tutorial + + + + + + -

Hello World

+
+

Hello World

+ +

This is my Thymeleaf index page. This is my changed.

+ +
-

This is my Thymeleaf index page. This is my changed.

\ No newline at end of file From 56f9fd5f33226201a42fd7f0604c5eb5ff56327a Mon Sep 17 00:00:00 2001 From: John Thompson Date: Sat, 7 Nov 2015 12:16:11 -0500 Subject: [PATCH 05/10] Added Product Listing --- .../controllers/ProductController.java | 29 ++++++++ .../guru/springframework/domain/Product.java | 45 ++++++++++++ .../services/ProductService.java | 13 ++++ .../services/ProductServiceImpl.java | 72 +++++++++++++++++++ src/main/resources/templates/products.html | 39 ++++++++++ 5 files changed, 198 insertions(+) create mode 100644 src/main/java/guru/springframework/controllers/ProductController.java create mode 100644 src/main/java/guru/springframework/domain/Product.java create mode 100644 src/main/java/guru/springframework/services/ProductService.java create mode 100644 src/main/java/guru/springframework/services/ProductServiceImpl.java create mode 100644 src/main/resources/templates/products.html diff --git a/src/main/java/guru/springframework/controllers/ProductController.java b/src/main/java/guru/springframework/controllers/ProductController.java new file mode 100644 index 00000000..1786ba99 --- /dev/null +++ b/src/main/java/guru/springframework/controllers/ProductController.java @@ -0,0 +1,29 @@ +package guru.springframework.controllers; + +import guru.springframework.services.ProductService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + * Created by jt on 11/6/15. + */ +@Controller +public class ProductController { + + private ProductService productService; + + @Autowired + public void setProductService(ProductService productService) { + this.productService = productService; + } + + @RequestMapping("/products") + public String listProducts(Model model){ + + model.addAttribute("products", productService.listAllProducts()); + + return "products"; + } +} diff --git a/src/main/java/guru/springframework/domain/Product.java b/src/main/java/guru/springframework/domain/Product.java new file mode 100644 index 00000000..d4bf7f75 --- /dev/null +++ b/src/main/java/guru/springframework/domain/Product.java @@ -0,0 +1,45 @@ +package guru.springframework.domain; + +import java.math.BigDecimal; + +/** + * Created by jt on 11/6/15. + */ +public class Product { + private Integer id; + private String description; + private BigDecimal price; + private String imageUrl; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public String getImageUrl() { + return imageUrl; + } + + public void setImageUrl(String imageUrl) { + this.imageUrl = imageUrl; + } +} diff --git a/src/main/java/guru/springframework/services/ProductService.java b/src/main/java/guru/springframework/services/ProductService.java new file mode 100644 index 00000000..1ac18384 --- /dev/null +++ b/src/main/java/guru/springframework/services/ProductService.java @@ -0,0 +1,13 @@ +package guru.springframework.services; + +import guru.springframework.domain.Product; + +import java.util.List; + +/** + * Created by jt on 11/6/15. + */ +public interface ProductService { + + List listAllProducts(); +} diff --git a/src/main/java/guru/springframework/services/ProductServiceImpl.java b/src/main/java/guru/springframework/services/ProductServiceImpl.java new file mode 100644 index 00000000..76ee193a --- /dev/null +++ b/src/main/java/guru/springframework/services/ProductServiceImpl.java @@ -0,0 +1,72 @@ +package guru.springframework.services; + +import guru.springframework.domain.Product; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by jt on 11/6/15. + */ +@Service +public class ProductServiceImpl implements ProductService { + + private Map products; + + public ProductServiceImpl() { + loadProducts(); + } + + @Override + public List listAllProducts() { + return new ArrayList<>(products.values()); + } + + private void loadProducts(){ + products = new HashMap<>(); + + Product product1 = new Product(); + product1.setId(1); + product1.setDescription("Product 1"); + product1.setPrice(new BigDecimal("12.99")); + product1.setImageUrl("http://example.com/product1"); + + products.put(1, product1); + + Product product2 = new Product(); + product2.setId(2); + product2.setDescription("Product 2"); + product2.setPrice(new BigDecimal("14.99")); + product2.setImageUrl("http://example.com/product2"); + + products.put(2, product2); + + Product product3 = new Product(); + product3.setId(3); + product3.setDescription("Product 3"); + product3.setPrice(new BigDecimal("34.99")); + product3.setImageUrl("http://example.com/product3"); + + products.put(3, product3); + + Product product4 = new Product(); + product4.setId(4); + product4.setDescription("Product 4"); + product4.setPrice(new BigDecimal("44.99")); + product4.setImageUrl("http://example.com/product4"); + + products.put(4, product4); + + Product product5 = new Product(); + product5.setId(5); + product5.setDescription("Product 2"); + product5.setPrice(new BigDecimal("25.99")); + product5.setImageUrl("http://example.com/product5"); + + products.put(5, product5); + } +} diff --git a/src/main/resources/templates/products.html b/src/main/resources/templates/products.html new file mode 100644 index 00000000..49e0495c --- /dev/null +++ b/src/main/resources/templates/products.html @@ -0,0 +1,39 @@ + + + + Spring Core Online Tutorial - List Products + + + + + + + + + +
+
+

Product List

+ + + + + + + + + + + + + +
IdDescriptionPriceImage URL
+
+
+ + + \ No newline at end of file From 3e4eebeb845fa1dee7505ac0f149e1198d50fc64 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Mon, 9 Nov 2015 17:09:48 -0500 Subject: [PATCH 06/10] Added Display Product --- .../controllers/ProductController.java | 9 +++ .../services/ProductService.java | 2 + .../services/ProductServiceImpl.java | 5 ++ src/main/resources/templates/product.html | 58 +++++++++++++++++++ src/main/resources/templates/products.html | 2 + 5 files changed, 76 insertions(+) create mode 100644 src/main/resources/templates/product.html diff --git a/src/main/java/guru/springframework/controllers/ProductController.java b/src/main/java/guru/springframework/controllers/ProductController.java index 1786ba99..2168e231 100644 --- a/src/main/java/guru/springframework/controllers/ProductController.java +++ b/src/main/java/guru/springframework/controllers/ProductController.java @@ -4,6 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; /** @@ -26,4 +27,12 @@ public String listProducts(Model model){ return "products"; } + + @RequestMapping("/product/{id}") + public String getProduct(@PathVariable Integer id, Model model){ + + model.addAttribute("product", productService.getProductById(id)); + + return "product"; + } } diff --git a/src/main/java/guru/springframework/services/ProductService.java b/src/main/java/guru/springframework/services/ProductService.java index 1ac18384..db3a69be 100644 --- a/src/main/java/guru/springframework/services/ProductService.java +++ b/src/main/java/guru/springframework/services/ProductService.java @@ -10,4 +10,6 @@ public interface ProductService { List listAllProducts(); + + Product getProductById(Integer id); } diff --git a/src/main/java/guru/springframework/services/ProductServiceImpl.java b/src/main/java/guru/springframework/services/ProductServiceImpl.java index 76ee193a..66d9b757 100644 --- a/src/main/java/guru/springframework/services/ProductServiceImpl.java +++ b/src/main/java/guru/springframework/services/ProductServiceImpl.java @@ -26,6 +26,11 @@ public List listAllProducts() { return new ArrayList<>(products.values()); } + @Override + public Product getProductById(Integer id) { + return products.get(id); + } + private void loadProducts(){ products = new HashMap<>(); diff --git a/src/main/resources/templates/product.html b/src/main/resources/templates/product.html new file mode 100644 index 00000000..e4e19485 --- /dev/null +++ b/src/main/resources/templates/product.html @@ -0,0 +1,58 @@ + + + + Spring Core Online Tutorial - Show Product + + + + + + + + + +
+ +
+
+

Show Product

+
+
+
+
+
+
+ +
+

Product Id

+
+
+
+ +
+

Description

+
+
+
+ +
+

Price

+
+
+
+ +
+

Image

+
+
+
+
+
+
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/products.html b/src/main/resources/templates/products.html index 49e0495c..a27f611b 100644 --- a/src/main/resources/templates/products.html +++ b/src/main/resources/templates/products.html @@ -24,12 +24,14 @@

Product List

Description Price Image URL + List + View From d75c97eb2947fbbc0ae43fc7b836b1ea47f9ff50 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Mon, 9 Nov 2015 17:44:59 -0500 Subject: [PATCH 07/10] Added Create a Product --- .../controllers/ProductController.java | 14 ++++++ .../services/ProductService.java | 2 + .../services/ProductServiceImpl.java | 23 +++++++-- src/main/resources/templates/productform.html | 50 +++++++++++++++++++ src/main/resources/templates/products.html | 5 ++ 5 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 src/main/resources/templates/productform.html diff --git a/src/main/java/guru/springframework/controllers/ProductController.java b/src/main/java/guru/springframework/controllers/ProductController.java index 2168e231..9cdda52e 100644 --- a/src/main/java/guru/springframework/controllers/ProductController.java +++ b/src/main/java/guru/springframework/controllers/ProductController.java @@ -1,11 +1,13 @@ package guru.springframework.controllers; +import guru.springframework.domain.Product; import guru.springframework.services.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; /** * Created by jt on 11/6/15. @@ -35,4 +37,16 @@ public String getProduct(@PathVariable Integer id, Model model){ return "product"; } + + @RequestMapping("/product/new") + public String newProduct(Model model){ + model.addAttribute("product", new Product()); + return "productform"; + } + + @RequestMapping(value = "/product", method = RequestMethod.POST) + public String saveOrUpdateProduct(Product product){ + Product savedProduct = productService.saveOrUpdateProduct(product); + return "redirect:/product/" + savedProduct.getId(); + } } diff --git a/src/main/java/guru/springframework/services/ProductService.java b/src/main/java/guru/springframework/services/ProductService.java index db3a69be..1abcfd3a 100644 --- a/src/main/java/guru/springframework/services/ProductService.java +++ b/src/main/java/guru/springframework/services/ProductService.java @@ -12,4 +12,6 @@ public interface ProductService { List listAllProducts(); Product getProductById(Integer id); + + Product saveOrUpdateProduct(Product product); } diff --git a/src/main/java/guru/springframework/services/ProductServiceImpl.java b/src/main/java/guru/springframework/services/ProductServiceImpl.java index 66d9b757..393ee674 100644 --- a/src/main/java/guru/springframework/services/ProductServiceImpl.java +++ b/src/main/java/guru/springframework/services/ProductServiceImpl.java @@ -4,10 +4,7 @@ import org.springframework.stereotype.Service; import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * Created by jt on 11/6/15. @@ -31,6 +28,24 @@ public Product getProductById(Integer id) { return products.get(id); } + @Override + public Product saveOrUpdateProduct(Product product) { + if (product != null){ + if (product.getId() == null){ + product.setId(getNextKey()); + } + products.put(product.getId(), product); + + return product; + } else { + throw new RuntimeException("Product Can't be nill"); + } + } + + private Integer getNextKey(){ + return Collections.max(products.keySet()) + 1; + } + private void loadProducts(){ products = new HashMap<>(); diff --git a/src/main/resources/templates/productform.html b/src/main/resources/templates/productform.html new file mode 100644 index 00000000..1d080fa2 --- /dev/null +++ b/src/main/resources/templates/productform.html @@ -0,0 +1,50 @@ + + + + Spring Core Online Tutorial - Product Form + + + + + + + + + +
+ +

Product Details

+
+
+ +
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+
+
+
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/products.html b/src/main/resources/templates/products.html index a27f611b..99c876c2 100644 --- a/src/main/resources/templates/products.html +++ b/src/main/resources/templates/products.html @@ -35,6 +35,11 @@

Product List

+
+ +
From b7b97e900f53bb84f0059fc6dcb70aec48814be5 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Mon, 9 Nov 2015 18:00:46 -0500 Subject: [PATCH 08/10] Added Create a Product --- .../guru/springframework/controllers/ProductController.java | 6 ++++++ src/main/resources/templates/products.html | 2 ++ 2 files changed, 8 insertions(+) diff --git a/src/main/java/guru/springframework/controllers/ProductController.java b/src/main/java/guru/springframework/controllers/ProductController.java index 9cdda52e..a4ee0ccf 100644 --- a/src/main/java/guru/springframework/controllers/ProductController.java +++ b/src/main/java/guru/springframework/controllers/ProductController.java @@ -38,6 +38,12 @@ public String getProduct(@PathVariable Integer id, Model model){ return "product"; } + @RequestMapping("product/edit/{id}") + public String edit(@PathVariable Integer id, Model model){ + model.addAttribute("product", productService.getProductById(id)); + return "productform"; + } + @RequestMapping("/product/new") public String newProduct(Model model){ model.addAttribute("product", new Product()); diff --git a/src/main/resources/templates/products.html b/src/main/resources/templates/products.html index 99c876c2..6dde5956 100644 --- a/src/main/resources/templates/products.html +++ b/src/main/resources/templates/products.html @@ -25,6 +25,7 @@

Product List

Price Image URL List + Edit @@ -32,6 +33,7 @@

Product List

View + Edit From 8be594aae0328e82c383678ff753ba30edd3880b Mon Sep 17 00:00:00 2001 From: John Thompson Date: Sat, 14 Nov 2015 14:17:50 -0500 Subject: [PATCH 09/10] Added delete a Product --- .../springframework/controllers/ProductController.java | 7 +++++++ .../java/guru/springframework/services/ProductService.java | 2 ++ .../guru/springframework/services/ProductServiceImpl.java | 5 +++++ src/main/resources/templates/products.html | 2 ++ 4 files changed, 16 insertions(+) diff --git a/src/main/java/guru/springframework/controllers/ProductController.java b/src/main/java/guru/springframework/controllers/ProductController.java index a4ee0ccf..f3352969 100644 --- a/src/main/java/guru/springframework/controllers/ProductController.java +++ b/src/main/java/guru/springframework/controllers/ProductController.java @@ -55,4 +55,11 @@ public String saveOrUpdateProduct(Product product){ Product savedProduct = productService.saveOrUpdateProduct(product); return "redirect:/product/" + savedProduct.getId(); } + + @RequestMapping("/product/delete/{id}") + public String delete(@PathVariable Integer id){ + productService.deleteProduct(id); + + return "redirect:/products"; + } } diff --git a/src/main/java/guru/springframework/services/ProductService.java b/src/main/java/guru/springframework/services/ProductService.java index 1abcfd3a..1f049a7e 100644 --- a/src/main/java/guru/springframework/services/ProductService.java +++ b/src/main/java/guru/springframework/services/ProductService.java @@ -14,4 +14,6 @@ public interface ProductService { Product getProductById(Integer id); Product saveOrUpdateProduct(Product product); + + void deleteProduct(Integer id); } diff --git a/src/main/java/guru/springframework/services/ProductServiceImpl.java b/src/main/java/guru/springframework/services/ProductServiceImpl.java index 393ee674..b7fa098a 100644 --- a/src/main/java/guru/springframework/services/ProductServiceImpl.java +++ b/src/main/java/guru/springframework/services/ProductServiceImpl.java @@ -46,6 +46,11 @@ private Integer getNextKey(){ return Collections.max(products.keySet()) + 1; } + @Override + public void deleteProduct(Integer id) { + products.remove(id); + } + private void loadProducts(){ products = new HashMap<>(); diff --git a/src/main/resources/templates/products.html b/src/main/resources/templates/products.html index 6dde5956..64a57a22 100644 --- a/src/main/resources/templates/products.html +++ b/src/main/resources/templates/products.html @@ -26,6 +26,7 @@

Product List

Image URL List Edit + Delete @@ -34,6 +35,7 @@

Product List

View Edit + Delete From c7f19c785c72f3aedde6a04577553d659e51b1fa Mon Sep 17 00:00:00 2001 From: John Thompson Date: Mon, 16 Nov 2015 18:15:54 -0500 Subject: [PATCH 10/10] added customer --- .../controllers/CustomerController.java | 61 +++++++++++ .../controllers/ProductController.java | 31 +++--- .../guru/springframework/domain/Customer.java | 100 ++++++++++++++++++ .../springframework/domain/DomainObject.java | 11 ++ .../guru/springframework/domain/Product.java | 2 +- .../services/AbstractMapService.java | 50 +++++++++ .../springframework/services/CRUDService.java | 16 +++ .../services/CustomerService.java | 10 ++ .../services/CustomerServiceImpl.java | 77 ++++++++++++++ .../services/ProductService.java | 11 +- .../services/ProductServiceImpl.java | 55 ++++------ .../templates/customer/customerform.html | 86 +++++++++++++++ .../resources/templates/customer/list.html | 50 +++++++++ .../resources/templates/customer/show.html | 94 ++++++++++++++++ src/main/resources/templates/index.html | 7 +- .../{products.html => product/list.html} | 4 +- .../templates/{ => product}/productform.html | 2 +- .../{product.html => product/show.html} | 2 +- 18 files changed, 599 insertions(+), 70 deletions(-) create mode 100644 src/main/java/guru/springframework/controllers/CustomerController.java create mode 100644 src/main/java/guru/springframework/domain/Customer.java create mode 100644 src/main/java/guru/springframework/domain/DomainObject.java create mode 100644 src/main/java/guru/springframework/services/AbstractMapService.java create mode 100644 src/main/java/guru/springframework/services/CRUDService.java create mode 100644 src/main/java/guru/springframework/services/CustomerService.java create mode 100644 src/main/java/guru/springframework/services/CustomerServiceImpl.java create mode 100644 src/main/resources/templates/customer/customerform.html create mode 100644 src/main/resources/templates/customer/list.html create mode 100644 src/main/resources/templates/customer/show.html rename src/main/resources/templates/{products.html => product/list.html} (92%) rename src/main/resources/templates/{ => product}/productform.html (97%) rename src/main/resources/templates/{product.html => product/show.html} (97%) diff --git a/src/main/java/guru/springframework/controllers/CustomerController.java b/src/main/java/guru/springframework/controllers/CustomerController.java new file mode 100644 index 00000000..5b835c0c --- /dev/null +++ b/src/main/java/guru/springframework/controllers/CustomerController.java @@ -0,0 +1,61 @@ +package guru.springframework.controllers; + +import guru.springframework.domain.Customer; +import guru.springframework.services.CustomerService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +/** + * Created by jt on 11/15/15. + */ +@RequestMapping("/customer") +@Controller +public class CustomerController { + + private CustomerService customerService; + + @Autowired + public void setCustomerService(CustomerService customerService) { + this.customerService = customerService; + } + + @RequestMapping({"/list", "/"}) + public String listCustomers(Model model){ + model.addAttribute("customers", customerService.listAll()); + return "customer/list"; + } + + @RequestMapping("/show/{id}") + public String showCustomer(@PathVariable Integer id, Model model){ + model.addAttribute("customer", customerService.getById(id)); + return "customer/show"; + } + + @RequestMapping("/edit/{id}") + public String edit(@PathVariable Integer id, Model model){ + model.addAttribute("customer", customerService.getById(id)); + return "customer/customerform"; + } + + @RequestMapping("/new") + public String newCustomer(Model model){ + model.addAttribute("customer", new Customer()); + return "customer/customerform"; + } + + @RequestMapping(method = RequestMethod.POST) + public String saveOrUpdate(Customer customer){ + Customer newCustomer = customerService.saveOrUpdate(customer); + return "redirect:customer/show/" + newCustomer.getId(); + } + + @RequestMapping("/delete/{id}") + public String delete(@PathVariable Integer id){ + customerService.delete(id); + return "redirect:/customer/list"; + } +} diff --git a/src/main/java/guru/springframework/controllers/ProductController.java b/src/main/java/guru/springframework/controllers/ProductController.java index f3352969..4c60ff50 100644 --- a/src/main/java/guru/springframework/controllers/ProductController.java +++ b/src/main/java/guru/springframework/controllers/ProductController.java @@ -22,44 +22,39 @@ public void setProductService(ProductService productService) { this.productService = productService; } - @RequestMapping("/products") + @RequestMapping("/product/list") public String listProducts(Model model){ - - model.addAttribute("products", productService.listAllProducts()); - - return "products"; + model.addAttribute("products", productService.listAll()); + return "product/list"; } - @RequestMapping("/product/{id}") + @RequestMapping("/product/show/{id}") public String getProduct(@PathVariable Integer id, Model model){ - - model.addAttribute("product", productService.getProductById(id)); - - return "product"; + model.addAttribute("product", productService.getById(id)); + return "product/show"; } @RequestMapping("product/edit/{id}") public String edit(@PathVariable Integer id, Model model){ - model.addAttribute("product", productService.getProductById(id)); - return "productform"; + model.addAttribute("product", productService.getById(id)); + return "product/productform"; } @RequestMapping("/product/new") public String newProduct(Model model){ model.addAttribute("product", new Product()); - return "productform"; + return "product/productform"; } @RequestMapping(value = "/product", method = RequestMethod.POST) public String saveOrUpdateProduct(Product product){ - Product savedProduct = productService.saveOrUpdateProduct(product); - return "redirect:/product/" + savedProduct.getId(); + Product savedProduct = productService.saveOrUpdate(product); + return "redirect:/product/show/" + savedProduct.getId(); } @RequestMapping("/product/delete/{id}") public String delete(@PathVariable Integer id){ - productService.deleteProduct(id); - - return "redirect:/products"; + productService.delete(id); + return "redirect:/product/list"; } } diff --git a/src/main/java/guru/springframework/domain/Customer.java b/src/main/java/guru/springframework/domain/Customer.java new file mode 100644 index 00000000..cd53347e --- /dev/null +++ b/src/main/java/guru/springframework/domain/Customer.java @@ -0,0 +1,100 @@ +package guru.springframework.domain; + +/** + * Created by jt on 11/14/15. + */ +public class Customer implements DomainObject { + + private Integer id; + private String firstName; + private String lastName; + private String email; + private String phoneNumber; + private String addressLine1; + private String addressLine2; + private String city; + private String state; + private String zipCode; + + @Override + public Integer getId() { + return id; + } + + @Override + public void setId(Integer id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public String getAddressLine1() { + return addressLine1; + } + + public void setAddressLine1(String addressLine1) { + this.addressLine1 = addressLine1; + } + + public String getAddressLine2() { + return addressLine2; + } + + public void setAddressLine2(String addressLine2) { + this.addressLine2 = addressLine2; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getZipCode() { + return zipCode; + } + + public void setZipCode(String zipCode) { + this.zipCode = zipCode; + } +} diff --git a/src/main/java/guru/springframework/domain/DomainObject.java b/src/main/java/guru/springframework/domain/DomainObject.java new file mode 100644 index 00000000..5744c4e7 --- /dev/null +++ b/src/main/java/guru/springframework/domain/DomainObject.java @@ -0,0 +1,11 @@ +package guru.springframework.domain; + +/** + * Created by jt on 11/14/15. + */ +public interface DomainObject { + + Integer getId(); + + void setId(Integer id); +} diff --git a/src/main/java/guru/springframework/domain/Product.java b/src/main/java/guru/springframework/domain/Product.java index d4bf7f75..79bff2a7 100644 --- a/src/main/java/guru/springframework/domain/Product.java +++ b/src/main/java/guru/springframework/domain/Product.java @@ -5,7 +5,7 @@ /** * Created by jt on 11/6/15. */ -public class Product { +public class Product implements DomainObject{ private Integer id; private String description; private BigDecimal price; diff --git a/src/main/java/guru/springframework/services/AbstractMapService.java b/src/main/java/guru/springframework/services/AbstractMapService.java new file mode 100644 index 00000000..1e21261e --- /dev/null +++ b/src/main/java/guru/springframework/services/AbstractMapService.java @@ -0,0 +1,50 @@ +package guru.springframework.services; + +import guru.springframework.domain.DomainObject; + +import java.util.*; + +/** + * Created by jt on 11/14/15. + */ +public abstract class AbstractMapService { + protected Map domainMap; + + public AbstractMapService() { + domainMap = new HashMap<>(); + loadDomainObjects(); + } + + public List listAll() { + return new ArrayList<>(domainMap.values()); + } + + public DomainObject getById(Integer id) { + return domainMap.get(id); + } + + public DomainObject saveOrUpdate(DomainObject domainObject) { + if (domainObject != null){ + + if (domainObject.getId() == null){ + domainObject.setId(getNextKey()); + } + domainMap.put(domainObject.getId(), domainObject); + + return domainObject; + } else { + throw new RuntimeException("Object Can't be null"); + } + } + + public void delete(Integer id) { + domainMap.remove(id); + } + + private Integer getNextKey(){ + return Collections.max(domainMap.keySet()) + 1; + } + + protected abstract void loadDomainObjects(); + +} diff --git a/src/main/java/guru/springframework/services/CRUDService.java b/src/main/java/guru/springframework/services/CRUDService.java new file mode 100644 index 00000000..7355d122 --- /dev/null +++ b/src/main/java/guru/springframework/services/CRUDService.java @@ -0,0 +1,16 @@ +package guru.springframework.services; + +import java.util.List; + +/** + * Created by jt on 11/14/15. + */ +public interface CRUDService { + List listAll(); + + T getById(Integer id); + + T saveOrUpdate(T domainObject); + + void delete(Integer id); +} diff --git a/src/main/java/guru/springframework/services/CustomerService.java b/src/main/java/guru/springframework/services/CustomerService.java new file mode 100644 index 00000000..441b15bd --- /dev/null +++ b/src/main/java/guru/springframework/services/CustomerService.java @@ -0,0 +1,10 @@ +package guru.springframework.services; + +import guru.springframework.domain.Customer; + +/** + * Created by jt on 11/14/15. + */ +public interface CustomerService extends CRUDService{ + +} diff --git a/src/main/java/guru/springframework/services/CustomerServiceImpl.java b/src/main/java/guru/springframework/services/CustomerServiceImpl.java new file mode 100644 index 00000000..c86adce5 --- /dev/null +++ b/src/main/java/guru/springframework/services/CustomerServiceImpl.java @@ -0,0 +1,77 @@ +package guru.springframework.services; + +import guru.springframework.domain.Customer; +import guru.springframework.domain.DomainObject; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; + +/** + * Created by jt on 11/14/15. + */ +@Service +public class CustomerServiceImpl extends AbstractMapService implements CustomerService { + + @Override + public List listAll() { + return super.listAll(); + } + + @Override + public Customer getById(Integer id) { + return (Customer) super.getById(id); + } + + @Override + public Customer saveOrUpdate(Customer domainObject) { + return (Customer) super.saveOrUpdate(domainObject); + } + + @Override + public void delete(Integer id) { + super.delete(id); + } + + @Override + protected void loadDomainObjects() { + domainMap = new HashMap<>(); + + Customer customer1 = new Customer(); + customer1.setId(1); + customer1.setFirstName("Micheal"); + customer1.setLastName("Weston"); + customer1.setAddressLine1("1 Main St"); + customer1.setCity("Miami"); + customer1.setState("Florida"); + customer1.setZipCode("33101"); + customer1.setEmail("micheal@burnnotice.com"); + customer1.setPhoneNumber("305.333.0101"); + + Customer customer2 = new Customer(); + customer2.setId(2); + customer2.setFirstName("Fiona"); + customer2.setLastName("Glenanne"); + customer2.setAddressLine1("1 Key Biscane Ave"); + customer2.setCity("Miami"); + customer2.setState("Florida"); + customer2.setZipCode("33101"); + customer2.setEmail("fiona@burnnotice.com"); + customer2.setPhoneNumber("305.323.0233"); + + Customer customer3 = new Customer(); + customer3.setId(3); + customer3.setFirstName("Sam"); + customer3.setLastName("Axe"); + customer3.setAddressLine1("1 Little Cuba Road"); + customer3.setCity("Miami"); + customer3.setState("Florida"); + customer3.setZipCode("33101"); + customer3.setEmail("sam@burnnotice.com"); + customer3.setPhoneNumber("305.426.9832"); + + domainMap.put(1, customer1); + domainMap.put(2, customer2); + domainMap.put(3, customer3); + } +} diff --git a/src/main/java/guru/springframework/services/ProductService.java b/src/main/java/guru/springframework/services/ProductService.java index 1f049a7e..62a35310 100644 --- a/src/main/java/guru/springframework/services/ProductService.java +++ b/src/main/java/guru/springframework/services/ProductService.java @@ -2,18 +2,9 @@ import guru.springframework.domain.Product; -import java.util.List; - /** * Created by jt on 11/6/15. */ -public interface ProductService { - - List listAllProducts(); - - Product getProductById(Integer id); - - Product saveOrUpdateProduct(Product product); +public interface ProductService extends CRUDService { - void deleteProduct(Integer id); } diff --git a/src/main/java/guru/springframework/services/ProductServiceImpl.java b/src/main/java/guru/springframework/services/ProductServiceImpl.java index b7fa098a..1382aad2 100644 --- a/src/main/java/guru/springframework/services/ProductServiceImpl.java +++ b/src/main/java/guru/springframework/services/ProductServiceImpl.java @@ -1,58 +1,41 @@ package guru.springframework.services; +import guru.springframework.domain.DomainObject; import guru.springframework.domain.Product; import org.springframework.stereotype.Service; import java.math.BigDecimal; -import java.util.*; +import java.util.HashMap; +import java.util.List; /** * Created by jt on 11/6/15. */ @Service -public class ProductServiceImpl implements ProductService { - - private Map products; - - public ProductServiceImpl() { - loadProducts(); - } +public class ProductServiceImpl extends AbstractMapService implements ProductService { @Override - public List listAllProducts() { - return new ArrayList<>(products.values()); + public List listAll() { + return super.listAll(); } @Override - public Product getProductById(Integer id) { - return products.get(id); + public Product getById(Integer id) { + return (Product) super.getById(id); } @Override - public Product saveOrUpdateProduct(Product product) { - if (product != null){ - if (product.getId() == null){ - product.setId(getNextKey()); - } - products.put(product.getId(), product); - - return product; - } else { - throw new RuntimeException("Product Can't be nill"); - } - } - - private Integer getNextKey(){ - return Collections.max(products.keySet()) + 1; + public Product saveOrUpdate(Product domainObject) { + return (Product) super.saveOrUpdate(domainObject); } @Override - public void deleteProduct(Integer id) { - products.remove(id); + public void delete(Integer id) { + super.delete(id); } - private void loadProducts(){ - products = new HashMap<>(); + protected void loadDomainObjects(){ + domainMap = new HashMap<>(); Product product1 = new Product(); product1.setId(1); @@ -60,7 +43,7 @@ private void loadProducts(){ product1.setPrice(new BigDecimal("12.99")); product1.setImageUrl("http://example.com/product1"); - products.put(1, product1); + domainMap.put(1, product1); Product product2 = new Product(); product2.setId(2); @@ -68,7 +51,7 @@ private void loadProducts(){ product2.setPrice(new BigDecimal("14.99")); product2.setImageUrl("http://example.com/product2"); - products.put(2, product2); + domainMap.put(2, product2); Product product3 = new Product(); product3.setId(3); @@ -76,7 +59,7 @@ private void loadProducts(){ product3.setPrice(new BigDecimal("34.99")); product3.setImageUrl("http://example.com/product3"); - products.put(3, product3); + domainMap.put(3, product3); Product product4 = new Product(); product4.setId(4); @@ -84,7 +67,7 @@ private void loadProducts(){ product4.setPrice(new BigDecimal("44.99")); product4.setImageUrl("http://example.com/product4"); - products.put(4, product4); + domainMap.put(4, product4); Product product5 = new Product(); product5.setId(5); @@ -92,6 +75,6 @@ private void loadProducts(){ product5.setPrice(new BigDecimal("25.99")); product5.setImageUrl("http://example.com/product5"); - products.put(5, product5); + domainMap.put(5, product5); } } diff --git a/src/main/resources/templates/customer/customerform.html b/src/main/resources/templates/customer/customerform.html new file mode 100644 index 00000000..c3b6fe1a --- /dev/null +++ b/src/main/resources/templates/customer/customerform.html @@ -0,0 +1,86 @@ + + + + Spring Core Online Tutorial - Customer Form + + + + + + + + + +
+ +

Customer Details

+
+
+ +
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+
+
+
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/customer/list.html b/src/main/resources/templates/customer/list.html new file mode 100644 index 00000000..4904641b --- /dev/null +++ b/src/main/resources/templates/customer/list.html @@ -0,0 +1,50 @@ + + + + Spring Core Online Tutorial - List Customers + + + + + + + + + +
+
+

Customer List

+ + + + + + + + + + + + + + + + + + + +
IdFirst NameLast NameEmailShowEditDelete
View Edit Delete
+
+
+ +
+
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/customer/show.html b/src/main/resources/templates/customer/show.html new file mode 100644 index 00000000..5dadfa44 --- /dev/null +++ b/src/main/resources/templates/customer/show.html @@ -0,0 +1,94 @@ + + + + Spring Core Online Tutorial - Show Customer + + + + + + + + + +
+ +
+
+

Show Customer

+
+
+
+
+
+
+ +
+

Customer Id

+
+
+
+ +
+

First Name

+
+
+
+ +
+

Last Name

+
+
+
+ +
+

Email

+
+
+
+ +
+

Phone

+
+
+
+ +
+

Address Line 1

+
+
+
+ +
+

Address Line 2

+
+
+
+ +
+

City

+
+
+
+ +
+

State

+
+
+
+ +
+

Zipcode

+
+
+
+
+
+
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index a2ccb97f..9622d970 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -19,7 +19,12 @@

Hello World

-

This is my Thymeleaf index page. This is my changed.

+

This is my Thymeleaf index page.

+ +
diff --git a/src/main/resources/templates/products.html b/src/main/resources/templates/product/list.html similarity index 92% rename from src/main/resources/templates/products.html rename to src/main/resources/templates/product/list.html index 64a57a22..b7ca555c 100644 --- a/src/main/resources/templates/products.html +++ b/src/main/resources/templates/product/list.html @@ -11,7 +11,7 @@ - @@ -33,7 +33,7 @@

Product List

- View + View Edit Delete diff --git a/src/main/resources/templates/productform.html b/src/main/resources/templates/product/productform.html similarity index 97% rename from src/main/resources/templates/productform.html rename to src/main/resources/templates/product/productform.html index 1d080fa2..cd9d9b6e 100644 --- a/src/main/resources/templates/productform.html +++ b/src/main/resources/templates/product/productform.html @@ -11,7 +11,7 @@ - diff --git a/src/main/resources/templates/product.html b/src/main/resources/templates/product/show.html similarity index 97% rename from src/main/resources/templates/product.html rename to src/main/resources/templates/product/show.html index e4e19485..069ea3c9 100644 --- a/src/main/resources/templates/product.html +++ b/src/main/resources/templates/product/show.html @@ -11,7 +11,7 @@ -