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

[feat] 이벤트 유저를 다양한 조건으로 검색할 수 있게 개선(#135) #139

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ dependencies {

// sms api
implementation 'net.nurigo:sdk:4.3.0'

// queryDsl
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}: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
Expand Up @@ -33,10 +33,11 @@ public class AdminEventUserController {
@GetMapping
public ResponseEntity<EventUserPageDto> getEventUsers(
@RequestParam(name="search", required = false, defaultValue = "") String search,
@RequestParam(name="field", required = false, defaultValue = "userName") String field,
@RequestParam(name="page", required = false, defaultValue = "0") @Min(0) Integer page,
@RequestParam(name="size", required = false, defaultValue = "10") @Min(1) Integer size
) {
var userPageDto = eventUserService.getUserBySearch(search, page, size);
var userPageDto = eventUserService.getUserBySearch(search, field, page, size);
return ResponseEntity.ok(userPageDto);
}

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/hyundai/softeer/orange/config/QueryDSLConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package hyundai.softeer.orange.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);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package hyundai.softeer.orange.eventuser.repository;

import hyundai.softeer.orange.eventuser.dto.EventUserScoreDto;
import hyundai.softeer.orange.eventuser.entity.EventUser;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.Collection;
import java.util.List;

public interface CustomEventUserRepository {
void updateScoreMany(List<EventUserScoreDto> dto);
Page<EventUser> findBySearch(String search, String field, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package hyundai.softeer.orange.eventuser.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import hyundai.softeer.orange.event.common.entity.QEventFrame;
import hyundai.softeer.orange.eventuser.dto.EventUserScoreDto;
import hyundai.softeer.orange.eventuser.entity.EventUser;
import hyundai.softeer.orange.eventuser.entity.QEventUser;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
Expand All @@ -14,6 +21,7 @@
@Repository
public class CustomEventUserRepositoryImpl implements CustomEventUserRepository {
private final JdbcTemplate jdbcTemplate;
private final JPAQueryFactory queryFactory;

@Override
public void updateScoreMany(List<EventUserScoreDto> userScores) {
Expand All @@ -33,4 +41,22 @@ public int getBatchSize() {
}
});
}

@Override
public Page<EventUser> findBySearch(String search, String field, Pageable pageable) {
QEventUser user = QEventUser.eventUser;
QEventFrame eventFrame = QEventFrame.eventFrame;

var query = queryFactory.select(user)
.from(user)
.leftJoin(user.eventFrame, eventFrame)
.fetchJoin();

if("userName".equals(field)) query.where(user.userName.contains(search));
else if("phoneNumber".equals(field))query.where(user.phoneNumber.contains(search));
else if("frameId".equals(field)) query.where(user.eventFrame.frameId.contains(search));

var data = query.offset(pageable.getOffset()).limit(pageable.getPageSize()).fetch();
return new PageImpl<>(data, pageable, data.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
import hyundai.softeer.orange.eventuser.entity.EventUser;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.FluentQuery;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface EventUserRepository extends JpaRepository<EventUser, Long>, CustomEventUserRepository {
public interface EventUserRepository extends JpaRepository<EventUser, Long>, CustomEventUserRepository, JpaSpecificationExecutor<EventUser> {

Optional<EventUser> findByUserNameAndPhoneNumber(String userName, String phoneNumber);

Expand All @@ -32,4 +36,4 @@ public interface EventUserRepository extends JpaRepository<EventUser, Long>, Cus
"JOIN event_metadata e ON e.event_frame_id = ef.id " +
"WHERE e.id = :rawEventId", nativeQuery = true)
List<EventUserScoreDto> findAllUserScoreByDrawEventId(@Param("rawEventId") long rawEventId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import hyundai.softeer.orange.core.jwt.JWTManager;
import hyundai.softeer.orange.event.common.entity.EventFrame;
import hyundai.softeer.orange.event.common.repository.EventFrameRepository;
import hyundai.softeer.orange.eventuser.dto.EventUserOnAdminDto;
import hyundai.softeer.orange.eventuser.dto.EventUserPageDto;
import hyundai.softeer.orange.eventuser.dto.RequestAuthCodeDto;
import hyundai.softeer.orange.eventuser.dto.RequestUserDto;
Expand All @@ -20,7 +19,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -53,10 +51,10 @@ public TokenDto login(RequestUserDto dto) {
}

@Transactional(readOnly = true)
public EventUserPageDto getUserBySearch(String search, int page, int size) {
public EventUserPageDto getUserBySearch(String search, String field, int page, int size) {
PageRequest pageRequest = PageRequest.of(page, size);
Page<EventUser> userPage = eventUserRepository.findBySearch(search, field, pageRequest);

Page<EventUser> userPage = eventUserRepository.findBySearch(search, pageRequest);
return EventUserPageDto.from(userPage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.testcontainers.junit.jupiter.Testcontainers;

Expand All @@ -16,5 +17,6 @@
@Testcontainers
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Import(TestConfig.class)
public @interface TCDataJpaTest {
}
18 changes: 18 additions & 0 deletions src/test/java/hyundai/softeer/orange/support/TestConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package hyundai.softeer.orange.support;

import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;

@TestConfiguration
public class TestConfig {
@PersistenceContext
private EntityManager entityManager;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QueryDSL의 경우 JPA persistence layer에 속해있지 않아 DataJPATest을 이용할 때 자동으로 bean으로 등록되지 않는다고 합니다. 이로 인해 DataJPATest에서 필요로 하는 QueryDSL 관련 Bean이 없는 상태가 되어 NoSuchBeanDefinitionException이 발생한다고 하네요. 이를 방지하기 위해 test configuration으로 설정을 만들어줬습니다.

참고 경로입니다.

https://velog.io/@gloz0315/QueryDSL-Test-%EC%BD%94%EB%93%9C-%EB%AC%B8%EC%A0%9C-%EB%B0%8F-%ED%95%B4%EA%B2%B0


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