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("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 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; } 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; 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; } } }