Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ”ง ๋ชฉํ‘œ ๊ธˆ์•ก ์œ ์ฆˆ ์ผ€์ด์Šค ๋ฆฌํŒฉํ† ๋ง #112

Merged
merged 21 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
907e8a5
fix: ๋ฐฐํฌ ํŒŒ์ดํ”„๋ผ์ธ ์ด๋ฏธ์ง€ ๋นŒ๋“œ ๋ฒ„์ „ ์ถ”๊ฐ€
psychology50 May 30, 2024
8c58d77
Merge branch 'dev' of https://github.com/CollaBu/pennyway-was into dev
psychology50 Jun 2, 2024
a2d469e
Merge branch 'dev' of https://github.com/CollaBu/pennyway-was into dev
psychology50 Jun 5, 2024
791d4fd
Merge branch 'dev' of https://github.com/CollaBu/pennyway-was into dev
psychology50 Jun 5, 2024
f062461
Merge branch 'dev' of https://github.com/CollaBu/pennyway-was into dev
psychology50 Jun 5, 2024
a48ab9a
Merge branch 'dev' of https://github.com/CollaBu/pennyway-was into dev
psychology50 Jun 6, 2024
893efe6
Merge branch 'dev' of https://github.com/CollaBu/pennyway-was into dev
psychology50 Jun 11, 2024
32ee785
refactor: create_target_amount() ๋ฆฌํŒฉํ† ๋ง
psychology50 Jun 12, 2024
8565871
rename: recent_target_amount_search_service -> target_amount_search_sโ€ฆ
psychology50 Jun 12, 2024
48a4522
refactor: get_target_amount_and_total_spending()
psychology50 Jun 12, 2024
5684d82
refactor: get_target_amounts_and_total_spendings()
psychology50 Jun 12, 2024
62caa04
refactor: update_target_amount ๋ฆฌํŒฉํ† ๋ง
psychology50 Jun 12, 2024
bf66172
refactor: target_amount_delete_service() ๋ฆฌํŒฉํ† ๋ง
psychology50 Jun 12, 2024
d51d5a3
style: ๋ถˆํ•„์š”ํ•œ transactional ์–ด๋…ธํ…Œ์ด์…˜ ์ œ๊ฑฐ
psychology50 Jun 12, 2024
d2985fe
refactor: ์›”๋ณ„ ์ด ์ง€์ถœ ๋‚ด์—ญ ์กฐํšŒ ๋ฉ”์„œ๋“œ ๋ถ„๋ฆฌ
psychology50 Jun 23, 2024
1c3283e
refactor: ์ง€์ถœ ์กฐํšŒ, ์ง€์ถœ ๋ชฉํ‘œ ๊ธˆ์•ก ์กฐํšŒ ์„œ๋น„์Šค ๋กœ์ง ๋ถ„๋ฆฌ
psychology50 Jun 23, 2024
0c27dc3
refactor: ๋ชฉํ‘œ๊ธˆ์•ก&์›”๋ณ„ ์ง€์ถœ ๋‚ด์—ญ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ ๋ฉ”์„œ๋“œ ๋ถ„๋ฆฌ
psychology50 Jun 23, 2024
a059df4
fix: target_amount_mapper start_at ์‚ฌ์šฉ์ž ํšŒ์›๊ฐ€์ž… ์ผ์ž -> ๊ฐ€์žฅ ์˜ค๋ž˜๋œ ๋ชฉํ‘œ ๊ธˆ์•ก ๋ฐ์ดํ„ฐ ๊ธฐ๋ฐ˜โ€ฆ
psychology50 Jun 25, 2024
2348bd2
docs: ๋ชฉํ‘œ๊ธˆ์•ก ๋ฆฌ์ŠคํŠธ ์กฐํšŒ ์Šค์›จ๊ฑฐ ๋ฌธ์„œ ์š”์•ฝ ์ˆ˜์ •
psychology50 Jun 25, 2024
b579501
Merge branch 'dev' of https://github.com/CollaBu/pennyway-was into dev
psychology50 Jun 25, 2024
36a339f
fix: merge last pr
psychology50 Jun 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

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.target.domain.TargetAmount;
import kr.co.pennyway.domain.domains.target.exception.TargetAmountErrorCode;
import kr.co.pennyway.domain.domains.target.exception.TargetAmountErrorException;
import kr.co.pennyway.domain.domains.target.service.TargetAmountService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class TargetAmountDeleteService {
private final TargetAmountService targetAmountService;

@Transactional
public void execute(Long targetAmountId) {
TargetAmount targetAmount = targetAmountService.readTargetAmount(targetAmountId)
.filter(TargetAmount::isAllocatedAmount)
.orElseThrow(() -> new TargetAmountErrorException(TargetAmountErrorCode.NOT_FOUND_TARGET_AMOUNT));

asn6878 marked this conversation as resolved.
Show resolved Hide resolved
if (!targetAmount.isThatMonth()) {
throw new TargetAmountErrorException(TargetAmountErrorCode.INVALID_TARGET_AMOUNT_DATE);
}

targetAmountService.deleteTargetAmount(targetAmount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,46 @@
import kr.co.pennyway.domain.domains.target.exception.TargetAmountErrorException;
import kr.co.pennyway.domain.domains.target.service.TargetAmountService;
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;

import java.time.LocalDate;

@Slf4j
@Service
@RequiredArgsConstructor
public class TargetAmountSaveService {
private final UserService userService;
private final TargetAmountService targetAmountService;

@DistributedLock(key = "#key.concat(#user.getId()).concat('_').concat(#date.getYear()).concat('-').concat(#date.getMonthValue())")
public TargetAmount createTargetAmount(String key, User user, LocalDate date) {
@DistributedLock(key = "#key.concat(#userId).concat('_').concat(#date.getYear()).concat('-').concat(#date.getMonthValue())")
public TargetAmount createTargetAmount(String key, Long userId, LocalDate date) {
User user = userService.readUser(userId).orElseThrow(() -> new UserErrorException(UserErrorCode.NOT_FOUND));

if (targetAmountService.isExistsTargetAmountThatMonth(user.getId(), date)) {
log.info("{}์— ๋Œ€ํ•œ ๋‚ ์งœ์˜ ๋ชฉํ‘œ ๊ธˆ์•ก์ด ์ด๋ฏธ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.", date);
throw new TargetAmountErrorException(TargetAmountErrorCode.ALREADY_EXIST_TARGET_AMOUNT);
}

return targetAmountService.createTargetAmount(TargetAmount.of(-1, user));
}

@Transactional
public TargetAmount updateTargetAmount(Long targetAmountId, Integer amount) {
TargetAmount targetAmount = targetAmountService.readTargetAmount(targetAmountId)
.orElseThrow(() -> new TargetAmountErrorException(TargetAmountErrorCode.NOT_FOUND_TARGET_AMOUNT));

if (!targetAmount.isThatMonth()) {
throw new TargetAmountErrorException(TargetAmountErrorCode.INVALID_TARGET_AMOUNT_DATE);
}

targetAmount.updateAmount(amount);

return targetAmount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package kr.co.pennyway.api.apis.ledger.service;

import kr.co.pennyway.api.apis.ledger.dto.TargetAmountDto;
import kr.co.pennyway.api.apis.ledger.mapper.TargetAmountMapper;
import kr.co.pennyway.domain.domains.spending.dto.TotalSpendingAmount;
import kr.co.pennyway.domain.domains.spending.service.SpendingService;
import kr.co.pennyway.domain.domains.target.domain.TargetAmount;
import kr.co.pennyway.domain.domains.target.exception.TargetAmountErrorCode;
import kr.co.pennyway.domain.domains.target.exception.TargetAmountErrorException;
import kr.co.pennyway.domain.domains.target.service.TargetAmountService;
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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
public class TargetAmountSearchService {
private final UserService userService;
private final SpendingService spendingService;
private final TargetAmountService targetAmountService;

@Transactional(readOnly = true)
public TargetAmountDto.WithTotalSpendingRes readTargetAmountAndTotalSpending(Long userId, LocalDate date) {
TargetAmount targetAmount = targetAmountService.readTargetAmountThatMonth(userId, date).orElseThrow(() -> new TargetAmountErrorException(TargetAmountErrorCode.NOT_FOUND_TARGET_AMOUNT));
Optional<TotalSpendingAmount> totalSpending = spendingService.readTotalSpendingAmountByUserId(userId, date);

return TargetAmountMapper.toWithTotalSpendingResponse(targetAmount, totalSpending.orElse(null), date);
}

@Transactional(readOnly = true)
public List<TargetAmountDto.WithTotalSpendingRes> readTargetAmountsAndTotalSpendings(Long userId, LocalDate date) {
User user = userService.readUser(userId).orElseThrow(() -> new UserErrorException(UserErrorCode.NOT_FOUND));

List<TargetAmount> targetAmounts = targetAmountService.readTargetAmountsByUserId(userId);
List<TotalSpendingAmount> totalSpendings = spendingService.readTotalSpendingsAmountByUserId(userId);

return TargetAmountMapper.toWithTotalSpendingResponses(targetAmounts, totalSpendings, user.getCreatedAt().toLocalDate(), date);
}

@Transactional(readOnly = true)
public Integer readRecentTargetAmount(Long userId) {
return targetAmountService.readRecentTargetAmount(userId)
.map(TargetAmount::getAmount)
.orElse(-1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,95 +2,54 @@

import kr.co.pennyway.api.apis.ledger.dto.TargetAmountDto;
import kr.co.pennyway.api.apis.ledger.mapper.TargetAmountMapper;
import kr.co.pennyway.api.apis.ledger.service.RecentTargetAmountSearchService;
import kr.co.pennyway.api.apis.ledger.service.TargetAmountDeleteService;
import kr.co.pennyway.api.apis.ledger.service.TargetAmountSaveService;
import kr.co.pennyway.api.apis.ledger.service.TargetAmountSearchService;
import kr.co.pennyway.common.annotation.UseCase;
import kr.co.pennyway.domain.common.redisson.DistributedLockPrefix;
import kr.co.pennyway.domain.domains.spending.dto.TotalSpendingAmount;
import kr.co.pennyway.domain.domains.spending.service.SpendingService;
import kr.co.pennyway.domain.domains.target.domain.TargetAmount;
import kr.co.pennyway.domain.domains.target.exception.TargetAmountErrorCode;
import kr.co.pennyway.domain.domains.target.exception.TargetAmountErrorException;
import kr.co.pennyway.domain.domains.target.service.TargetAmountService;
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;

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

@Slf4j
@UseCase
@RequiredArgsConstructor
public class TargetAmountUseCase {
private final UserService userService;
private final TargetAmountService targetAmountService;
private final SpendingService spendingService;

private final TargetAmountSaveService targetAmountSaveService;
private final RecentTargetAmountSearchService recentTargetAmountSearchService;
private final TargetAmountSearchService targetAmountSearchService;
private final TargetAmountDeleteService targetAmountDeleteService;

@Transactional
public TargetAmountDto.TargetAmountInfo createTargetAmount(Long userId, int year, int month) {
User user = userService.readUser(userId).orElseThrow(() -> new UserErrorException(UserErrorCode.NOT_FOUND));

TargetAmount targetAmount = targetAmountSaveService.createTargetAmount(DistributedLockPrefix.TARGET_AMOUNT_USER, user, LocalDate.of(year, month, 1));
TargetAmount targetAmount = targetAmountSaveService.createTargetAmount(DistributedLockPrefix.TARGET_AMOUNT_USER, userId, LocalDate.of(year, month, 1));

return TargetAmountDto.TargetAmountInfo.from(targetAmount);
}

@Transactional(readOnly = true)
public TargetAmountDto.WithTotalSpendingRes getTargetAmountAndTotalSpending(Long userId, LocalDate date) {
TargetAmount targetAmount = targetAmountService.readTargetAmountThatMonth(userId, date).orElseThrow(() -> new TargetAmountErrorException(TargetAmountErrorCode.NOT_FOUND_TARGET_AMOUNT));
Optional<TotalSpendingAmount> totalSpending = spendingService.readTotalSpendingAmountByUserId(userId, date);

return TargetAmountMapper.toWithTotalSpendingResponse(targetAmount, totalSpending.orElse(null), date);
return targetAmountSearchService.readTargetAmountAndTotalSpending(userId, date);
}

@Transactional(readOnly = true)
public List<TargetAmountDto.WithTotalSpendingRes> getTargetAmountsAndTotalSpendings(Long userId, LocalDate date) {
User user = userService.readUser(userId).orElseThrow(() -> new UserErrorException(UserErrorCode.NOT_FOUND));

List<TargetAmount> targetAmounts = targetAmountService.readTargetAmountsByUserId(userId);
List<TotalSpendingAmount> totalSpendings = spendingService.readTotalSpendingsAmountByUserId(userId);

return TargetAmountMapper.toWithTotalSpendingResponses(targetAmounts, totalSpendings, user.getCreatedAt().toLocalDate(), date);
return targetAmountSearchService.readTargetAmountsAndTotalSpendings(userId, date);
}

@Transactional(readOnly = true)
public TargetAmountDto.RecentTargetAmountRes getRecentTargetAmount(Long userId) {
return TargetAmountMapper.toRecentTargetAmountResponse(recentTargetAmountSearchService.readRecentTargetAmount(userId));
return TargetAmountMapper.toRecentTargetAmountResponse(targetAmountSearchService.readRecentTargetAmount(userId));
}

@Transactional
public TargetAmountDto.TargetAmountInfo updateTargetAmount(Long targetAmountId, Integer amount) {
TargetAmount targetAmount = targetAmountService.readTargetAmount(targetAmountId)
.orElseThrow(() -> new TargetAmountErrorException(TargetAmountErrorCode.NOT_FOUND_TARGET_AMOUNT));

if (!targetAmount.isThatMonth()) {
throw new TargetAmountErrorException(TargetAmountErrorCode.INVALID_TARGET_AMOUNT_DATE);
}

targetAmount.updateAmount(amount);
TargetAmount targetAmount = targetAmountSaveService.updateTargetAmount(targetAmountId, amount);

return TargetAmountDto.TargetAmountInfo.from(targetAmount);
}

@Transactional
public void deleteTargetAmount(Long targetAmountId) {
TargetAmount targetAmount = targetAmountService.readTargetAmount(targetAmountId)
.filter(TargetAmount::isAllocatedAmount)
.orElseThrow(() -> new TargetAmountErrorException(TargetAmountErrorCode.NOT_FOUND_TARGET_AMOUNT));

if (!targetAmount.isThatMonth()) {
throw new TargetAmountErrorException(TargetAmountErrorCode.INVALID_TARGET_AMOUNT_DATE);
}

targetAmountService.deleteTargetAmount(targetAmount);
targetAmountDeleteService.execute(targetAmountId);
}
}
Loading