/** * Method to delete meal by using meal ID which is unique for each meal. * * @return */ @Security.Authenticated(RestaurantFilter.class) public static Result deleteMeal(int id) { Meal m = Meal.find(id); Restaurant r = m.restaurant; m.restaurant = null; List<Meal> rMeals = r.meals; Iterator<Meal> it = rMeals.iterator(); while (it.hasNext()) { int index = 0; Meal current = it.next(); if (current.id == id) { rMeals.remove(index); break; } index++; } try { Meal.delete(m); Logger.info("Restaurant " + r.name + " just deleted meal."); flash("deletedMeal", "You have successfully deleted your meal"); } catch (Exception e) { Logger.info("Restaurant " + r.name + " just failed to delete meal."); } return redirect("/restaurantOwner/" + Session.getCurrentUser(ctx()).email); }
@Security.Authenticated(RestaurantFilter.class) public static Result editMealURL(int id) { String userEmail = Session.getCurrentUser(ctx()).email; Meal oldMeal = Meal.find(id); User user = Session.getCurrentUser(ctx()); Restaurant restaurant = user.restaurant; return ok(views.html.restaurant.restaurantOwnerEditMeal.render(oldMeal, userEmail, restaurant)); }
@Security.Authenticated(RestaurantFilter.class) public static Result restaurant(String email) { List<Restaurant> restaurants = findR.all(); User u = User.find(email); List<Meal> meals = Meal.allById(u); Restaurant restaurant = u.restaurant; List<TransactionU> tobeapproved = restaurant.toBeApproved; return ok( views.html.restaurant.restaurantOwner.render( email, meals, restaurant, restaurants, tobeapproved)); }
public static Result editMeal(int id) { String userEmail = Session.getCurrentUser(ctx()).email; Meal oldMeal = Meal.find(id); String mealName = inputForm.bindFromRequest().field("name").value(); String mealPrice = inputForm.bindFromRequest().field("price").value(); String mealCategory = inputForm.bindFromRequest().field("category").value(); String mealDescription = inputForm.bindFromRequest().field("description").value(); mealPrice = mealPrice.replace(',', '.'); Double price = Double.parseDouble(mealPrice); try { Meal.modifyMeal(oldMeal, mealName, price, mealCategory, mealDescription); flash("successEdited", "You have successfully edited your meal"); Logger.info("User " + userEmail + " just edited meal " + oldMeal.id); } catch (Exception e) { Logger.error("User " + userEmail + " failed to edit meal " + oldMeal.id); } return redirect("/restaurantOwner/" + userEmail); }
/** * Method to create meal. Use Meal.create method from models. * * @return */ @Security.Authenticated(RestaurantFilter.class) public static Result createMeal() { User u = Session.getCurrentUser(ctx()); if (!u.role.equalsIgnoreCase("RESTAURANT")) { return ok(views.html.admin.wrong.render("Cannot create meal if you're not reastaurant! ")); } String mealName = inputForm.bindFromRequest().field("name").value(); String mealPrice = inputForm.bindFromRequest().field("price").value(); String mealCategory = inputForm.bindFromRequest().field("category").value(); String mealDescription = inputForm.bindFromRequest().field("description").value(); mealPrice = mealPrice.replace(',', '.'); Double price = Double.parseDouble(mealPrice); Restaurant currentUser = u.restaurant; if ((Meal.create(mealName, price, mealCategory, currentUser, mealDescription)) == true) { Meal m = findM .where() .eq("name", mealName) .eq("price", mealPrice) .eq("restaurant_id", u.restaurant.id) .findUnique(); String userEmail = Session.getCurrentUser(ctx()).email; Logger.debug(m.name); session("email", userEmail); flash("successMeal", "Succesfully created meal!"); Logger.info("Restaurant " + currentUser.name + " just created meal"); return ok( views.html.restaurant.fileUploadMeal.render("", userEmail, m, Restaurant.all(), m.image)); // return redirect("/restaurantOwner/" + userEmail); } Logger.error("Restaurant " + currentUser.name + " failed to create meal."); return TODO; }