[EDIT] 収入・支出の一覧を取得する機能追加しました。

This commit is contained in:
s-choi
2026-09-18 11:11:02 +09:00
parent 8f3ea4cd26
commit ed8e971368
3 changed files with 69 additions and 13 deletions

View File

@@ -5,6 +5,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import com.example.demo.app.domain.model.TransactionModel; 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 { public interface TransactionService {
@@ -14,10 +16,16 @@ public interface TransactionService {
public int findTotalIncome(LocalDate startDate, LocalDate endDate, Long userId); 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 createTransaction(TransactionModel transactionModel);
public int updateTransaction(TransactionModel transactionModel); public int updateTransaction(TransactionModel transactionModel);
public void deleteTransaction(Long transactionId);
public List<Income> findTotalIncomeList(LocalDate startDate, LocalDate endDate, Long userId);
public List<Expense> findTotalExpenseList(LocalDate startDate, LocalDate endDate, Long userId);
} }

View File

@@ -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.CategoryRepository;
import com.example.demo.app.infra.repository.TransactionRepository; import com.example.demo.app.infra.repository.TransactionRepository;
import com.example.demo.app.infra.repository.UserRepository; 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; import lombok.RequiredArgsConstructor;
@@ -27,19 +29,38 @@ public class TransactionServiceImpl implements TransactionService {
private final UserRepository userRepository; private final UserRepository userRepository;
private final CategoryRepository categoryRepository; private final CategoryRepository categoryRepository;
public Map<LocalDate, List<TransactionModel>> findAllByDate(LocalDate startDate, LocalDate endDate, Long userId) { public Map<LocalDate, List<TransactionModel>> 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<TransactionEntity> transactionEntityList = transactionRepository.findAllByDateAndUserId(startDate, endDate, List<TransactionEntity> transactionEntityList = transactionRepository.findAllByDateAndUserId(startDate, endDate,
userId); userId);
List<TransactionModel> transactionModelList = TransactionModel System.out.println("Entity count = " + transactionEntityList.size());
.convertFrom(transactionEntityList);
transactionEntityList.forEach(t -> {
System.out.println(
"date = " + t.getTransactionDate()
+ ", amount = " + t.getAmount());
});
List<TransactionModel> transactionModelList = TransactionModel.convertFrom(transactionEntityList);
System.out.println("Model count = " + transactionModelList.size());
Map<LocalDate, List<TransactionModel>> transactionModelMap = transactionModelList.stream() Map<LocalDate, List<TransactionModel>> transactionModelMap = transactionModelList.stream()
.collect(Collectors.groupingBy(TransactionModel::getTransactionDate)); .collect(Collectors.groupingBy(
TransactionModel::getTransactionDate));
System.out.println("Map = " + transactionModelMap);
return transactionModelMap; return transactionModelMap;
} }
public int findTotalIncome(LocalDate startDate, LocalDate endDate, Long userId) { 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<Income> findTotalIncomeList(LocalDate startDate, LocalDate endDate, Long userId) {
return transactionRepository.findTotalIncomeList(startDate, endDate, userId);
}
public List<Expense> findTotalExpenseList(LocalDate startDate, LocalDate endDate, Long userId) {
return transactionRepository.findTotalExpenseList(startDate, endDate, userId);
} }
@@ -115,4 +148,9 @@ public class TransactionServiceImpl implements TransactionService {
} }
} }
public void deleteTransaction(Long transactionId) {
transactionRepository.deleteById(transactionId);
}
} }

View File

@@ -7,21 +7,31 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import com.example.demo.app.infra.entity.TransactionEntity; 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<TransactionEntity, Long> { public interface TransactionRepository extends JpaRepository<TransactionEntity, Long> {
@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<TransactionEntity> findByUserIdAndDate(LocalDate todayDate, Long userId); public List<TransactionEntity> 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") @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<TransactionEntity> findAllByDateAndUserId(LocalDate startDate, LocalDate endDate, Long userId); public List<TransactionEntity> 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); 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); 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); 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<Expense> 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<Income> findTotalIncomeList(LocalDate startDate, LocalDate endDate, Long userId);
} }