Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GET /recipes/{recieId} endpoint implemented. #198

Merged
merged 1 commit into from
May 13, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<List<RecipeDto>> getRecipes(@RequestParam(required = false) String sort,
@RequestParam(required = false) String dishId,
Expand All @@ -29,6 +41,8 @@ public ResponseEntity<List<RecipeDto>> getRecipes(@RequestParam(required = false
return ResponseEntity.ok(recipes);
}



@PostMapping("/recipes")
public ResponseEntity<?> createRecipe(@RequestBody NewRecipeDto newRecipe) throws Exception{
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.group1.cuisines.dao.response;
import lombok.*;
@Data
@AllArgsConstructor
public class SuccessResponse <T>{
private T data;
private String message;

}
16 changes: 16 additions & 0 deletions backend/src/main/java/com/group1/cuisines/dto/AuthorDto.java
Original file line number Diff line number Diff line change
@@ -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;

}
13 changes: 13 additions & 0 deletions backend/src/main/java/com/group1/cuisines/dto/CuisineDto.java
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions backend/src/main/java/com/group1/cuisines/dto/DishDto.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<IngredientsDto> ingredients;
//private List<String> images;
private Integer cookTime;
private Integer servingSize;
// private List<String> allergens;
private CuisineDto cuisine;
private DishDto dish;
private Double avgRating;

private AuthorDto author;



}
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,43 @@ public List<CommentsDto> getCommentsByRecipeId(Integer recipeId) {
.build())
.collect(Collectors.toList());
}

public RecipeDetailsDto getRecipeById(Integer recipeId) {
Optional<Recipe> 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;
}

}
Loading