Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ateam.codefest.web;
- import com.ateam.codefest.model.enums.RecipeType;
- import com.ateam.codefest.model.jpa.Recipe;
- import com.ateam.codefest.model.jpa.RecipeIngredient;
- import com.ateam.codefest.persistence.RecipeIngredientRepository;
- import com.ateam.codefest.persistence.RecipeRepository;
- import com.ateam.codefest.persistence.TestRepository;
- import com.ateam.codefest.search.RecipeIngredientSearch;
- import com.ateam.codefest.search.RecipeSearch;
- import com.ateam.codefest.service.RecipeService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
- import org.springframework.web.bind.annotation.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.UUID;
- import java.util.stream.Collectors;
- /**
- * Created by Alisa on 05.06.2017.
- */
- @RestController
- @RequestMapping(value = "/api/recipess")
- public class TestController {
- @Autowired
- RecipeSearch recipeSearch;
- @Autowired
- TestRepository testRepository;
- @Autowired
- RecipeRepository recipeRepository;
- @Autowired
- RecipeIngredientRepository recipeIngredientRepository;
- @Autowired
- RecipeIngredientSearch recipeIngredientSearch;
- @Autowired
- RecipeService recipeService;
- @Autowired
- private ElasticsearchTemplate elasticsearchTemplate;
- @RequestMapping(value = "/getByName", method = RequestMethod.GET)
- public List<Recipe> getRecipesByName(@RequestParam String name) {
- List<Recipe> recipes = recipeSearch.findByRecipeName(name);
- System.out.println("EYYY INGREDIENTS COMIN AT YOU");
- for(Recipe r : recipes) {
- for(RecipeIngredient recipeIngredient : r.getIngredients()) {
- System.out.println(recipeIngredient.getIngredientName());
- }
- }
- return recipes;
- }
- @RequestMapping(value = "/getLunchDinnerByIngredient", method = RequestMethod.POST)
- public List<Recipe> getRecipesByIngredient(@RequestBody List<String> names) {
- List<Recipe> result = new ArrayList<>();
- List<Recipe> recipes = recipeRepository.findDistinctByIngredientsIngredientNameIn(names);
- result.addAll(recipes.stream().filter(r -> r.getRecipeType() == RecipeType.LUNCH).collect(Collectors.toList()));
- return result;
- }
- @RequestMapping(value = "/getBreakfastByIngredient", method = RequestMethod.POST)
- public List<Recipe> getBreakfastByIngredient(@RequestBody List<String> names) {
- List<Recipe> result = new ArrayList<>();
- List<Recipe> recipes = recipeRepository.findDistinctByIngredientsIngredientNameIn(names);
- result.addAll(recipes.stream().filter(r -> r.getRecipeType() == RecipeType.BREAKFAST).collect(Collectors.toList()));
- return result;
- }
- @RequestMapping(value = "/getDessertByIngredient", method = RequestMethod.POST)
- public List<Recipe> getDessertByIngredient(@RequestBody List<String> names) {
- List<Recipe> result = new ArrayList<>();
- List<Recipe> recipes = recipeRepository.findDistinctByIngredientsIngredientNameIn(names);
- result.addAll(recipes.stream().filter(r -> r.getRecipeType() == RecipeType.DESSERT).collect(Collectors.toList()));
- return result;
- }
- // @RequestMapping(value = "/createIngredient", method = RequestMethod.POST)
- // public RecipeIngredient createIngredient(@RequestBody RecipeIngredient ingredient) {
- // return recipeIngredientSearch.save(ingredient);
- // }
- @RequestMapping(value = "/saveRecipe", method = RequestMethod.POST)
- public Recipe createRecipe(@RequestBody Recipe recipe) {
- return recipeSearch.save(recipe);
- }
- @RequestMapping(value = "/getByRating", method = RequestMethod.GET)
- public List<Recipe> getRecipesByRating(@RequestParam Double rating) {
- return recipeSearch.findByRating(rating);
- }
- // Takes all recipes from database and saves them using the ES repo
- @RequestMapping(value = "/indexAll", method = RequestMethod.GET)
- public void indexAll() {
- // Comment out these lines if you need to delete and re-create the index
- // elasticsearchTemplate.deleteIndex(Recipe.class);
- // elasticsearchTemplate.deleteIndex(RecipeIngredient.class);
- // elasticsearchTemplate.createIndex(Recipe.class);
- // elasticsearchTemplate.createIndex(RecipeIngredient.class);
- // elasticsearchTemplate.refresh(Recipe.class);
- // elasticsearchTemplate.refresh(RecipeIngredient.class);
- for(Recipe r : recipeRepository.findAll()) {
- r.setId(UUID.randomUUID().toString());
- recipeSearch.save(r);
- }
- // for(RecipeIngredient ingredient : recipeIngredientRepository.findAll()) {
- // ingredient.setId(UUID.randomUUID().toString());
- // recipeIngredientSearch.save(ingredient);
- // }
- }
- @RequestMapping(value = "/getRecipeDetails/{recipeId}", method = RequestMethod.GET)
- public Recipe getRecipeDetails(@PathVariable String recipeId) {
- return recipeRepository.findOne(recipeId);
- }
- @RequestMapping(value = "/randomRecipes", method = RequestMethod.GET)
- public List<Recipe> getRandomRecipes() {
- return recipeRepository.findAllByOrderByRatingDesc();
- }
- @RequestMapping(value = "/cooked", method = RequestMethod.GET)
- public void cookedRecipe(@RequestParam Long userId, @RequestParam String recipeId) {
- recipeService.cookedRecipe(userId, recipeId);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement