Notes
Notes - notes.io |
public class ProductService {
@Autowired
private ProductRepository productRepository;
public Object postProduct(@RequestBody Product product){
if(productRepository.existsBySku(product.getSku())) {
return productRepository.status(HttpStatus.BAD_REQUEST).build();
} try{
Product newProduct=new Product(product.getName(), product.getDescription(),product.getVendor(),
product.getPrice(), product.getStock(), product.getCurrency(),product.getImage_url(),product.getSku());
productRepository.save(newProduct);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
catch(Exception e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
}
public Object getproduct(){
List<Product> products = productRepository.findAll();
if(products.isEmpty()){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
else{
return ResponseEntity.ok(products);
}
}
public ResponseEntity<Object> getSimilarBVendor(@RequestParam String value){
List<Products> similarProducts = productRepository.findByVendor(value);
if(similarProducts.isEmpty()){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
else{
return ResponseEntity.ok(similarProducts);
}
}
public ResponseEntity<Object> updateProduct(@PathVariable int id, @RequestBody Product product){
Optional<Product> optionalProduct = productRepository.findById(id);
if(!optionalProduct.isPresent()){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
try{
Product existingProduct = optionalProduct.get();
existingProduct.setPrice(product.getPrice());
existingProduct.setStock(product.getStock());
Product savedProduct = productRepository.save(existingProduct);
return ResponseEntity.status(HttpStatus.OK).body(savedProduct);
}
catch(Exception e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
}
public ResponseEntity<Object> deleteProductById(@Pathvariable int id){
if(!productRepository.existsById(id)){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
try{
productRepository.deleteById(id);
return ResponseEntity.status(HttpStatus.OK).build();
}
catch(Exception e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
}
}
}
------------------------------------------------------------------------------------------------------------------------
@Controller
public class ProductController{
@Autowired
private ProductService productService;
@PostMapping("/product/add")
private Object postObject(@RequestBody Product product){
return productService.postproduct(product);
}
@GetMapping("/product/get")
private Object getproduct(){
return productService.getProduct();
}
@PutMapping("/product/{id}")
private ResponseEntity<Object> updateProduct(@PathVariable int id, @RequestBodyProduct product){
return productService.updateProduct(id,product);
}
@DeleteMapping("/product/{id}")
private ResponseEntity<Object> deleteProductById(@PathVariable int id){
return productService.deleteById(id);
}
@GetMapping("/product/vendor")
public ResponseEntity<Object> getSimilarVendor(@RequestParam String value){
return productService.getSimilarVendor(value);
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
