|
|
|
package com.mousetech.gourmetj.persistence.service;
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
import javax.enterprise.context.ApplicationScoped;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
import javax.inject.Named;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
import com.mousetech.gourmetj.persistence.dao.CategoryRepository;
|
|
|
|
import com.mousetech.gourmetj.persistence.dao.IngredientRepository;
|
|
|
|
import com.mousetech.gourmetj.persistence.dao.RecipeRepository;
|
|
|
|
import com.mousetech.gourmetj.persistence.dao.ShopcatRepository;
|
|
|
|
import com.mousetech.gourmetj.persistence.model.Ingredient;
|
|
|
|
import com.mousetech.gourmetj.persistence.model.Recipe;
|
|
|
|
import com.mousetech.gourmetj.persistence.model.Shopcat;
|
|
|
|
|
|
|
|
@Named
|
|
|
|
@ApplicationScoped
|
|
|
|
@Transactional
|
|
|
|
/**
|
|
|
|
* Persistence service class for Recipes.
|
|
|
|
*
|
|
|
|
* While normally, I would have separate service classes for
|
|
|
|
* different Entity Working Sets, Recipes are so central to this
|
|
|
|
* application, this class contains additional functions that
|
|
|
|
* would ordinarily have their own Service classes.
|
|
|
|
*
|
|
|
|
* @author timh
|
|
|
|
* @since Jan 4, 2022
|
|
|
|
*/
|
|
|
|
public class RecipeService implements Serializable {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
|
|
private static final Logger log =
|
|
|
|
LoggerFactory.getLogger(RecipeService.class);
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
private RecipeRepository recipeRepository;
|
|
|
|
|
|
|
|
public List<Recipe> findAll() {
|
|
|
|
return recipeRepository.findAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Recipe> findByTitle(String searchText) {
|
|
|
|
return recipeRepository
|
|
|
|
.findByTitleContaining(searchText);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Recipe findByPrimaryKey(Long recipeId) {
|
|
|
|
return recipeRepository.findById(recipeId).orElse(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Recipe findDetails(Long recipeId) {
|
|
|
|
return recipeRepository.findDetailsById(recipeId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> findCuisines() {
|
|
|
|
return recipeRepository.FindCuisinesNative();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean save(Recipe recipe) {
|
|
|
|
recipeRepository.save(recipe);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void delete(Recipe recipe) {
|
|
|
|
recipeRepository.delete(recipe);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ShopcatService as a sub-function of RecipeService
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
ShopcatRepository shopcatRepository;
|
|
|
|
|
|
|
|
public List<String> findShoppingCategories() {
|
|
|
|
return shopcatRepository.findDistinctCategoryNative();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Shopcat findShopcatForIngredientKey(String ingkey) {
|
|
|
|
return shopcatRepository.findShopcatByIngkey(ingkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void deleteShopcatByIngKey(String key) {
|
|
|
|
shopcatRepository.deleteByIngkey(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void saveShopcat(Shopcat sc) {
|
|
|
|
shopcatRepository.save(sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteShopcat(Shopcat sc) {
|
|
|
|
shopcatRepository.delete(sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CategoryService as a sub-function of RecipeService
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
CategoryRepository categoryRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return All Category names that are not null/blank,
|
|
|
|
* sorted.
|
|
|
|
*/
|
|
|
|
public List<String> findCategories() {
|
|
|
|
return categoryRepository.findDistinctCategoryNative();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Recipe> findByCategoryLike(String searchText) {
|
|
|
|
return recipeRepository.findByCategories_CategoryContains(searchText);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Recipe> findByCuisineLike(String searchText) {
|
|
|
|
return recipeRepository.findByCuisineContains(searchText);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Recipe> findByIngredientLike(String searchText) {
|
|
|
|
return recipeRepository.findDistinctByIngredientHash_ItemContains(searchText);
|
|
|
|
}
|
|
|
|
}
|