From 56efbd7940387a8407e1ee43757b27f7aee11ca8 Mon Sep 17 00:00:00 2001 From: localhorst Date: Fri, 15 May 2020 18:13:12 +0200 Subject: [PATCH] code cleanup --- .../intern/InternArticleController.java | 79 +++++++++++-------- .../hso/ecommerce/entities/shop/Article.java | 71 +++++++---------- .../hso/ecommerce/entities/shop/Category.java | 11 +++ .../repos/shop/CategoryRepository.java | 17 ++++ .../templates/intern/listedArticles/id.html | 3 +- 5 files changed, 105 insertions(+), 76 deletions(-) create mode 100644 prototype/src/main/java/org/hso/ecommerce/repos/shop/CategoryRepository.java diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/intern/InternArticleController.java b/prototype/src/main/java/org/hso/ecommerce/controller/intern/InternArticleController.java index f3c0d52..1ead61d 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/intern/InternArticleController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/intern/InternArticleController.java @@ -3,11 +3,10 @@ package org.hso.ecommerce.controller.intern; import java.util.ArrayList; import java.util.List; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - import org.hso.ecommerce.entities.shop.Article; +import org.hso.ecommerce.entities.shop.Category; import org.hso.ecommerce.repos.shop.ArticleRepository; +import org.hso.ecommerce.repos.shop.CategoryRepository; import org.hso.ecommerce.repos.warehouse.WarehouseBookingPositionSlotEntryRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -24,6 +23,9 @@ public class InternArticleController { @Autowired private final WarehouseBookingPositionSlotEntryRepository warehouseEntryRepository = null; + @Autowired + private final CategoryRepository categoryRepository = null; + @GetMapping("/") public String internListedArticles(Model model) { @@ -56,40 +58,40 @@ public class InternArticleController { @PostMapping("/{id}/saveChanges") public RedirectView saveChanges( - @ModelAttribute UImodelArticle changedArticleTotal, - @RequestParam("units-per-slot") String sWarehouseUnitsPerSlot, - @RequestParam("price_netto") String sPrice_netto, - @RequestParam("reorderMaxPrice") String sReorderMaxPrice, - @RequestParam("autobuy") String sShouldReorder, - @RequestParam("categories") String sCategories) { + @PathVariable int id, + @RequestParam("title") String title, + @RequestParam("description") String description, + @RequestParam("units-per-slot") String warehouseUnitsPerSlot, + @RequestParam("price_netto") String pricenetto, + @RequestParam("reorderMaxPrice") String reorderMaxPrice, + @RequestParam("autobuy") Boolean shouldReorder, + @RequestParam("categories") String categories) { - Article oldArticle = articleRepository.findArticleById(changedArticleTotal.id); + Article oldArticle = articleRepository.findArticleById(id); // TODO img - // TODO categories - /* - * String separatedCategories[] = sCategories.split("\\r?\\n"); - * - * - * for (int i = 0; i < separatedCategories.length; i++) { - * - * oldArticle.categories.add(separatedCategories[i]); - * - * } - */ - oldArticle.shouldReorder = (sShouldReorder.equals("true")) ? true : false; - oldArticle.reorderMaxPrice = (int) (Float.parseFloat(sReorderMaxPrice) * 100); - oldArticle.shopPricePerUnitNetCent = (int) (Float.parseFloat(sPrice_netto) * 100); - oldArticle.warehouseUnitsPerSlot = Integer.parseInt(sWarehouseUnitsPerSlot); - oldArticle.title = changedArticleTotal.title; - oldArticle.description = changedArticleTotal.description; + String separatedCategories[] = categories.split("\n"); + + oldArticle.categories.clear(); + + // loop through all categories strings and create a new category if a new one; also adds the categorys to the article + for (String category : separatedCategories) { + oldArticle.categories.add(categoryRepository.findCategoryByName(category.trim()) + .orElseGet(() -> new Category(category.trim()))); + } + + oldArticle.shouldReorder = shouldReorder; + oldArticle.reorderMaxPrice = (int) (Float.parseFloat(reorderMaxPrice) * 100); + oldArticle.shopPricePerUnitNetCent = (int) (Float.parseFloat(pricenetto) * 100); + oldArticle.warehouseUnitsPerSlot = Integer.parseInt(warehouseUnitsPerSlot); + oldArticle.title = title; + oldArticle.description = description; articleRepository.save(oldArticle); // save updated article - return new RedirectView("../"); //return to overview page + return new RedirectView("../"); // return to overview page } - public static class UImodelArticles { public String imgPath; @@ -113,7 +115,14 @@ public class InternArticleController { this.title = article.title; this.price_netto = String.format("%.2f", ((float) article.shopPricePerUnitNetCent / 100)); this.price = String.format("%.2f", ((float) article.getPriceGross() / 100)); - this.categorie = article.getCategories(); + + StringBuilder result = new StringBuilder(); + + for (Category temp : article.categories) { + result.append(temp.name + " "); + } + this.categorie = result.toString(); + this.stock = stock; this.offer_id = article.related.id; this.id = article.id; @@ -236,7 +245,15 @@ public class InternArticleController { this.title = article.title; this.price_netto = String.format("%.2f", ((float) article.shopPricePerUnitNetCent / 100)); this.price = String.format("%.2f", ((float) article.getPriceGross() / 100)); - this.categorie = article.getCategories(); + + StringBuilder result = new StringBuilder(); + + for (Category temp : article.categories) { + result.append(temp.name); + result.append("\n"); + } + this.categorie = result.toString(); + this.stock = stock; this.offer_id = article.related.id; this.id = article.id; diff --git a/prototype/src/main/java/org/hso/ecommerce/entities/shop/Article.java b/prototype/src/main/java/org/hso/ecommerce/entities/shop/Article.java index 98b8a15..3ecc489 100644 --- a/prototype/src/main/java/org/hso/ecommerce/entities/shop/Article.java +++ b/prototype/src/main/java/org/hso/ecommerce/entities/shop/Article.java @@ -9,56 +9,41 @@ import java.util.Set; @Entity @Table(name = "articles") -public class Article -{ +public class Article { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Basic - public long id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Basic + public long id; - @ManyToOne(optional = false) - public ArticleOffer related; + @ManyToOne(optional = false) + public ArticleOffer related; - public int shopPricePerUnitNetCent; + public int shopPricePerUnitNetCent; + public int warehouseUnitsPerSlot; - public int warehouseUnitsPerSlot; + public boolean shouldReorder; + public int reorderMaxPrice; - public boolean shouldReorder; + @NotNull + public String title; - public int reorderMaxPrice; + @NotNull + public String description; - @NotNull - public String title; + @OneToOne(optional = true) + @Basic(fetch = FetchType.LAZY) + public Image image; - @NotNull - public String description; + @ManyToMany + @JoinTable(name = "article_categories_bindings") + public Set categories = new HashSet<>(); - @OneToOne(optional = true) - @Basic(fetch = FetchType.LAZY) - public Image image; + public int getVat() { + return (shopPricePerUnitNetCent * related.vatPercent) / 100; + } - @ManyToMany - @JoinTable(name = "article_categories_bindings") - public Set categories = new HashSet<>(); - - // returns all categories as string - public String getCategories() - { - StringBuilder result = new StringBuilder(); - for (Category temp : categories) { - result.append(temp.name); - } - return result.toString(); - } - - public int getVat() - { - return (shopPricePerUnitNetCent * related.vatPercent) / 100; - } - - public int getPriceGross() - { - return shopPricePerUnitNetCent + getVat(); - } -} + public int getPriceGross() { + return shopPricePerUnitNetCent + getVat(); + } +} \ No newline at end of file diff --git a/prototype/src/main/java/org/hso/ecommerce/entities/shop/Category.java b/prototype/src/main/java/org/hso/ecommerce/entities/shop/Category.java index 242826d..d552869 100644 --- a/prototype/src/main/java/org/hso/ecommerce/entities/shop/Category.java +++ b/prototype/src/main/java/org/hso/ecommerce/entities/shop/Category.java @@ -20,4 +20,15 @@ public class Category { @ManyToMany(mappedBy = "categories") public Set
articles = new HashSet<>(); + + + public Category() { + + } + + + public Category (String name) { + this.name = name; + } + } diff --git a/prototype/src/main/java/org/hso/ecommerce/repos/shop/CategoryRepository.java b/prototype/src/main/java/org/hso/ecommerce/repos/shop/CategoryRepository.java new file mode 100644 index 0000000..b345d7d --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/repos/shop/CategoryRepository.java @@ -0,0 +1,17 @@ +package org.hso.ecommerce.repos.shop; + +import org.hso.ecommerce.entities.shop.Category; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface CategoryRepository extends JpaRepository { + + @Query("SELECT a FROM Category a WHERE a.name = :name") + Optional findCategoryByName(@Param("name") String name); + +} diff --git a/prototype/src/main/resources/templates/intern/listedArticles/id.html b/prototype/src/main/resources/templates/intern/listedArticles/id.html index 4667d39..295bf80 100644 --- a/prototype/src/main/resources/templates/intern/listedArticles/id.html +++ b/prototype/src/main/resources/templates/intern/listedArticles/id.html @@ -27,8 +27,7 @@

Gelisteter Artikel ID

-
- +