Skip to content

Commit 7decc81

Browse files
committed
updated for delete method
1 parent e9f75d8 commit 7decc81

File tree

6 files changed

+17
-20
lines changed

6 files changed

+17
-20
lines changed

src/main/java/guru/springframework/controllers/ProductController.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public void setProductService(ProductService productService) {
2222
@RequestMapping(value = "/products", method = RequestMethod.GET)
2323
public String list(Model model){
2424
model.addAttribute("products", productService.listAllProducts());
25-
System.out.println("Returning rpoducts:");
2625
return "products";
2726
}
2827

@@ -46,10 +45,14 @@ public String newProduct(Model model){
4645

4746
@RequestMapping(value = "product", method = RequestMethod.POST)
4847
public String saveProduct(Product product){
49-
5048
productService.saveProduct(product);
51-
5249
return "redirect:/product/" + product.getId();
5350
}
5451

52+
@RequestMapping("product/delete/{id}")
53+
public String delete(@PathVariable Integer id){
54+
productService.deleteProduct(id);
55+
return "redirect:/products";
56+
}
57+
5558
}

src/main/java/guru/springframework/services/ProductService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ public interface ProductService {
99
Product getProductById(Integer id);
1010

1111
Product saveProduct(Product product);
12+
13+
void deleteProduct(Integer id);
1214
}

src/main/java/guru/springframework/services/ProductServiceImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ public Product getProductById(Integer id) {
2828
public Product saveProduct(Product product) {
2929
return productRepository.save(product);
3030
}
31+
32+
@Override
33+
public void deleteProduct(Integer id) {
34+
productRepository.delete(id);
35+
}
3136
}

src/main/resources/templates/fragments/header.html

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
<!DOCTYPE html>
22
<html xmlns:th="http://www.thymeleaf.org">
33
<head lang="en">
4-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
5-
<link href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.4/css/bootstrap.min.css"
6-
th:href="@{/webjars/bootstrap/3.3.4/css/bootstrap.min.css}"
7-
rel="stylesheet" media="screen"/>
84

9-
<script src="http://cdn.jsdelivr.net/webjars/jquery/2.1.4/jquery.min.js"
10-
th:src="@{/webjars/jquery/2.1.4/jquery.min.js}"></script>
11-
12-
<link href="../../static/css/guru.css"
13-
th:href="@{css/guru.css}" rel="stylesheet" media="screen"/>
145
</head>
156
<body>
167

@@ -22,7 +13,7 @@
2213
<a class="navbar-brand" href="#" th:href="@{/}">Home</a>
2314
<ul class="nav navbar-nav">
2415
<li><a href="#" th:href="@{/products}">Products</a></li>
25-
<li><a href="#" th:href="@{/products/new}">Create Product</a></li>
16+
<li><a href="#" th:href="@{/product/new}">Create Product</a></li>
2617
</ul>
2718

2819
</div>

src/main/resources/templates/productform.html

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ <h2>Product Details</h2>
1515
<form class="form-horizontal" th:object="${product}" th:action="@{/product}" method="post">
1616
<input type="hidden" th:field="*{id}"/>
1717
<input type="hidden" th:field="*{version}"/>
18-
<div class="form-group">
19-
<label class="col-sm-2 control-label">Product Id:</label>
20-
<div class="col-sm-10">
21-
<input type="text" class="form-control" th:field="*{productId}"/>
22-
</div>
23-
</div>
2418
<div class="form-group">
2519
<label class="col-sm-2 control-label">Description:</label>
2620
<div class="col-sm-10">

src/main/resources/templates/products.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ <h2>Product List</h2>
1919
<th>Price</th>
2020
<th>View</th>
2121
<th>Edit</th>
22+
<th>Delete</th>
2223
</tr>
2324
<tr th:each="product : ${products}">
2425
<td th:text="${product.id}"><a href="/product/${product.id}">Id</a></td>
2526
<td th:text="${product.productId}">Product Id</td>
2627
<td th:text="${product.description}">descirption</td>
2728
<td th:text="${product.price}">price</td>
28-
<td><a th:href="${ '/product/' + product.id}">View</a></td>
29+
<td><a th:href="${'/product/' + product.id}">View</a></td>
2930
<td><a th:href="${'/product/edit/' + product.id}">Edit</a></td>
31+
<td><a th:href="${'/product/delete/' + product.id}">Delete</a></td>
3032
</tr>
3133
</table>
3234

0 commit comments

Comments
 (0)