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.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.*; import org.springframework.web.servlet.view.RedirectView; @Controller @RequestMapping("intern/articles") 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)); // System.out.println("GETshouldReorder: " + articleRepository.findArticleById(articleid).shouldReorder); model.addAttribute("ArticleID", total); return "intern/listedArticles/id"; } @PostMapping("/{id}/saveChanges") public RedirectView saveChanges(@ModelAttribute ListedArticlesListIdTotal changedArticleTotal) { Article oldArticle = articleRepository.findArticleById(changedArticleTotal.id); //TODO img //TODO price //TODO reorder price //TODO categories // oldArticle.shopPricePerUnitNetCent = changedArticleTotal.price_netto; // System.out.println("netto: " + changedArticleTotal.price_netto); //System.out.println("POSTshouldReorder: " + oldArticle.shouldReorder); //oldArticle.shouldReorder = changedArticleTotal.shouldReorder; System.out.println("POSTwarehouseUnitsPerSlot: " + oldArticle.warehouseUnitsPerSlot); //oldArticle.warehouseUnitsPerSlot = Integer.parseInt(changedArticleTotal.warehouseUnitsPerSlot); oldArticle.title = changedArticleTotal.title; //works oldArticle.description = changedArticleTotal.description; //works articleRepository.save(oldArticle); // save updated article return new RedirectView("../"); } 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 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 int getReorderMaxPrice() { return reorderMaxPrice; } public void setReorderMaxPrice(int 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; } 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 String 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 = String.valueOf(article.warehouseUnitsPerSlot); this.description = article.description; } } }