Skip to content

Commit 8be594a

Browse files
Added delete a Product
1 parent b7b97e9 commit 8be594a

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,11 @@ public String saveOrUpdateProduct(Product product){
5555
Product savedProduct = productService.saveOrUpdateProduct(product);
5656
return "redirect:/product/" + savedProduct.getId();
5757
}
58+
59+
@RequestMapping("/product/delete/{id}")
60+
public String delete(@PathVariable Integer id){
61+
productService.deleteProduct(id);
62+
63+
return "redirect:/products";
64+
}
5865
}

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

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

1616
Product saveOrUpdateProduct(Product product);
17+
18+
void deleteProduct(Integer id);
1719
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ private Integer getNextKey(){
4646
return Collections.max(products.keySet()) + 1;
4747
}
4848

49+
@Override
50+
public void deleteProduct(Integer id) {
51+
products.remove(id);
52+
}
53+
4954
private void loadProducts(){
5055
products = new HashMap<>();
5156

src/main/resources/templates/products.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ <h2>Product List</h2>
2626
<th>Image URL</th>
2727
<th>List</th>
2828
<th>Edit</th>
29+
<th>Delete</th>
2930
</tr>
3031
<tr th:each="product : ${products}">
3132
<td th:text="${product.id}"></td>
@@ -34,6 +35,7 @@ <h2>Product List</h2>
3435
<td th:text="${product.imageUrl}"></td>
3536
<td><a th:href="${'/product/' + product.id}">View</a> </td>
3637
<td><a th:href="${'/product/edit/' + product.id}">Edit</a> </td>
38+
<td><a th:href="${'/product/delete/' + product.id}">Delete</a> </td>
3739
</tr>
3840
</table>
3941
</div>

0 commit comments

Comments
 (0)