Skip to content

Commit

Permalink
feat: 기타(GENERAL) 소속은 모든 유저에게 노출되도록 한다. (#12)
Browse files Browse the repository at this point in the history
* feat: 기타(GENERAL) 소속은 모든 유저에게 노출되도록 한다.

* refactor: nullSafeBooleanBuilder로 NPE를 방지한다.
  • Loading branch information
rlarltj authored Jul 19, 2024
1 parent 114fa32 commit 4e52120
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

public enum AgencyType {
STUDENT_COUNCIL,
IN_SCHOOL_CLUB
IN_SCHOOL_CLUB,
GENERAL,
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.moneymong.domain.agency.repository;

import java.util.List;

import com.moneymong.domain.agency.entity.Agency;
import com.moneymong.domain.agency.entity.enums.AgencyType;

import org.springframework.data.jpa.repository.JpaRepository;

public interface AgencyRepository extends JpaRepository<Agency, Long>, AgencyRepositoryCustom {
List<Agency> findAgenciesByAgencyType(AgencyType agencyType);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.moneymong.domain.agency.repository;

import com.moneymong.domain.agency.entity.Agency;
import com.moneymong.domain.agency.entity.enums.AgencyType;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface AgencyRepositoryCustom {
Page<Agency> findByUniversityNameByPaging(String universityName, Pageable pageable);

Page<Agency> findByUniversityNameAndAgencyTypeByPaging(String universityName, AgencyType type, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.moneymong.domain.agency.repository;

import com.moneymong.domain.agency.entity.Agency;
import com.moneymong.domain.agency.entity.enums.AgencyType;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -11,6 +13,7 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.function.Supplier;

import static com.moneymong.domain.agency.entity.QAgency.agency;

Expand All @@ -33,12 +36,40 @@ public Page<Agency> findByUniversityNameByPaging(String universityName, Pageable
return PageableExecutionUtils.getPage(result, pageable, () -> countQuery.fetch().size());
}

@Override
public Page<Agency> findByUniversityNameAndAgencyTypeByPaging(String universityName, AgencyType type,
Pageable pageable) {
JPAQuery<Agency> query = queryFactory.selectFrom(agency)
.where(agency.agencyType.eq(type).or(eqUniversityName(universityName)))
.limit(pageable.getPageSize())
.offset(pageable.getOffset());

List<Agency> result = query.fetch();
JPAQuery<Agency> countQuery = getCountQuery(universityName, type);

return PageableExecutionUtils.getPage(result, pageable, () -> countQuery.fetch().size());

}

private JPAQuery<Agency> getCountQuery(String universityName) {
return queryFactory.selectFrom(agency)
.where(eqUniversityName(universityName));
}

private BooleanExpression eqUniversityName(String universityName) {
return universityName != null ? agency.universityName.eq(universityName) : null;
private JPAQuery<Agency> getCountQuery(String universityName, AgencyType type) {
return queryFactory.selectFrom(agency)
.where(eqUniversityName(universityName).or(agency.agencyType.eq(type)));
}

private BooleanBuilder eqUniversityName(String universityName) {
return nullSafeBooleanBuilder(() -> agency.universityName.eq(universityName));
}

private BooleanBuilder nullSafeBooleanBuilder(Supplier<BooleanExpression> supplier) {
try {
return new BooleanBuilder(supplier.get());
} catch (IllegalArgumentException e) {
return new BooleanBuilder();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.moneymong.domain.agency.api.response.*;
import com.moneymong.domain.agency.entity.Agency;
import com.moneymong.domain.agency.entity.AgencyUser;
import com.moneymong.domain.agency.entity.enums.AgencyType;
import com.moneymong.domain.agency.entity.enums.AgencyUserRole;
import com.moneymong.domain.agency.exception.BlockedAgencyUserException;
import com.moneymong.domain.agency.repository.AgencyRepository;
Expand All @@ -29,6 +30,7 @@

import java.util.List;

import static com.moneymong.domain.agency.entity.enums.AgencyType.GENERAL;
import static com.moneymong.domain.agency.entity.enums.AgencyUserRole.*;

@Service
Expand All @@ -47,16 +49,18 @@ public class AgencyService {
public SearchAgencyResponse getAgencyList(Long userId, Pageable pageable) {
String universityName = getUniversityName(userId);

Page<Agency> findByUniversityNameResult = agencyRepository.findByUniversityNameByPaging(universityName,
pageable);
Page<Agency> findByUniversityNameResult = agencyRepository.findByUniversityNameAndAgencyTypeByPaging(
universityName,
GENERAL,
pageable);

long totalCount = findByUniversityNameResult.getTotalElements();

List<AgencyResponse> responseList = findByUniversityNameResult.stream()
List<AgencyResponse> responses = findByUniversityNameResult.stream()
.map(AgencyResponse::from)
.toList();

return new SearchAgencyResponse(responseList, totalCount);
return new SearchAgencyResponse(responses, totalCount);
}

@Transactional
Expand Down Expand Up @@ -87,10 +91,9 @@ public CreateAgencyResponse create(Long userId, CreateAgencyRequest request) {
}

private String getUniversityName(Long userId) {
UserUniversity university = userUniversityRepository.findByUserId(userId)
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_UNIVERSITY_NOT_FOUND));

return university.getUniversityName();
return userUniversityRepository.findByUserId(userId)
.map(UserUniversity::getUniversityName)
.orElse(null);
}

public AgencyUserResponses getAgencyUserList(Long userId, Long agencyId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
public class UserService {

public static final String DEFAULT_ROLE = "ROLE_USER";
public static final String DEFAULT_NICKNAME = "유저";

private final UserRepository userRepository;
private final UserUniversityRepository userUniversityRepository;
Expand All @@ -49,7 +50,7 @@ public User save(User unsavedUser) {
public User registerUser(OAuthUserInfo oauthUserInfo) {
User newUser = User.of(
oauthUserInfo.getEmail(),
oauthUserInfo.getNickname() == null ? "유저" : oauthUserInfo.getNickname(),
oauthUserInfo.getNickname() == null ? DEFAULT_NICKNAME : oauthUserInfo.getNickname(),
oauthUserInfo.getProvider(),
oauthUserInfo.getOauthId()
);
Expand Down

0 comments on commit 4e52120

Please sign in to comment.