From ed8e97136886da9f1f8063c90b520d335c5582fa Mon Sep 17 00:00:00 2001 From: s-choi Date: Fri, 18 Sep 2026 11:11:02 +0900 Subject: [PATCH] =?UTF-8?q?[EDIT]=20=E5=8F=8E=E5=85=A5=E3=83=BB=E6=94=AF?= =?UTF-8?q?=E5=87=BA=E3=81=AE=E4=B8=80=E8=A6=A7=E3=82=92=E5=8F=96=E5=BE=97?= =?UTF-8?q?=E3=81=99=E3=82=8B=E6=A9=9F=E8=83=BD=E8=BF=BD=E5=8A=A0=E3=81=97?= =?UTF-8?q?=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/service/TransactionService.java | 10 +++- .../service/impl/TransactionServiceImpl.java | 54 ++++++++++++++++--- .../repository/TransactionRepository.java | 18 +++++-- 3 files changed, 69 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/example/demo/app/domain/service/TransactionService.java b/src/main/java/com/example/demo/app/domain/service/TransactionService.java index 84edc52..ab4aa12 100644 --- a/src/main/java/com/example/demo/app/domain/service/TransactionService.java +++ b/src/main/java/com/example/demo/app/domain/service/TransactionService.java @@ -5,6 +5,8 @@ import java.util.List; import java.util.Map; import com.example.demo.app.domain.model.TransactionModel; +import com.example.demo.app.presentation.form.Expense; +import com.example.demo.app.presentation.form.Income; public interface TransactionService { @@ -14,10 +16,16 @@ public interface TransactionService { public int findTotalIncome(LocalDate startDate, LocalDate endDate, Long userId); - public int findTotalexpense(LocalDate startDate, LocalDate endDate, Long userId); + public int findTotalExpense(LocalDate startDate, LocalDate endDate, Long userId); public int createTransaction(TransactionModel transactionModel); public int updateTransaction(TransactionModel transactionModel); + public void deleteTransaction(Long transactionId); + + public List findTotalIncomeList(LocalDate startDate, LocalDate endDate, Long userId); + + public List findTotalExpenseList(LocalDate startDate, LocalDate endDate, Long userId); + } diff --git a/src/main/java/com/example/demo/app/domain/service/impl/TransactionServiceImpl.java b/src/main/java/com/example/demo/app/domain/service/impl/TransactionServiceImpl.java index d33dc25..9467f9c 100644 --- a/src/main/java/com/example/demo/app/domain/service/impl/TransactionServiceImpl.java +++ b/src/main/java/com/example/demo/app/domain/service/impl/TransactionServiceImpl.java @@ -16,6 +16,8 @@ import com.example.demo.app.infra.entity.UserEntity; import com.example.demo.app.infra.repository.CategoryRepository; import com.example.demo.app.infra.repository.TransactionRepository; import com.example.demo.app.infra.repository.UserRepository; +import com.example.demo.app.presentation.form.Expense; +import com.example.demo.app.presentation.form.Income; import lombok.RequiredArgsConstructor; @@ -27,19 +29,38 @@ public class TransactionServiceImpl implements TransactionService { private final UserRepository userRepository; private final CategoryRepository categoryRepository; - public Map> findAllByDate(LocalDate startDate, LocalDate endDate, Long userId) { + public Map> findAllByDate( + LocalDate startDate, + LocalDate endDate, + Long userId) { + + System.out.println("========== findAllByDate =========="); + System.out.println("startDate = " + startDate); + System.out.println("endDate = " + endDate); + System.out.println("userId = " + userId); List transactionEntityList = transactionRepository.findAllByDateAndUserId(startDate, endDate, userId); - List transactionModelList = TransactionModel - .convertFrom(transactionEntityList); + System.out.println("Entity count = " + transactionEntityList.size()); + + transactionEntityList.forEach(t -> { + System.out.println( + "date = " + t.getTransactionDate() + + ", amount = " + t.getAmount()); + }); + + List transactionModelList = TransactionModel.convertFrom(transactionEntityList); + + System.out.println("Model count = " + transactionModelList.size()); Map> transactionModelMap = transactionModelList.stream() - .collect(Collectors.groupingBy(TransactionModel::getTransactionDate)); + .collect(Collectors.groupingBy( + TransactionModel::getTransactionDate)); + + System.out.println("Map = " + transactionModelMap); return transactionModelMap; - } public int findTotalIncome(LocalDate startDate, LocalDate endDate, Long userId) { @@ -48,9 +69,21 @@ public class TransactionServiceImpl implements TransactionService { } - public int findTotalexpense(LocalDate startDate, LocalDate endDate, Long userId) { + public int findTotalExpense(LocalDate startDate, LocalDate endDate, Long userId) { - return transactionRepository.findTotalIncome(startDate, endDate, userId); + return transactionRepository.findTotalExpense(startDate, endDate, userId); + + } + + public List findTotalIncomeList(LocalDate startDate, LocalDate endDate, Long userId) { + + return transactionRepository.findTotalIncomeList(startDate, endDate, userId); + + } + + public List findTotalExpenseList(LocalDate startDate, LocalDate endDate, Long userId) { + + return transactionRepository.findTotalExpenseList(startDate, endDate, userId); } @@ -91,7 +124,7 @@ public class TransactionServiceImpl implements TransactionService { if (!transactionRepository.existsById(transactionModel.getTransactionId())) { return -1; } - + Optional userEntityOptional = userRepository.findById(transactionModel.getUserId()); if (userEntityOptional.isEmpty()) { @@ -115,4 +148,9 @@ public class TransactionServiceImpl implements TransactionService { } } + + public void deleteTransaction(Long transactionId) { + transactionRepository.deleteById(transactionId); + } + } diff --git a/src/main/java/com/example/demo/app/infra/repository/TransactionRepository.java b/src/main/java/com/example/demo/app/infra/repository/TransactionRepository.java index 5fe10a1..e68e169 100644 --- a/src/main/java/com/example/demo/app/infra/repository/TransactionRepository.java +++ b/src/main/java/com/example/demo/app/infra/repository/TransactionRepository.java @@ -7,21 +7,31 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import com.example.demo.app.infra.entity.TransactionEntity; +import com.example.demo.app.presentation.form.Expense; +import com.example.demo.app.presentation.form.Income; public interface TransactionRepository extends JpaRepository { - @Query("select t from TransactionEntity t where t.userEntity.userId = :userId and t.transactionDate = :transactionDate") + @Query("select t from TransactionEntity t where t.userEntity.userId = :userId and t.transactionDate = :todayDate") public List findByUserIdAndDate(LocalDate todayDate, Long userId); @Query("select t from TransactionEntity t where (t.transactionDate between :startDate and :endDate) and (t.userEntity.userId = :userId) order by t.transactionDate asc") public List findAllByDateAndUserId(LocalDate startDate, LocalDate endDate, Long userId); - @Query("select sum(t.amount) from TransactionEntity t inner join CategoryEntity c on t.categoryEntity.categoryId = c.categoryId where (t.userEntity.userId = :userId and t.transactionDate between :startDate and :endDate) and c.transactionType = 0") + @Query("select coalesce(sum(t.amount), 0) from TransactionEntity t inner join CategoryEntity c on t.categoryEntity.categoryId = c.categoryId where (t.userEntity.userId = :userId and t.transactionDate between :startDate and :endDate) and c.transactionType = 0") public Integer findTotalExpense(LocalDate startDate, LocalDate endDate, Long userId); - @Query("select sum(t.amount) from TransactionEntity t inner join CategoryEntity c on t.categoryEntity.categoryId = c.categoryId where (t.userEntity.userId = :userId and t.transactionDate between :startDate and :endDate) and c.transactionType = 1") + @Query("select coalesce(sum(t.amount), 0) from TransactionEntity t inner join CategoryEntity c on t.categoryEntity.categoryId = c.categoryId where (t.userEntity.userId = :userId and t.transactionDate between :startDate and :endDate) and c.transactionType = 1") public Integer findTotalIncome(LocalDate startDate, LocalDate endDate, Long userId); - @Query("select sum(t.amount) from TransactionEntity t where t.userEntity.userId = :userId and t.categoryEntity.categoryId = :categoryId and t.transactionDate between :startDate and :endDate") + @Query("select coalesce(sum(t.amount), 0) from TransactionEntity t where t.userEntity.userId = :userId and t.categoryEntity.categoryId = :categoryId and t.transactionDate between :startDate and :endDate") public Integer findCategoryExpense(LocalDate startDate, LocalDate endDate, Long userId, Long categoryId); + + @Query("select new com.example.demo.app.presentation.form.Expense(t.transactionDate, coalesce(sum(t.amount), 0)) from TransactionEntity t inner join CategoryEntity c on t.categoryEntity.categoryId = c.categoryId where (t.userEntity.userId = :userId and t.transactionDate between :startDate and :endDate) and c.transactionType = 0 group by t.transactionDate order by t.transactionDate asc") + public List findTotalExpenseList(LocalDate startDate, LocalDate endDate, Long userId); + + @Query("select new com.example.demo.app.presentation.form.Income(t.transactionDate, coalesce(sum(t.amount), 0)) from TransactionEntity t inner join CategoryEntity c on t.categoryEntity.categoryId = c.categoryId where (t.userEntity.userId = :userId and t.transactionDate between :startDate and :endDate) and c.transactionType = 1 group by t.transactionDate order by t.transactionDate asc") + public List findTotalIncomeList(LocalDate startDate, LocalDate endDate, Long userId); + + }