示例#1
0
  @SuppressWarnings("unchecked")
  @ResponseBody
  @RequestMapping(value = "/order/updateFreeItem.ajax", method = RequestMethod.POST)
  public ResponseEntity<byte[]> updateFreeItem(
      HttpServletRequest request, @RequestParam(value = "body") String body) throws Exception {

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Updating free item: " + body);
    }

    Map<String, Object> model = new HashMap<String, Object>();

    try {
      // Extract request parameters
      Map<String, Object> params = (Map<String, Object>) jsonUtils.deserialize(body);
      String discountId = (String) params.get("discountId");
      String freeItem = (String) params.get("freeItem");

      HttpSession session = request.getSession(true);
      String orderId = (String) session.getAttribute("orderid");
      Order order = null;
      if (orderId != null) {
        order = orderRepository.findByOrderId(orderId);
        if (order != null) {
          OrderDiscount orderDiscount = order.getOrderDiscount(discountId);
          if (orderDiscount != null) {
            orderDiscount.setSelectedFreeItem(freeItem);
            order = orderRepository.save(order);
          }

          // Update order restaurant id session attribute if any items present
          if (order.getOrderItems().size() > 0) {
            session.setAttribute("orderrestaurantid", order.getRestaurantId());
            session.setAttribute("orderrestauranturl", order.getRestaurant().getUrl());
          } else {
            session.removeAttribute("orderrestaurantid");
            session.removeAttribute("orderrestauranturl");
          }

          // Update can checkout status of order
          session.setAttribute("cancheckout", order.getCanCheckout());
        }
      }

      model.put("success", true);
      model.put("order", order);
    } catch (Exception ex) {
      LOGGER.error("", ex);
      model.put("success", false);
      model.put("message", ex.getMessage());
    }
    return buildOrderResponse(model);
  }