-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathProductController.java
More file actions
92 lines (82 loc) · 3.61 KB
/
ProductController.java
File metadata and controls
92 lines (82 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package gift.controller;
import gift.exception.InvalidProductException;
import gift.exception.ProductNotFoundException;
import gift.repository.ProductRepository;
import gift.model.Product;
import gift.service.ProductService;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@Controller
@RequestMapping("/products")
public class ProductController {
private final ProductRepository productRepository;
private final ProductService productService;
public ProductController(ProductRepository productRepository, ProductService productService) {
this.productRepository = productRepository;
this.productService = productService;
}
@GetMapping
public String getProducts(Model model) {
model.addAttribute("products", productService.getAllProducts());
model.addAttribute("product", new Product());
return "product-list";
}
@PostMapping
public String addProduct(@ModelAttribute @Valid Product product, RedirectAttributes redirectAttributes) {
try {
productService.addProduct(product);
} catch (InvalidProductException e) {
redirectAttributes.addFlashAttribute("errorMessage", "Invalid product: " + e.getMessage());
}
return "redirect:/products";
}
@PostMapping("/{id}")
public String updateProduct(@Valid @PathVariable Long id, @ModelAttribute Product product, RedirectAttributes redirectAttributes) {
try {
productService.updateProduct(id, product);
} catch (InvalidProductException e) {
redirectAttributes.addFlashAttribute("errorMessage", "Invalid product: " + e.getMessage());
} catch (ProductNotFoundException e) {
redirectAttributes.addFlashAttribute("errorMessage", "Product not found: " + e.getMessage());
} catch (Exception e) {
redirectAttributes.addFlashAttribute("errorMessage", "Error updating product: " + e.getMessage());
}
return "redirect:/products";
}
@PostMapping("/delete/{id}")
public String deleteProduct(@PathVariable Long id, RedirectAttributes redirectAttributes) {
try {
productService.deleteProduct(id);
} catch (ProductNotFoundException e) {
redirectAttributes.addFlashAttribute("errorMessage", "Product not found: " + e.getMessage());
} catch (Exception e) {
redirectAttributes.addFlashAttribute("errorMessage", "Error deleting product: " + e.getMessage());
}
return "redirect:/products";
}
@GetMapping("/view/{id}")
public String getProductDetails(@PathVariable("id") Long id, Model model, RedirectAttributes redirectAttributes) {
try {
Product product = productService.getProductById(id);
model.addAttribute("product", product);
} catch (ProductNotFoundException e) {
redirectAttributes.addFlashAttribute("errorMessage", "Product not found: " + e.getMessage());
} catch (Exception e) {
redirectAttributes.addFlashAttribute("errorMessage", "Product not found" );
return "redirect:/products";
}
return "product-detail";
}
@GetMapping("/{id}")
@ResponseBody
public Product getProductById(@PathVariable("id") Long id) {
try {
return productService.getProductById(id);
} catch (Exception e) {
throw new IllegalArgumentException("Product not found: " + e.getMessage());
}
}
}