Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.51 KB | None | 0 0
  1. package com.ateam.codefest.web;
  2.  
  3. import com.ateam.codefest.model.enums.RecipeType;
  4. import com.ateam.codefest.model.jpa.Recipe;
  5. import com.ateam.codefest.model.jpa.RecipeIngredient;
  6. import com.ateam.codefest.persistence.RecipeIngredientRepository;
  7. import com.ateam.codefest.persistence.RecipeRepository;
  8. import com.ateam.codefest.persistence.TestRepository;
  9. import com.ateam.codefest.search.RecipeIngredientSearch;
  10. import com.ateam.codefest.search.RecipeSearch;
  11. import com.ateam.codefest.service.RecipeService;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
  14. import org.springframework.web.bind.annotation.*;
  15.  
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import java.util.UUID;
  19. import java.util.stream.Collectors;
  20.  
  21. /**
  22.  * Created by Alisa on 05.06.2017.
  23.  */
  24. @RestController
  25. @RequestMapping(value = "/api/recipess")
  26. public class TestController {
  27.  
  28.     @Autowired
  29.     RecipeSearch recipeSearch;
  30.  
  31.     @Autowired
  32.     TestRepository testRepository;
  33.  
  34.     @Autowired
  35.     RecipeRepository recipeRepository;
  36.  
  37.     @Autowired
  38.     RecipeIngredientRepository recipeIngredientRepository;
  39.  
  40.     @Autowired
  41.     RecipeIngredientSearch recipeIngredientSearch;
  42.  
  43.     @Autowired
  44.     RecipeService recipeService;
  45.  
  46.     @Autowired
  47.     private ElasticsearchTemplate elasticsearchTemplate;
  48.  
  49.     @RequestMapping(value = "/getByName", method = RequestMethod.GET)
  50.     public List<Recipe> getRecipesByName(@RequestParam String name) {
  51.         List<Recipe> recipes = recipeSearch.findByRecipeName(name);
  52.         System.out.println("EYYY INGREDIENTS COMIN AT YOU");
  53.         for(Recipe r : recipes) {
  54.             for(RecipeIngredient recipeIngredient : r.getIngredients()) {
  55.                 System.out.println(recipeIngredient.getIngredientName());
  56.             }
  57.         }
  58.         return recipes;
  59.     }
  60.  
  61.     @RequestMapping(value = "/getLunchDinnerByIngredient", method = RequestMethod.POST)
  62.     public List<Recipe> getRecipesByIngredient(@RequestBody List<String> names) {
  63.         List<Recipe> result = new ArrayList<>();
  64.         List<Recipe> recipes = recipeRepository.findDistinctByIngredientsIngredientNameIn(names);
  65.         result.addAll(recipes.stream().filter(r -> r.getRecipeType() == RecipeType.LUNCH).collect(Collectors.toList()));
  66.         return result;
  67.     }
  68.  
  69.     @RequestMapping(value = "/getBreakfastByIngredient", method = RequestMethod.POST)
  70.     public List<Recipe> getBreakfastByIngredient(@RequestBody List<String> names) {
  71.         List<Recipe> result = new ArrayList<>();
  72.         List<Recipe> recipes = recipeRepository.findDistinctByIngredientsIngredientNameIn(names);
  73.         result.addAll(recipes.stream().filter(r -> r.getRecipeType() == RecipeType.BREAKFAST).collect(Collectors.toList()));
  74.         return result;
  75.     }
  76.  
  77.     @RequestMapping(value = "/getDessertByIngredient", method = RequestMethod.POST)
  78.     public List<Recipe> getDessertByIngredient(@RequestBody List<String> names) {
  79.         List<Recipe> result = new ArrayList<>();
  80.         List<Recipe> recipes = recipeRepository.findDistinctByIngredientsIngredientNameIn(names);
  81.         result.addAll(recipes.stream().filter(r -> r.getRecipeType() == RecipeType.DESSERT).collect(Collectors.toList()));
  82.         return result;
  83.     }
  84.  
  85. //    @RequestMapping(value = "/createIngredient", method = RequestMethod.POST)
  86. //    public RecipeIngredient createIngredient(@RequestBody RecipeIngredient ingredient) {
  87. //        return recipeIngredientSearch.save(ingredient);
  88. //    }
  89.  
  90.     @RequestMapping(value = "/saveRecipe", method = RequestMethod.POST)
  91.     public Recipe createRecipe(@RequestBody Recipe recipe) {
  92.         return recipeSearch.save(recipe);
  93.     }
  94.  
  95.     @RequestMapping(value = "/getByRating", method = RequestMethod.GET)
  96.     public List<Recipe> getRecipesByRating(@RequestParam Double rating) {
  97.         return recipeSearch.findByRating(rating);
  98.     }
  99.  
  100.     // Takes all recipes from database and saves them using the ES repo
  101.     @RequestMapping(value = "/indexAll", method = RequestMethod.GET)
  102.     public void indexAll() {
  103. //        Comment out these lines if you need to delete and re-create the index
  104. //        elasticsearchTemplate.deleteIndex(Recipe.class);
  105. //        elasticsearchTemplate.deleteIndex(RecipeIngredient.class);
  106. //        elasticsearchTemplate.createIndex(Recipe.class);
  107. //        elasticsearchTemplate.createIndex(RecipeIngredient.class);
  108. //        elasticsearchTemplate.refresh(Recipe.class);
  109. //        elasticsearchTemplate.refresh(RecipeIngredient.class);
  110.         for(Recipe r : recipeRepository.findAll()) {
  111.             r.setId(UUID.randomUUID().toString());
  112.             recipeSearch.save(r);
  113.         }
  114. //        for(RecipeIngredient ingredient : recipeIngredientRepository.findAll()) {
  115. //            ingredient.setId(UUID.randomUUID().toString());
  116. //            recipeIngredientSearch.save(ingredient);
  117. //        }
  118.     }
  119.  
  120.  
  121.     @RequestMapping(value = "/getRecipeDetails/{recipeId}", method = RequestMethod.GET)
  122.     public Recipe getRecipeDetails(@PathVariable String recipeId) {
  123.         return recipeRepository.findOne(recipeId);
  124.     }
  125.  
  126.     @RequestMapping(value = "/randomRecipes", method = RequestMethod.GET)
  127.     public List<Recipe> getRandomRecipes() {
  128.         return recipeRepository.findAllByOrderByRatingDesc();
  129.     }
  130.  
  131.     @RequestMapping(value = "/cooked", method = RequestMethod.GET)
  132.     public void cookedRecipe(@RequestParam Long userId, @RequestParam String recipeId) {
  133.         recipeService.cookedRecipe(userId, recipeId);
  134.     }
  135.  
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement