diff --git a/src/main/java/com/moneymong/domain/agency/entity/enums/AgencyType.java b/src/main/java/com/moneymong/domain/agency/entity/enums/AgencyType.java index f2e05ef..0235edb 100644 --- a/src/main/java/com/moneymong/domain/agency/entity/enums/AgencyType.java +++ b/src/main/java/com/moneymong/domain/agency/entity/enums/AgencyType.java @@ -2,5 +2,6 @@ public enum AgencyType { STUDENT_COUNCIL, - IN_SCHOOL_CLUB + IN_SCHOOL_CLUB, + GENERAL, } diff --git a/src/main/java/com/moneymong/domain/agency/repository/AgencyRepository.java b/src/main/java/com/moneymong/domain/agency/repository/AgencyRepository.java index 655b7af..0079234 100644 --- a/src/main/java/com/moneymong/domain/agency/repository/AgencyRepository.java +++ b/src/main/java/com/moneymong/domain/agency/repository/AgencyRepository.java @@ -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, AgencyRepositoryCustom { + List findAgenciesByAgencyType(AgencyType agencyType); } diff --git a/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryCustom.java b/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryCustom.java index 612b288..2c87a96 100644 --- a/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryCustom.java +++ b/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryCustom.java @@ -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 findByUniversityNameByPaging(String universityName, Pageable pageable); + + Page findByUniversityNameAndAgencyTypeByPaging(String universityName, AgencyType type, Pageable pageable); } diff --git a/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryImpl.java b/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryImpl.java index 7885b90..bb53f58 100644 --- a/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryImpl.java +++ b/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryImpl.java @@ -1,6 +1,7 @@ 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.types.dsl.BooleanExpression; import com.querydsl.jpa.impl.JPAQuery; import com.querydsl.jpa.impl.JPAQueryFactory; @@ -33,11 +34,30 @@ public Page findByUniversityNameByPaging(String universityName, Pageable return PageableExecutionUtils.getPage(result, pageable, () -> countQuery.fetch().size()); } + @Override + public Page findByUniversityNameAndAgencyTypeByPaging(String universityName, AgencyType type, + Pageable pageable) { + JPAQuery query = queryFactory.selectFrom(agency) + .where(eqUniversityName(universityName).or(agency.agencyType.eq(type))) + .limit(pageable.getPageSize()) + .offset(pageable.getOffset()); + + List result = query.fetch(); + JPAQuery countQuery = getCountQuery(universityName, type); + + return PageableExecutionUtils.getPage(result, pageable, () -> countQuery.fetch().size()); + + } + private JPAQuery getCountQuery(String universityName) { return queryFactory.selectFrom(agency) .where(eqUniversityName(universityName)); } + private JPAQuery getCountQuery(String universityName, AgencyType type) { + return queryFactory.selectFrom(agency) + .where(eqUniversityName(universityName).or(agency.agencyType.eq(type))); + } private BooleanExpression eqUniversityName(String universityName) { return universityName != null ? agency.universityName.eq(universityName) : null; } diff --git a/src/main/java/com/moneymong/domain/agency/service/AgencyService.java b/src/main/java/com/moneymong/domain/agency/service/AgencyService.java index aa476a8..1bbcf28 100644 --- a/src/main/java/com/moneymong/domain/agency/service/AgencyService.java +++ b/src/main/java/com/moneymong/domain/agency/service/AgencyService.java @@ -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; @@ -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 @@ -47,16 +49,18 @@ public class AgencyService { public SearchAgencyResponse getAgencyList(Long userId, Pageable pageable) { String universityName = getUniversityName(userId); - Page findByUniversityNameResult = agencyRepository.findByUniversityNameByPaging(universityName, - pageable); + Page findByUniversityNameResult = agencyRepository.findByUniversityNameAndAgencyTypeByPaging( + universityName, + GENERAL, + pageable); long totalCount = findByUniversityNameResult.getTotalElements(); - List responseList = findByUniversityNameResult.stream() + List responses = findByUniversityNameResult.stream() .map(AgencyResponse::from) .toList(); - return new SearchAgencyResponse(responseList, totalCount); + return new SearchAgencyResponse(responses, totalCount); } @Transactional @@ -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) { diff --git a/src/main/java/com/moneymong/domain/user/service/UserService.java b/src/main/java/com/moneymong/domain/user/service/UserService.java index 959a8ba..68c07d2 100644 --- a/src/main/java/com/moneymong/domain/user/service/UserService.java +++ b/src/main/java/com/moneymong/domain/user/service/UserService.java @@ -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; @@ -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() );