From 6b04d5be2e9d978d57ffefe3c8fef22c5527d62a Mon Sep 17 00:00:00 2001 From: EnesBaserr Date: Mon, 13 May 2024 12:11:36 +0300 Subject: [PATCH] GET /recipes/{recieId} endpoint implemented. --- .../controllers/RecipeController.java | 14 +++++++ .../dao/response/SuccessResponse.java | 9 +++++ .../com/group1/cuisines/dto/AuthorDto.java | 16 ++++++++ .../com/group1/cuisines/dto/CuisineDto.java | 13 +++++++ .../java/com/group1/cuisines/dto/DishDto.java | 10 +++++ .../group1/cuisines/dto/RecipeDetailsDto.java | 29 ++++++++++++++ .../cuisines/services/RecipeService.java | 39 +++++++++++++++++++ 7 files changed, 130 insertions(+) create mode 100644 backend/src/main/java/com/group1/cuisines/dao/response/SuccessResponse.java create mode 100644 backend/src/main/java/com/group1/cuisines/dto/AuthorDto.java create mode 100644 backend/src/main/java/com/group1/cuisines/dto/CuisineDto.java create mode 100644 backend/src/main/java/com/group1/cuisines/dto/DishDto.java create mode 100644 backend/src/main/java/com/group1/cuisines/dto/RecipeDetailsDto.java diff --git a/backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java b/backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java index 7623f0b8..7f12b83b 100644 --- a/backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java +++ b/backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java @@ -1,4 +1,5 @@ package com.group1.cuisines.controllers; +import com.group1.cuisines.dao.response.SuccessResponse; import com.group1.cuisines.dto.*; import com.group1.cuisines.entities.Comment; import com.group1.cuisines.entities.User; @@ -21,6 +22,17 @@ public class RecipeController { private RecipeService recipeService; + @GetMapping("/recipes/{recipeId}") + public ResponseEntity getRecipeById(@PathVariable Integer recipeId) { + RecipeDetailsDto recipeDetails = recipeService.getRecipeById(recipeId); + if (recipeDetails != null) { + return ResponseEntity.ok(new SuccessResponse<>(recipeDetails, "Recipe fetched successfully")); + } else { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Recipe not found"); + } + } + + @GetMapping("/recipes") public ResponseEntity> getRecipes(@RequestParam(required = false) String sort, @RequestParam(required = false) String dishId, @@ -29,6 +41,8 @@ public ResponseEntity> getRecipes(@RequestParam(required = false return ResponseEntity.ok(recipes); } + + @PostMapping("/recipes") public ResponseEntity createRecipe(@RequestBody NewRecipeDto newRecipe) throws Exception{ Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); diff --git a/backend/src/main/java/com/group1/cuisines/dao/response/SuccessResponse.java b/backend/src/main/java/com/group1/cuisines/dao/response/SuccessResponse.java new file mode 100644 index 00000000..be965ff0 --- /dev/null +++ b/backend/src/main/java/com/group1/cuisines/dao/response/SuccessResponse.java @@ -0,0 +1,9 @@ +package com.group1.cuisines.dao.response; +import lombok.*; +@Data +@AllArgsConstructor +public class SuccessResponse { + private T data; + private String message; + +} diff --git a/backend/src/main/java/com/group1/cuisines/dto/AuthorDto.java b/backend/src/main/java/com/group1/cuisines/dto/AuthorDto.java new file mode 100644 index 00000000..d208f44e --- /dev/null +++ b/backend/src/main/java/com/group1/cuisines/dto/AuthorDto.java @@ -0,0 +1,16 @@ +package com.group1.cuisines.dto; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class AuthorDto { + private Integer id; + private String name; + private String username; + private Integer followersCount; + private Integer recipesCount; + +} diff --git a/backend/src/main/java/com/group1/cuisines/dto/CuisineDto.java b/backend/src/main/java/com/group1/cuisines/dto/CuisineDto.java new file mode 100644 index 00000000..efdd9272 --- /dev/null +++ b/backend/src/main/java/com/group1/cuisines/dto/CuisineDto.java @@ -0,0 +1,13 @@ +package com.group1.cuisines.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class CuisineDto { + private String id; + private String name; +} \ No newline at end of file diff --git a/backend/src/main/java/com/group1/cuisines/dto/DishDto.java b/backend/src/main/java/com/group1/cuisines/dto/DishDto.java new file mode 100644 index 00000000..93879540 --- /dev/null +++ b/backend/src/main/java/com/group1/cuisines/dto/DishDto.java @@ -0,0 +1,10 @@ +package com.group1.cuisines.dto; +import lombok.*; +@Data +@NoArgsConstructor +@AllArgsConstructor +public class DishDto { + private String id; + private String name; + private String image; +} \ No newline at end of file diff --git a/backend/src/main/java/com/group1/cuisines/dto/RecipeDetailsDto.java b/backend/src/main/java/com/group1/cuisines/dto/RecipeDetailsDto.java new file mode 100644 index 00000000..5f5ec185 --- /dev/null +++ b/backend/src/main/java/com/group1/cuisines/dto/RecipeDetailsDto.java @@ -0,0 +1,29 @@ +package com.group1.cuisines.dto; + +import java.util.List; + +import com.group1.cuisines.entities.Ingredient; +import lombok.*; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class RecipeDetailsDto { + private Integer id; + private String name; + // private String description; + private String instructions; + private List ingredients; + //private List images; + private Integer cookTime; + private Integer servingSize; + // private List allergens; + private CuisineDto cuisine; + private DishDto dish; + private Double avgRating; + + private AuthorDto author; + + + +} diff --git a/backend/src/main/java/com/group1/cuisines/services/RecipeService.java b/backend/src/main/java/com/group1/cuisines/services/RecipeService.java index 316d7b1c..71ba3672 100644 --- a/backend/src/main/java/com/group1/cuisines/services/RecipeService.java +++ b/backend/src/main/java/com/group1/cuisines/services/RecipeService.java @@ -178,4 +178,43 @@ public List getCommentsByRecipeId(Integer recipeId) { .build()) .collect(Collectors.toList()); } + + public RecipeDetailsDto getRecipeById(Integer recipeId) { + Optional recipe = recipeRepository.findById(recipeId); + + + if (recipe.isPresent()) { + CuisineDto cuisineDto = new CuisineDto(); + Recipe r = recipe.get(); + if (r.getDish() != null && !r.getDish().getCuisines().isEmpty()) { + + cuisineDto.setId(r.getDish().getCuisines().get(0).getId()); + cuisineDto.setName(r.getDish().getCuisines().get(0).getName()); + + } + else if(r.getDish() != null && r.getDish().getCuisines().isEmpty()){ + cuisineDto.setId("No cuisine Id from wikidata"); + cuisineDto.setName("No cuisine name from wikidata"); + } + // Conversion from Recipe entity to RecipeDetailsDto + return new RecipeDetailsDto( + r.getId(), + r.getTitle(), + + r.getInstructions(), + r.getIngredients().stream().map(ingredient -> new IngredientsDto( ingredient.getName())).collect(Collectors.toList()), + + r.getCookingTime(), + r.getServingSize(), + cuisineDto, + + new DishDto(r.getDish().getId(), r.getDish().getName(), r.getDish().getImage()), + r.getAverageRating(), + new AuthorDto(r.getUser().getId(), r.getUser().getFirstName() , r.getUser().getUsername(), r.getUser().getFollowers().size(), r.getUser().getRecipeCount()) + + ); + } + return null; + } + }