diff --git a/src/main/java/com/sendback/domain/feedback/repository/FeedbackSubmitRepository.java b/src/main/java/com/sendback/domain/feedback/repository/FeedbackSubmitRepository.java index 698211e..2587024 100644 --- a/src/main/java/com/sendback/domain/feedback/repository/FeedbackSubmitRepository.java +++ b/src/main/java/com/sendback/domain/feedback/repository/FeedbackSubmitRepository.java @@ -11,6 +11,4 @@ public interface FeedbackSubmitRepository extends JpaRepository { Optional findByUserAndProject(User user, Project project); boolean existsByUserAndProjectAndIsDeletedIsFalse(User user, Project project); - Long countByProjectIn(List projects); + Long countByProjectInAndIsDeletedIsFalse(List projects); } diff --git a/src/main/java/com/sendback/domain/project/repository/ProjectRepository.java b/src/main/java/com/sendback/domain/project/repository/ProjectRepository.java index 601ad64..505f20c 100644 --- a/src/main/java/com/sendback/domain/project/repository/ProjectRepository.java +++ b/src/main/java/com/sendback/domain/project/repository/ProjectRepository.java @@ -1,14 +1,14 @@ package com.sendback.domain.project.repository; import com.sendback.domain.project.entity.Project; +import com.sendback.domain.user.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import java.time.LocalDate; import java.util.List; public interface ProjectRepository extends JpaRepository, ProjectRepositoryCustom { - Long countByUserId(Long userId); - List findByUserId(Long userId); - List findTop12ByOrderByLikeCountDesc(); + Long countByUserAndIsDeletedIsFalse(User user); + List findByUserAndIsDeletedIsFalse(User user); List findAllByEndedAtBeforeAndIsDeletedIsFalse(LocalDate localDate); } diff --git a/src/main/java/com/sendback/domain/project/repository/ProjectRepositoryCustom.java b/src/main/java/com/sendback/domain/project/repository/ProjectRepositoryCustom.java index 29c2567..172f3ef 100644 --- a/src/main/java/com/sendback/domain/project/repository/ProjectRepositoryCustom.java +++ b/src/main/java/com/sendback/domain/project/repository/ProjectRepositoryCustom.java @@ -7,7 +7,6 @@ import com.sendback.global.common.constants.FieldName; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; - import java.util.List; public interface ProjectRepositoryCustom { @@ -17,7 +16,7 @@ public interface ProjectRepositoryCustom { Page findAllScrappedProjectsByMe(Pageable pageable, Long userId, Boolean isFinished); List findRecommendedProjects(List fieldNameList, int size); - + List findRecommendedProjects(int size); Page findAllByPageableAndFieldAndIsFinishedAndSort( Pageable pageable, String keyword, String field, Boolean isFinished, Long sort); Page findAllSubmittedProjectsByMe(Pageable pageable, Long userId, Boolean isFinished); diff --git a/src/main/java/com/sendback/domain/project/repository/ProjectRepositoryImpl.java b/src/main/java/com/sendback/domain/project/repository/ProjectRepositoryImpl.java index 67055ef..a1edf3d 100644 --- a/src/main/java/com/sendback/domain/project/repository/ProjectRepositoryImpl.java +++ b/src/main/java/com/sendback/domain/project/repository/ProjectRepositoryImpl.java @@ -135,13 +135,29 @@ public List findRecommendedProjects(List filedNameList, int List content = queryFactory .selectFrom(project) .join(project.likes, like) - .groupBy(project) + .groupBy(project.id) .where( like.isDeleted.eq(false), project.isDeleted.eq(false), project.fieldName.in(filedNameList) ) - .orderBy(like.count().desc()) + .orderBy(project.id.count().desc()) + .limit(size) + .fetch(); + return content; + } + + @Override + public List findRecommendedProjects(int size) { + List content = queryFactory + .selectFrom(project) + .join(project.likes, like) + .groupBy(project.id) + .where( + like.isDeleted.eq(false), + project.isDeleted.eq(false) + ) + .orderBy(project.id.count().desc()) .limit(size) .fetch(); return content; diff --git a/src/main/java/com/sendback/domain/project/service/ProjectService.java b/src/main/java/com/sendback/domain/project/service/ProjectService.java index fb8275f..4d3c063 100644 --- a/src/main/java/com/sendback/domain/project/service/ProjectService.java +++ b/src/main/java/com/sendback/domain/project/service/ProjectService.java @@ -31,12 +31,10 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.Optional; import static com.sendback.domain.project.exception.ProjectExceptionType.*; -import static com.sendback.global.common.constants.FieldName.*; @Service @RequiredArgsConstructor @@ -124,10 +122,14 @@ public List getRecommendedProject(Long userId){ List fieldNameList = fieldList.stream().map(Field::getName).collect(Collectors.toList()); List projects = projectRepository.findRecommendedProjects(fieldNameList, 12); responseDtos.addAll(projects.stream().map(project -> RecommendedProjectResponseDto.of(project)).collect(Collectors.toList())); + if(projects.size()<12) { + List extraProject = projectRepository.findRecommendedProjects(12 - projects.size()); + responseDtos.addAll(extraProject.stream().map(project -> RecommendedProjectResponseDto.of(project)).collect(Collectors.toList())); + } } else{ - List projects = projectRepository.findTop12ByOrderByLikeCountDesc(); - responseDtos = projects.stream().map(project -> RecommendedProjectResponseDto.of(project)).collect(Collectors.toList()); + List projects = projectRepository.findRecommendedProjects(12); + responseDtos.addAll(projects.stream().map(project -> RecommendedProjectResponseDto.of(project)).collect(Collectors.toList())); } return responseDtos; } diff --git a/src/main/java/com/sendback/domain/user/exception/UserExceptionType.java b/src/main/java/com/sendback/domain/user/exception/UserExceptionType.java index 15cb48a..409f795 100644 --- a/src/main/java/com/sendback/domain/user/exception/UserExceptionType.java +++ b/src/main/java/com/sendback/domain/user/exception/UserExceptionType.java @@ -13,7 +13,8 @@ public enum UserExceptionType implements ExceptionType { NOT_FOUND_LEVEL(2040, "유효하지 않은 level 형식입니다."), INVALID_NICKNAME(2050, "닉네임은 한글, 영어(2~8자) 이내로 입력해주세요."), NOT_FOUND_USER(2060, "유저를 찾을 수 없습니다."), - DUPLICATED_NICKNAME(2070, "중복된 유저 닉네임입니다."); + DUPLICATED_NICKNAME(2070, "중복된 유저 닉네임입니다."), + PREVIOUS_REGISTERED_USER(2080, "이전에 회원 가입한 내역이 있습니다."); private final int statusCode; private final String message; diff --git a/src/main/java/com/sendback/domain/user/service/UserService.java b/src/main/java/com/sendback/domain/user/service/UserService.java index 6fea64a..becdda2 100644 --- a/src/main/java/com/sendback/domain/user/service/UserService.java +++ b/src/main/java/com/sendback/domain/user/service/UserService.java @@ -30,7 +30,6 @@ import java.util.List; import java.util.Optional; import java.util.stream.Collectors; - import static com.sendback.domain.user.exception.UserExceptionType.*; @Service @@ -53,6 +52,9 @@ public Token signUpUser(@RequestBody SignUpRequestDto signUpRequestDto) { } jwtProvider.validateSignToken(signUpRequestDto.signToken()); SigningAccount signingAccount = jwtProvider.getSignUserInfo(signUpRequestDto.signToken()); +// if(userRepository.findBySocialId(signingAccount.socialId()).isPresent()){ +// throw new BadRequestException(PREVIOUS_REGISTERED_USER); +// } User user = User.of(signingAccount, signUpRequestDto); User savedUser = userRepository.save(user); List fieldList = signUpRequestDto.fields().stream() @@ -74,10 +76,10 @@ public UserInfoResponseDto getUserInfo(Long userId){ User user = userRepository.findById(userId).orElseThrow( () -> new NotFoundException(NOT_FOUND_USER) ); - Long projectCount = projectRepository.countByUserId(userId); - Long feedbackCount = feedbackSubmitRepository.countByUserId(userId); - List projectList = projectRepository.findByUserId(userId); - Long likeCount = likeRepository.countByProjectIn(projectList); + Long projectCount = projectRepository.countByUserAndIsDeletedIsFalse(user); + Long feedbackCount = feedbackSubmitRepository.countByUserAndIsDeletedIsFalse(user); + List projectList = projectRepository.findByUserAndIsDeletedIsFalse(user); + Long likeCount = likeRepository.countByProjectInAndIsDeletedIsFalse(projectList); List fieldList = fieldRepository.findAllByUserId(userId); List fieldNameList = fieldList.stream() .map(Field::getName) diff --git a/src/test/java/com/sendback/domain/feedback/repository/FeedbackSubmitRepositoryTest.java b/src/test/java/com/sendback/domain/feedback/repository/FeedbackSubmitRepositoryTest.java index 7e89959..5a468b0 100644 --- a/src/test/java/com/sendback/domain/feedback/repository/FeedbackSubmitRepositoryTest.java +++ b/src/test/java/com/sendback/domain/feedback/repository/FeedbackSubmitRepositoryTest.java @@ -2,6 +2,7 @@ import com.sendback.domain.feedback.entity.FeedbackSubmit; import com.sendback.global.RepositoryTest; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -59,7 +60,7 @@ public void success_true() { FeedbackSubmit feedbackSubmit = feedbackSubmitTestPersister.builder().save(); // when - Long feedBackCount = feedbackSubmitRepository.countByUserId(feedbackSubmit.getUser().getId()); + Long feedBackCount = feedbackSubmitRepository.countByUserAndIsDeletedIsFalse(feedbackSubmit.getUser()); // then assertThat(feedBackCount).isEqualTo(1); diff --git a/src/test/java/com/sendback/domain/like/repository/LikeRepositoryTest.java b/src/test/java/com/sendback/domain/like/repository/LikeRepositoryTest.java index 675dfe8..07285fc 100644 --- a/src/test/java/com/sendback/domain/like/repository/LikeRepositoryTest.java +++ b/src/test/java/com/sendback/domain/like/repository/LikeRepositoryTest.java @@ -99,7 +99,7 @@ public void success() { Like like = likeTestPersister.builder().user(user).project(project).save(); // when - Long feedBackCount = likeRepository.countByProjectIn(projectList); + Long feedBackCount = likeRepository.countByProjectInAndIsDeletedIsFalse(projectList); // then assertThat(feedBackCount).isEqualTo(1); diff --git a/src/test/java/com/sendback/domain/project/repository/ProjectRepositoryTest.java b/src/test/java/com/sendback/domain/project/repository/ProjectRepositoryTest.java index b74b3c4..65a4ac5 100644 --- a/src/test/java/com/sendback/domain/project/repository/ProjectRepositoryTest.java +++ b/src/test/java/com/sendback/domain/project/repository/ProjectRepositoryTest.java @@ -33,7 +33,7 @@ public void countByUserId() { Project project = projectTestPersister.builder().save(); //when - Long projectCount = projectRepository.countByUserId(project.getUser().getId()); + Long projectCount = projectRepository.countByUserAndIsDeletedIsFalse(project.getUser()); //then assertThat(projectCount).isEqualTo(1); @@ -46,7 +46,7 @@ public void findByUserId() { Project project = projectTestPersister.builder().save(); //when - List findProject = projectRepository.findByUserId(project.getUser().getId()); + List findProject = projectRepository.findByUserAndIsDeletedIsFalse(project.getUser()); //then assertThat(findProject.size()).isEqualTo(1); diff --git a/src/test/java/com/sendback/domain/user/service/UserServiceTest.java b/src/test/java/com/sendback/domain/user/service/UserServiceTest.java index b6ad6e8..bf4c7c1 100644 --- a/src/test/java/com/sendback/domain/user/service/UserServiceTest.java +++ b/src/test/java/com/sendback/domain/user/service/UserServiceTest.java @@ -178,12 +178,12 @@ void getUserInfo_success() { Long needToFeedbackCount = Level.getRemainCountUntilNextLevel(feedbackCount); given(userRepository.findById(mockUserId)).willReturn(Optional.of(user)); - given(projectRepository.countByUserId(mockUserId)).willReturn(projectCount); - given(feedbackSubmitRepository.countByUserId(mockUserId)).willReturn(feedbackCount); + given(projectRepository.countByUserAndIsDeletedIsFalse(user)).willReturn(projectCount); + given(feedbackSubmitRepository.countByUserAndIsDeletedIsFalse(user)).willReturn(feedbackCount); List projectList = new ArrayList<>(); projectList.add(project); - given(projectRepository.findByUserId(mockUserId)).willReturn(projectList); - given(likeRepository.countByProjectIn(projectList)).willReturn(likeCount); + given(projectRepository.findByUserAndIsDeletedIsFalse(user)).willReturn(projectList); + given(likeRepository.countByProjectInAndIsDeletedIsFalse(projectList)).willReturn(likeCount); given(fieldRepository.findAllByUserId(mockUserId)).willReturn(mock_Fields); List mock_fieldNameList = mock_Fields.stream() .map(Field::getName)