Advertisement
andersonalmada2

Untitled

Aug 1st, 2022
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 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.dto.ProductDTO;
  19. import br.ufc.mandacaru5.model.Product;
  20. import br.ufc.mandacaru5.service.ProductService;
  21.  
  22. @RestController
  23. @RequestMapping(path = "/api/products")
  24. public class ProductController {
  25.  
  26.     @Autowired
  27.     ProductService service;
  28.  
  29.     @GetMapping
  30.     public ResponseEntity<List<ProductDTO>> findAll() {
  31.         return new ResponseEntity<List<ProductDTO>>(service.findAll(), HttpStatus.OK);
  32.     }
  33.  
  34.     @GetMapping(path = "{id}")
  35.     public ResponseEntity<ProductDTO> find(@PathVariable("id") int id) {
  36.         ProductDTO product = service.find(id);
  37.        
  38.         if(product != null) {
  39.             return new ResponseEntity<ProductDTO>(product, HttpStatus.OK); 
  40.         } else {
  41.             return new ResponseEntity<ProductDTO>(HttpStatus.NOT_FOUND);
  42.         }
  43.     }
  44.  
  45.     @GetMapping(path = "/search")
  46.     public ResponseEntity<ProductDTO> findByName(@RequestParam("name") String name) {
  47.         ProductDTO product = service.findByName(name);
  48.        
  49.         if(product != null) {
  50.             return new ResponseEntity<ProductDTO>(product, HttpStatus.OK); 
  51.         } else {
  52.             return new ResponseEntity<ProductDTO>(HttpStatus.NOT_FOUND);
  53.         }
  54.     }
  55.  
  56.     @PostMapping
  57.     public void save(@RequestBody ProductDTO product) {
  58.         service.save(0, product);
  59.     }
  60.  
  61.     @PutMapping(path = "{id}")
  62.     public void update(@PathVariable("id") int id, @RequestBody ProductDTO product) {
  63.         service.save(id, product);
  64.     }
  65.  
  66.     @DeleteMapping(path = "{id}")
  67.     public void delete(@PathVariable("id") int id) {
  68.         service.delete(id);
  69.     }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement