diff --git a/ERDiagram.png b/ERDiagram.png new file mode 100644 index 0000000..28e060e Binary files /dev/null and b/ERDiagram.png differ diff --git a/README.md b/README.md index 1621ef5..8c11963 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,12 @@ ReciperVerse is a console-based Java application for managing recipes, ingredien ## Table of Contents 1. [Introduction](#introduction) 2. [Application Structure](#application-structure) -3. [Features](#features) -4. [Getting Started](#getting-started) -5. [Usage](#usage) -6. [Contributing](#contributing) -7. [License](#license) +3. [ERDiagram](#erdiagram) +4. [Features](#features) +5. [Getting Started](#getting-started) +6. [Usage](#usage) +7. [Contributing](#contributing) +8. [License](#license) ## Introduction @@ -24,6 +25,10 @@ The ReciperVerse application consists of several components, including: - **DAO (Data Access Objects)**: Provides data access to the database. - **DTO (Data Transfer Objects)**: Represents the data structures for recipes, ingredients, and users. +## ERDiagram +![ER-Diagram](https://github.com/AK016/toothsome-downtown-2195-/blob/Review-Day1/ERDiagram.png) + + ## Features ReciperVerse offers the following key features: diff --git a/RecipeVerse/src/main/java/com/masai/DAO/IngredientDAO.java b/RecipeVerse/src/main/java/com/masai/DAO/IngredientDAO.java index b235870..f9d2532 100644 --- a/RecipeVerse/src/main/java/com/masai/DAO/IngredientDAO.java +++ b/RecipeVerse/src/main/java/com/masai/DAO/IngredientDAO.java @@ -3,11 +3,13 @@ import java.util.List; import com.masai.DTO.IngredientDTO; +import com.masai.Exceptions.NoRecordFoundException; +import com.masai.Exceptions.SomethingWentWrongException; public interface IngredientDAO { void saveIngredient(IngredientDTO ingredient); void updateIngredient(IngredientDTO ingredient); - void deleteIngredient(Long ingredientId); - IngredientDTO getIngredientById(Long ingredientId); + void deleteIngredient(Long ingredientId) throws NoRecordFoundException; + IngredientDTO getIngredientById(Long ingredientId) throws SomethingWentWrongException; List getIngredientsByRecipeId(Long recipeId); } diff --git a/RecipeVerse/src/main/java/com/masai/DAO/IngredientDAOImpl.java b/RecipeVerse/src/main/java/com/masai/DAO/IngredientDAOImpl.java new file mode 100644 index 0000000..cd6066b --- /dev/null +++ b/RecipeVerse/src/main/java/com/masai/DAO/IngredientDAOImpl.java @@ -0,0 +1,168 @@ +package com.masai.DAO; + +import java.util.List; + +import com.masai.DTO.IngredientDTO; +import com.masai.Exceptions.NoRecordFoundException; +import com.masai.Exceptions.SomethingWentWrongException; +import com.masai.utility.EmUtils; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityTransaction; +import jakarta.persistence.PersistenceException; + +public class IngredientDAOImpl implements IngredientDAO { + + @Override + public IngredientDTO getIngredientById(Long id) throws SomethingWentWrongException { + IngredientDTO ingredient = null; + EntityManager em = null; + + try { + // Get an EntityManager from the utility class + em = EmUtils.getEntityManager(); + + // Find the IngredientDTO by its ID + ingredient = em.find(IngredientDTO.class, id); + + // Check if the ingredient is null and throw an exception if not found + if (ingredient == null) { + throw new NoRecordFoundException("Invalid Ingredient Id"); + } + } catch (PersistenceException | NoRecordFoundException e) { + // Handle any exceptions that occur during database operations + throw new SomethingWentWrongException("Unable to get Ingredient, try again later"); + } finally { + if (em != null) { + // Close the EntityManager if it was opened + em.close(); + } + } + return ingredient; + } + + @Override + public void saveIngredient(IngredientDTO ingredient) { + EntityManager em = null; + EntityTransaction et = null; + + try { + // Get an EntityManager from the utility class + em = EmUtils.getEntityManager(); + et = em.getTransaction(); + + // Begin the transaction + et.begin(); + + // Persist the ingredient to the database + em.persist(ingredient); + + // Commit the transaction + et.commit(); + } catch (PersistenceException e) { + if (et != null) { + // Rollback the transaction if an exception occurs + et.rollback(); + } + } finally { + if (em != null) { + // Close the EntityManager if it was opened + em.close(); + } + } + } + + @Override + public void updateIngredient(IngredientDTO ingredient) { + EntityManager em = null; + EntityTransaction et = null; + + try { + // Get an EntityManager from the utility class + em = EmUtils.getEntityManager(); + et = em.getTransaction(); + + // Begin the transaction + et.begin(); + + // Merge (update) the ingredient in the database + em.merge(ingredient); + + // Commit the transaction + et.commit(); + } catch (PersistenceException e) { + if (et != null) { + // Rollback the transaction if an exception occurs + et.rollback(); + } + // Handle the exception or log the error + } finally { + if (em != null) { + // Close the EntityManager if it was opened + em.close(); + } + } + } + + @Override + public void deleteIngredient(Long ingredientId) throws NoRecordFoundException { + EntityManager em = null; + EntityTransaction et = null; + try { + // Get an EntityManager from the utility class + em = EmUtils.getEntityManager(); + et = em.getTransaction(); + + // Begin the transaction + et.begin(); + + // Find the IngredientDTO by its ID + IngredientDTO ingredient = em.find(IngredientDTO.class, ingredientId); + + // Check if the ingredient is null and throw an exception if not found + if (ingredient == null) { + throw new NoRecordFoundException("Invalid Ingredient Id"); + } + + // Remove (delete) the ingredient from the database + em.remove(ingredient); + + // Commit the transaction + et.commit(); + } catch (PersistenceException e) { + if (et != null) { + // Rollback the transaction if an exception occurs + et.rollback(); + } + // Handle the exception or log the error + } finally { + if (em != null) { + // Close the EntityManager if it was opened + em.close(); + } + } + } + + public List getAllIngredients() { + EntityManager em = null; + + try { + // Get an EntityManager from the utility class + em = EmUtils.getEntityManager(); + + // Query to retrieve all ingredients + return em.createQuery("SELECT i FROM IngredientDTO i", IngredientDTO.class).getResultList(); + } finally { + if (em != null) { + // Close the EntityManager if it was opened + em.close(); + } + } + } + + @Override + public List getIngredientsByRecipeId(Long recipeId) { + // TODO Auto-generated method stub + return null; + } +} diff --git a/RecipeVerse/src/main/java/com/masai/DAO/RecipeDAO.java b/RecipeVerse/src/main/java/com/masai/DAO/RecipeDAO.java index 6056729..0478598 100644 --- a/RecipeVerse/src/main/java/com/masai/DAO/RecipeDAO.java +++ b/RecipeVerse/src/main/java/com/masai/DAO/RecipeDAO.java @@ -5,7 +5,7 @@ import com.masai.DTO.RecipeDTO; public interface RecipeDAO { - void saveRecipe(RecipeDTO recipe); + String saveRecipe(RecipeDTO recipe); void updateRecipe(RecipeDTO recipe); void deleteRecipe(Long recipeId); RecipeDTO getRecipeById(Long recipeId); diff --git a/RecipeVerse/src/main/java/com/masai/DAO/RecipeDAOImpl.java b/RecipeVerse/src/main/java/com/masai/DAO/RecipeDAOImpl.java new file mode 100644 index 0000000..c9a11a0 --- /dev/null +++ b/RecipeVerse/src/main/java/com/masai/DAO/RecipeDAOImpl.java @@ -0,0 +1,127 @@ +package com.masai.DAO; + +import java.util.List; + +import com.masai.DTO.RecipeDTO; +import com.masai.utility.EmUtils; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityTransaction; +import jakarta.persistence.PersistenceException; +import jakarta.persistence.TypedQuery; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; + +public class RecipeDAOImpl implements RecipeDAO { + + // Method to save a new recipe to the database + @Override + public String saveRecipe(RecipeDTO recipe) { + EntityManager em = null; + EntityTransaction et = null; + + try { + em = EmUtils.getEntityManager(); + et = em.getTransaction(); + et.begin(); + em.persist(recipe); + et.commit(); + return "Recipe Added Successfully"; + } catch (PersistenceException e) { + et.rollback(); + return "Unable to add Recipe " + e.getMessage(); + } finally { + em.close(); + } + } + + // Method to update an existing recipe in the database + @Override + public void updateRecipe(RecipeDTO recipe) { + EntityManager em = null; + try { + em = EmUtils.getEntityManager(); + em.getTransaction().begin(); + em.merge(recipe); + em.getTransaction().commit(); + } catch (Exception e) { + if (em != null) { + em.getTransaction().rollback(); + } + } finally { + if (em != null) { + em.close(); + } + } + } + + // Method to delete a recipe from the database + @Override + public void deleteRecipe(Long recipeId) { + EntityManager em = null; + try { + em = EmUtils.getEntityManager(); + em.getTransaction().begin(); + RecipeDTO recipe = em.find(RecipeDTO.class, recipeId); + if (recipe != null) { + em.remove(recipe); + } + em.getTransaction().commit(); + } catch (Exception e) { + if (em != null) { + em.getTransaction().rollback(); + } + } finally { + if (em != null) { + em.close(); + } + } + } + + // Method to retrieve a recipe by its unique ID + @Override + public RecipeDTO getRecipeById(Long recipeId) { + EntityManager em = null; + try { + em = EmUtils.getEntityManager(); + return em.find(RecipeDTO.class, recipeId); + } finally { + if (em != null) { + em.close(); + } + } + } + + // Method to retrieve all recipes from the database + @Override + public List getAllRecipes() { + EntityManager em = null; + try { + em = EmUtils.getEntityManager(); + + // Create a CriteriaBuilder to build a query + CriteriaBuilder cb = em.getCriteriaBuilder(); + + // Create a CriteriaQuery for RecipeDTO + CriteriaQuery cq = cb.createQuery(RecipeDTO.class); + + // Set the root entity (from clause) for the query + Root root = cq.from(RecipeDTO.class); + + // Define the select clause + cq.select(root); + + // Create a TypedQuery using the CriteriaQuery + TypedQuery query = em.createQuery(cq); + + // Execute the query and return the list of RecipeDTO + return query.getResultList(); + } finally { + if (em != null) { + em.close(); + } + } + } +} + diff --git a/RecipeVerse/src/main/java/com/masai/DTO/IngredientDTO.java b/RecipeVerse/src/main/java/com/masai/DTO/IngredientDTO.java index 07d43ac..0661e95 100644 --- a/RecipeVerse/src/main/java/com/masai/DTO/IngredientDTO.java +++ b/RecipeVerse/src/main/java/com/masai/DTO/IngredientDTO.java @@ -1,5 +1,6 @@ package com.masai.DTO; +import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -21,53 +22,59 @@ public class IngredientDTO { @Column(name = "name", nullable = false) private String name; - @ManyToOne(fetch = FetchType.LAZY) + @Column(name = "quantity", nullable = false) + private double quantity; + + @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST) @JoinColumn(name = "recipe_id", nullable = false) private RecipeDTO recipe; - - // Constructors, getters, and setters public IngredientDTO() { - super(); + super(); + } + + public IngredientDTO(Long id, String name, double quantity, RecipeDTO recipe) { + super(); + this.id = id; + this.name = name; + this.quantity = quantity; + this.recipe = recipe; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; } - - public IngredientDTO(Long id, String name, RecipeDTO recipe) { - super(); - this.id = id; - this.name = name; - this.recipe = recipe; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public RecipeDTO getRecipe() { - return recipe; - } - - public void setRecipe(RecipeDTO recipe) { - this.recipe = recipe; - } - - @Override - public String toString() { - return "IngredientDTO id=" + id + ", name=" + name + ", recipe=" + recipe + "\n"; - } - - - -} + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double getQuantity() { + return quantity; + } + + public void setQuantity(double quantity) { + this.quantity = quantity; + } + + public RecipeDTO getRecipe() { + return recipe; + } + + public void setRecipe(RecipeDTO recipe) { + this.recipe = recipe; + } + + @Override + public String toString() { + return "IngredientDTO id=" + id + ", name=" + name + ", quantity=" + quantity + ", recipe=" + recipe + "\n"; + } +} diff --git a/RecipeVerse/src/main/java/com/masai/DTO/RecipeDTO.java b/RecipeVerse/src/main/java/com/masai/DTO/RecipeDTO.java index 2cc618d..71abae8 100644 --- a/RecipeVerse/src/main/java/com/masai/DTO/RecipeDTO.java +++ b/RecipeVerse/src/main/java/com/masai/DTO/RecipeDTO.java @@ -27,24 +27,24 @@ public class RecipeDTO { @Column(name = "price") private double price; - @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, fetch = FetchType.EAGER) private List ingredients; @Column(name = "time_taken") private int timeTaken; - @ManyToMany(mappedBy = "likedRecipes") + @ManyToMany(mappedBy = "likedRecipes", fetch = FetchType.EAGER) private List likedByCustomers; + // Constructors, getters, and setters public RecipeDTO() { super(); } - public RecipeDTO(Long id, String recipeName, double price, List ingredients, int timeTaken, + public RecipeDTO(String recipeName, double price, List ingredients, int timeTaken, List likedByCustomers) { super(); - this.id = id; this.recipeName = recipeName; this.price = price; this.ingredients = ingredients; diff --git a/RecipeVerse/src/main/java/com/masai/UI/Main.java b/RecipeVerse/src/main/java/com/masai/UI/Main.java index 6c1cd11..07fdf22 100644 --- a/RecipeVerse/src/main/java/com/masai/UI/Main.java +++ b/RecipeVerse/src/main/java/com/masai/UI/Main.java @@ -1,16 +1,35 @@ package com.masai.UI; +import java.util.ArrayList; +import java.util.List; import java.util.Scanner; +import com.masai.DAO.IngredientDAO; +import com.masai.DAO.IngredientDAOImpl; import com.masai.DAO.UserDAOImpl; import com.masai.DTO.CustomerDTO; +import com.masai.DTO.IngredientDTO; +import com.masai.DTO.RecipeDTO; import com.masai.services.CustomerService; import com.masai.services.CustomerServiceImpl; +import com.masai.services.IngredientService; +import com.masai.services.IngredientServiceImpl; +import com.masai.services.RecipeService; +import com.masai.services.RecipeServiceImpl; public class Main { - private static CustomerService customerService = new CustomerServiceImpl(new UserDAOImpl()); private static Scanner scanner = new Scanner(System.in); + private static CustomerService customerService = new CustomerServiceImpl(new UserDAOImpl()); + private static RecipeService recipeService = new RecipeServiceImpl(); + // Create the IngredientDAO + private static IngredientDAO ingredientDAO = new IngredientDAOImpl(); + + // Create the IngredientService with the injected IngredientDAO + private static IngredientService ingredientService = new IngredientServiceImpl(ingredientDAO); +// private static IngredientService ingredientService = new IngredientServiceImpl(); + + public static void main(String[] args) { try { while (true) { @@ -21,7 +40,7 @@ public static void main(String[] args) { System.out.println("4. Exit"); System.out.print("Enter your choice: "); int choice = scanner.nextInt(); - scanner.nextLine(); // Consume the newline character + scanner.nextLine(); switch (choice) { case 1: @@ -51,44 +70,40 @@ public static void main(String[] args) { // to DAOImpl layer } } - - private static void adminLogin() { - System.out.print("Enter Admin Username: "); - String adminUsernameInput = scanner.nextLine(); - System.out.print("Enter Admin Password: "); - String adminPasswordInput = scanner.nextLine(); - - if (customerService.login(adminUsernameInput, adminPasswordInput, "admin")) { - adminMenu(); - } else { - System.out.println("Invalid admin credentials."); - } - } - - - - private static void signUpUser() { - System.out.print("Enter Username: "); - String username = scanner.nextLine(); - System.out.print("Enter Email: "); // Add prompt for the email - String email = scanner.nextLine(); - System.out.print("Enter Password: "); - String password = scanner.nextLine(); - System.out.print("Enter Role (admin/customer): "); - String role = scanner.nextLine(); - - // Save the new user to the database - CustomerDTO newUser = new CustomerDTO(username, email, password, role); - - // Pass the CustomerDTO object to the signUp method - if (customerService.signUp(newUser)) { - System.out.println("Sign up successful. You can now log in."); - } else { - System.out.println("Sign up failed. Please try again."); - } - } + private static void adminLogin() { + System.out.print("Enter Admin Username: "); + String adminUsernameInput = scanner.nextLine(); + System.out.print("Enter Admin Password: "); + String adminPasswordInput = scanner.nextLine(); + if (customerService.login(adminUsernameInput, adminPasswordInput, "admin")) { + adminMenu(); + } else { + System.out.println("Invalid admin credentials."); + } + } + + private static void signUpUser() { + System.out.print("Enter Username: "); + String username = scanner.nextLine(); + System.out.print("Enter Email: "); + String email = scanner.nextLine(); + System.out.print("Enter Password: "); + String password = scanner.nextLine(); + System.out.print("Enter Role (admin/customer): "); + String role = scanner.nextLine(); + + // Save the new user to the database + CustomerDTO newUser = new CustomerDTO(username, email, password, role); + + // Pass the CustomerDTO object to the signUp method + if (customerService.signUp(newUser)) { + System.out.println("Sign up successful. You can now log in."); + } else { + System.out.println("Sign up failed. Please try again."); + } + } private static void adminMenu() { while (true) { @@ -97,7 +112,8 @@ private static void adminMenu() { System.out.println("2. View Recipe by ID"); System.out.println("3. Delete Recipe by ID"); System.out.println("4. Update Recipe Ingredients"); - System.out.println("5. Go back to Main Menu"); + System.out.println("5. View Recipe List"); + System.out.println("6. Go back to Main Menu"); System.out.print("Enter your choice: "); int choice = scanner.nextInt(); scanner.nextLine(); @@ -118,10 +134,12 @@ private static void adminMenu() { case 4: updateRecipeIngredients(); break; - case 5: + getAllRecipes(); + break; + case 6: System.out.println("Going back to Main Menu..."); - return; // Return to Main Menu + return; default: System.out.println("Invalid choice. Please try again."); @@ -130,16 +148,47 @@ private static void adminMenu() { } } + private static void customerLogin() { + System.out.println("1. Login"); + System.out.println("2. Sign Up"); + System.out.print("Enter your choice: "); + int loginOrSignUpChoice = scanner.nextInt(); + scanner.nextLine(); + + switch (loginOrSignUpChoice) { + case 1: + System.out.print("Enter Customer Username: "); + String customerUsernameInput = scanner.nextLine(); + System.out.print("Enter Customer Password: "); + String customerPasswordInput = scanner.nextLine(); + + if (customerService.login(customerUsernameInput, customerPasswordInput, "customer")) { + customerMenu(); + } else { + System.out.println("Invalid customer credentials."); + } + break; + + case 2: + signUpUser(); + break; + + default: + System.out.println("Invalid choice. Returning to Main Menu."); + break; + } + } + private static void customerMenu() { while (true) { System.out.println("Welcome, Customer!"); System.out.println("1. View Recipe by ID"); System.out.println("2. Like Recipe"); System.out.println("3. View Liked Recipes"); - System.out.println("4. Go back to Main Menu"); // Option to go back to Main Menu + System.out.println("4. Go back to Main Menu"); System.out.print("Enter your choice: "); int choice = scanner.nextInt(); - scanner.nextLine(); // Consume the newline character + scanner.nextLine(); switch (choice) { case 1: @@ -157,7 +206,6 @@ private static void customerMenu() { case 4: System.out.println("Going back to Main Menu..."); return; - // Return to Main Menu default: System.out.println("Invalid choice. Please try again."); @@ -165,66 +213,118 @@ private static void customerMenu() { } } } - - - private static void customerLogin() { - System.out.println("1. Login"); - System.out.println("2. Sign Up"); - System.out.print("Enter your choice: "); - int loginOrSignUpChoice = scanner.nextInt(); - scanner.nextLine(); // Consume the newline character - - switch (loginOrSignUpChoice) { - case 1: - System.out.print("Enter Customer Username: "); - String customerUsernameInput = scanner.nextLine(); - System.out.print("Enter Customer Password: "); - String customerPasswordInput = scanner.nextLine(); - - if (customerService.login(customerUsernameInput, customerPasswordInput, "customer")) { - customerMenu(); - } else { - System.out.println("Invalid customer credentials."); - } - break; - - case 2: - signUpUser(); - break; - - default: - System.out.println("Invalid choice. Returning to Main Menu."); - break; - } - } private static void addRecipe() { - // Implement adding a recipe from DAO layer - // ... + System.out.print("Enter Recipe Name: "); + String name = scanner.nextLine(); + + System.out.print("Enter Recipe Price: "); + double price = scanner.nextDouble(); + scanner.nextLine(); + + System.out.print("Enter Recipe Ingredients separated by a Space: "); + String ingredientsInput = scanner.nextLine(); + List ingredients = new ArrayList<>(); + + // Split the ingredientsInput string into individual ingredient names + String[] ingredientNames = ingredientsInput.split(" "); + + // Create and save IngredientDTO objects for each ingredient name + for (String ingredientName : ingredientNames) { + IngredientDTO ingredient = new IngredientDTO(); + ingredient.setName(ingredientName); + ingredient.setQuantity(1.0); + ingredientService.saveIngredient(ingredient); + ingredients.add(ingredient); + } + + System.out.print("Enter Time Taken to Cook this Recipe: "); + int timeTaken = scanner.nextInt(); + scanner.nextLine(); + + List likes = new ArrayList<>(); + + RecipeDTO recipe = new RecipeDTO(name, price, ingredients, timeTaken, likes); + String s = recipeService.saveRecipe(recipe); + System.out.println(s); } private static void viewRecipeById() { - // Implement viewing a recipe by ID from DAO layer - // ... + System.out.print("Enter Recipe ID: "); + Long recipeId = scanner.nextLong(); + scanner.nextLine(); + + // Call the service method to get the recipe by ID + RecipeDTO recipe = recipeService.getRecipeById(recipeId); + + if (recipe != null) { + System.out.println("Recipe Details:"); + System.out.println(recipe); + } else { + System.out.println("Recipe not found with the given ID."); + } } private static void deleteRecipeById() { - // Implement deleting a recipe by ID from DAO layer - // ... + System.out.print("Enter Recipe ID to Delete: "); + Long recipeId = scanner.nextLong(); + scanner.nextLine(); + + // Call the service method to delete the recipe by ID + recipeService.deleteRecipe(recipeId); + System.out.println("Recipe with ID " + recipeId + " has been deleted successfully!"); } private static void updateRecipeIngredients() { - // Implement updating recipe ingredients from DAO layer - // ... + System.out.print("Enter Recipe ID to Update Ingredients: "); + Long recipeId = scanner.nextLong(); + scanner.nextLine(); + + // Call the service method to get the recipe by ID + RecipeDTO recipe = recipeService.getRecipeById(recipeId); + + if (recipe != null) { + // Assuming you have a method in RecipeDTO to get the list of ingredients + List ingredients = recipe.getIngredients(); + + if (ingredients != null && !ingredients.isEmpty()) { + for (IngredientDTO ingredient : ingredients) { + System.out.print("Enter new quantity for " + ingredient.getName() + ": "); + double newQuantity = scanner.nextDouble(); + scanner.nextLine(); + + // Set the new quantity for the ingredient + ingredient.setQuantity(newQuantity); + } + + // Call the service method to update the recipe with new ingredients + recipeService.updateRecipe(recipe); + System.out.println("Recipe ingredients updated successfully!"); + } else { + System.out.println("No ingredients found for the recipe."); + } + } else { + System.out.println("Recipe not found with the given ID."); + } + } + + private static void getAllRecipes() { + List recipes = recipeService.getAllRecipes(); + if (recipes.isEmpty()) { + System.out.println("No recipes found."); + } else { + System.out.println("All Recipes:"); + for (RecipeDTO recipe : recipes) { + System.out.println(recipe); + } + } } private static void likeRecipe() { - // Implement liking a recipe by a customer from DAO layer - // ... + } private static void viewLikedRecipes() { - // Implement viewing liked recipes by a customer from DAO layer - // ... + } } diff --git a/RecipeVerse/src/main/java/com/masai/services/IngredientService.java b/RecipeVerse/src/main/java/com/masai/services/IngredientService.java index 814eaf9..fafa2ff 100644 --- a/RecipeVerse/src/main/java/com/masai/services/IngredientService.java +++ b/RecipeVerse/src/main/java/com/masai/services/IngredientService.java @@ -1,10 +1,15 @@ package com.masai.services; +import java.util.List; + import com.masai.DTO.IngredientDTO; +import com.masai.Exceptions.NoRecordFoundException; +import com.masai.Exceptions.SomethingWentWrongException; public interface IngredientService { - IngredientDTO getIngredientById(Long id); - void saveIngredient(IngredientDTO ingredient); + void saveIngredient(IngredientDTO ingredient); void updateIngredient(IngredientDTO ingredient); - void deleteIngredient(IngredientDTO ingredient); + void deleteIngredient(Long ingredientId) throws NoRecordFoundException; + IngredientDTO getIngredientById(Long ingredientId) throws NoRecordFoundException, SomethingWentWrongException; + List getIngredientsByRecipeId(Long recipeId); } diff --git a/RecipeVerse/src/main/java/com/masai/services/IngredientServiceImpl.java b/RecipeVerse/src/main/java/com/masai/services/IngredientServiceImpl.java new file mode 100644 index 0000000..cc78429 --- /dev/null +++ b/RecipeVerse/src/main/java/com/masai/services/IngredientServiceImpl.java @@ -0,0 +1,47 @@ +package com.masai.services; + +import java.util.List; + +import com.masai.DAO.IngredientDAO; +import com.masai.DTO.IngredientDTO; +import com.masai.Exceptions.NoRecordFoundException; +import com.masai.Exceptions.SomethingWentWrongException; + +public class IngredientServiceImpl implements IngredientService { + + private IngredientDAO ingredientDAO; + + public IngredientServiceImpl(IngredientDAO ingredientDAO) { + this.ingredientDAO = ingredientDAO; + } + +// IngredientDAO ingredientDAO=new IngredientDAOImpl(); + + @Override + public void saveIngredient(IngredientDTO ingredient) { + ingredientDAO.saveIngredient(ingredient); + } + + @Override + public void updateIngredient(IngredientDTO ingredient) { + ingredientDAO.updateIngredient(ingredient); + } + + @Override + public void deleteIngredient(Long ingredientId) throws NoRecordFoundException { + ingredientDAO.deleteIngredient(ingredientId); + } + + @Override + public IngredientDTO getIngredientById(Long ingredientId) + throws NoRecordFoundException, SomethingWentWrongException { + return ingredientDAO.getIngredientById(ingredientId); + } + + @Override + public List getIngredientsByRecipeId(Long recipeId) { + return ingredientDAO.getIngredientsByRecipeId(recipeId); + } + + +} diff --git a/RecipeVerse/src/main/java/com/masai/services/RecipeService.java b/RecipeVerse/src/main/java/com/masai/services/RecipeService.java index 9acfa84..9114baf 100644 --- a/RecipeVerse/src/main/java/com/masai/services/RecipeService.java +++ b/RecipeVerse/src/main/java/com/masai/services/RecipeService.java @@ -1,10 +1,13 @@ package com.masai.services; +import java.util.List; + import com.masai.DTO.RecipeDTO; public interface RecipeService { + String saveRecipe(RecipeDTO recipe); RecipeDTO getRecipeById(Long id); - void saveRecipe(RecipeDTO recipe); void updateRecipe(RecipeDTO recipe); void deleteRecipe(long id); + List getAllRecipes(); } diff --git a/RecipeVerse/src/main/java/com/masai/services/RecipeServiceImpl.java b/RecipeVerse/src/main/java/com/masai/services/RecipeServiceImpl.java index 52cb74c..ebb0c5b 100644 --- a/RecipeVerse/src/main/java/com/masai/services/RecipeServiceImpl.java +++ b/RecipeVerse/src/main/java/com/masai/services/RecipeServiceImpl.java @@ -1,31 +1,40 @@ package com.masai.services; +import java.util.List; + import com.masai.DAO.RecipeDAO; +import com.masai.DAO.RecipeDAOImpl; import com.masai.DTO.RecipeDTO; public class RecipeServiceImpl implements RecipeService { - private RecipeDAO recipeDAO; - - // Constructor or setter injection for RecipeDAO - - @Override + @Override + public String saveRecipe(RecipeDTO recipe) { + RecipeDAO recipeDAO=new RecipeDAOImpl(); + return recipeDAO.saveRecipe(recipe); + } + + @Override public RecipeDTO getRecipeById(Long id) { + RecipeDAO recipeDAO=new RecipeDAOImpl(); return recipeDAO.getRecipeById(id); } - @Override - public void saveRecipe(RecipeDTO recipe) { - recipeDAO.saveRecipe(recipe); - } - @Override public void updateRecipe(RecipeDTO recipe) { + RecipeDAO recipeDAO=new RecipeDAOImpl(); recipeDAO.updateRecipe(recipe); } @Override public void deleteRecipe(long id) { + RecipeDAO recipeDAO=new RecipeDAOImpl(); recipeDAO.deleteRecipe(id); } + + @Override + public List getAllRecipes(){ + RecipeDAO recipeDAO=new RecipeDAOImpl(); + return recipeDAO.getAllRecipes(); + } } diff --git a/RecipeVerse/target/classes/META-INF/maven/com.masai/RecipeVerse/pom.properties b/RecipeVerse/target/classes/META-INF/maven/com.masai/RecipeVerse/pom.properties index 27e9ec6..47a53ab 100644 --- a/RecipeVerse/target/classes/META-INF/maven/com.masai/RecipeVerse/pom.properties +++ b/RecipeVerse/target/classes/META-INF/maven/com.masai/RecipeVerse/pom.properties @@ -1,5 +1,5 @@ #Generated by Maven Integration for Eclipse -#Sat Jul 22 22:34:00 IST 2023 +#Wed Oct 11 19:01:09 IST 2023 m2e.projectLocation=C\:\\Users\\Akshk\\Documents\\workspace-spring-tool-suite-4-4.18.1.RELEASE\\RecipeVerse m2e.projectName=RecipeVerse groupId=com.masai diff --git a/RecipeVerse/target/classes/com/masai/DAO/IngredientDAO.class b/RecipeVerse/target/classes/com/masai/DAO/IngredientDAO.class index 2b13cbb..5188202 100644 Binary files a/RecipeVerse/target/classes/com/masai/DAO/IngredientDAO.class and b/RecipeVerse/target/classes/com/masai/DAO/IngredientDAO.class differ diff --git a/RecipeVerse/target/classes/com/masai/DAO/IngredientDAOImpl.class b/RecipeVerse/target/classes/com/masai/DAO/IngredientDAOImpl.class new file mode 100644 index 0000000..46a46c0 Binary files /dev/null and b/RecipeVerse/target/classes/com/masai/DAO/IngredientDAOImpl.class differ diff --git a/RecipeVerse/target/classes/com/masai/DAO/RecipeDAO.class b/RecipeVerse/target/classes/com/masai/DAO/RecipeDAO.class index c3d608d..fb749d3 100644 Binary files a/RecipeVerse/target/classes/com/masai/DAO/RecipeDAO.class and b/RecipeVerse/target/classes/com/masai/DAO/RecipeDAO.class differ diff --git a/RecipeVerse/target/classes/com/masai/DAO/RecipeDAOImpl.class b/RecipeVerse/target/classes/com/masai/DAO/RecipeDAOImpl.class new file mode 100644 index 0000000..ad56d7f Binary files /dev/null and b/RecipeVerse/target/classes/com/masai/DAO/RecipeDAOImpl.class differ diff --git a/RecipeVerse/target/classes/com/masai/DTO/IngredientDTO.class b/RecipeVerse/target/classes/com/masai/DTO/IngredientDTO.class index 54cd153..7965a04 100644 Binary files a/RecipeVerse/target/classes/com/masai/DTO/IngredientDTO.class and b/RecipeVerse/target/classes/com/masai/DTO/IngredientDTO.class differ diff --git a/RecipeVerse/target/classes/com/masai/DTO/RecipeDTO.class b/RecipeVerse/target/classes/com/masai/DTO/RecipeDTO.class index f0d031e..3bff898 100644 Binary files a/RecipeVerse/target/classes/com/masai/DTO/RecipeDTO.class and b/RecipeVerse/target/classes/com/masai/DTO/RecipeDTO.class differ diff --git a/RecipeVerse/target/classes/com/masai/UI/Main.class b/RecipeVerse/target/classes/com/masai/UI/Main.class index caf5fb8..a6e2dd5 100644 Binary files a/RecipeVerse/target/classes/com/masai/UI/Main.class and b/RecipeVerse/target/classes/com/masai/UI/Main.class differ diff --git a/RecipeVerse/target/classes/com/masai/services/IngredientService.class b/RecipeVerse/target/classes/com/masai/services/IngredientService.class index f95fa9c..fbedc02 100644 Binary files a/RecipeVerse/target/classes/com/masai/services/IngredientService.class and b/RecipeVerse/target/classes/com/masai/services/IngredientService.class differ diff --git a/RecipeVerse/target/classes/com/masai/services/IngredientServiceImpl.class b/RecipeVerse/target/classes/com/masai/services/IngredientServiceImpl.class new file mode 100644 index 0000000..fd9cb77 Binary files /dev/null and b/RecipeVerse/target/classes/com/masai/services/IngredientServiceImpl.class differ diff --git a/RecipeVerse/target/classes/com/masai/services/RecipeService.class b/RecipeVerse/target/classes/com/masai/services/RecipeService.class index 1624cb1..8770bbe 100644 Binary files a/RecipeVerse/target/classes/com/masai/services/RecipeService.class and b/RecipeVerse/target/classes/com/masai/services/RecipeService.class differ diff --git a/RecipeVerse/target/classes/com/masai/services/RecipeServiceImpl.class b/RecipeVerse/target/classes/com/masai/services/RecipeServiceImpl.class index 5207b4f..b9b1f42 100644 Binary files a/RecipeVerse/target/classes/com/masai/services/RecipeServiceImpl.class and b/RecipeVerse/target/classes/com/masai/services/RecipeServiceImpl.class differ diff --git a/SQLQueries.sql b/SQLQueries.sql new file mode 100644 index 0000000..2c22872 --- /dev/null +++ b/SQLQueries.sql @@ -0,0 +1,68 @@ +-- Create tables +CREATE TABLE Recipe ( + id INTEGER PRIMARY KEY, + recipe_name VARCHAR(255), + price DOUBLE, + time_taken INT +); + +CREATE TABLE Ingredient ( + id INTEGER PRIMARY KEY, + name VARCHAR(255), + quantity DOUBLE, + recipe_id INTEGER, + FOREIGN KEY (recipe_id) REFERENCES Recipe(id) +); + +CREATE TABLE Customer ( + id INTEGER PRIMARY KEY, + name VARCHAR(255), + email VARCHAR(255), + password VARCHAR(255), + role VARCHAR(255) +); + +-- Insert sample data + +-- Recipes +INSERT INTO Recipe (recipe_name, price, time_taken) +VALUES ('Paneer Masala', 250.00, 60); + +-- Ingredients +INSERT INTO Ingredient (name, quantity, recipe_id) +VALUES ('Paneer', 200.0, 1); + +-- Customers +INSERT INTO Customer (name, email, password, role) +VALUES ('John Doe', 'john@example.com', 'password123', 'customer'); + +-- CRUD operations + +-- Read (Retrieve) Operation +-- Retrieve all recipes +SELECT * FROM Recipe; + +-- Retrieve all ingredients for a specific recipe (e.g., Recipe ID 1) +SELECT * FROM Ingredient WHERE recipe_id = 1; + +-- Retrieve a specific customer by their ID (e.g., Customer ID 1) +SELECT * FROM Customer WHERE id = 1; + +-- Update Operation +-- Update the price of a recipe (e.g., Recipe ID 1) +UPDATE Recipe +SET price = 260.00 +WHERE id = 1; + +-- Update the quantity of an ingredient (e.g., Ingredient ID 1) +UPDATE Ingredient +SET quantity = 250.0 +WHERE id = 1; + +-- Delete Operation +-- Delete a recipe and its associated ingredients (e.g., Recipe ID 1) +DELETE FROM Ingredient WHERE recipe_id = 1; +DELETE FROM Recipe WHERE id = 1; + +-- Delete a customer by their ID (e.g., Customer ID 1) +DELETE FROM Customer WHERE id = 1;