Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added ERDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions RecipeVerse/src/main/java/com/masai/DAO/IngredientDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<IngredientDTO> getIngredientsByRecipeId(Long recipeId);
}
168 changes: 168 additions & 0 deletions RecipeVerse/src/main/java/com/masai/DAO/IngredientDAOImpl.java
Original file line number Diff line number Diff line change
@@ -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<IngredientDTO> 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<IngredientDTO> getIngredientsByRecipeId(Long recipeId) {
// TODO Auto-generated method stub
return null;
}
}
2 changes: 1 addition & 1 deletion RecipeVerse/src/main/java/com/masai/DAO/RecipeDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
127 changes: 127 additions & 0 deletions RecipeVerse/src/main/java/com/masai/DAO/RecipeDAOImpl.java
Original file line number Diff line number Diff line change
@@ -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<RecipeDTO> 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<RecipeDTO> cq = cb.createQuery(RecipeDTO.class);

// Set the root entity (from clause) for the query
Root<RecipeDTO> root = cq.from(RecipeDTO.class);

// Define the select clause
cq.select(root);

// Create a TypedQuery using the CriteriaQuery
TypedQuery<RecipeDTO> query = em.createQuery(cq);

// Execute the query and return the list of RecipeDTO
return query.getResultList();
} finally {
if (em != null) {
em.close();
}
}
}
}

Loading