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
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"configurations": [
{
"type": "java",
"name": "Spring Boot-SpringBootWebApplication<spring-boot-web>",
"request": "launch",
"cwd": "${workspaceFolder}",
"mainClass": "guru.springframework.SpringBootWebApplication",
"projectName": "spring-boot-web",
"args": "",
"envFile": "${workspaceFolder}/.env"
}
]
}
Original file line number Diff line number Diff line change
@@ -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<Product> 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<Product> 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<Product> 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<String> 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.");
}
}

}