Skip to content
This repository has been archived by the owner on Mar 13, 2022. It is now read-only.

Commit

Permalink
fix createBento and uploadImage
Browse files Browse the repository at this point in the history
  • Loading branch information
OoiJunHao committed Apr 11, 2021
1 parent d26a1ff commit 73fbc03
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ public MealEntity retrieveMealById(Long mealId) throws MealNotFoundException {

@Override
public List<MealEntity> retrieveAllMeals() {
Query query = em.createQuery("SELECT m FROM MealEntity m");
Query query = em.createQuery("SELECT m FROM MealEntity m WHERE m.isAvailable = true");
return query.getResultList();
}


// TO include those that are not available as this is used by RESTFul for the admin side
@Override
public List<MealEntity> retrieveAllMealsSortedByAvailability() {
Query query = em.createQuery("SELECT m FROM MealEntity m");
Expand All @@ -109,15 +110,15 @@ public List<MealEntity> retrieveAllMealsSortedByAvailability() {

@Override
public List<BentoEntity> retriveAllBentos() {
Query query = em.createQuery("SELECT b FROM BentoEntity b");
Query query = em.createQuery("SELECT b FROM BentoEntity b WHERE b.isAvailable = true");
return query.getResultList();
}

@Override
public List<BentoEntity> retrieveBentosByCategory(String category) {

//Query query = em.createQuery("SELECT b FROM BentoEntity b WHERE :inCategory MEMBER OF (b.categories)"); //This is not possible because categories is not an entity
Query query = em.createQuery("SELECT b FROM BentoEntity b");
Query query = em.createQuery("SELECT b FROM BentoEntity b WHERE b.isAvailable = true");

List<BentoEntity> bentos = query.getResultList();
List<BentoEntity> res = new ArrayList<>();
Expand All @@ -143,7 +144,7 @@ private String prepareInputDataValidationErrorsMessage(Set<ConstraintViolation<M
}

public List<MealEntity> sortMealEntityByRating() {
Query query = em.createQuery("SELECT meals FROM MealEntity meals ORDER BY meals.averageRating DESC");
Query query = em.createQuery("SELECT meals FROM MealEntity meals WHERE meals.isAvailable = true ORDER BY meals.averageRating DESC");
return query.getResultList();
}

Expand Down
19 changes: 9 additions & 10 deletions OTFoodRws/src/java/ws/datamodel/CreateMealReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
package ws.datamodel;

import entity.MealEntity;
import entity.BentoEntity;

/**
*
Expand All @@ -14,12 +14,12 @@
public class CreateMealReq {
private String username;
private String password;
private MealEntity mealEntity;
private BentoEntity bentoEntity;

public CreateMealReq(String username, String password, MealEntity mealEntity) {
public CreateMealReq(String username, String password, BentoEntity bentoEntity) {
this.username = username;
this.password = password;
this.mealEntity = mealEntity;
this.bentoEntity = bentoEntity;
}

public CreateMealReq() {
Expand All @@ -41,13 +41,12 @@ public void setPassword(String password) {
this.password = password;
}

public MealEntity getMealEntity() {
return mealEntity;
public BentoEntity getBentoEntity() {
return bentoEntity;
}

public void setMealEntity(MealEntity mealEntity) {
this.mealEntity = mealEntity;
public void setBentoEntity(BentoEntity bentoEntity) {
this.bentoEntity = bentoEntity;
}



}
5 changes: 3 additions & 2 deletions OTFoodRws/src/java/ws/rest/BentoResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ejb.session.stateless.IngredientEntitySessionBeanLocal;
import ejb.session.stateless.MealEntitySessionBeanLocal;
import ejb.session.stateless.StaffEntitySessionBeanLocal;
import entity.BentoEntity;
import entity.IngredientEntity;
import entity.MealEntity;
import entity.ReviewEntity;
Expand Down Expand Up @@ -117,10 +118,10 @@ public Response retrieveMeal(@PathParam("mealId") Long mealId, @QueryParam("user
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createMeal(CreateMealReq wrapper) {
if (wrapper.getMealEntity() != null) {
if (wrapper.getBentoEntity() != null) {
try {
StaffEntity staffEntity = staffEntitySessionBeanLocal.staffLogin(wrapper.getUsername(), wrapper.getPassword());
Long newMealId = mealEntitySessionBeanLocal.createNewMeal(wrapper.getMealEntity());
Long newMealId = mealEntitySessionBeanLocal.createNewMeal(wrapper.getBentoEntity());
return Response.status(Status.OK).entity(newMealId).build();
} catch (InputDataValidationException | UnknownPersistenceException | MealExistsException ex) {
return Response.status(Response.Status.BAD_REQUEST).entity(ex.getMessage()).build();
Expand Down
50 changes: 45 additions & 5 deletions OTFoodRws/src/java/ws/rest/IngredientManagementResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import ejb.session.stateless.IngredientEntitySessionBeanLocal;
import ejb.session.stateless.StaffEntitySessionBeanLocal;
import entity.IngredientEntity;
import entity.PromoCodeEntity;
import entity.StaffEntity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Produces;
Expand All @@ -27,6 +27,8 @@
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import util.exception.IngredientEntityExistsException;
import util.exception.IngredientEntityNotFoundException;
import util.exception.InputDataValidationException;
Expand Down Expand Up @@ -116,5 +118,43 @@ public Response updateIngredient(UpdateIngredientReq updateIngredientReq) {
}

}

@Path("uploadImage")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadImage(
@FormDataParam("image") InputStream uploadedInputStream,
@FormDataParam("image") FormDataContentDisposition fileDetail,
@QueryParam("name") String name) {

System.out.println("************* Image being uploaded ******************");

//String uploadedFileLocation = "C:/glassfish-5.1.0-uploadedFiles/" + fileDetail.getFileName();
String uploadedFileLocation = "C:/glassfish-5.1.0-uploadedFiles/uploadedFiles/ingredients/" + name + ".jpg";
// save it
saveToFile(uploadedInputStream, uploadedFileLocation);

String output = "File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation;

return Response.status(Response.Status.OK).build();

}

private void saveToFile(InputStream uploadedInputStream, String uploadedFileLocation) {
try {
OutputStream out = null;
int read = 0;
byte[] bytes = new byte[1024];

out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

0 comments on commit 73fbc03

Please sign in to comment.