Skip to content
Snippets Groups Projects
Commit d8adc693 authored by BEAUVAIS ANTOINE's avatar BEAUVAIS ANTOINE
Browse files

Added category filtering for products.

parent 3a2c7a1e
Branches jakarta-ee-dev
No related merge requests found
......@@ -14,7 +14,7 @@ That way multiple projects can share the same settings (useful for formatting ru
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_j2eeVersion>9.0-web</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_j2eeVersion>
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>gfv610ee9</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>
<netbeans.hint.licensePath>${project.basedir}/licenseheader.txt</netbeans.hint.licensePath>
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>gfv610ee9</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>
</properties>
</project-shared-configuration>
......@@ -24,4 +24,13 @@ public class JakartaRestConfiguration extends Application {
* Mapping for product.
*/
public static final String API_PRODUCT = "product";
/*
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<>();
classes.add(EntityNotFoundMapper.class);
// add your additional JAX-RS classes here
return classes;
} */
}
/*
* CONTRAT DE LICENCE DE LOGICIEL LIBRE CECILL-B
* https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html
*/
package fr.unistra.sil.erp.back.mappers;
import jakarta.persistence.EntityNotFoundException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
/**
* Automatically routes an entity not found in the database into
* a 404 Not Found for the client.
* @author BEAUVAIS ANTOINE
*/
@Provider
public class EntityNotFoundMapper implements
ExceptionMapper<EntityNotFoundException> {
@Override
public Response toResponse(EntityNotFoundException arg0) {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
......@@ -27,6 +27,7 @@ import java.util.Objects;
@Table( name = "product" )
@NamedQueries({
@NamedQuery(name="Product.getAllProducts", query="SELECT p FROM Product p"),
@NamedQuery(name="Product.getProductsForCategory", query="SELECT p FROM Product p WHERE p.category = :category"),
@NamedQuery(name="Product.getProduct", query="SELECT p FROM Product p WHERE p.id = :id")
})
public class Product implements Serializable {
......
......@@ -12,6 +12,7 @@ import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.GenericEntity;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
......@@ -29,15 +30,35 @@ public class ProductResource {
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getProducts() {
//EntityManager em = this.emf.createEntityManager();
List<Product> lp = em.createNamedQuery(
"Product.getAllProducts", Product.class).getResultList();
if(lp == null || lp.isEmpty()) {
return Response.noContent().build();
public Response getProducts(@QueryParam("category") String category) {
List<Product> lp;
GenericEntity<List<Product>> res;
if(category != null) {
long lCatId;
try {
lCatId = Long.parseLong(category);
}
catch(NumberFormatException ex) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
lp = em.createNamedQuery("Product.getProductsForCategory",
Product.class).setParameter("category", lCatId)
.getResultList();
if (lp == null || lp.isEmpty()) {
return Response.noContent().build();
}
res = new GenericEntity<List<Product>>(lp) {};
}
GenericEntity<List<Product>> ents = new GenericEntity<List<Product>>(lp) {};
return Response.ok(ents).build();
else {
lp = em.createNamedQuery("Product.getAllProducts", Product.class)
.getResultList();
if (lp == null || lp.isEmpty()) {
return Response.noContent().build();
}
res = new GenericEntity<List<Product>>(lp) {};
}
return Response.ok(res).build();
}
/**
......@@ -56,14 +77,8 @@ public class ProductResource {
catch(NumberFormatException ex) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
List<Product> lp = em.createNamedQuery(
"Product.getProduct", Product.class).setParameter("id", lId)
.getResultList();
if(lp == null || lp.isEmpty()) {
return Response.status(Response.Status.NOT_FOUND).build();
}
Product res = lp.get(0);
Product res = this.em.getReference(Product.class, lId);
GenericEntity<Product> entities = new GenericEntity<Product>(res) {};
return Response.ok(entities).build();
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment