diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..40ccb54c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + "configurations": [ + { + "type": "java", + "name": "Spring Boot-SpringBootWebApplication", + "request": "launch", + "cwd": "${workspaceFolder}", + "mainClass": "guru.springframework.SpringBootWebApplication", + "projectName": "spring-boot-web", + "args": "", + "envFile": "${workspaceFolder}/.env" + } + ] +} \ No newline at end of file diff --git a/src/main/java/guru/springframework/controllers/ProductController.java b/src/main/java/guru/springframework/controllers/ProductController.java index 21144b30..7211aad3 100644 --- a/src/main/java/guru/springframework/controllers/ProductController.java +++ b/src/main/java/guru/springframework/controllers/ProductController.java @@ -1,55 +1,75 @@ package guru.springframework.controllers; -import guru.springframework.domain.Product; -import guru.springframework.services.ProductService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; + +import guru.springframework.domain.Product; +import guru.springframework.services.ProductService; + @Controller +@RequestMapping("/product") public class ProductController { - private ProductService productService; + private final ProductService productService; @Autowired - public void setProductService(ProductService productService) { + public ProductController(ProductService productService) { this.productService = productService; } - @RequestMapping(value = "/products", method = RequestMethod.GET) - public String list(Model model){ + private ResponseEntity getProductById(Integer id) { + Product product = productService.getProductById(id); + if (product == null) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + return new ResponseEntity<>(product, HttpStatus.OK); + } + + @GetMapping("/list") + public String list(Model model) { model.addAttribute("products", productService.listAllProducts()); - System.out.println("Returning rpoducts:"); return "products"; } - @RequestMapping("product/{id}") - public String showProduct(@PathVariable Integer id, Model model){ - model.addAttribute("product", productService.getProductById(id)); - return "productshow"; + @GetMapping("/{id}") + public ResponseEntity showProduct(@PathVariable Integer id) { + return getProductById(id); } - @RequestMapping("product/edit/{id}") - public String edit(@PathVariable Integer id, Model model){ - model.addAttribute("product", productService.getProductById(id)); + @GetMapping("/edit/{id}") + public String editProduct(@PathVariable Integer id, Model model) { + ResponseEntity responseEntity = getProductById(id); + if (responseEntity.getStatusCode() == HttpStatus.NOT_FOUND) { + return "error/404"; // Return 404 page if product is not found + } + model.addAttribute("product", responseEntity.getBody()); return "productform"; } - @RequestMapping("product/new") - public String newProduct(Model model){ + @GetMapping("/new") + public String newProduct(Model model) { model.addAttribute("product", new Product()); return "productform"; } - @RequestMapping(value = "product", method = RequestMethod.POST) - public String saveProduct(Product product){ - - productService.saveProduct(product); - - return "redirect:/product/" + product.getId(); + @PostMapping + public ResponseEntity saveProduct(@ModelAttribute Product product) { + try { + productService.saveProduct(product); + return ResponseEntity.status(HttpStatus.CREATED) + .body("Product created with ID: " + product.getId()); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("An error occurred while saving the product."); + } } - }