35 lines
977 B
Java
35 lines
977 B
Java
package com.cubetiqs.demo.rest;
|
|
|
|
import com.cubetiqs.demo.domain.Product;
|
|
import com.cubetiqs.demo.repository.ProductRepository;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.List;
|
|
|
|
@RestController
|
|
@RequestMapping(path = "/products")
|
|
public class ProductController {
|
|
private final ProductRepository productRepository;
|
|
|
|
@Autowired
|
|
public ProductController(ProductRepository productRepository) {
|
|
this.productRepository = productRepository;
|
|
}
|
|
|
|
@RequestMapping(method = {RequestMethod.GET})
|
|
public List<Product> getAllProducts() {
|
|
return productRepository.findAll();
|
|
}
|
|
|
|
@GetMapping("/{id}")
|
|
public Product getOneProduct(@PathVariable Long id) {
|
|
return productRepository.findById(id).orElse(null);
|
|
}
|
|
|
|
@PostMapping
|
|
public Product createProduct(@RequestBody Product item) {
|
|
return productRepository.save(item);
|
|
}
|
|
}
|