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

♻️ split transactional function readonly and readwrite #361

Merged
merged 5 commits into from
Sep 23, 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
Expand Up @@ -19,6 +19,7 @@
import net.pengcook.user.domain.User;
import net.pengcook.user.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand All @@ -29,6 +30,7 @@ public class LoginService {
private final JwtTokenManager jwtTokenManager;
private final S3ClientService s3ClientService;

@Transactional(readOnly = true)
public GoogleLoginResponse loginWithGoogle(GoogleLoginRequest googleLoginRequest) {
FirebaseToken decodedToken = decodeIdToken(googleLoginRequest.idToken());
String email = decodedToken.getEmail();
Expand All @@ -46,6 +48,7 @@ public GoogleLoginResponse loginWithGoogle(GoogleLoginRequest googleLoginRequest
return new GoogleLoginResponse(true, accessToken, refreshToken);
}

@Transactional
public GoogleSignUpResponse signUpWithGoogle(GoogleSignUpRequest googleSignUpRequest) {
User user = createUser(googleSignUpRequest);

Expand Down Expand Up @@ -73,6 +76,12 @@ public TokenRefreshResponse refresh(String refreshToken) {
);
}

@Transactional(readOnly = true)
public void checkToken(long userId) {
userRepository.findById(userId)
.orElseThrow(() -> new NoSuchUserException("존재하지 않는 사용자입니다."));
}

private User createUser(GoogleSignUpRequest googleSignUpRequest) {
FirebaseToken decodedToken = decodeIdToken(googleSignUpRequest.idToken());

Expand Down Expand Up @@ -100,9 +109,4 @@ private FirebaseToken decodeIdToken(String idToken) {
throw new FirebaseTokenException("구글 인증에 실패했습니다.");
}
}

public void checkToken(long userId) {
userRepository.findById(userId)
.orElseThrow(() -> new NoSuchUserException("존재하지 않는 사용자입니다."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.pengcook.category.repository.CategoryRepository;
import net.pengcook.recipe.domain.Recipe;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand All @@ -16,10 +17,12 @@ public class CategoryService {
private final CategoryRepository categoryRepository;
private final CategoryRecipeRepository categoryRecipeRepository;

@Transactional
public void saveCategories(Recipe recipe, List<String> categories) {
categories.forEach(category -> saveCategoryRecipe(recipe, category));
}

@Transactional
public void deleteCategoryRecipe(Recipe recipe) {
categoryRecipeRepository.deleteByRecipe(recipe);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.pengcook.comment.service;

import jakarta.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,6 +15,7 @@
import net.pengcook.user.domain.User;
import net.pengcook.user.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand All @@ -25,6 +25,7 @@ public class CommentService {
private final RecipeRepository recipeRepository;
private final UserRepository userRepository;

@Transactional(readOnly = true)
public List<CommentOfRecipeResponse> readComments(Long recipeId, UserInfo userInfo) {
List<Comment> comments = commentRepository.findByRecipeId(recipeId);

Expand Down Expand Up @@ -61,10 +62,12 @@ public void deleteComment(long commentId, UserInfo userInfo) {
recipe.decreaseCommentCount();
}

@Transactional
public void deleteCommentsByRecipe(long recipeId) {
commentRepository.deleteByRecipeId(recipeId);
}

@Transactional
public void deleteCommentsByUser(long userId) {
commentRepository.deleteByUserId(userId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.pengcook.ingredient.repository.IngredientRecipeRepository;
import net.pengcook.recipe.domain.Recipe;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand All @@ -16,11 +17,13 @@ public class IngredientRecipeService {
private final IngredientRecipeRepository ingredientRecipeRepository;
private final IngredientSubstitutionService ingredientSubstitutionService;

@Transactional
public IngredientRecipe save(Ingredient ingredient, Recipe recipe, Requirement requirement) {
IngredientRecipe ingredientRecipe = new IngredientRecipe(ingredient, recipe, requirement);
return ingredientRecipeRepository.save(ingredientRecipe);
}

@Transactional
public void deleteIngredientRecipe(long recipeId) {
List<IngredientRecipe> ingredientRecipes = ingredientRecipeRepository.findAllByRecipeId(recipeId);
for (IngredientRecipe ingredientRecipe : ingredientRecipes) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.pengcook.ingredient.service;


import jakarta.transaction.Transactional;
import java.util.HashSet;
import java.util.List;
import lombok.Getter;
Expand All @@ -14,9 +13,9 @@
import net.pengcook.ingredient.repository.IngredientRepository;
import net.pengcook.recipe.domain.Recipe;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
@Getter
public class IngredientService {
Expand All @@ -25,6 +24,7 @@ public class IngredientService {
private final IngredientRecipeService ingredientRecipeService;
private final IngredientSubstitutionService ingredientSubstitutionService;

@Transactional
public void register(List<IngredientCreateRequest> requests, Recipe recipe) {
validateDuplicateNames(getIngredientNames(requests));
for (IngredientCreateRequest request : requests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@
import net.pengcook.ingredient.domain.Requirement;
import net.pengcook.ingredient.repository.IngredientSubstitutionRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class IngredientSubstitutionService {

private final IngredientSubstitutionRepository ingredientSubstitutionRepository;

@Transactional
public void save(IngredientRecipe ingredientRecipe, Ingredient substitution) {
IngredientSubstitution ingredientSubstitution = new IngredientSubstitution(ingredientRecipe, substitution);
ingredientSubstitutionRepository.save(ingredientSubstitution);
}

@Transactional
public void delete(IngredientRecipe ingredientRecipe) {
if (ingredientRecipe.getRequirement() == Requirement.ALTERNATIVE) {
List<IngredientSubstitution> substitutions =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.pengcook.like.service;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import net.pengcook.authentication.domain.UserInfo;
import net.pengcook.like.domain.RecipeLike;
Expand All @@ -13,6 +12,7 @@
import net.pengcook.user.domain.User;
import net.pengcook.user.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand All @@ -22,6 +22,7 @@ public class RecipeLikeService {
private final UserRepository userRepository;
private final RecipeRepository recipeRepository;

@Transactional(readOnly = true)
public RecipeLikeResponse readLike(UserInfo userInfo, long recipeId) {
boolean isLike = likeRepository.existsByUserIdAndRecipeId(userInfo.getId(), recipeId);

Expand Down Expand Up @@ -60,10 +61,12 @@ public void deleteLike(UserInfo userInfo, long recipeId) {
recipeRepository.save(recipe);
}

@Transactional
public void deleteLikesByRecipe(long recipeId) {
likeRepository.deleteByRecipeId(recipeId);
}

@Transactional
public void deleteLikesByUser(long userId) {
likeRepository.deleteByUserId(userId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class RecipeService {

Expand All @@ -49,6 +48,7 @@ public class RecipeService {
private final CommentService commentService;
private final RecipeLikeService recipeLikeService;

@Transactional(readOnly = true)
public List<RecipeHomeWithMineResponse> readRecipes(UserInfo userInfo, PageRecipeRequest pageRecipeRequest) {
Pageable pageable = pageRecipeRequest.getPageable();
List<Long> recipeIds = recipeRepository.findRecipeIdsByCategoryAndKeyword(
Expand All @@ -66,6 +66,7 @@ public List<RecipeHomeWithMineResponse> readRecipes(UserInfo userInfo, PageRecip
.toList();
}

@Transactional(readOnly = true)
public List<RecipeHomeWithMineResponse> readLikeRecipes(UserInfo userInfo) {
List<Long> likeRecipeIds = likeRepository.findRecipeIdsByUserId(userInfo.getId());
List<RecipeHomeResponse> recipeHomeResponses = recipeRepository.findRecipeData(likeRecipeIds);
Expand All @@ -76,6 +77,7 @@ public List<RecipeHomeWithMineResponse> readLikeRecipes(UserInfo userInfo) {
.toList();
}

@Transactional
public RecipeResponse createRecipe(UserInfo userInfo, RecipeRequest recipeRequest) {
User author = userRepository.findById(userInfo.getId()).orElseThrow();
String thumbnailUrl = s3ClientService.getImageUrl(recipeRequest.thumbnail()).url();
Expand All @@ -96,6 +98,7 @@ public RecipeResponse createRecipe(UserInfo userInfo, RecipeRequest recipeReques
return new RecipeResponse(savedRecipe);
}

@Transactional(readOnly = true)
public RecipeDescriptionResponse readRecipeDescription(UserInfo userInfo, long recipeId) {
List<RecipeDataResponse> recipeDataResponses = recipeRepository.findRecipeData(recipeId);

Expand All @@ -107,6 +110,7 @@ public RecipeDescriptionResponse readRecipeDescription(UserInfo userInfo, long r
);
}

@Transactional
public void deleteRecipe(UserInfo userInfo, long recipeId) {
Optional<Recipe> targetRecipe = recipeRepository.findById(recipeId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,27 @@
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class RecipeStepService {

private final RecipeStepRepository recipeStepRepository;
private final RecipeRepository recipeRepository;
private final S3ClientService s3ClientService;

@Transactional(readOnly = true)
public List<RecipeStepResponse> readRecipeSteps(long recipeId) {
List<RecipeStep> recipeSteps = recipeStepRepository.findAllByRecipeIdOrderBySequence(recipeId);
return convertToRecipeStepResponses(recipeSteps);
}

@Transactional
public void saveRecipeSteps(Long savedRecipeId, List<RecipeStepRequest> recipeStepRequests) {
Recipe savedRecipe = recipeRepository.findById(savedRecipeId)
.orElseThrow(() -> new NotFoundException("해당되는 레시피가 없습니다."));
recipeStepRequests.forEach(recipeStepRequest -> saveRecipeStep(savedRecipe, recipeStepRequest));
}

@Transactional
public void deleteRecipeStepsByRecipe(long recipeId) {
recipeStepRepository.deleteByRecipeId(recipeId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.pengcook.user.service;

import jakarta.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -28,6 +27,7 @@
import net.pengcook.user.repository.UserReportRepository;
import net.pengcook.user.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
Expand All @@ -43,13 +43,15 @@ public class UserService {
private final UserReportRepository userReportRepository;
private final S3ClientService s3ClientService;

@Transactional(readOnly = true)
public ProfileResponse getUserById(long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new UserNotFoundException("사용자를 찾을 수 없습니다."));
long recipeCount = recipeRepository.countByAuthorId(userId);
return new ProfileResponse(user, recipeCount);
}

@Transactional(readOnly = true)
public UsernameCheckResponse checkUsername(String username) {
boolean userExists = userRepository.existsByUsername(username);
return new UsernameCheckResponse(!userExists);
Expand All @@ -74,6 +76,7 @@ public UpdateProfileResponse updateProfile(long userId, UpdateProfileRequest upd
return new UpdateProfileResponse(user);
}

@Transactional
public UserBlockResponse blockUser(long blockerId, long blockeeId) {
User blocker = userRepository.findById(blockerId)
.orElseThrow(() -> new UserNotFoundException("정상적으로 로그인되지 않았습니다."));
Expand All @@ -86,6 +89,7 @@ public UserBlockResponse blockUser(long blockerId, long blockeeId) {
new UserResponse(userBlock.getBlockee()));
}

@Transactional
public ReportResponse report(long reporterId, ReportRequest reportRequest) {
User reporter = userRepository.findById(reporterId)
.orElseThrow(() -> new NotFoundException("신고자 정보를 조회할 수 없습니다."));
Expand All @@ -104,6 +108,7 @@ public ReportResponse report(long reporterId, ReportRequest reportRequest) {
return new ReportResponse(savedUserReport);
}

@Transactional(readOnly = true)
public BlockedUserGroup getBlockedUserGroup(long blockerId) {

return userBlockRepository.findAllByBlockerId(blockerId).stream()
Expand Down
Loading