Skip to content

Commit

Permalink
Merge pull request #76 from mju-likelion/feature/refactor-calculate-u…
Browse files Browse the repository at this point in the history
…ser-grade-#71

Feature/#71 설문조사 응답을 이용한 유저 등급 계산 로직 리팩토링
  • Loading branch information
rnqhstmd authored Jul 31, 2024
2 parents 17b7b6f + ca8f33a commit a0f4fd7
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
@Builder
public class UserGradeResponseDto {
private String name;
private UserGrade userGrade;
private UserGrade grade;

public static UserGradeResponseDto of(String name, UserGrade userGrade) {
return UserGradeResponseDto.builder()
.name(name)
.userGrade(userGrade)
.grade(userGrade)
.build();
}

public static UserGradeResponseDto from(UserGrade userGrade) {
return UserGradeResponseDto.builder()
.userGrade(userGrade)
.grade(userGrade)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.mutsideout_mju.repository;
package com.example.mutsideout_mju.repository.planner;

import java.time.LocalDate;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.example.mutsideout_mju.repository;
package com.example.mutsideout_mju.repository.planner;

import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.mutsideout_mju.repository;
package com.example.mutsideout_mju.repository.planner;

import com.example.mutsideout_mju.entity.Planner;
import com.example.mutsideout_mju.entity.User;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.mutsideout_mju.repository.usersurvey;

import java.util.UUID;

public interface CustomUserSurveyRepository {
long countValidSurveyResponse(UUID userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.mutsideout_mju.repository.usersurvey;


import com.example.mutsideout_mju.entity.SurveyOption;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;

import java.util.UUID;

import static com.example.mutsideout_mju.entity.QUserSurvey.userSurvey;

public class CustomUserSurveyRepositoryImpl implements CustomUserSurveyRepository {

private final JPAQueryFactory queryFactory;

public CustomUserSurveyRepositoryImpl(EntityManager entityManager) {
this.queryFactory = new JPAQueryFactory(entityManager);
}

@Override
public long countValidSurveyResponse(UUID userId) {
return queryFactory
.selectFrom(userSurvey)
.where(userSurvey.user.id.eq(userId),
userSurvey.survey.number.between(1, 3)
.and(userSurvey.surveyOption.in(SurveyOption.NORMAL, SurveyOption.YES))
.or(userSurvey.survey.number.between(4, 6)
.and(userSurvey.surveyOption.eq(SurveyOption.YES))))
.fetchCount();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.example.mutsideout_mju.repository;
package com.example.mutsideout_mju.repository.usersurvey;

import com.example.mutsideout_mju.entity.UserSurvey;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.UUID;

public interface UserSurveyRepository extends JpaRepository<UserSurvey, UUID> {
public interface UserSurveyRepository extends JpaRepository<UserSurvey, UUID>, CustomUserSurveyRepository {
List<UserSurvey> findByUserId(UUID userId);

boolean existsByUserId(UUID userId);
List<UserSurvey> findByUserIdAndSurveyIdIn(UUID userId, List<UUID> surveyIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.example.mutsideout_mju.exception.NotFoundException;
import com.example.mutsideout_mju.exception.UnauthorizedException;
import com.example.mutsideout_mju.exception.errorCode.ErrorCode;
import com.example.mutsideout_mju.repository.PlannerRepository;
import com.example.mutsideout_mju.repository.planner.PlannerRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.example.mutsideout_mju.exception.NotFoundException;
import com.example.mutsideout_mju.exception.errorCode.ErrorCode;
import com.example.mutsideout_mju.repository.SurveyRepository;
import com.example.mutsideout_mju.repository.UserSurveyRepository;
import com.example.mutsideout_mju.repository.usersurvey.UserSurveyRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down
25 changes: 3 additions & 22 deletions src/main/java/com/example/mutsideout_mju/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@
import com.example.mutsideout_mju.dto.response.user.ProfileResponseData;
import com.example.mutsideout_mju.dto.response.user.UserGradeResponseDto;
import com.example.mutsideout_mju.entity.UserGrade;
import com.example.mutsideout_mju.entity.SurveyOption;
import com.example.mutsideout_mju.entity.User;
import com.example.mutsideout_mju.entity.UserSurvey;
import com.example.mutsideout_mju.exception.ConflictException;
import com.example.mutsideout_mju.exception.ForbiddenException;
import com.example.mutsideout_mju.exception.errorCode.ErrorCode;
import com.example.mutsideout_mju.repository.UserRepository;
import com.example.mutsideout_mju.repository.UserSurveyRepository;
import com.example.mutsideout_mju.repository.usersurvey.UserSurveyRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UserService {
Expand All @@ -28,15 +24,11 @@ public class UserService {
private final PasswordHashEncryption passwordHashEncryption;

/**
* 유저 등급 계산
* 설문조사 유효 응답갯수로 유저 등급 계산
*/
@Transactional
public UserGradeResponseDto calculateUserGrade(User user) {
List<UserSurvey> userSurveyList = userSurveyRepository.findByUserId(user.getId());

long count = userSurveyList.stream()
.filter(userSurvey -> isValidSurveyOption(userSurvey))
.count();
long count = userSurveyRepository.countValidSurveyResponse(user.getId());
UserGrade userGrade = user.determineGrade(count);

user.setUserGrade(userGrade);
Expand All @@ -52,17 +44,6 @@ public UserGradeResponseDto getUserGrade(User user) {
return UserGradeResponseDto.from(user.getUserGrade());
}

/**
* 설문조사 응답 유효 검사
*/
public static boolean isValidSurveyOption(UserSurvey userSurvey) {
Long questionNumber = userSurvey.getSurvey().getNumber();
SurveyOption option = userSurvey.getSurveyOption();

return (questionNumber >= 1 && questionNumber <= 3 && (option == SurveyOption.NORMAL || option == SurveyOption.YES))
|| (questionNumber >= 4 && questionNumber <= 6 && option == SurveyOption.YES);
}

/**
* 유저 탈퇴
*/
Expand Down

0 comments on commit a0f4fd7

Please sign in to comment.