diff --git a/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java b/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java index 9d67055..a4c4627 100644 --- a/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java +++ b/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java @@ -46,12 +46,12 @@ public class RequestController { return "login"; } - if (!user.get().validatePassword(password)) { + if (!user.get().validatePassword(password)) { request.setAttribute("error", "Passwort falsch."); response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED); return "login"; } - + session.setAttribute("userId", user.get().getId()); if (gto != null && gto.startsWith("/")) { @@ -94,28 +94,7 @@ public class RequestController { public String intern() { return "intern/index"; } - - @GetMapping("/intern/listedArticles/") - public String internListedArticles() { - return "intern/listedArticles/index"; - } - - @GetMapping("/intern/listedArticles/{id}") - public String internListedArticlesId() { - return "intern/listedArticles/id"; - } - - - @GetMapping("/intern/articles/") - public String internArticles() { - return "intern/articles/index"; - } - - @GetMapping("/intern/articles/{id}") - public String internArticlesId() { - return "intern/articles/id"; - } - + @GetMapping("/intern/customers/") public String internCustomers() { return "intern/customers/index"; 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 d078f54..0081c88 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 @@ -1,8 +1,268 @@ package org.hso.ecommerce.controller.intern; +import java.util.ArrayList; +import java.util.List; + +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; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.view.RedirectView; @Controller -//@RequestMapping("...") +@RequestMapping("intern/articles") public class InternArticleController { + @Autowired + private final ArticleRepository articleRepository = null; + + @Autowired + private final WarehouseBookingPositionSlotEntryRepository warehouseEntryRepository = null; + + @Autowired + private final CategoryRepository categoryRepository = null; + + @GetMapping("/") + public String internListedArticles(Model model) { + + List totals = new ArrayList(); + + for (Article article : articleRepository.findAll()) { + UImodelArticles tmp = new UImodelArticles(); + tmp.addListedArticle(article, warehouseEntryRepository.getArticleStock(article.id).orElse(0)); + totals.add(tmp); + } + + model.addAttribute("ListedArticles", totals); + return "intern/listedArticles/index"; + } + + @GetMapping("/{id}") + public String internListedArticlesId(Model model, @PathVariable String id) { + + int articleid = Integer.parseInt(id); + + UImodelArticle total = new UImodelArticle(); + + total.addArticle(articleRepository.findArticleById(articleid), + warehouseEntryRepository.getArticleStock(articleid).orElse(0)); + + model.addAttribute("ArticleID", total); + + return "intern/listedArticles/id"; + } + + @PostMapping("/{id}/saveChanges") + public RedirectView saveChanges( + @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(id); + + // TODO img + + 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 + } + + public static class UImodelArticles { + + public String imgPath; + + public String title; + + public String price; + + public String price_netto; + + public String categorie; + + public int stock; + + public long offer_id; + + public long id; + + void addListedArticle(Article article, int stock) { + this.imgPath = article.image.path; + this.title = article.title; + this.price_netto = String.format("%.2f", ((float) article.shopPricePerUnitNetCent / 100)); + this.price = String.format("%.2f", ((float) article.getPriceGross() / 100)); + + 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; + } + } + + public static class UImodelArticle { + + public String imgPath; + public String title; + public String price; + public String price_netto; + public String reorderMaxPrice; + public String categorie; + public int stock; + public long offer_id; + public long id; + public boolean shouldReorder; + public String warehouseUnitsPerSlot; + public String description; + + + public String getImgPath() { + return imgPath; + } + + public void setImgPath(String imgPath) { + this.imgPath = imgPath; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getPrice() { + return price; + } + + public void setPrice(String price) { + this.price = price; + } + + public String getPrice_netto() { + return price_netto; + } + + public void setPrice_netto(String price_netto) { + this.price_netto = price_netto; + } + + public String getReorderMaxPrice() { + return reorderMaxPrice; + } + + public void setReorderMaxPrice(String reorderMaxPrice) { + this.reorderMaxPrice = reorderMaxPrice; + } + + public String getCategorie() { + return categorie; + } + + public void setCategorie(String categorie) { + this.categorie = categorie; + } + + public int getStock() { + return stock; + } + + public void setStock(int stock) { + this.stock = stock; + } + + public long getOffer_id() { + return offer_id; + } + + public void setOffer_id(long offer_id) { + this.offer_id = offer_id; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public boolean isShouldReorder() { + return shouldReorder; + } + + public void setShouldReorder(boolean shouldReorder) { + this.shouldReorder = shouldReorder; + } + + public String getWarehouseUnitsPerSlot() { + return warehouseUnitsPerSlot; + } + + public void setWarehouseUnitsPerSlot(String warehouseUnitsPerSlot) { + this.warehouseUnitsPerSlot = warehouseUnitsPerSlot; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + void addArticle(Article article, int stock) { + this.imgPath = article.image.path; + this.title = article.title; + this.price_netto = String.format("%.2f", ((float) article.shopPricePerUnitNetCent / 100)); + this.price = String.format("%.2f", ((float) article.getPriceGross() / 100)); + + 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; + this.reorderMaxPrice = String.format("%.2f", ((float) article.reorderMaxPrice / 100)); + this.shouldReorder = article.shouldReorder; + this.warehouseUnitsPerSlot = String.valueOf(article.warehouseUnitsPerSlot); + this.description = article.description; + } + } + } 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 a26cb39..355b5f9 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 @@ -35,7 +35,7 @@ public class Article { @Basic(fetch = FetchType.LAZY) public Image image; - @ManyToMany + @ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "article_categories_bindings") public Set categories = new HashSet<>(); @@ -46,4 +46,4 @@ public class Article { 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 13b0b54..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 @@ -1,7 +1,5 @@ package org.hso.ecommerce.entities.shop; -import org.hso.ecommerce.entities.shop.Article; - import javax.persistence.*; import javax.validation.constraints.NotNull; import java.util.HashSet; @@ -22,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/ArticleRepository.java b/prototype/src/main/java/org/hso/ecommerce/repos/shop/ArticleRepository.java index 0b108f1..6f79487 100644 --- a/prototype/src/main/java/org/hso/ecommerce/repos/shop/ArticleRepository.java +++ b/prototype/src/main/java/org/hso/ecommerce/repos/shop/ArticleRepository.java @@ -11,17 +11,20 @@ import java.util.List; @Repository public interface ArticleRepository extends JpaRepository { - @Query("SELECT a FROM Article a WHERE a.id = :articleId") - Article findArticleById(@Param("articleId") long articleId); + @Query("SELECT a FROM Article a WHERE a.id = :articleId") + Article findArticleById(@Param("articleId") long articleId); + @Query("SELECT a FROM Article a") + List
findAll(); @Query("SELECT a FROM Article a JOIN a.related ao WHERE ao.shouldBeAdvertised = true") List
getAdvertisedArticles(); + @Query("SELECT a FROM CustomerOrderPosition cop JOIN cop.order co JOIN co.customer c JOIN cop.article a ORDER BY co.id DESC") + List
getOrderedArticles(); @Query("SELECT a FROM CustomerOrderPosition cop JOIN cop.order co JOIN co.customer c JOIN cop.article a WHERE c.id = :customerId ORDER BY co.id DESC") List
getOrderedArticles(long customerId); } - 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/java/org/hso/ecommerce/repos/warehouse/WarehouseBookingPositionSlotEntryRepository.java b/prototype/src/main/java/org/hso/ecommerce/repos/warehouse/WarehouseBookingPositionSlotEntryRepository.java index c3eab4b..b342a06 100644 --- a/prototype/src/main/java/org/hso/ecommerce/repos/warehouse/WarehouseBookingPositionSlotEntryRepository.java +++ b/prototype/src/main/java/org/hso/ecommerce/repos/warehouse/WarehouseBookingPositionSlotEntryRepository.java @@ -6,12 +6,17 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface WarehouseBookingPositionSlotEntryRepository extends JpaRepository { @Query(value = "Select e.id, e.article_id, e.new_sum_slot, e.slot_id from warehouse_booking_position_entries as e, warehouse_slots as s where e.slot_id = s.id AND e.article_id = :article GROUP BY s.slot_num HAVING max(e.id)", nativeQuery = true) List getByArticle(long article); - + + + @Query(value = "SELECT SUM(w.new_sum_articles) FROM warehouse_booking_position_entries as w WHERE w.article_id = :articleid", nativeQuery = true) + Optional getArticleStock(long articleid); + } diff --git a/prototype/src/main/resources/templates/fragments/intern.html b/prototype/src/main/resources/templates/fragments/intern.html index 79d5f7e..6676abf 100644 --- a/prototype/src/main/resources/templates/fragments/intern.html +++ b/prototype/src/main/resources/templates/fragments/intern.html @@ -13,11 +13,7 @@
  • Dashboard
  • -
  • Gelistete Artikel - +
  • Gelistete Artikel
  • Alle Buchungen
      @@ -38,7 +34,7 @@
    • Lieferanten
    • diff --git a/prototype/src/main/resources/templates/intern/listedArticles/id.html b/prototype/src/main/resources/templates/intern/listedArticles/id.html index 20b64c3..295bf80 100644 --- a/prototype/src/main/resources/templates/intern/listedArticles/id.html +++ b/prototype/src/main/resources/templates/intern/listedArticles/id.html @@ -5,7 +5,7 @@ - Gelistete Artikel + Bearbeiten: Artikel @@ -15,7 +15,7 @@