Skip to content

Commit

Permalink
Merge pull request #194 from bounswe/backend/feature/193-get-/recipes…
Browse files Browse the repository at this point in the history
…-endpoint

Implemented GET /recipes endpoint .
  • Loading branch information
EnesBaserr authored May 13, 2024
2 parents 32ec451 + 1b85137 commit b16ce15
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.group1.cuisines.controllers;
import com.group1.cuisines.dto.CommentsDto;
import com.group1.cuisines.dto.NewRecipeDto;
import com.group1.cuisines.dto.RatingDto;
import com.group1.cuisines.dto.RecipeDetailDto;
import com.group1.cuisines.dto.*;
import com.group1.cuisines.entities.Comment;
import com.group1.cuisines.entities.User;
import com.group1.cuisines.services.RecipeService;
Expand All @@ -23,6 +20,15 @@ public class RecipeController {
@Autowired
private RecipeService recipeService;


@GetMapping("/recipes")
public ResponseEntity<List<RecipeDto>> getRecipes(@RequestParam(required = false) String sort,
@RequestParam(required = false) String dishId,
@RequestParam(required = false) String cuisineId) {
List<RecipeDto> recipes = recipeService.findRecipes(sort, dishId, cuisineId);
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
Expand Up @@ -14,5 +14,5 @@ public class NewRecipeDto {
private int cookingTime;
private int servingSize;
private String dishId;
private List<IngredientsDto> ingredients; // Nested DTOs for ingredients
private List<IngredientsDto> ingredients;
}
23 changes: 23 additions & 0 deletions backend/src/main/java/com/group1/cuisines/dto/RecipeDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.group1.cuisines.dto;

import lombok.*;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RecipeDto {
private Integer id;
private String title;
private String instructions;
private int preparationTime;
private int cookingTime;
private int servingSize;
private double averageRating;
private String dishName;




}
16 changes: 15 additions & 1 deletion backend/src/main/java/com/group1/cuisines/entities/Cuisine.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@Data
Expand All @@ -32,8 +34,20 @@ public class Cuisine {
inverseJoinColumns = @JoinColumn(name = "dish_id")
)

private List<Dish> dishes = new ArrayList<>();
private Set<Dish> dishes = new HashSet<>();

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Cuisine cuisine = (Cuisine) o;
return id != null && id.equals(cuisine.id);
}

@Override
public int hashCode() {
return 31;
}
@Override
public String toString() {
return "Cuisine{" +
Expand Down
13 changes: 13 additions & 0 deletions backend/src/main/java/com/group1/cuisines/entities/Dish.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public class Dish {
@ManyToMany(mappedBy = "dishes")
private List<Cuisine> cuisines = new ArrayList<>();

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dish dish = (Dish) o;
return id != null && id.equals(dish.id);
}

@Override
public int hashCode() {
return 31;
}

@Override
public String toString() {
return "Dish{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
Expand Down Expand Up @@ -31,6 +32,11 @@ public class Recipe {
@Builder.Default
private List<Rating> ratings = new ArrayList<>();

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_at", nullable = false, updatable = false)
@org.hibernate.annotations.CreationTimestamp
private Date createdAt;

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private User user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,34 @@
import com.group1.cuisines.entities.Recipe;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface RecipeRepository extends JpaRepository<Recipe,Integer> {
public interface RecipeRepository extends JpaRepository<Recipe, Integer> {




@Query("SELECT r FROM Recipe r WHERE " +
"(:dishId IS NULL OR r.dish.id = :dishId) AND " +
"(:cuisineId IS NULL OR EXISTS (SELECT d FROM Dish d JOIN d.cuisines c WHERE d = r.dish AND c.id = :cuisineId)) " +
"ORDER BY CASE WHEN :sort = 'recent' THEN r.createdAt ELSE r.averageRating END DESC")
List<Recipe> findByDishIdAndCuisineIdWithSort(
@Param("dishId") String dishId,
@Param("cuisineId") String cuisineId,
@Param("sort") String sort);

// Fetch recipes by dishId and cuisineId
@Query("SELECT r FROM Recipe r JOIN r.dish d JOIN d.cuisines c WHERE d.id = :dishId AND c.id = :cuisineId ORDER BY r.createdAt DESC")
List<Recipe> findByDishIdAndCuisineIdOrderByCreatedAtDesc(String dishId, String cuisineId);

@Query("SELECT r FROM Recipe r JOIN r.dish d JOIN d.cuisines c WHERE d.id = :dishId AND c.id = :cuisineId ORDER BY r.averageRating DESC")
List<Recipe> findByDishIdAndCuisineIdOrderByAverageRatingDesc(String dishId, String cuisineId);
@Query("SELECT r FROM Recipe r JOIN r.dish d JOIN d.cuisines c WHERE d.id = :dishId AND c.id = :cuisineId")
List<Recipe> findByDishIdAndCuisineId(String dishId, String cuisineId);
@Query("SELECT AVG(r.ratingValue) FROM Rating r WHERE r.recipe.id = :recipeId")
Double findAverageByRecipeId(Integer recipeId);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.group1.cuisines.services;

import com.group1.cuisines.dto.CommentsDto;
import com.group1.cuisines.dto.IngredientsDto;
import com.group1.cuisines.dto.NewRecipeDto;
import com.group1.cuisines.dto.RecipeDetailDto;
import com.group1.cuisines.dto.*;
import com.group1.cuisines.entities.*;
import com.group1.cuisines.repositories.*;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -35,6 +32,25 @@ public class RecipeService {
@Autowired
private CommentRepository commentRepository;




public List<RecipeDto> findRecipes(String sort, String dishId, String cuisineId) {
List<Recipe> recipes = recipeRepository.findByDishIdAndCuisineIdWithSort(dishId, cuisineId, sort);

return recipes.stream()
.map(recipe -> new RecipeDto(
recipe.getId(),
recipe.getTitle(),
recipe.getInstructions(),
recipe.getPreparationTime(),
recipe.getCookingTime(),
recipe.getServingSize(),
recipe.getAverageRating(),
recipe.getTitle()))
.collect(Collectors.toList());
}

@Transactional
public RecipeDetailDto createRecipe(NewRecipeDto newRecipe, String username) throws Exception {
Optional<User> user = userRepository.findByUsername(username);
Expand Down
8 changes: 6 additions & 2 deletions backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ jwt.signing.key=413F4428472B4B6250655368566D5970337336763979244226452948404D6351
spring.application.name=cuisines
spring.datasource.url=jdbc:mysql://localhost:3306/cuisines-test?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=adminadmin
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
logging.level.com.group1.cuisines=DEBUG

spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
logging.level.org.hibernate.type.descriptor.sql=trace


spring.banner.location=./banner.txt

0 comments on commit b16ce15

Please sign in to comment.