Esempio n. 1
0
  /**
   * Adds an all-in-one computer (combined with four freely customizable parts) to the cart.
   *
   * @param article requested all-in-one computer
   * @param number the amount of requested articles
   * @param cart contains a fresh initialized cart
   * @param success notification about adding an article to the cart
   * @return redirect to template "allinone"
   */
  @RequestMapping(value = "/cart2", method = RequestMethod.POST)
  public String addcomp(
      @RequestParam("pid") Computer article,
      @RequestParam("number") int number,
      @ModelAttribute Cart cart,
      RedirectAttributes success) {

    Optional<InventoryItem> item = inventory.findByProductIdentifier(article.getIdentifier());

    Quantity quantity = item.map(InventoryItem::getQuantity).orElse(NONE);
    BigDecimal amount1 = quantity.getAmount();
    int i = amount1.intValue();
    int amount = number;
    if (number <= 0) {
      amount = 1;
    }
    if (number >= i) {
      amount = i;
    }

    cart.addOrUpdateItem(article, Quantity.of(amount));
    cart.addOrUpdateItem(article.getProzessor().get(0), Quantity.of(amount));
    cart.addOrUpdateItem(article.getGraka().get(0), Quantity.of(amount));
    cart.addOrUpdateItem(article.getHdd().get(0), Quantity.of(amount));
    cart.addOrUpdateItem(article.getRam().get(0), Quantity.of(amount));

    article.getGraka().clear();
    article.getHdd().clear();
    article.getProzessor().clear();
    article.getRam().clear();

    success.addFlashAttribute(
        "success", "Der Artikel wurde erfolgreich Ihrem Warenkorb hinzugefügt.");
    return "redirect:allinone";
  }