genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
hs_monolith/documentation/microservice_stock/chapter/monolith.patch

313 lines
12 KiB
Diff
Raw Normal View History

2017-06-23 11:52:53 +02:00
From 0754b05118df8af98963428fe32baf7a81920cb7 Mon Sep 17 00:00:00 2001
2017-06-23 13:00:55 +02:00
From: Martin Geno <geno+dev@fireorbit.de>
2017-06-23 11:52:53 +02:00
Date: Thu, 22 Jun 2017 20:15:07 +0200
2017-06-23 13:00:55 +02:00
Subject: [PATCH 1/2] [TASK] patch for microservice stock
2017-06-23 11:52:53 +02:00
---
.../monolith/domain/DataTransferObjectFactory.java | 2 ++
.../de/mstock/monolith/domain/ProductRepository.java | 5 +++++
.../java/de/mstock/monolith/service/ShopService.java | 13 +++++++++++++
.../java/de/mstock/monolith/web/HomepageController.java | 17 +++++++++++++++--
.../java/de/mstock/monolith/web/ProductController.java | 15 +++++++++++++++
src/main/java/de/mstock/monolith/web/ProductDTO.java | 10 ++++++++++
src/main/resources/templates/product.html | 4 ++++
7 files changed, 64 insertions(+), 2 deletions(-)
diff --git a/src/main/java/de/mstock/monolith/domain/DataTransferObjectFactory.java b/src/main/java/de/mstock/monolith/domain/DataTransferObjectFactory.java
index 914d1ae..b026020 100644
--- a/src/main/java/de/mstock/monolith/domain/DataTransferObjectFactory.java
+++ b/src/main/java/de/mstock/monolith/domain/DataTransferObjectFactory.java
@@ -86,6 +86,8 @@ private ProductDTO createProductWithoutReviewsDTO(Product product, Locale locale
ProductI18n i18n = product.getI18n().get(locale.getLanguage());
String price = numberFormat.format(i18n.getPrice());
ProductDTO productDTO = new ProductDTO();
+ // Addition: productDTO.setID()
+ productDTO.setId(product.getId());
productDTO.setItemNumber(product.getItemNumber());
productDTO.setUnit(product.getUnit());
productDTO.setName(i18n.getName());
diff --git a/src/main/java/de/mstock/monolith/domain/ProductRepository.java b/src/main/java/de/mstock/monolith/domain/ProductRepository.java
index e811855..d72fb53 100644
--- a/src/main/java/de/mstock/monolith/domain/ProductRepository.java
+++ b/src/main/java/de/mstock/monolith/domain/ProductRepository.java
@@ -1,5 +1,7 @@
package de.mstock.monolith.domain;
2017-06-23 13:00:55 +02:00
2017-06-23 11:52:53 +02:00
+import java.util.List;
+
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
2017-06-23 13:00:55 +02:00
2017-06-23 11:52:53 +02:00
@@ -9,4 +11,7 @@
+ "where key(i) = ?1 and lower(i.name) = ?2")
Product findByI18nName(String language, String name);
2017-06-23 13:00:55 +02:00
2017-06-23 11:52:53 +02:00
+ @Query("select p from Product p")
+ List<Product> findAll();
+
}
diff --git a/src/main/java/de/mstock/monolith/service/ShopService.java b/src/main/java/de/mstock/monolith/service/ShopService.java
index c230e45..c96f266 100644
--- a/src/main/java/de/mstock/monolith/service/ShopService.java
+++ b/src/main/java/de/mstock/monolith/service/ShopService.java
@@ -59,6 +59,19 @@
}
2017-06-23 13:00:55 +02:00
2017-06-23 11:52:53 +02:00
/**
+ * Gets all products in the current language.
+ *
+ * @return A simplified Data Transfer Object.
+ */
+ public List<ProductDTO> getAllProducts(Locale locale) {
+ String language = locale.getLanguage();
+ List<Product> products = productRepository.findAll();
+ List<ProductDTO> productsDTO =
+ dtoFactory.createProductWithoutReviewsDTOs(products, locale);
+ return Collections.unmodifiableList(productsDTO);
+ }
+
+ /**
* Gets a product in the current language.
2017-06-23 13:00:55 +02:00
*
2017-06-23 11:52:53 +02:00
* @return A simplified Data Transfer Object.
diff --git a/src/main/java/de/mstock/monolith/web/HomepageController.java b/src/main/java/de/mstock/monolith/web/HomepageController.java
index 90a0acc..4395a42 100644
--- a/src/main/java/de/mstock/monolith/web/HomepageController.java
+++ b/src/main/java/de/mstock/monolith/web/HomepageController.java
@@ -17,11 +17,24 @@
2017-06-23 13:00:55 +02:00
2017-06-23 11:52:53 +02:00
@Autowired
private ShopService shopService;
+ // Addition: contant with the address of the stock microservice adminfrontend
+ private final String STOCKADMINFRONTENDTEMPLATE = "https://stock.pub.warehost.de/index.html";
2017-06-23 13:00:55 +02:00
2017-06-23 11:52:53 +02:00
/**
- * Homepage
2017-06-23 13:00:55 +02:00
- *
2017-06-23 11:52:53 +02:00
+ * Redirect to stock admin frontend
+ *
* @param model Template model
+ * @return The constant template name for the stock admin frontend.
+ */
+ @RequestMapping(value = "/stockadmin", method = RequestMethod.GET)
+ public String redirect(Model model) {
+ return "redirect:" + this.STOCKADMINFRONTENDTEMPLATE;
+ }
+
+ /**
+ * Homepage
+ *
+ * @param model Template model
* @param locale Current locale
* @return The template's name.
*/
diff --git a/src/main/java/de/mstock/monolith/web/ProductController.java b/src/main/java/de/mstock/monolith/web/ProductController.java
index 52f1ed3..5fbcacf 100644
--- a/src/main/java/de/mstock/monolith/web/ProductController.java
+++ b/src/main/java/de/mstock/monolith/web/ProductController.java
@@ -1,6 +1,7 @@
package de.mstock.monolith.web;
2017-06-23 13:00:55 +02:00
2017-06-23 11:52:53 +02:00
import java.util.Locale;
+import java.util.List;
2017-06-23 13:00:55 +02:00
2017-06-23 11:52:53 +02:00
import javax.validation.Valid;
2017-06-23 13:00:55 +02:00
2017-06-23 11:52:53 +02:00
@@ -11,6 +12,7 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseBody;
2017-06-23 13:00:55 +02:00
2017-06-23 11:52:53 +02:00
import de.mstock.monolith.service.ReviewService;
import de.mstock.monolith.service.ShopService;
@@ -66,4 +68,17 @@ public String post(@Valid ReviewForm reviewForm, BindingResult bindingResult,
model.addAttribute("product", shopService.getProduct(locale, prettyUrlFragment));
return TEMPLATE;
}
+
+ @RequestMapping(value = "/products/{prettyUrlFragment:[\\w-]+}.json", method = RequestMethod.GET)
+ @ResponseBody
+ public ProductDTO productJson(@PathVariable String prettyUrlFragment, Locale locale) {
+ return shopService.getProduct(locale, prettyUrlFragment);
+ }
+
+ @RequestMapping(value = "/products.json", method = RequestMethod.GET)
+ @ResponseBody
+ public List<ProductDTO> allProductJson(Locale locale) {
+ return shopService.getAllProducts(locale);
+ }
+
}
diff --git a/src/main/java/de/mstock/monolith/web/ProductDTO.java b/src/main/java/de/mstock/monolith/web/ProductDTO.java
index a5b76b6..a7d3245 100644
--- a/src/main/java/de/mstock/monolith/web/ProductDTO.java
+++ b/src/main/java/de/mstock/monolith/web/ProductDTO.java
@@ -6,6 +6,7 @@
2017-06-23 13:00:55 +02:00
2017-06-23 11:52:53 +02:00
public class ProductDTO {
2017-06-23 13:00:55 +02:00
2017-06-23 11:52:53 +02:00
+ private int id;
private String itemNumber;
private ProductWeightUnit unit;
private String name;
@@ -14,6 +15,15 @@
private String description;
private List<ReviewDTO> reviews;
2017-06-23 13:00:55 +02:00
2017-06-23 11:52:53 +02:00
+ // Addition: int id, getId() and setID()
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
public String getItemNumber() {
return itemNumber;
}
diff --git a/src/main/resources/templates/product.html b/src/main/resources/templates/product.html
index 0d7bc31..9b2d0bf 100644
--- a/src/main/resources/templates/product.html
+++ b/src/main/resources/templates/product.html
@@ -31,6 +31,10 @@
</div>
<div class="col-md-8">
<h2 th:text="${product.name}">Product Name</h2>
+
+ <!-- Addition: traffic light food labeling system of the stock microservice -->
+ <img width="10%" class="icon" th:src="${'https://stock.pub.warehost.de/api/good/availablity/'+product.id}"/>
+
<p class="text-info text-uppercase" th:text="${product.price}">0,00 Euro</p>
<p class="lead" th:text="${product.description}">Description.</p>
<div th:replace="fragments/reviews :: reviews"></div>
2017-06-23 13:00:55 +02:00
From 0aa115e34ca231eefe2003919467fb017d60a7c2 Mon Sep 17 00:00:00 2001
From: Martin Geno <geno+dev@fireorbit.de>
Date: Fri, 23 Jun 2017 12:45:24 +0200
Subject: [PATCH 2/2] [TASK] fix for presentation
---
.../de/mstock/monolith/domain/ProductRepository.java | 4 ++++
.../java/de/mstock/monolith/service/ShopService.java | 19 ++++++++++++++++---
.../de/mstock/monolith/web/ProductController.java | 10 +++++-----
src/main/resources/templates/product.html | 4 ++--
4 files changed, 27 insertions(+), 10 deletions(-)
diff --git a/src/main/java/de/mstock/monolith/domain/ProductRepository.java b/src/main/java/de/mstock/monolith/domain/ProductRepository.java
index d72fb53..a807fae 100644
--- a/src/main/java/de/mstock/monolith/domain/ProductRepository.java
+++ b/src/main/java/de/mstock/monolith/domain/ProductRepository.java
@@ -11,6 +11,10 @@
+ "where key(i) = ?1 and lower(i.name) = ?2")
Product findByI18nName(String language, String name);
+ @Query("select p from Product p join fetch p.i18n i left join fetch i.reviews r "
+ + "where key(i) = ?1 and p.id = ?2")
+ Product findByID(String language, Integer id);
+
@Query("select p from Product p")
List<Product> findAll();
diff --git a/src/main/java/de/mstock/monolith/service/ShopService.java b/src/main/java/de/mstock/monolith/service/ShopService.java
index c96f266..4b860d5 100644
--- a/src/main/java/de/mstock/monolith/service/ShopService.java
+++ b/src/main/java/de/mstock/monolith/service/ShopService.java
@@ -30,7 +30,7 @@
/**
* Gets all categories of the current language.
- *
+ *
* @return A simplified Data Transfer Object.
*/
public List<CategoryDTO> getCategories(Locale locale) {
@@ -44,7 +44,7 @@
/**
* Gets all products for a category in the current language.
- *
+ *
* @return A simplified Data Transfer Object.
*/
public List<ProductDTO> getProductsForCategory(Locale locale, String prettyUrlFragment) {
@@ -73,7 +73,20 @@
/**
* Gets a product in the current language.
- *
+ *
+ * @return A simplified Data Transfer Object.
+ */
+ public ProductDTO getProduct(Locale locale, Integer id) {
+ Product product = productRepository.findByID(locale.getLanguage(), id);
+ if (product == null) {
+ throw new NotFoundException();
+ }
+ return dtoFactory.createProductDTO(product, locale);
+ }
+
+ /**
+ * Gets a product in the current language.
+ *
* @return A simplified Data Transfer Object.
*/
public ProductDTO getProduct(Locale locale, String prettyUrlFragment) {
diff --git a/src/main/java/de/mstock/monolith/web/ProductController.java b/src/main/java/de/mstock/monolith/web/ProductController.java
index 5fbcacf..22730c5 100644
--- a/src/main/java/de/mstock/monolith/web/ProductController.java
+++ b/src/main/java/de/mstock/monolith/web/ProductController.java
@@ -31,7 +31,7 @@
/**
* Product page
- *
+ *
* @param prettyUrlFragment Pretty URL fragment
* @param model Template model
* @param locale Current locale
@@ -46,7 +46,7 @@ public String product(@PathVariable String prettyUrlFragment, Model model, Local
/**
* Post a review
- *
+ *
* @param reviewForm Form data
* @param bindingResult Form binding result after validation
* @param prettyUrlFragment Product context
@@ -69,10 +69,10 @@ public String post(@Valid ReviewForm reviewForm, BindingResult bindingResult,
return TEMPLATE;
}
- @RequestMapping(value = "/products/{prettyUrlFragment:[\\w-]+}.json", method = RequestMethod.GET)
+ @RequestMapping(value = "/products/{id}.json", method = RequestMethod.GET)
@ResponseBody
- public ProductDTO productJson(@PathVariable String prettyUrlFragment, Locale locale) {
- return shopService.getProduct(locale, prettyUrlFragment);
+ public ProductDTO productJson(@PathVariable Integer id, Locale locale) {
+ return shopService.getProduct(locale, id);
}
@RequestMapping(value = "/products.json", method = RequestMethod.GET)
diff --git a/src/main/resources/templates/product.html b/src/main/resources/templates/product.html
index 9b2d0bf..235f690 100644
--- a/src/main/resources/templates/product.html
+++ b/src/main/resources/templates/product.html
@@ -33,7 +33,7 @@
<h2 th:text="${product.name}">Product Name</h2>
<!-- Addition: traffic light food labeling system of the stock microservice -->
- <img width="10%" class="icon" th:src="${'https://stock.pub.warehost.de/api/good/availablity/'+product.id}"/>
+ <img width="10%" class="icon" th:src="${'http://localhost:8081/api/good/availablity/'+product.id}"/>
<p class="text-info text-uppercase" th:text="${product.price}">0,00 Euro</p>
<p class="lead" th:text="${product.description}">Description.</p>
@@ -53,4 +53,4 @@ <h2 th:text="${product.name}">Product Name</h2>
<script src="../static/js/bootstrap.min.js"
th:src="@{/js/bootstrap.min.js}"></script>
</body>
-</html>
\ No newline at end of file
+</html>