• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / jpetstore-6 / 2384

25 Jul 2026 02:39AM UTC coverage: 83.278% (+11.3%) from 72.006%
2384

Pull #1076

github

web-flow
Merge c4f0bb4d2 into 2117c6fa6
Pull Request #1076: Migrate JPetstore to Jakarta EE

47 of 98 branches covered (47.96%)

107 of 197 new or added lines in 5 files covered. (54.31%)

498 of 598 relevant lines covered (83.28%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

26.67
/src/main/java/org/mybatis/jpetstore/web/controllers/CartController.java
1
/*
2
 *    Copyright 2010-2026 the original author or authors.
3
 *
4
 *    Licensed under the Apache License, Version 2.0 (the "License");
5
 *    you may not use this file except in compliance with the License.
6
 *    You may obtain a copy of the License at
7
 *
8
 *       https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *    Unless required by applicable law or agreed to in writing, software
11
 *    distributed under the License is distributed on an "AS IS" BASIS,
12
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.mybatis.jpetstore.web.controllers;
17

18
import jakarta.servlet.http.HttpServletRequest;
19
import jakarta.servlet.http.HttpSession;
20

21
import java.util.Iterator;
22

23
import org.mybatis.jpetstore.domain.Cart;
24
import org.mybatis.jpetstore.domain.CartItem;
25
import org.mybatis.jpetstore.domain.Item;
26
import org.mybatis.jpetstore.service.CatalogService;
27
import org.springframework.beans.factory.annotation.Autowired;
28
import org.springframework.stereotype.Controller;
29
import org.springframework.ui.Model;
30
import org.springframework.web.bind.annotation.GetMapping;
31
import org.springframework.web.bind.annotation.PostMapping;
32
import org.springframework.web.bind.annotation.RequestMapping;
33
import org.springframework.web.bind.annotation.RequestParam;
34

35
@Controller
36
@RequestMapping("/cart")
37
public class CartController {
1✔
38

39
  private static final String ERROR_VIEW = "common/Error";
40

41
  @Autowired
42
  private CatalogService catalogService;
43

44
  private Cart getCart(HttpSession session) {
45
    Cart cart = (Cart) session.getAttribute("cart");
1✔
46
    if (cart == null) {
1!
NEW
47
      cart = new Cart();
×
NEW
48
      session.setAttribute("cart", cart);
×
49
    }
50
    return cart;
1✔
51
  }
52

53
  @GetMapping({ "", "/" })
54
  public String viewCart(HttpSession session, Model model) {
55
    model.addAttribute("cart", getCart(session));
1✔
56
    return "cart/Cart";
1✔
57
  }
58

59
  @GetMapping("/addItem")
60
  public String addItemToCart(@RequestParam(value = "workingItemId", required = false) String workingItemId,
61
      HttpSession session, Model model) {
62
    if (workingItemId == null || workingItemId.trim().isEmpty()) {
1!
63
      model.addAttribute("message", "Invalid item ID: cannot add item to cart.");
1✔
64
      return ERROR_VIEW;
1✔
65
    }
NEW
66
    Cart cart = getCart(session);
×
NEW
67
    if (cart.containsItemId(workingItemId)) {
×
NEW
68
      cart.incrementQuantityByItemId(workingItemId);
×
69
    } else {
NEW
70
      boolean isInStock = catalogService.isItemInStock(workingItemId);
×
NEW
71
      Item item = catalogService.getItem(workingItemId);
×
NEW
72
      cart.addItem(item, isInStock);
×
73
    }
NEW
74
    model.addAttribute("cart", cart);
×
NEW
75
    return "cart/Cart";
×
76
  }
77

78
  @GetMapping("/removeItem")
79
  public String removeItemFromCart(@RequestParam(value = "workingItemId", required = false) String workingItemId,
80
      HttpSession session, Model model) {
81
    if (workingItemId == null || workingItemId.trim().isEmpty()) {
1!
82
      model.addAttribute("message", "Invalid item ID: cannot remove item from cart.");
1✔
83
      return ERROR_VIEW;
1✔
84
    }
NEW
85
    Cart cart = getCart(session);
×
NEW
86
    Item item = cart.removeItemById(workingItemId);
×
NEW
87
    if (item == null) {
×
NEW
88
      model.addAttribute("message", "Attempted to remove null CartItem from Cart.");
×
NEW
89
      return ERROR_VIEW;
×
90
    }
NEW
91
    model.addAttribute("cart", cart);
×
NEW
92
    return "cart/Cart";
×
93
  }
94

95
  @PostMapping("/update")
96
  public String updateCartQuantities(HttpServletRequest request, HttpSession session, Model model) {
NEW
97
    Cart cart = getCart(session);
×
NEW
98
    Iterator<CartItem> cartItems = cart.getAllCartItems();
×
NEW
99
    while (cartItems.hasNext()) {
×
NEW
100
      CartItem cartItem = cartItems.next();
×
NEW
101
      String itemId = cartItem.getItem().getItemId();
×
102
      try {
NEW
103
        int quantity = Integer.parseInt(request.getParameter(itemId));
×
NEW
104
        cart.setQuantityByItemId(itemId, quantity);
×
NEW
105
        if (quantity < 1) {
×
NEW
106
          cartItems.remove();
×
107
        }
NEW
108
      } catch (NumberFormatException e) {
×
109
        // ignore invalid numeric input on purpose
NEW
110
      }
×
NEW
111
    }
×
NEW
112
    model.addAttribute("cart", cart);
×
NEW
113
    return "cart/Cart";
×
114
  }
115

116
  @GetMapping("/checkout")
117
  public String checkOut(HttpSession session, Model model) {
NEW
118
    model.addAttribute("cart", getCart(session));
×
NEW
119
    return "cart/Checkout";
×
120
  }
121

122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc