Skip to content

Commit

Permalink
Merge pull request #145 from BudgetBuddiesTeam/feat/#131
Browse files Browse the repository at this point in the history
[feat] 월별 소비 조회 전체 소비금액 구현 및 일별 반환
  • Loading branch information
JunRain2 authored Aug 19, 2024
2 parents 70c5ad7 + a73943d commit e1e85e3
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseRequestDto;
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseUpdateRequestDto;
import com.bbteam.budgetbuddies.domain.expense.dto.MonthlyExpenseCompactResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.MonthlyExpenseResponseDto;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -42,7 +42,7 @@ ResponseEntity<ExpenseResponseDto> createExpense(
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "AUTH003", description = "access 토큰을 주세요!", content = @Content(schema = @Schema(implementation = ApiResponse.class))),
@ApiResponse(responseCode = "AUTH004", description = "access 토큰 만료", content = @Content(schema = @Schema(implementation = ApiResponse.class))),
@ApiResponse(responseCode = "AUTH006", description = "access 토큰 모양이 이상함", content = @Content(schema = @Schema(implementation = ApiResponse.class)))})
ResponseEntity<MonthlyExpenseCompactResponseDto> findExpensesForMonth(
ResponseEntity<MonthlyExpenseResponseDto> findExpensesForMonth(
@PathVariable @Param("userId") Long userId,
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseRequestDto;
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseUpdateRequestDto;
import com.bbteam.budgetbuddies.domain.expense.dto.MonthlyExpenseCompactResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.MonthlyExpenseResponseDto;
import com.bbteam.budgetbuddies.domain.expense.service.ExpenseService;

import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -40,7 +40,7 @@ public ResponseEntity<ExpenseResponseDto> createExpense(

@Override
@GetMapping("/{userId}")
public ResponseEntity<MonthlyExpenseCompactResponseDto> findExpensesForMonth(
public ResponseEntity<MonthlyExpenseResponseDto> findExpensesForMonth(
@PathVariable @Param("userId") Long userId,
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bbteam.budgetbuddies.domain.expense.converter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.TextStyle;
import java.util.List;
import java.util.Locale;
Expand All @@ -12,9 +11,10 @@

import com.bbteam.budgetbuddies.domain.category.entity.Category;
import com.bbteam.budgetbuddies.domain.expense.dto.CompactExpenseResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.DailyExpenseResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseRequestDto;
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.MonthlyExpenseCompactResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.MonthlyExpenseResponseDto;
import com.bbteam.budgetbuddies.domain.expense.entity.Expense;
import com.bbteam.budgetbuddies.domain.user.entity.User;

Expand Down Expand Up @@ -43,24 +43,33 @@ public ExpenseResponseDto toExpenseResponseDto(Expense expense) {
.build();
}

public MonthlyExpenseCompactResponseDto toMonthlyExpenseCompactResponseDto(List<Expense> expenseList,
LocalDate startOfMonth) {
public MonthlyExpenseResponseDto toMonthlyExpenseResponseDto(List<Expense> expenseList, LocalDate startOfMonth) {
Long totalConsumptionAmount = expenseList.stream().mapToLong(Expense::getAmount).sum();

Map<String, List<CompactExpenseResponseDto>> expenses = expenseList.stream().collect(
Collectors.groupingBy(e -> this.convertDayToKorean(e.getExpenseDate()),
Collectors.mapping(this::toExpenseCompactResponseDto, Collectors.toList())));
List<DailyExpenseResponseDto> dailyExpenses = toDailyExpenseResponseDto(expenseList);

return MonthlyExpenseCompactResponseDto.builder()
return MonthlyExpenseResponseDto.builder()
.expenseMonth(startOfMonth)
.totalConsumptionAmount(totalConsumptionAmount)
.expenses(expenses)
.dailyExpenses(dailyExpenses)
.build();
}

private String convertDayToKorean(LocalDateTime localDateTime) {
return localDateTime.getDayOfMonth() + "일 " + localDateTime.getDayOfWeek()
.getDisplayName(TextStyle.FULL, Locale.KOREAN);
private List<DailyExpenseResponseDto> toDailyExpenseResponseDto(List<Expense> expenseList) {
Map<LocalDate, List<CompactExpenseResponseDto>> expenses = expenseList.stream()
.collect(Collectors.groupingBy(e -> e.getExpenseDate().toLocalDate(),
Collectors.mapping(this::toExpenseCompactResponseDto, Collectors.toList())));

return expenses.keySet().stream().map(k -> this.generateDailyExpenseResponseDto(k, expenses.get(k))).toList();
}

private DailyExpenseResponseDto generateDailyExpenseResponseDto(LocalDate date,
List<CompactExpenseResponseDto> expenses) {
return DailyExpenseResponseDto.builder()
.daysOfMonth(date.getDayOfMonth())
.daysOfTheWeek(date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.KOREAN))
.expenses(expenses)
.build();
}

private CompactExpenseResponseDto toExpenseCompactResponseDto(Expense expense) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.bbteam.budgetbuddies.domain.expense.dto;

import java.util.List;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor
@Builder
@Getter
public class DailyExpenseResponseDto {
private Integer daysOfMonth;
private String daysOfTheWeek;
private List<CompactExpenseResponseDto> expenses;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -14,9 +13,9 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor
@Builder
public class MonthlyExpenseCompactResponseDto {
public class MonthlyExpenseResponseDto {
private LocalDate expenseMonth;
private Long totalConsumptionAmount;

private Map<String, List<CompactExpenseResponseDto>> expenses;
private List<DailyExpenseResponseDto> dailyExpenses;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseRequestDto;
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseUpdateRequestDto;
import com.bbteam.budgetbuddies.domain.expense.dto.MonthlyExpenseCompactResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.MonthlyExpenseResponseDto;

public interface ExpenseService {
ExpenseResponseDto createExpense(Long userId, ExpenseRequestDto expenseRequestDto);

MonthlyExpenseCompactResponseDto getMonthlyExpense(Long userId, LocalDate localDate);
MonthlyExpenseResponseDto getMonthlyExpense(Long userId, LocalDate localDate);

ExpenseResponseDto findExpenseResponseFromUserIdAndExpenseId(Long userId, Long expenseId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseRequestDto;
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseUpdateRequestDto;
import com.bbteam.budgetbuddies.domain.expense.dto.MonthlyExpenseCompactResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.MonthlyExpenseResponseDto;
import com.bbteam.budgetbuddies.domain.expense.entity.Expense;
import com.bbteam.budgetbuddies.domain.expense.repository.ExpenseRepository;
import com.bbteam.budgetbuddies.domain.user.entity.User;
Expand Down Expand Up @@ -92,7 +92,7 @@ public void deleteExpense(Long expenseId) {

@Override
@Transactional(readOnly = true)
public MonthlyExpenseCompactResponseDto getMonthlyExpense(Long userId, LocalDate localDate) {
public MonthlyExpenseResponseDto getMonthlyExpense(Long userId, LocalDate localDate) {
LocalDate startOfMonth = localDate.withDayOfMonth(1);
LocalDate endOfMonth = localDate.withDayOfMonth(startOfMonth.lengthOfMonth());

Expand All @@ -101,7 +101,7 @@ public MonthlyExpenseCompactResponseDto getMonthlyExpense(Long userId, LocalDate
List<Expense> expenseSlice = expenseRepository.findAllByUserIdForPeriod(user,
startOfMonth.atStartOfDay(), endOfMonth.atStartOfDay());

return expenseConverter.toMonthlyExpenseCompactResponseDto(expenseSlice, startOfMonth);
return expenseConverter.toMonthlyExpenseResponseDto(expenseSlice, startOfMonth);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
Expand All @@ -24,8 +23,9 @@
import com.bbteam.budgetbuddies.domain.category.repository.CategoryRepository;
import com.bbteam.budgetbuddies.domain.expense.converter.ExpenseConverter;
import com.bbteam.budgetbuddies.domain.expense.dto.CompactExpenseResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.DailyExpenseResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.MonthlyExpenseCompactResponseDto;
import com.bbteam.budgetbuddies.domain.expense.dto.MonthlyExpenseResponseDto;
import com.bbteam.budgetbuddies.domain.expense.entity.Expense;
import com.bbteam.budgetbuddies.domain.expense.repository.ExpenseRepository;
import com.bbteam.budgetbuddies.domain.user.entity.User;
Expand Down Expand Up @@ -54,7 +54,7 @@ void setUp() {
}

@Test
@DisplayName("월별 소비 조회 소비를 d일 N요일로 묶어서 반환")
@DisplayName("월별 소비 조회 소비를 DailyExpenseResponseDto로 반환")
void getMonthlyExpense_Success() {
// given
given(userRepository.findById(user.getId())).willReturn(Optional.of(user));
Expand All @@ -69,27 +69,32 @@ void getMonthlyExpense_Success() {
given(expenseRepository.findAllByUserIdForPeriod(any(User.class), any(LocalDateTime.class),
any(LocalDateTime.class))).willReturn(expenses);

MonthlyExpenseCompactResponseDto expected =
MonthlyExpenseCompactResponseDto.builder()
.expenseMonth(LocalDate.of(2024, 07, 01))
.totalConsumptionAmount(300_000L)
.expenses(Map.of(
"2일 화요일", List.of(CompactExpenseResponseDto.builder()
.amount(200_000L)
.description("User 소비")
.expenseId(-2L)
.categoryId(userCategory.getId())
.build()),
"1일 월요일", List.of(CompactExpenseResponseDto.builder()
.amount(100_000L)
.description("User 소비")
.expenseId(-1L)
.categoryId(userCategory.getId())
.build())))
.build();
MonthlyExpenseResponseDto expected = MonthlyExpenseResponseDto.builder()
.expenseMonth(LocalDate.of(2024, 07, 01))
.totalConsumptionAmount(300_000L)
.dailyExpenses(List.of(DailyExpenseResponseDto.builder()
.daysOfMonth(2)
.daysOfTheWeek("화요일")
.expenses(List.of(CompactExpenseResponseDto.builder()
.amount(200_000L)
.description("User 소비")
.expenseId(-2L)
.categoryId(userCategory.getId())
.build()))
.build(), DailyExpenseResponseDto.builder()
.daysOfMonth(1)
.daysOfTheWeek("월요일")
.expenses(List.of(CompactExpenseResponseDto.builder()
.amount(100_000L)
.description("User 소비")
.expenseId(-1L)
.categoryId(userCategory.getId())
.build()))
.build()))
.build();

// when
MonthlyExpenseCompactResponseDto result = expenseService.getMonthlyExpense(user.getId(), requestMonth);
MonthlyExpenseResponseDto result = expenseService.getMonthlyExpense(user.getId(), requestMonth);

// then
assertThat(result).usingRecursiveComparison().isEqualTo(expected);
Expand Down

0 comments on commit e1e85e3

Please sign in to comment.