Skip to content

Commit

Permalink
refactor: 🔧 지출 카테고리 유즈케이스 리팩토링 (#114)
Browse files Browse the repository at this point in the history
* fix: 배포 파이프라인 이미지 빌드 버전 추가

* refactor: spending_category create 유즈케이스 리팩토링

* refactor: get_spending_categories usecase 리팩토링
  • Loading branch information
psychology50 authored Jun 25, 2024
1 parent 9b69bd4 commit b976bcc
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package kr.co.pennyway.api.apis.ledger.mapper;

import kr.co.pennyway.api.apis.ledger.dto.SpendingCategoryDto;
import kr.co.pennyway.common.annotation.Mapper;
import kr.co.pennyway.domain.domains.spending.domain.SpendingCustomCategory;
import kr.co.pennyway.domain.domains.spending.dto.CategoryInfo;

import java.util.List;

@Mapper
public class SpendingCategoryMapper {
public static SpendingCategoryDto.Res toResponse(SpendingCustomCategory category) {
return SpendingCategoryDto.Res.from(CategoryInfo.of(category.getId(), category.getName(), category.getIcon()));
}

public static List<SpendingCategoryDto.Res> toResponses(List<SpendingCustomCategory> categories) {
return categories.stream()
.map(SpendingCategoryMapper::toResponse)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package kr.co.pennyway.api.apis.ledger.service;

import kr.co.pennyway.domain.domains.spending.domain.SpendingCustomCategory;
import kr.co.pennyway.domain.domains.spending.service.SpendingCustomCategoryService;
import kr.co.pennyway.domain.domains.spending.type.SpendingCategory;
import kr.co.pennyway.domain.domains.user.domain.User;
import kr.co.pennyway.domain.domains.user.exception.UserErrorCode;
import kr.co.pennyway.domain.domains.user.exception.UserErrorException;
import kr.co.pennyway.domain.domains.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@RequiredArgsConstructor
public class SpendingCategorySaveService {
private final UserService userService;
private final SpendingCustomCategoryService spendingCustomCategoryService;

@Transactional
public SpendingCustomCategory execute(Long userId, String categoryName, SpendingCategory icon) {
User user = userService.readUser(userId).orElseThrow(() -> new UserErrorException(UserErrorCode.NOT_FOUND));

return spendingCustomCategoryService.createSpendingCustomCategory(SpendingCustomCategory.of(categoryName, icon, user));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package kr.co.pennyway.api.apis.ledger.service;

import kr.co.pennyway.domain.domains.spending.domain.SpendingCustomCategory;
import kr.co.pennyway.domain.domains.spending.service.SpendingCustomCategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class SpendingCategorySearchService {
private final SpendingCustomCategoryService spendingCustomCategoryService;

@Transactional(readOnly = true)
public List<SpendingCustomCategory> readSpendingCustomCategories(Long userId) {
return spendingCustomCategoryService.readSpendingCustomCategories(userId);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package kr.co.pennyway.api.apis.ledger.usecase;

import kr.co.pennyway.api.apis.ledger.dto.SpendingCategoryDto;
import kr.co.pennyway.api.apis.ledger.mapper.SpendingCategoryMapper;
import kr.co.pennyway.api.apis.ledger.service.SpendingCategorySaveService;
import kr.co.pennyway.api.apis.ledger.service.SpendingCategorySearchService;
import kr.co.pennyway.common.annotation.UseCase;
import kr.co.pennyway.domain.domains.spending.domain.SpendingCustomCategory;
import kr.co.pennyway.domain.domains.spending.dto.CategoryInfo;
import kr.co.pennyway.domain.domains.spending.service.SpendingCustomCategoryService;
import kr.co.pennyway.domain.domains.spending.type.SpendingCategory;
import kr.co.pennyway.domain.domains.user.domain.User;
import kr.co.pennyway.domain.domains.user.exception.UserErrorCode;
import kr.co.pennyway.domain.domains.user.exception.UserErrorException;
import kr.co.pennyway.domain.domains.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -20,24 +17,20 @@
@UseCase
@RequiredArgsConstructor
public class SpendingCategoryUseCase {
private final UserService userService;
private final SpendingCustomCategoryService spendingCustomCategoryService;
private final SpendingCategorySaveService spendingCategorySaveService;
private final SpendingCategorySearchService spendingCategorySearchService;

@Transactional
public SpendingCategoryDto.Res createSpendingCategory(Long userId, String categoryName, SpendingCategory icon) {
User user = userService.readUser(userId).orElseThrow(() -> new UserErrorException(UserErrorCode.NOT_FOUND));
SpendingCustomCategory category = spendingCategorySaveService.execute(userId, categoryName, icon);

SpendingCustomCategory category = spendingCustomCategoryService.createSpendingCustomCategory(SpendingCustomCategory.of(categoryName, icon, user));

return SpendingCategoryDto.Res.from(CategoryInfo.of(category.getId(), category.getName(), category.getIcon()));
return SpendingCategoryMapper.toResponse(category);
}

@Transactional(readOnly = true)
public List<SpendingCategoryDto.Res> getSpendingCategories(Long userId) {
List<SpendingCustomCategory> categories = spendingCustomCategoryService.readSpendingCustomCategories(userId);
List<SpendingCustomCategory> categories = spendingCategorySearchService.readSpendingCustomCategories(userId);

return categories.stream()
.map(category -> SpendingCategoryDto.Res.from(CategoryInfo.of(category.getId(), category.getName(), category.getIcon())))
.toList();
return SpendingCategoryMapper.toResponses(categories);
}
}

0 comments on commit b976bcc

Please sign in to comment.