/** * Buying articles for business customers as employee. * * @param cart cart for the business customer, will be cleared * @param nickname nickname of the currently logged in employee * @param success notification of successfully buying articles * @return redirect to template "index" (home page) */ @RequestMapping(value = "/checkout_employee", method = RequestMethod.POST) public String buyforbusinesscustomer( @ModelAttribute Cart cart, @RequestParam("bcustomer") String nickname, RedirectAttributes success) { Optional<UserAccount> businessCustomer = userAccountManager.findByUsername(nickname); return businessCustomer .map( account -> { Order order = new Order(account, Cash.CASH); cart.addItemsTo(order); orderManager.payOrder(order); orderManager.completeOrder(order); cart.clear(); success.addFlashAttribute( "success", "Die Bestellung für den Geschäftskunden ist abgeschlossen."); return "redirect:/"; }) .orElse("redirect:/cart"); }
/** * 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"; }
/** * Deletes an article of the cart. * * @param cart the current cart of the user * @param id ID of the requested article * @param success notification of deleting the article * @return redirect to template "cart" */ @RequestMapping(value = "/delete", method = RequestMethod.POST) public String delete( @ModelAttribute Cart cart, @RequestParam("identification") String id, RedirectAttributes success) { cart.removeItem(id); success.addFlashAttribute( "success", "Der Artikel wurde erfolgreich aus Ihrem Warenkorb entfernt."); return "redirect:/cart"; }
/** * Buying articles. * * @param cart the current cart of the user, will be cleared * @param userAccount currently logged in user * @param success notification of successfully buying articles * @return redirect to template "index" (home page) */ @RequestMapping(value = "/checkout", method = RequestMethod.POST) public String buy( @ModelAttribute Cart cart, @LoggedIn Optional<UserAccount> userAccount, RedirectAttributes success) { return userAccount .map( account -> { Order order = new Order(account, Cash.CASH); cart.addItemsTo(order); orderManager.payOrder(order); orderManager.completeOrder(order); cart.clear(); success.addFlashAttribute("success", "Vielen Dank für Ihre Bestellung."); return "redirect:/"; }) .orElse("redirect:/cart"); }
/** * Adds an article (notebook, supply, software) to the cart. * * @param article the requested article * @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 the category the requested article is part of */ @RequestMapping(value = "/cart", method = RequestMethod.POST) public String addarticle( @RequestParam("pid") Article 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(); // Herrje, wer das schöner schreiben will, kann das gerne machen int i = amount1.intValue(); // Endlich funktioniert die Validierung, besser als beim int amount = number; // Videoshop :P Kevin if (number <= 0) { amount = 1; } if (number >= i) { amount = i; } cart.addOrUpdateItem(article, Quantity.of(amount)); success.addFlashAttribute( "success", "Der Artikel wurde erfolgreich Ihrem Warenkorb hinzugefügt."); switch (article.getType()) { case NOTEBOOK: return "redirect:laptop"; case SOFTWARE: return "redirect:software"; case ZUBE: default: return "redirect:zubehoer"; } }