@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
  public String update(
      @PathVariable Long id,
      @RequestParam(required = false) String name,
      @RequestParam(required = false) String introduced,
      @RequestParam(required = false) String discontinued,
      @RequestParam(required = false) String company,
      ModelMap model,
      RedirectAttributes redirectAttributes) {
    ComputerForm form = new ComputerForm();
    form.setId(id);
    form.setName(name);
    form.setIntroduced(introduced);
    form.setDiscontinued(discontinued);
    form.setCompany(company);

    if (!form.isValid()) {
      model.addAttribute("form", form);
      model.addAttribute("companies", companyService.findAll());

      return "/edit";
    } else {
      Computer computer = form.toComputer();
      computer = computerService.update(computer);

      redirectAttributes.addFlashAttribute(
          "success", "Computer with id " + computer.getId() + " has been updated");

      return "redirect:/computers";
    }
  }
Example #2
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";
  }
  @RequestMapping(value = "/save", method = RequestMethod.POST)
  public String save(
      @ModelAttribute Computer computer,
      BindingResult result,
      SessionStatus status,
      ModelMap model,
      RedirectAttributes redirectAttributes) {
    computerValidator.validate(computer, result);

    if (result.hasErrors()) {
      model.addAttribute("computer", computer);
      model.addAttribute("companies", companyService.findAll());
      model.addAttribute("result", result);

      return "/create";
    } else {
      computer = computerService.create(computer);

      status.setComplete();

      redirectAttributes.addFlashAttribute(
          "success", "Computer named " + computer.getName() + " has been created");

      return "redirect:/computers";
    }
  }