Advertisement
andersonalmada

Untitled

Jul 21st, 2022
1,184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. package br.ufc.mandacaru5.controller;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.http.HttpStatus;
  7. import org.springframework.http.ResponseEntity;
  8. import org.springframework.web.bind.annotation.DeleteMapping;
  9. import org.springframework.web.bind.annotation.GetMapping;
  10. import org.springframework.web.bind.annotation.PathVariable;
  11. import org.springframework.web.bind.annotation.PostMapping;
  12. import org.springframework.web.bind.annotation.PutMapping;
  13. import org.springframework.web.bind.annotation.RequestBody;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RequestParam;
  16. import org.springframework.web.bind.annotation.RestController;
  17.  
  18. import br.ufc.mandacaru5.model.Product;
  19. import br.ufc.mandacaru5.service.ProductService;
  20.  
  21. @RestController
  22. @RequestMapping(path = "/api/products")
  23. public class ProductController {
  24.  
  25.     @Autowired
  26.     ProductService service;
  27.  
  28.     @GetMapping
  29.     public ResponseEntity<List<Product>> findAll() {
  30.         return new ResponseEntity<List<Product>>(service.findAll(), HttpStatus.OK);
  31.     }
  32.  
  33.     @GetMapping(path = "{id}")
  34.     public ResponseEntity<Product> find(@PathVariable("id") int id) {
  35.         return new ResponseEntity<Product>(service.find(id), HttpStatus.OK);
  36.     }
  37.  
  38.     @GetMapping(path = "/search")
  39.     public ResponseEntity<Product> findByName(@RequestParam("name") String name) {
  40.         Product product = service.findByName(name);
  41.        
  42.         if(product != null) {
  43.             return new ResponseEntity<Product>(product, HttpStatus.OK);
  44.         } else {
  45.             return new ResponseEntity<Product>(HttpStatus.NOT_FOUND);
  46.         }
  47.     }
  48.  
  49.     @PostMapping
  50.     public void save(@RequestBody Product product) {
  51.         service.save(0, product);
  52.     }
  53.  
  54.     @PutMapping(path = "{id}")
  55.     public void update(@PathVariable("id") int id, @RequestBody Product product) {
  56.         service.save(id, product);
  57.     }
  58.  
  59.     @DeleteMapping(path = "{id}")
  60.     public void delete(@PathVariable("id") int id) {
  61.         service.delete(id);
  62.     }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement