Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.microsoft.shinyay.ai.controller;

import com.microsoft.shinyay.ai.model.CartItem;
import com.microsoft.shinyay.ai.model.Product;
import com.microsoft.shinyay.ai.service.ShoppingCartService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class ShoppingCartController {

private final ShoppingCartService shoppingCartService;

public ShoppingCartController(ShoppingCartService shoppingCartService) {
this.shoppingCartService = shoppingCartService;
}

@PostMapping("/add")
public ResponseEntity<Void> addItemToCart(@RequestBody Product product, @RequestParam int quantity) {
shoppingCartService.addItemToCart(product, quantity);
return ResponseEntity.ok().build();
}

@GetMapping("/items")
public ResponseEntity<List<CartItem>> viewCartItems() {
return ResponseEntity.ok(shoppingCartService.getCartItems());
}

@DeleteMapping("/clear")
public ResponseEntity<Void> clearCart() {
shoppingCartService.clearCart();
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.microsoft.shinyay.ai.model;

public class CartItem {
private Product product;
private int quantity;

public CartItem() {
}

public Product getProduct() {
return product;
}

public void setProduct(Product product) {
this.product = product;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.microsoft.shinyay.ai.model;

public class Product {
private Long id;
private String name;
private String description;
private double price;

public Product() {
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.microsoft.shinyay.ai.service;

import com.microsoft.shinyay.ai.model.CartItem;
import com.microsoft.shinyay.ai.model.Product;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class ShoppingCartService {

private final List<CartItem> cartItems = new ArrayList<>();

public void addItemToCart(Product product, int quantity) {
Optional<CartItem> existingItem = cartItems.stream()
.filter(item -> item.getProduct().getId().equals(product.getId()))
.findFirst();

existingItem.ifPresentOrElse(item -> item.setQuantity(item.getQuantity() + quantity),
() -> cartItems.add(new CartItem(product, quantity)));
}

public List<CartItem> getCartItems() {
return cartItems;
}

public double calculateTotalPrice() {
return cartItems.stream()
.mapToDouble(item -> item.getProduct().getPrice() * item.getQuantity())
.sum();
}

public void clearCart() {
cartItems.clear();
}
}
49 changes: 49 additions & 0 deletions workspace/src/main/resources/static/css/cart.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* Basic styling for the shopping cart page */
.cart-container {
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
}

.cart-header {
font-size: 24px;
margin-bottom: 20px;
}

.cart-items {
width: 100%;
border-collapse: collapse;
}

.cart-items th, .cart-items td {
text-align: left;
padding: 8px;
border-bottom: 1px solid #ddd;
}

.cart-summary {
margin-top: 20px;
align-self: flex-end;
}

.cart-summary h3 {
margin: 0;
}

.cart-actions {
margin-top: 20px;
}

.cart-actions button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
margin-right: 10px;
border: none;
cursor: pointer;
}

.cart-actions button:hover {
background-color: #45a049;
}
44 changes: 44 additions & 0 deletions workspace/src/main/resources/templates/cart.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Shopping Cart</title>
<link rel="stylesheet" href="/css/cart.css">
</head>
<body>
<div class="container">
<h2>Shopping Cart</h2>
<div th:if="${not #lists.isEmpty(cartItems)}">
<table>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr th:each="item : ${cartItems}">
<td th:text="${item.product.name}">Product Name</td>
<td th:text="${item.product.price}">Product Price</td>
<td th:text="${item.quantity}">Quantity</td>
<td th:text="${item.product.price * item.quantity}">Total</td>
</tr>
</tbody>
</table>
<div>
<h3>Total: <span th:text="${totalPrice}">Total Price</span></h3>
</div>
</div>
<div th:if="${#lists.isEmpty(cartItems)}">
<p>Your cart is empty.</p>
</div>
<div>
<form th:action="@{/cart/clear}" method="post">
<button type="submit">Clear Cart</button>
</form>
</div>
</div>
</body>
</html>