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 4d768bc..a65d524 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 @@ -1,9 +1,26 @@ package com.example.demo.app.infra.repository; +import java.time.LocalDate; +import java.util.List; + import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import com.example.demo.app.infra.entity.TransactionEntity; -public interface TransactionRepository extends JpaRepository{ +public interface TransactionRepository extends JpaRepository { + + @Query("select t from TransactionEntity t where t.userEntity.userId = :userId") + public TransactionEntity findByUserId(@Param("userId") Long userId); + + @Query("select t from TransactionEntity t where (t.transactionDate between :startDate and :endDate) and (t.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.categoryId = c.categoryId where (t.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.categoryId = c.categoryId where (t.userId = :userId and t.transactionDate between :startDate and :endDate) and c.transactionType = 1") + public Integer findTotalIncome(LocalDate startDate, LocalDate endDate, Long userId); }