Skip to content

Commit

Permalink
Merge pull request #53 from HanaPiece/feat/query-dsl-set
Browse files Browse the repository at this point in the history
feat/#16 QueryDSL 설정
  • Loading branch information
duddn2012 authored Jun 5, 2024
2 parents f908990 + d82aed3 commit ab56c02
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'com.google.code.gson:gson:2.7'
implementation 'com.google.guava:guava:11.0.2'

compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
implementation 'org.openapitools:jackson-databind-nullable:0.2.4'
implementation 'io.jsonwebtoken:jjwt:0.9.1'
implementation 'javax.xml.bind:jaxb-api:2.3.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

//QueryDSL
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:5.0.0:jakarta"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.project.hana_piece.account.repository;

import static com.project.hana_piece.account.domain.QAccountTransaction.accountTransaction;

import com.project.hana_piece.account.domain.AccountTransaction;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class AccountTransactionRepositoryCustom {

private final JPAQueryFactory queryFactory;
public List<AccountTransaction> findDailyTransactionList(Long accountId, Integer transactionMonth){
return queryFactory.selectFrom(accountTransaction)
.where(accountTransaction.createdAt.month().eq(transactionMonth)
.and(accountTransaction.account.accountId.eq(accountId))
.and(accountTransaction.amount.lt(0)))
.orderBy(accountTransaction.createdAt.desc())
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.project.hana_piece.account.repository.AccountAutoDebitRepository;
import com.project.hana_piece.account.repository.AccountRepository;
import com.project.hana_piece.account.repository.AccountTransactionRepository;
import com.project.hana_piece.account.repository.AccountTransactionRepositoryCustom;
import com.project.hana_piece.goal.domain.UserGoal;
import com.project.hana_piece.goal.exception.UserGoalNotFoundException;
import com.project.hana_piece.goal.repository.UserGoalRepository;
Expand All @@ -50,6 +51,7 @@ public class AccountService {
private final AccountRepository accountRepository;
private final AccountAutoDebitRepository accountAutoDebitRepository;
private final AccountTransactionRepository accountTransactionRepository;
private final AccountTransactionRepositoryCustom accountTransactionRepositoryCustom;
private final UserRepository userRepository;
private final UserGoalRepository userGoalRepository;

Expand Down Expand Up @@ -156,7 +158,7 @@ public List<AccountTransactionGetResponse> findGoalAccountTransactionList(Long u
public AccountMonthTransactionGetResponse findAccountMonthTransactionList(Long userId, Long accountId, Integer transactionMonth) {
Account account = accountRepository.findById(accountId)
.orElseThrow(() -> new AccountNotFoundException(accountId));
List<AccountTransaction> dailyTransactionProjectionList = accountTransactionRepository.findDailyTransactionList(
List<AccountTransaction> dailyTransactionProjectionList = accountTransactionRepositoryCustom.findDailyTransactionList(
accountId, transactionMonth);
// Projection List -> DTO List
List<AccountDailyTransactionGetResponse> dailyTransactionList = dailyTransactionProjectionList.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.project.hana_piece.common.config;

import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
public class QueryDSLConfig {

private final EntityManager entityManager;

@Bean
public JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(entityManager);
}
}

0 comments on commit ab56c02

Please sign in to comment.