Advertisement
andersonalmada

Untitled

Jul 16th, 2022
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. package mandacaru4.controller;
  2.  
  3. import java.util.List;
  4.  
  5. import jakarta.ws.rs.Consumes;
  6. import jakarta.ws.rs.DELETE;
  7. import jakarta.ws.rs.FormParam;
  8. import jakarta.ws.rs.GET;
  9. import jakarta.ws.rs.POST;
  10. import jakarta.ws.rs.PUT;
  11. import jakarta.ws.rs.Path;
  12. import jakarta.ws.rs.PathParam;
  13. import jakarta.ws.rs.Produces;
  14. import jakarta.ws.rs.QueryParam;
  15. import jakarta.ws.rs.core.MediaType;
  16. import mandacaru4.dao.ProductDAO;
  17. import mandacaru4.dao.ProductHibernateDAO;
  18. import mandacaru4.entities.Product;
  19.  
  20. @Path("/products")
  21. public class ProductController {
  22.  
  23.     ProductDAO productDAO = new ProductHibernateDAO();
  24.  
  25.     @GET
  26.     @Produces(MediaType.APPLICATION_JSON)
  27.     public List<Product> getAllProducts() {
  28.         return productDAO.findAll();
  29.     }
  30.  
  31.     @GET
  32.     @Path("/{id}")
  33.     @Produces(MediaType.APPLICATION_JSON)
  34.     public Product getById(@PathParam("id") int id) {
  35.         return productDAO.find(id);
  36.     }
  37.  
  38.     @GET
  39.     @Path("/search")
  40.     @Produces(MediaType.APPLICATION_JSON)
  41.     public Product getByName(@QueryParam("name") String name) {
  42.         return productDAO.findByName(name);
  43.     }
  44.  
  45.     @POST
  46.     @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  47.     public void addProduct(@FormParam("name") String name, @FormParam("price") double price) {
  48.         Product product = new Product(0, name, price);
  49.         productDAO.save(product);
  50.     }
  51.  
  52.     @PUT
  53.     @Path("/{id}")
  54.     @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  55.     public void updateProduct(@PathParam("id") int id, @FormParam("name") String name,
  56.             @FormParam("price") double price) {
  57.         Product product = new Product(id, name, price);
  58.         productDAO.save(product);
  59.     }
  60.  
  61.     @DELETE
  62.     @Path("/{id}")
  63.     public void deleteProduct(@PathParam("id") int id) {
  64.         productDAO.delete(id);
  65.     }
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement