Skip to content
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
@@ -0,0 +1,35 @@
package com.webservice.algorithmchef.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.webservice.algorithmchef.dto.recipereview.RecipeReviewRequest;
import com.webservice.algorithmchef.dto.recipereview.RecipeReviewResponse;
import com.webservice.algorithmchef.service.RecipeReviewService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
public class RecipeReviewController {

private final RecipeReviewService rService;

@PostMapping("/recipe/review")
public ResponseEntity<?> makeReview(@RequestBody RecipeReviewRequest request,
@AuthenticationPrincipal UserDetails userDetails){
try {
String userId = userDetails.getUsername();
RecipeReviewResponse response = rService.makeReview(request, userId);
return ResponseEntity.status(HttpStatus.CREATED).body(response);
}catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.webservice.algorithmchef.dto.recipereview;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class RecipeReviewRequest {

private String name;
private float rating;
private String content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.webservice.algorithmchef.dto.recipereview;

import com.webservice.algorithmchef.model.RecipeReview;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class RecipeReviewResponse {

private String name;
private String userId;
private float rating;

public RecipeReviewResponse(RecipeReview review) {
this.name = review.getRecipe().getName();
this.userId = review.getUser().getUserId();
this.rating = review.getRating();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,4 @@ public class Ingredient {
@OneToMany(mappedBy = "ingredient",orphanRemoval = true,cascade = CascadeType.ALL)
private List<FridgeIngredient> ingredients;

@OneToMany(mappedBy = "ingredient",orphanRemoval = true,cascade = CascadeType.ALL)
private List<RecipeIngredient> recipeIngredients;

}
20 changes: 15 additions & 5 deletions src/main/java/com/webservice/algorithmchef/model/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Recipe {
@Column(name="recipe_id")
private Long id;

@Column(nullable = false, unique = true)
@Column(nullable = false)
private String name;

@Lob
Expand All @@ -45,14 +45,24 @@ public class Recipe {
@Column(nullable = false)
private String type;

@Column(nullable = false)
@Lob
private String neededIngredients;

@Column(nullable = false)
private double kcal;

@OneToMany(mappedBy = "recipe", orphanRemoval = true)
private List<RecipeTag> recipeTags;
@Column
private String tag;

@OneToMany(mappedBy = "recipe", orphanRemoval = true)
private List<RecipeIngredient> recipeIngredients;
@Column
private String portions;

@Column
private String time;

@Column
private String tip;

@OneToMany(mappedBy = "recipe", orphanRemoval = true)
private List<RecipeReview> recipeReviews;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.Lob;
import jakarta.persistence.ManyToOne;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -44,6 +45,10 @@ public class RecipeReview {
@Column(nullable = false)
private float rating;

@Column
@Lob
private String content;

@CreationTimestamp
private LocalDateTime createdAt;

Expand Down
38 changes: 0 additions & 38 deletions src/main/java/com/webservice/algorithmchef/model/RecipeTag.java

This file was deleted.

38 changes: 0 additions & 38 deletions src/main/java/com/webservice/algorithmchef/model/Tag.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.webservice.algorithmchef.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.webservice.algorithmchef.model.Recipe;

public interface RecipeRepository extends JpaRepository<Recipe, Long>{

public Optional<Recipe> findByName(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.webservice.algorithmchef.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.webservice.algorithmchef.model.Recipe;
import com.webservice.algorithmchef.model.RecipeReview;
import com.webservice.algorithmchef.model.User;

public interface RecipeReviewRepository extends JpaRepository<RecipeReview, Long> {

boolean existsByUserAndRecipe(User user, Recipe recipe);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.webservice.algorithmchef.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.webservice.algorithmchef.dto.recipereview.RecipeReviewRequest;
import com.webservice.algorithmchef.dto.recipereview.RecipeReviewResponse;
import com.webservice.algorithmchef.model.Recipe;
import com.webservice.algorithmchef.model.RecipeReview;
import com.webservice.algorithmchef.model.User;
import com.webservice.algorithmchef.repository.RecipeRepository;
import com.webservice.algorithmchef.repository.RecipeReviewRepository;
import com.webservice.algorithmchef.repository.UserRepository;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class RecipeReviewService {

private final RecipeRepository recipeRepository;
private final RecipeReviewRepository recipeReviewRepository;
private final UserRepository userRepository;


@Transactional
public RecipeReviewResponse makeReview(RecipeReviewRequest reviewRequest, String userId) {
User user = userRepository.findByUserId(userId)
.orElseThrow(()-> new IllegalArgumentException("해당하는 아이디의 사용작 없습니다."));
String recipeName = reviewRequest.getName();
float rating = reviewRequest.getRating();
String content = reviewRequest.getContent();
Recipe recipe = recipeRepository.findByName(recipeName)
.orElseThrow(()-> new IllegalArgumentException("해당하는 이름의 recipe가 존재하지 않습니다."));
if (recipeReviewRepository.existsByUserAndRecipe(user, recipe)) {
throw new IllegalStateException("이미 리뷰를 작성한 레시피입니다.");
}
RecipeReview review = RecipeReview.builder()
.content(content)
.rating(rating)
.user(user)
.recipe(recipe)
.build();

RecipeReview recipeReview = recipeReviewRepository.save(review);
return new RecipeReviewResponse(recipeReview);

}

}