-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecipeService.java
249 lines (214 loc) Β· 10.9 KB
/
RecipeService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package net.pengcook.recipe.service;
import java.time.LocalTime;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import net.pengcook.authentication.domain.UserInfo;
import net.pengcook.category.dto.CategoryResponse;
import net.pengcook.category.service.CategoryService;
import net.pengcook.comment.service.CommentService;
import net.pengcook.image.service.ImageClientService;
import net.pengcook.ingredient.dto.IngredientResponse;
import net.pengcook.ingredient.service.IngredientRecipeService;
import net.pengcook.ingredient.service.IngredientService;
import net.pengcook.like.repository.RecipeLikeRepository;
import net.pengcook.like.service.RecipeLikeService;
import net.pengcook.recipe.domain.Recipe;
import net.pengcook.recipe.dto.PageRecipeRequest;
import net.pengcook.recipe.dto.RecipeDescriptionResponse;
import net.pengcook.recipe.dto.RecipeHomeWithMineResponse;
import net.pengcook.recipe.dto.RecipeHomeWithMineResponseV1;
import net.pengcook.recipe.dto.RecipeRequest;
import net.pengcook.recipe.dto.RecipeResponse;
import net.pengcook.recipe.dto.RecipeUpdateRequest;
import net.pengcook.recipe.exception.NotFoundException;
import net.pengcook.recipe.exception.UnauthorizedException;
import net.pengcook.recipe.repository.RecipeRepository;
import net.pengcook.recipe.repository.RecipeStepRepository;
import net.pengcook.user.domain.User;
import net.pengcook.user.domain.UserFollow;
import net.pengcook.user.repository.UserFollowRepository;
import net.pengcook.user.repository.UserRepository;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@RequiredArgsConstructor
public class RecipeService {
private static final String CREATION_DATE = "createdAt";
private final RecipeRepository recipeRepository;
private final UserRepository userRepository;
private final RecipeLikeRepository likeRepository;
private final RecipeStepRepository recipeStepRepository;
private final UserFollowRepository userFollowRepository;
private final CategoryService categoryService;
private final IngredientService ingredientService;
private final ImageClientService imageClientService;
private final RecipeStepService recipeStepService;
private final IngredientRecipeService ingredientRecipeService;
private final CommentService commentService;
private final RecipeLikeService recipeLikeService;
@Transactional(readOnly = true)
public List<RecipeHomeWithMineResponse> readRecipes(UserInfo userInfo, PageRecipeRequest pageRecipeRequest) {
Pageable pageable = pageRecipeRequest.getPageable();
List<Recipe> recipes = recipeRepository.findAllByCategoryAndKeyword(
pageable,
pageRecipeRequest.category(),
pageRecipeRequest.keyword(),
pageRecipeRequest.userId()
);
return getRecipeHomeWithMineResponses(userInfo, recipes);
}
@Transactional(readOnly = true)
public List<RecipeHomeWithMineResponseV1> readRecipesV1(UserInfo userInfo, PageRecipeRequest pageRecipeRequest) {
List<Recipe> recipes = findRecipesByMultipleCondition(pageRecipeRequest);
return recipes.stream()
.map(recipe -> new RecipeHomeWithMineResponseV1(userInfo, recipe))
.toList();
}
private List<Recipe> findRecipesByMultipleCondition(PageRecipeRequest pageRecipeRequest) {
Pageable pageable = pageRecipeRequest.getPageable();
long conditionCount = pageRecipeRequest.getConditionCount();
if (conditionCount == 0) {
Pageable descPageable = PageRequest.of(
pageable.getPageNumber(),
pageable.getPageSize(),
Sort.by(CREATION_DATE).descending()
);
return recipeRepository.findAll(descPageable)
.toList();
}
if (conditionCount == 1) {
return findRecipesBySingleCondition(pageRecipeRequest);
}
return recipeRepository.findAllByCategoryAndKeyword(
pageable,
pageRecipeRequest.category(),
pageRecipeRequest.keyword(),
pageRecipeRequest.userId()
);
}
private List<Recipe> findRecipesBySingleCondition(PageRecipeRequest pageRecipeRequest) {
Pageable pageable = pageRecipeRequest.getPageable();
String category = pageRecipeRequest.category();
String keyword = pageRecipeRequest.keyword();
Long userId = pageRecipeRequest.userId();
if (category != null) {
return recipeRepository.findAllByCategory(pageable, category);
}
if (keyword != null) {
return recipeRepository.findAllByKeyword(pageable, keyword);
}
if (userId != null) {
return recipeRepository.findAllByAuthorIdOrderByCreatedAtDesc(pageable, userId);
}
// TODO: need to throw illegal state
return List.of();
}
@Transactional(readOnly = true)
public List<RecipeHomeWithMineResponse> readLikeRecipes(UserInfo userInfo) {
List<Long> likeRecipeIds = likeRepository.findRecipeIdsByUserId(userInfo.getId());
List<Recipe> recipes = recipeRepository.findAllByIdInOrderByCreatedAtDesc(likeRecipeIds);
return getRecipeHomeWithMineResponses(userInfo, recipes);
}
@Transactional(readOnly = true)
public List<RecipeHomeWithMineResponseV1> readLikeRecipesV1(UserInfo userInfo) {
List<Long> likeRecipeIds = likeRepository.findRecipeIdsByUserId(userInfo.getId());
List<Recipe> recipes = recipeRepository.findAllByIdInOrderByCreatedAtDesc(likeRecipeIds);
return recipes.stream()
.map(recipe -> new RecipeHomeWithMineResponseV1(userInfo, recipe))
.toList();
}
@Transactional(readOnly = true)
public List<RecipeHomeWithMineResponseV1> readFollowRecipes(UserInfo userInfo,
PageRecipeRequest pageRecipeRequest) {
List<UserFollow> followings = userFollowRepository.findAllByFollowerId(userInfo.getId());
List<Long> followeeIds = followings.stream()
.map(userFollow -> userFollow.getFollowee().getId())
.toList();
List<Recipe> recipes = recipeRepository.findAllByAuthorIdInOrderByCreatedAtDesc(followeeIds,
pageRecipeRequest.getPageable());
return recipes.stream()
.map(recipe -> new RecipeHomeWithMineResponseV1(userInfo, recipe))
.toList();
}
@Transactional
public RecipeResponse createRecipe(UserInfo userInfo, RecipeRequest recipeRequest) {
User author = userRepository.findById(userInfo.getId()).orElseThrow();
String thumbnailUrl = imageClientService.getImageUrl(recipeRequest.thumbnail()).url();
Recipe recipe = new Recipe(
recipeRequest.title(),
author,
LocalTime.parse(recipeRequest.cookingTime()),
thumbnailUrl,
recipeRequest.difficulty(),
recipeRequest.description()
);
Recipe savedRecipe = recipeRepository.save(recipe);
categoryService.saveCategories(savedRecipe, recipeRequest.categories());
ingredientService.register(recipeRequest.ingredients(), savedRecipe);
recipeStepService.saveRecipeSteps(savedRecipe.getId(), recipeRequest.recipeSteps());
return new RecipeResponse(savedRecipe);
}
@Transactional
public void updateRecipe(UserInfo userInfo, Long recipeId, RecipeUpdateRequest recipeUpdateRequest) {
Recipe recipe = recipeRepository.findById(recipeId).orElseThrow();
verifyRecipeOwner(userInfo, recipe);
Recipe updatedRecipe = recipe.updateRecipe(
recipeUpdateRequest.title(),
LocalTime.parse(recipeUpdateRequest.cookingTime()),
imageClientService.getImageUrl(recipeUpdateRequest.thumbnail()).url(),
recipeUpdateRequest.difficulty(),
recipeUpdateRequest.description()
);
ingredientRecipeService.deleteIngredientRecipe(recipe.getId());
ingredientService.register(recipeUpdateRequest.ingredients(), updatedRecipe);
categoryService.deleteCategoryRecipe(recipe);
categoryService.saveCategories(updatedRecipe, recipeUpdateRequest.categories());
recipeStepService.deleteRecipeStepsByRecipe(updatedRecipe.getId());
recipeStepRepository.flush();
recipeStepService.saveRecipeSteps(updatedRecipe.getId(), recipeUpdateRequest.recipeSteps());
}
@Transactional(readOnly = true)
public RecipeDescriptionResponse readRecipeDescription(UserInfo userInfo, long recipeId) {
Recipe recipe = recipeRepository.findById(recipeId)
.orElseThrow(() -> new NotFoundException("μ‘΄μ¬νμ§ μλ λ μνΌμ
λλ€."));
List<CategoryResponse> categories = categoryService.findCategoryByRecipe(recipe);
List<IngredientResponse> ingredients = ingredientService.findIngredientByRecipe(recipe);
boolean isLike = likeRepository.existsByUserIdAndRecipeId(userInfo.getId(), recipeId);
return new RecipeDescriptionResponse(userInfo, recipe, categories, ingredients, isLike);
}
@Transactional
public void deleteRecipe(UserInfo userInfo, long recipeId) {
Optional<Recipe> targetRecipe = recipeRepository.findById(recipeId);
targetRecipe.ifPresent(recipe -> deleteRecipe(userInfo, recipe));
}
@Transactional
public void deleteRecipe(UserInfo userInfo, Recipe recipe) {
verifyRecipeOwner(userInfo, recipe);
ingredientRecipeService.deleteIngredientRecipe(recipe.getId());
categoryService.deleteCategoryRecipe(recipe);
commentService.deleteCommentsByRecipe(recipe.getId());
recipeLikeService.deleteLikesByRecipe(recipe.getId());
recipeStepService.deleteRecipeStepsByRecipe(recipe.getId());
recipeRepository.delete(recipe);
}
private List<RecipeHomeWithMineResponse> getRecipeHomeWithMineResponses(UserInfo userInfo, List<Recipe> recipes) {
return recipes.stream()
.map(recipe -> {
List<CategoryResponse> categories = categoryService.findCategoryByRecipe(recipe);
List<IngredientResponse> ingredients = ingredientService.findIngredientByRecipe(recipe);
return new RecipeHomeWithMineResponse(userInfo, recipe, categories, ingredients);
})
.toList();
}
private void verifyRecipeOwner(UserInfo userInfo, Recipe recipe) {
User author = recipe.getAuthor();
long authorId = author.getId();
if (!userInfo.isSameUser(authorId)) {
throw new UnauthorizedException("λ μνΌμ λν κΆνμ΄ μμ΅λλ€.");
}
}
}