Skip to content

Commit

Permalink
Merge pull request #221 from BudgetBuddiesTeam/refactor/#220
Browse files Browse the repository at this point in the history
[Refactor] ver1 에서 구현된 코드 리팩토링 #220
  • Loading branch information
JunRain2 authored Nov 18, 2024
2 parents 591d7a8 + 265c0e5 commit cb05ac5
Show file tree
Hide file tree
Showing 22 changed files with 421 additions and 610 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ public interface CategoryService {

List<CategoryResponseDto> getUserCategories(Long userId);

Category handleCategoryChange(Expense expense, ExpenseUpdateRequestDto request, User user);

void deleteCategory(Long id, Long userId);

Category getCategory(Long categoryId);

List<Category> getUserCategoryList(Long userId);
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Optional;
import java.util.stream.Collectors;

import com.bbteam.budgetbuddies.domain.expense.repository.ExpenseRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -16,9 +15,8 @@
import com.bbteam.budgetbuddies.domain.category.repository.CategoryRepository;
import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal;
import com.bbteam.budgetbuddies.domain.consumptiongoal.repository.ConsumptionGoalRepository;
import com.bbteam.budgetbuddies.domain.consumptiongoal.service.ConsumptionGoalService;
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseUpdateRequestDto;
import com.bbteam.budgetbuddies.domain.expense.entity.Expense;
import com.bbteam.budgetbuddies.domain.expense.repository.ExpenseRepository;
import com.bbteam.budgetbuddies.domain.user.entity.User;
import com.bbteam.budgetbuddies.domain.user.repository.UserRepository;

Expand All @@ -30,7 +28,6 @@
public class CategoryServiceImpl implements CategoryService {

private final CategoryRepository categoryRepository;
private final ConsumptionGoalService consumptionGoalService;
private final UserRepository userRepository;
private final CategoryConverter categoryConverter;
private final ConsumptionGoalRepository consumptionGoalRepository;
Expand All @@ -40,11 +37,11 @@ public class CategoryServiceImpl implements CategoryService {
@Transactional
public CategoryResponseDto createCategory(Long userId, CategoryRequestDto categoryRequestDto) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new IllegalArgumentException("Invalid user ID"));
.orElseThrow(() -> new IllegalArgumentException("Invalid user ID"));

// 동일한 이름의 삭제된 카테고리가 존재하는지 확인
Optional<Category> existingCategory = categoryRepository.findByNameAndUserIdAndDeletedTrue(
categoryRequestDto.getName(), userId);
categoryRequestDto.getName(), userId);

if (existingCategory.isPresent()) {
// 삭제된 카테고리가 존재하면 복구 (deleted = false)
Expand All @@ -54,7 +51,7 @@ public CategoryResponseDto createCategory(Long userId, CategoryRequestDto catego

// 해당 카테고리의 삭제된 ConsumptionGoal도 복구
Optional<ConsumptionGoal> existingConsumptionGoal = consumptionGoalRepository.findByUserAndCategoryAndDeletedTrue(
user, categoryToRestore);
user, categoryToRestore);

if (existingConsumptionGoal.isPresent()) {
ConsumptionGoal consumptionGoalToRestore = existingConsumptionGoal.get();
Expand All @@ -65,13 +62,13 @@ public CategoryResponseDto createCategory(Long userId, CategoryRequestDto catego
} else {
// ConsumptionGoal이 존재하지 않으면 새로 생성
ConsumptionGoal newConsumptionGoal = ConsumptionGoal.builder()
.user(user)
.category(categoryToRestore)
.goalMonth(LocalDate.now().withDayOfMonth(1)) // 현재 달로 목표 설정
.consumeAmount(0L)
.goalAmount(0L)
.deleted(false) // 생성할 때 삭제 상태가 아니도록
.build();
.user(user)
.category(categoryToRestore)
.goalMonth(LocalDate.now().withDayOfMonth(1)) // 현재 달로 목표 설정
.consumeAmount(0L)
.goalAmount(0L)
.deleted(false) // 생성할 때 삭제 상태가 아니도록
.build();
consumptionGoalRepository.save(newConsumptionGoal);
}

Expand All @@ -83,13 +80,13 @@ public CategoryResponseDto createCategory(Long userId, CategoryRequestDto catego

// 새로운 카테고리에 대한 ConsumptionGoal도 생성
ConsumptionGoal newConsumptionGoal = ConsumptionGoal.builder()
.user(user)
.category(newCategory)
.goalMonth(LocalDate.now().withDayOfMonth(1)) // 현재 달로 목표 설정
.consumeAmount(0L)
.goalAmount(0L)
.deleted(false) // 생성할 때 삭제 상태가 아니도록
.build();
.user(user)
.category(newCategory)
.goalMonth(LocalDate.now().withDayOfMonth(1)) // 현재 달로 목표 설정
.consumeAmount(0L)
.goalAmount(0L)
.deleted(false) // 생성할 때 삭제 상태가 아니도록
.build();
consumptionGoalRepository.save(newConsumptionGoal);

return categoryConverter.toCategoryResponseDto(newCategory);
Expand All @@ -100,21 +97,15 @@ public CategoryResponseDto createCategory(Long userId, CategoryRequestDto catego
public List<CategoryResponseDto> getUserCategories(Long userId) {

userRepository.findById(userId)
.orElseThrow(() -> new IllegalArgumentException("User not found with id: " + userId));
.orElseThrow(() -> new IllegalArgumentException("User not found with id: " + userId));

List<Category> categories = categoryRepository.findUserCategoryByUserId(userId);
return categories.stream().map(categoryConverter::toCategoryResponseDto).collect(Collectors.toList());
}

@Override
@Transactional(readOnly = true)
public Category handleCategoryChange(Expense expense, ExpenseUpdateRequestDto request, User user) {
Category categoryToReplace = categoryRepository.findById(request.getCategoryId())
.orElseThrow(() -> new IllegalArgumentException("Not found category"));

consumptionGoalService.recalculateConsumptionAmount(expense, request, user);

return categoryToReplace;
public Category getCategory(Long id) {
return categoryRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Not found category"));
}

@Override
Expand All @@ -131,15 +122,15 @@ public void deleteCategory(Long categoryId, Long userId) {

// 현재 월에 해당하는 삭제되지 않은 Expense 조회 (deleted = false)
List<Expense> currentMonthExpenses = expenseRepository.findByCategoryIdAndUserIdAndExpenseDateBetweenAndDeletedFalse(
categoryId, userId, startOfMonth.atStartOfDay(), endOfMonth.atTime(23, 59, 59));
categoryId, userId, startOfMonth.atStartOfDay(), endOfMonth.atTime(23, 59, 59));

long totalAmount = currentMonthExpenses.stream()
.mapToLong(Expense::getAmount)
.sum();
.mapToLong(Expense::getAmount)
.sum();

// category_id = 10(기타 카테고리)의 소비 목표 업데이트 (custom 카테고리 삭제로 인한 소비 내역은 삭제되지 않고 기타 카테고리로..)
ConsumptionGoal goal = consumptionGoalRepository.findByCategoryIdAndUserId(10L, userId)
.orElseThrow(() -> new IllegalArgumentException("No consumption goal found for category_id 10."));
.orElseThrow(() -> new IllegalArgumentException("No consumption goal found for category_id 10."));
goal.setConsumeAmount(goal.getConsumeAmount() + totalAmount);
consumptionGoalRepository.save(goal);

Expand All @@ -161,4 +152,8 @@ public void deleteCategory(Long categoryId, Long userId) {
});
}

@Override
public List<Category> getUserCategoryList(Long userId) {
return categoryRepository.findUserCategoryByUserId(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopCategoryConsumptionDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto;
import com.bbteam.budgetbuddies.domain.user.dto.UserDto;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand Down Expand Up @@ -53,10 +54,10 @@ ApiResponse<List<AllConsumptionCategoryResponseDto>> getAllConsumptionGoalCatego

@Operation(summary = "[User] 소비 목표 조회", description = "date={yyyy-MM-dd} 형식의 query string을 통해서 사용자의 목표 달을 조회하는 API 입니다.")
@Parameters({@Parameter(name = "date", description = "yyyy-MM-dd 형식으로 목표 달의 소비를 조회")})
ApiResponse<ConsumptionGoalResponseListDto> findUserConsumptionGoal(LocalDate date, Long userId);
ApiResponse<ConsumptionGoalResponseListDto> findUserConsumptionGoal(LocalDate date, UserDto.AuthUserDto user);

@Operation(summary = "[User] 이번 달 소비 목표 수정", description = "다른 달의 소비 목표를 업데이트하는 것은 불가능하고 오직 이번 달의 소비 목표만 업데이트 하는 API 입니다.")
ResponseEntity<ConsumptionGoalResponseListDto> updateOrElseGenerateConsumptionGoal(Long userId,
ResponseEntity<ConsumptionGoalResponseListDto> updateOrElseGenerateConsumptionGoal(UserDto.AuthUserDto user,
ConsumptionGoalListRequestDto consumptionGoalListRequestDto);

@Operation(summary = "[User] 또래들이 가장 많이한 소비 카테고리 조회 Top3", description = "특정 사용자의 또래 소비 카테고리별 소비 건 수을 조회하는 API 입니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -23,6 +22,8 @@
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopCategoryConsumptionDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.service.ConsumptionGoalService;
import com.bbteam.budgetbuddies.domain.user.dto.UserDto;
import com.bbteam.budgetbuddies.global.security.utils.AuthUser;

import lombok.RequiredArgsConstructor;

Expand Down Expand Up @@ -67,22 +68,21 @@ public ApiResponse<PeerInfoResponseDto> getPeerInfo(@RequestParam(name = "userId
return ApiResponse.onSuccess(response);
}

@GetMapping("/{userId}")
@GetMapping()
public ApiResponse<ConsumptionGoalResponseListDto> findUserConsumptionGoal(
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date, @PathVariable Long userId) {
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date, @AuthUser UserDto.AuthUserDto user) {

ConsumptionGoalResponseListDto response = consumptionGoalService.findUserConsumptionGoalList(userId, date);

return ApiResponse.onSuccess(response);
return ApiResponse.onSuccess(consumptionGoalService.findUserConsumptionGoalList(user.getId(), date));
}

@Override
@PostMapping("/{userId}")
public ResponseEntity<ConsumptionGoalResponseListDto> updateOrElseGenerateConsumptionGoal(@PathVariable Long userId,
@PostMapping()
public ResponseEntity<ConsumptionGoalResponseListDto> updateOrElseGenerateConsumptionGoal(
@AuthUser UserDto.AuthUserDto user,
@RequestBody ConsumptionGoalListRequestDto consumptionGoalListRequestDto) {

return ResponseEntity.ok()
.body(consumptionGoalService.updateConsumptionGoals(userId, consumptionGoalListRequestDto));
.body(consumptionGoalService.updateConsumptionGoals(user.getId(), consumptionGoalListRequestDto));
}

@GetMapping("/categories/top-consumptions/top-3")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.bbteam.budgetbuddies.domain.consumptiongoal.converter;

import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;

import org.springframework.stereotype.Component;

import com.bbteam.budgetbuddies.domain.category.entity.Category;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto;
Expand All @@ -17,15 +17,6 @@

@Component
public class ConsumptionGoalConverter {
public ConsumptionGoalResponseDto toConsumptionGoalResponseDto(Category category) {
return ConsumptionGoalResponseDto.builder()
.categoryName(category.getName())
.categoryId(category.getId())
.goalAmount(0L)
.consumeAmount(0L)
.build();
}

public ConsumptionGoalResponseDto toConsumptionGoalResponseDto(ConsumptionGoal consumptionGoal) {
return ConsumptionGoalResponseDto.builder()
.categoryName(consumptionGoal.getCategory().getName())
Expand All @@ -35,27 +26,24 @@ public ConsumptionGoalResponseDto toConsumptionGoalResponseDto(ConsumptionGoal c
.build();
}

public ConsumptionGoalResponseDto toConsumptionGoalResponseDtoFromPreviousGoal(ConsumptionGoal consumptionGoal) {
return ConsumptionGoalResponseDto.builder()
.categoryName(consumptionGoal.getCategory().getName())
.categoryId(consumptionGoal.getCategory().getId())
.goalAmount(consumptionGoal.getGoalAmount())
.consumeAmount(0L)
.build();
public ConsumptionGoalResponseListDto toConsumptionGoalResponseListDto(
List<ConsumptionGoal> consumptionGoalList, LocalDate goalMonth) {

}
List<ConsumptionGoalResponseDto> consumptionGoalResponseList = consumptionGoalList
.stream()
.map(this::toConsumptionGoalResponseDto)
.sorted(Comparator.comparingLong(ConsumptionGoalResponseDto::getRemainingBalance).reversed())
.toList();

public ConsumptionGoalResponseListDto toConsumptionGoalResponseListDto(
List<ConsumptionGoalResponseDto> consumptionGoalList, LocalDate goalMonth) {
Long totalGoalAmount = sumTotalGoalAmount(consumptionGoalList);
Long totalConsumptionAmount = sumTotalConsumptionAmount(consumptionGoalList);
Long totalGoalAmount = sumTotalGoalAmount(consumptionGoalResponseList);
Long totalConsumptionAmount = sumTotalConsumptionAmount(consumptionGoalResponseList);

return ConsumptionGoalResponseListDto.builder()
.goalMonth(goalMonth)
.totalGoalAmount(totalGoalAmount)
.totalConsumptionAmount(totalConsumptionAmount)
.totalRemainingBalance(totalGoalAmount - totalConsumptionAmount)
.consumptionGoalList(consumptionGoalList)
.consumptionGoalList(consumptionGoalResponseList)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class ConsumptionGoal extends BaseEntity {
@JoinColumn(name = "category_id")
private Category category;

public void updateConsumeAmount(Long amount) {
public void addConsumeAmount(Long amount) {
this.consumeAmount += amount;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public interface ConsumptionGoalRepository extends JpaRepository<ConsumptionGoal
@Query(value = "SELECT cg FROM ConsumptionGoal AS cg WHERE cg.user.id = :userId AND cg.goalMonth = :goalMonth")
List<ConsumptionGoal> findConsumptionGoalByUserIdAndGoalMonth(Long userId, LocalDate goalMonth);

Optional<ConsumptionGoal> findConsumptionGoalByUserAndCategoryAndGoalMonth(User user, Category category,
LocalDate goalMonth);
Optional<ConsumptionGoal> findByUserAndCategoryAndGoalMonth(User user, Category category, LocalDate goalMonth);

@Query("SELECT AVG(cg.consumeAmount) FROM ConsumptionGoal cg " +
"JOIN cg.category c " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.springframework.stereotype.Service;

import com.bbteam.budgetbuddies.domain.category.entity.Category;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AllConsumptionCategoryResponseDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto;
Expand All @@ -13,8 +14,7 @@
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopCategoryConsumptionDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseUpdateRequestDto;
import com.bbteam.budgetbuddies.domain.expense.entity.Expense;
import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal;
import com.bbteam.budgetbuddies.domain.user.entity.User;

@Service
Expand All @@ -35,7 +35,8 @@ ConsumptionGoalResponseListDto updateConsumptionGoals(Long userId,

ConsumptionAnalysisResponseDto getTopCategoryAndConsumptionAmount(Long userId);

void recalculateConsumptionAmount(Expense expense, ExpenseUpdateRequestDto request, User user);
void recalculateConsumptionAmount(ConsumptionGoal beforeConsumptionGoal, Long beforeAmount,
ConsumptionGoal afterConsumptionGoal, Long afterAmount);

void updateConsumeAmount(Long userId, Long categoryId, Long amount);

Expand All @@ -52,4 +53,6 @@ List<AllConsumptionCategoryResponseDto> getAllConsumptionCategories(Long userId,
MonthReportResponseDto getMonthReport(Long userId);

String getConsumptionMention(Long userId);

ConsumptionGoal getUserConsumptionGoal(User user, Category category, LocalDate goalDate);
}
Loading

0 comments on commit cb05ac5

Please sign in to comment.