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.repos.shop.ArticleRepository; 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.*; @Controller @RequestMapping("intern/listedArticles") public class InternArticleController { @Autowired private final ArticleRepository articleRepository = null; @Autowired private final WarehouseBookingPositionSlotEntryRepository warehouseEntryRepository = null; @GetMapping("/") public String internListedArticles(Model model) { List totals = new ArrayList(); for (Article article : articleRepository.findAll()) { ListedArticlesListTotals tmp = new ListedArticlesListTotals(); 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); ListedArticlesListIdTotal total = new ListedArticlesListIdTotal(); total.addArticle(articleRepository.findArticleById(articleid), warehouseEntryRepository.getArticleStock(articleid).orElse(0)); model.addAttribute("ArticleID", total); return "intern/listedArticles/id"; } public static class ListedArticlesListTotals { 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)); this.categorie = article.getCategories(); this.stock = stock; this.offer_id = article.related.id; this.id = article.id; } } public static class ListedArticlesListIdTotal { public String imgPath; public String title; public String price; public String price_netto; public int reorderMaxPrice; public String categorie; public int stock; public long offer_id; public long id; public boolean shouldReorder; public int warehouseUnitsPerSlot; public String 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)); this.categorie = article.getCategories(); this.stock = stock; this.offer_id = article.related.id; this.id = article.id; this.reorderMaxPrice = article.reorderMaxPrice; this.shouldReorder = article.shouldReorder; this.warehouseUnitsPerSlot = article.warehouseUnitsPerSlot; this.description = article.description; } } }