Updated rest

This commit is contained in:
Sambo Chea 2020-06-09 18:22:53 +07:00
parent 5652436981
commit 121dd53752

View File

@ -1,10 +1,7 @@
package com.cubetiqs.demo.rest; package com.cubetiqs.demo.rest;
import com.cubetiqs.demo.domain.Product; import com.cubetiqs.demo.domain.Product;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
@ -13,11 +10,23 @@ import java.util.List;
@RestController @RestController
@RequestMapping(path = "/products") @RequestMapping(path = "/products")
public class ProductController { public class ProductController {
@RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}) private final List<Product> items = new ArrayList<>();
@RequestMapping(method = {RequestMethod.GET})
public List<Product> getAllProducts() { public List<Product> getAllProducts() {
List<Product> items = new ArrayList<>();
items.add(new Product(1L, "Apple", BigDecimal.valueOf(1.0), false)); items.add(new Product(1L, "Apple", BigDecimal.valueOf(1.0), false));
items.add(new Product(2L, "Apple", BigDecimal.valueOf(0.5), true)); items.add(new Product(2L, "Apple", BigDecimal.valueOf(0.5), true));
return items; 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;
}
} }