diff --git a/src/main/java/com/cubetiqs/demo/rest/ProductController.java b/src/main/java/com/cubetiqs/demo/rest/ProductController.java index d36db26..0bca7ef 100644 --- a/src/main/java/com/cubetiqs/demo/rest/ProductController.java +++ b/src/main/java/com/cubetiqs/demo/rest/ProductController.java @@ -1,10 +1,7 @@ package com.cubetiqs.demo.rest; import com.cubetiqs.demo.domain.Product; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.math.BigDecimal; import java.util.ArrayList; @@ -13,11 +10,23 @@ import java.util.List; @RestController @RequestMapping(path = "/products") public class ProductController { - @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}) + private final List items = new ArrayList<>(); + + @RequestMapping(method = {RequestMethod.GET}) public List getAllProducts() { - List items = new ArrayList<>(); items.add(new Product(1L, "Apple", BigDecimal.valueOf(1.0), false)); items.add(new Product(2L, "Apple", BigDecimal.valueOf(0.5), true)); return items; } + + @GetMapping("/{id}") + public Product getOneProduct(@PathVariable int id) { + return items.get(id); + } + + @PostMapping + public Product createProduct(@RequestBody Product item) { + items.add(item); + return item; + } }