Skip to content

Commit

Permalink
♻️ divide transactions readonly and readwrite for methods
Browse files Browse the repository at this point in the history
  • Loading branch information
geoje committed Sep 23, 2024
1 parent d80d7ad commit 6a0b781
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 6 deletions.
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 @@ -101,6 +104,7 @@ private FirebaseToken decodeIdToken(String idToken) {
}
}

@Transactional(readOnly = true)
public void checkToken(long userId) {
userRepository.findById(userId)
.orElseThrow(() -> new NoSuchUserException("존재하지 않는 사용자입니다."));
Expand Down
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,6 +17,7 @@ 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));
}
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
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 @@ -21,6 +22,7 @@ public IngredientRecipe save(Ingredient ingredient, Recipe recipe, Requirement r
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,6 +8,7 @@
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
Expand All @@ -20,6 +21,7 @@ public void save(IngredientRecipe ingredientRecipe, Ingredient 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@
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("해당되는 레시피가 없습니다."));
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 Down Expand Up @@ -104,6 +106,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

0 comments on commit 6a0b781

Please sign in to comment.