From 121dd5375290528e491e3a49eb03fefc22e522d4 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Tue, 9 Jun 2020 18:22:53 +0700 Subject: [PATCH] Updated rest --- .../cubetiqs/demo/rest/ProductController.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) 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; + } }