22
33import apptive .team5 .user .domain .UserEntity ;
44import com .querydsl .core .types .dsl .BooleanExpression ;
5+ import com .querydsl .jpa .JPAExpressions ;
56import com .querydsl .jpa .impl .JPAQuery ;
67import com .querydsl .jpa .impl .JPAQueryFactory ;
78import jakarta .persistence .EntityManager ;
1213import org .springframework .transaction .annotation .Transactional ;
1314
1415import java .util .List ;
16+ import java .util .Set ;
1517
18+ import static apptive .team5 .diary .domain .QDiaryLikeEntity .diaryLikeEntity ;
1619import static apptive .team5 .user .domain .QUserEntity .userEntity ;
1720
1821@ Transactional
@@ -25,27 +28,37 @@ public QUserRepository(EntityManager entityManager) {
2528 this .queryFactory = new JPAQueryFactory (entityManager );
2629 }
2730
28- public Page <UserEntity > findByTagOrUsername (String searchCond , Pageable pageable ) {
31+ public Page <UserEntity > findByTagOrUsernameExcludingBlocked (Set <Long > blockedUserIds , String searchCond , Pageable pageable ) {
32+ BooleanExpression searchCondition = tagLike (searchCond ).or (usernameLike (searchCond ));
33+ BooleanExpression notBlockedCondition = notInBlockedUserIds (blockedUserIds );
2934
3035 List <UserEntity > content = queryFactory
3136 .selectFrom (userEntity )
32- .where (tagLike ( searchCond ). or ( usernameLike ( searchCond )) )
37+ .where (searchCondition , notBlockedCondition )
3338 .offset (pageable .getOffset ())
3439 .limit (pageable .getPageSize ())
3540 .fetch ();
3641
37- JPAQuery <Long > countQuery = queryFactory .select (userEntity .count ())
38- .where (tagLike (searchCond ).or (usernameLike (searchCond )))
39- .from (userEntity );
42+ JPAQuery <Long > countQuery = queryFactory
43+ .select (userEntity .count ())
44+ .from (userEntity )
45+ .where (searchCondition , notBlockedCondition );
4046
4147 return PageableExecutionUtils .getPage (content , pageable , countQuery ::fetchOne );
4248 }
4349
50+ private BooleanExpression notInBlockedUserIds (Set <Long > blockedUserIds ) {
51+ if (blockedUserIds == null || blockedUserIds .isEmpty ()) {
52+ return null ;
53+ }
54+ return userEntity .id .notIn (blockedUserIds );
55+ }
56+
4457 private BooleanExpression tagLike (String tag ) {
45- return tag != null ? userEntity .tag .like ("%" + tag + "%" ) : null ;
58+ return tag != null ? userEntity .tag .like ("%" + tag + "%" ) : null ;
4659 }
4760
4861 private BooleanExpression usernameLike (String username ) {
49- return username != null ? userEntity .username .like ("%" + username + "%" ) : null ;
62+ return username != null ? userEntity .username .like ("%" + username + "%" ) : null ;
5063 }
5164}
0 commit comments