Skip to content

Commit

Permalink
Merge pull request #46 from dnd-side-project/feature/#45
Browse files Browse the repository at this point in the history
전체적인 에러 수정 및 리팩토링/#45
  • Loading branch information
strangehoon authored Feb 22, 2024
2 parents 9d0d659 + d41e765 commit d2fff4e
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ public interface FeedbackSubmitRepository extends JpaRepository<FeedbackSubmit,

boolean existsByUserAndFeedbackAndIsDeletedIsFalse(User user, Feedback feedback);
Long countByUserAndIsDeletedIsFalse(User user);
Long countByUserId(Long userId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public interface LikeRepository extends JpaRepository<Like, Long> {

Optional<Like> findByUserAndProject(User user, Project project);
boolean existsByUserAndProjectAndIsDeletedIsFalse(User user, Project project);
Long countByProjectIn(List<Project> projects);
Long countByProjectInAndIsDeletedIsFalse(List<Project> projects);
}
Original file line number Diff line number Diff line change
@@ -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<Project, Long>, ProjectRepositoryCustom {
Long countByUserId(Long userId);
List<Project> findByUserId(Long userId);
List<Project> findTop12ByOrderByLikeCountDesc();
Long countByUserAndIsDeletedIsFalse(User user);
List<Project> findByUserAndIsDeletedIsFalse(User user);
List<Project> findAllByEndedAtBeforeAndIsDeletedIsFalse(LocalDate localDate);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,7 +16,7 @@ public interface ProjectRepositoryCustom {
Page<ScrappedProjectResponseDto> findAllScrappedProjectsByMe(Pageable pageable, Long userId, Boolean isFinished);

List<Project> findRecommendedProjects(List<FieldName> fieldNameList, int size);

List<Project> findRecommendedProjects(int size);
Page<Project> findAllByPageableAndFieldAndIsFinishedAndSort(
Pageable pageable, String keyword, String field, Boolean isFinished, Long sort);
Page<SubmittedFeedbackResponseDto> findAllSubmittedProjectsByMe(Pageable pageable, Long userId, Boolean isFinished);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,29 @@ public List<Project> findRecommendedProjects(List<FieldName> filedNameList, int
List<Project> 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<Project> findRecommendedProjects(int size) {
List<Project> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -124,10 +122,14 @@ public List<RecommendedProjectResponseDto> getRecommendedProject(Long userId){
List<FieldName> fieldNameList = fieldList.stream().map(Field::getName).collect(Collectors.toList());
List<Project> projects = projectRepository.findRecommendedProjects(fieldNameList, 12);
responseDtos.addAll(projects.stream().map(project -> RecommendedProjectResponseDto.of(project)).collect(Collectors.toList()));
if(projects.size()<12) {
List<Project> extraProject = projectRepository.findRecommendedProjects(12 - projects.size());
responseDtos.addAll(extraProject.stream().map(project -> RecommendedProjectResponseDto.of(project)).collect(Collectors.toList()));
}
}
else{
List<Project> projects = projectRepository.findTop12ByOrderByLikeCountDesc();
responseDtos = projects.stream().map(project -> RecommendedProjectResponseDto.of(project)).collect(Collectors.toList());
List<Project> projects = projectRepository.findRecommendedProjects(12);
responseDtos.addAll(projects.stream().map(project -> RecommendedProjectResponseDto.of(project)).collect(Collectors.toList()));
}
return responseDtos;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/sendback/domain/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Field> fieldList = signUpRequestDto.fields().stream()
Expand All @@ -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<Project> projectList = projectRepository.findByUserId(userId);
Long likeCount = likeRepository.countByProjectIn(projectList);
Long projectCount = projectRepository.countByUserAndIsDeletedIsFalse(user);
Long feedbackCount = feedbackSubmitRepository.countByUserAndIsDeletedIsFalse(user);
List<Project> projectList = projectRepository.findByUserAndIsDeletedIsFalse(user);
Long likeCount = likeRepository.countByProjectInAndIsDeletedIsFalse(projectList);
List<Field> fieldList = fieldRepository.findAllByUserId(userId);
List<String> fieldNameList = fieldList.stream()
.map(Field::getName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -46,7 +46,7 @@ public void findByUserId() {
Project project = projectTestPersister.builder().save();

//when
List<Project> findProject = projectRepository.findByUserId(project.getUser().getId());
List<Project> findProject = projectRepository.findByUserAndIsDeletedIsFalse(project.getUser());

//then
assertThat(findProject.size()).isEqualTo(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project> 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<String> mock_fieldNameList = mock_Fields.stream()
.map(Field::getName)
Expand Down

0 comments on commit d2fff4e

Please sign in to comment.