Skip to content

Commit

Permalink
[Feat/#94] 계정 카테고리 목록 조회 API 로직 수정 (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnsugyeong authored Feb 9, 2024
1 parent 327551e commit d73700e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ public ResponseDto<Boolean> deleteTertiaryCategory(@AuthMember Member member,
@GetMapping
public ResponseDto<AccountCategoryListDto> getAccountCategories(@AuthMember Member member) {
return ResponseDto.onSuccess(
CategoryConverter.toAccountCategoryListDto(categoryQueryService.getTertiaryCategories(member)));
CategoryConverter.toAccountCategoryListDto(
categoryQueryService.getSecondaryCategories(),
categoryQueryService.getTertiaryCategories(member)
));
}

}
Original file line number Diff line number Diff line change
@@ -1,33 +1,51 @@
package com.friends.easybud.category.converter;

import com.friends.easybud.category.domain.SecondaryCategory;
import com.friends.easybud.category.domain.TertiaryCategory;
import com.friends.easybud.category.dto.CategoryResponse.AccountCategoryDto;
import com.friends.easybud.category.dto.CategoryResponse.AccountCategoryListDto;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class CategoryConverter {

public static AccountCategoryDto toAccountCategoryDto(TertiaryCategory tertiaryCategory) {
return AccountCategoryDto.builder()
.primaryCategoryId(tertiaryCategory.getSecondaryCategory().getPrimaryCategory().getId())
.primaryCategoryContent(tertiaryCategory.getSecondaryCategory().getPrimaryCategory().getContent())
.secondaryCategoryId(tertiaryCategory.getSecondaryCategory().getId())
.secondaryCategoryContent(tertiaryCategory.getSecondaryCategory().getContent())
.tertiaryCategoryId(tertiaryCategory.getId())
.tertiaryCategoryContent(tertiaryCategory.getContent())
.isDefault(tertiaryCategory.getIsDefault())
.build();
public static AccountCategoryDto toAccountCategoryDto(SecondaryCategory secondaryCategory,
TertiaryCategory tertiaryCategory) {
AccountCategoryDto.AccountCategoryDtoBuilder builder = AccountCategoryDto.builder()
.primaryCategoryId(secondaryCategory.getPrimaryCategory().getId())
.primaryCategoryContent(secondaryCategory.getPrimaryCategory().getContent())
.secondaryCategoryId(secondaryCategory.getId())
.secondaryCategoryContent(secondaryCategory.getContent());

if (tertiaryCategory != null) {
builder.tertiaryCategoryId(tertiaryCategory.getId())
.tertiaryCategoryContent(tertiaryCategory.getContent())
.isDefault(tertiaryCategory.getIsDefault());
}

return builder.build();
}

public static AccountCategoryListDto toAccountCategoryListDto(List<TertiaryCategory> tertiaryCategories) {
List<AccountCategoryDto> accountCategoryDtos = tertiaryCategories.stream()
.map(CategoryConverter::toAccountCategoryDto)
.collect(Collectors.toList());
public static AccountCategoryListDto toAccountCategoryListDto(List<SecondaryCategory> secondaryCategories,
List<TertiaryCategory> tertiaryCategories) {
List<AccountCategoryDto> accountCategoryDtos = new ArrayList<>();

for (SecondaryCategory secondaryCategory : secondaryCategories) {
List<TertiaryCategory> relatedTertiaryCategories = tertiaryCategories.stream()
.filter(tc -> tc.getSecondaryCategory().equals(secondaryCategory))
.collect(Collectors.toList());

if (relatedTertiaryCategories.isEmpty()) {
accountCategoryDtos.add(toAccountCategoryDto(secondaryCategory, null));
} else {
relatedTertiaryCategories.forEach(tertiaryCategory ->
accountCategoryDtos.add(toAccountCategoryDto(secondaryCategory, tertiaryCategory)));
}
}

return AccountCategoryListDto.builder()
.accountCategories(accountCategoryDtos)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.friends.easybud.category.service;

import com.friends.easybud.category.domain.SecondaryCategory;
import com.friends.easybud.category.domain.TertiaryCategory;
import com.friends.easybud.member.domain.Member;
import java.util.List;
Expand All @@ -8,4 +9,6 @@ public interface CategoryQueryService {

List<TertiaryCategory> getTertiaryCategories(Member member);

List<SecondaryCategory> getSecondaryCategories();

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.friends.easybud.category.service;

import com.friends.easybud.category.domain.SecondaryCategory;
import com.friends.easybud.category.domain.TertiaryCategory;
import com.friends.easybud.category.repository.SecondaryCategoryRepository;
import com.friends.easybud.category.repository.TertiaryCategoryRepository;
import com.friends.easybud.member.domain.Member;
import java.util.List;
Expand All @@ -13,11 +15,17 @@
@Service
public class CategoryQueryServiceImpl implements CategoryQueryService {

private final SecondaryCategoryRepository secondaryCategoryRepository;
private final TertiaryCategoryRepository tertiaryCategoryRepository;

@Override
public List<TertiaryCategory> getTertiaryCategories(Member member) {
return tertiaryCategoryRepository.findByMemberIdOrIsDefaultTrue(member.getId());
}

@Override
public List<SecondaryCategory> getSecondaryCategories() {
return secondaryCategoryRepository.findAll();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ private void createAndSaveAccount(AccountCreateDto accountDto, Transaction trans
Card card = cardRepository.findById(accountDto.getCardId())
.orElseThrow(() -> new GeneralException(ErrorStatus.CARD_NOT_FOUND));
account = buildAccount(accountDto, card, null);
} else {
} else if (accountDto.getTertiaryCategoryId() != null) {
TertiaryCategory tertiaryCategory = tertiaryCategoryRepository.findById(accountDto.getTertiaryCategoryId())
.orElseThrow(() -> new GeneralException(ErrorStatus.TERTIARY_CATEGORY_NOT_FOUND));
account = buildAccount(accountDto, null, tertiaryCategory);
} else {
account = buildAccount(accountDto, null, null);
}

account.addTransaction(transaction);
Expand Down

0 comments on commit d73700e

Please sign in to comment.