-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCartController.java
More file actions
67 lines (60 loc) · 2.66 KB
/
CartController.java
File metadata and controls
67 lines (60 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package onlineshop.controllers;
import onlineshop.Cart;
import onlineshop.Shop;
import onlineshop.merchandise.Article;
import onlineshop.merchandise.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@Controller
@RequestMapping(value = "/cart")
public class CartController extends BaseController {
@Autowired
Shop shop;
@Autowired
Cart cart;
@GetMapping(value = {"/add/{articleNo}"})
public String addToCart(@PathVariable(name = "articleNo") Integer articleNo, RedirectAttributes atts) {
String message = "Book with article no. \"" + articleNo + "\" not found.";
Book book = shop.getArticleByNumber(articleNo);
if (book != null) {
cart.addArticle(book);
message = "Article \"" + book.getTitle() + "\" added to cart.";
}
showMessage(atts, message);
return "redirect:/index.html";
}
@GetMapping(value = {"/increase/{articleNo}"})
public String increaseQuantity(@PathVariable(name = "articleNo") Integer articleNo, RedirectAttributes atts) {
String message = "Book with article no. \"" + articleNo + "\" not found.";
Book book = shop.getArticleByNumber(articleNo);
if (book != null) {
cart.increaseQuantity(book.getArticleNo());
} else {
showMessage(atts, message);
}
return "redirect:/cart.html";
}
@GetMapping(value = {"/decrease/{articleNo}"})
public String decreaseQuantity(@PathVariable(name = "articleNo") Integer articleNo, RedirectAttributes atts) {
Article article = shop.getArticleByNumber(articleNo);
String message = "Article '" + article.getTitle() + "' removed from cart.";
if (!cart.decreaseQuantity(articleNo)) {
showMessage(atts, message);
}
return "redirect:/cart.html";
}
@GetMapping(value = {"/remove/{articleNo}"})
public String removeFromCart(@PathVariable(name = "articleNo") Integer articleNo, RedirectAttributes atts) {
String message = "Article with article no. \"" + articleNo + "\" not found in cart.";
Article article = shop.getArticleByNumber(articleNo);
if (article != null && cart.removeArticle(articleNo)) {
message = "Article \"" + article.getTitle() + "\" removed from cart.";
}
showMessage(atts, message);
return "redirect:/cart.html";
}
}