diff --git a/build.gradle b/build.gradle index 5460260..0ea69c9 100644 --- a/build.gradle +++ b/build.gradle @@ -38,6 +38,12 @@ dependencies { testImplementation 'org.springframework.security:spring-security-test' testImplementation 'org.projectlombok:lombok:1.18.26' + //ssh tunneling + // jsch 라이브러리, for ssh tunneling + implementation 'com.github.mwiede:jsch:0.2.16' + // application.yml의 환경 변수를 class에서 사용하기 위한 라이브러리 + annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' + //jwt runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.11.5") implementation("io.jsonwebtoken:jjwt-api:0.11.5") diff --git a/src/main/java/com/umc/mada/auth/handler/OAuth2LoginSuccessHandler.java b/src/main/java/com/umc/mada/auth/handler/OAuth2LoginSuccessHandler.java index acfa1d0..a8138f8 100644 --- a/src/main/java/com/umc/mada/auth/handler/OAuth2LoginSuccessHandler.java +++ b/src/main/java/com/umc/mada/auth/handler/OAuth2LoginSuccessHandler.java @@ -66,11 +66,13 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo private String makeRedirectUrl(String accessToken, boolean newUser){ if(newUser){ //회원가입한 유저라면 닉네임 입력받는 곳으로 리다이렉트 - return UriComponentsBuilder.fromUriString("http://www.madaumc.store/user/signup") //"http://localhost:8080/login/oauth2/code/google" + return UriComponentsBuilder.fromUriString("http://www.madaumc.shop/user/signup") + //return UriComponentsBuilder.fromUriString("http://www.madaumc.store/user/signup") //"http://localhost:8080/login/oauth2/code/google" .queryParam("token", accessToken) .build().toUriString(); } - return UriComponentsBuilder.fromUriString("http://www.madaumc.store/user/test") //"http://localhost:8080/login/oauth2/code/google" + return UriComponentsBuilder.fromUriString("http://www.madaumc.shop/user/test") +// return UriComponentsBuilder.fromUriString("http://www.madaumc.store/user/test") //"http://localhost:8080/login/oauth2/code/google" .queryParam("token", accessToken) .build().toUriString(); } diff --git a/src/main/java/com/umc/mada/config/SecurityConfig.java b/src/main/java/com/umc/mada/config/SecurityConfig.java index ed2df80..de4f9ac 100644 --- a/src/main/java/com/umc/mada/config/SecurityConfig.java +++ b/src/main/java/com/umc/mada/config/SecurityConfig.java @@ -114,7 +114,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { .and() .logout() - .logoutSuccessUrl("http://localhost:8080/oauth2/authorization/naver") + .logoutSuccessUrl("http://localhost:8080/oauth2/authorization/naver") //TODO: 이거 우리 서버 url로 바꿔야 함 .and() .oauth2Login(oauth2 -> oauth2 diff --git a/src/main/java/com/umc/mada/exception/ControllerAdvice.java b/src/main/java/com/umc/mada/exception/ControllerAdvice.java index 33bb2d9..aea7acb 100644 --- a/src/main/java/com/umc/mada/exception/ControllerAdvice.java +++ b/src/main/java/com/umc/mada/exception/ControllerAdvice.java @@ -33,6 +33,11 @@ public ResponseEntity testErrorHandler(final ServerInternalExcept return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse); } + @ExceptionHandler(DuplicationItemException.class) + public ResponseEntity duplicateItemHandler(final DuplicationItemException e){ + return ResponseEntity.status(HttpStatus.CONFLICT).body(new ErrorResponse(e.getMessage())); + } + // @ExceptionHandler(Exception.class) // private ResponseEntity unExpectedException(final Exception e){ //TODO: final HttpServletRequest request이거 추가해서 에러로그를 띄우기 // return ResponseEntity.internalServerError().body(new ErrorResponse("예상치 못한 서버 에러가 발생했습니다.")); diff --git a/src/main/java/com/umc/mada/todo/dto/StatisticsResponseDto.java b/src/main/java/com/umc/mada/todo/dto/StatisticsResponseDto.java index 73e4f6c..65c0eb0 100644 --- a/src/main/java/com/umc/mada/todo/dto/StatisticsResponseDto.java +++ b/src/main/java/com/umc/mada/todo/dto/StatisticsResponseDto.java @@ -7,7 +7,6 @@ import java.util.List; import java.util.stream.Collectors; -import java.util.Collections; @Builder @Getter @@ -29,99 +28,100 @@ public class StatisticsResponseDto { public static StatisticsResponseDto ofDay(List categoryStatisticsVOList, PreviousCategoryStatisticsVO previousCategoryStatisticsVO, List todoBarGraphStatisticsVOList, int totalCount, List achievementRateStatisticsVOList){ - if (!categoryStatisticsVOList.isEmpty()) { - //각 list의 index 0은 오늘, 1은 어제이다. - return StatisticsResponseDto.builder() - .categoryStatistics(categoryStatisticsVOList.stream().map(vo -> CategoryStatisticsDto.of(vo.getCategoryName(), vo.getColor(), vo.getRate())).collect(Collectors.toList())) - .mostCategory(categoryStatisticsVOList.get(0).getCategoryName()) - .nowCategoryCount(categoryStatisticsVOList.get(0).getCount()) - .beforeCategoryCount(previousCategoryStatisticsVO.getCount()) - .todoStatistics(todoBarGraphStatisticsVOList.stream().map(vo -> TodoStatisticsDto.of(vo.getTodoDate(), vo.getCount())).collect(Collectors.toList())) - .nowTotalCount(totalCount) - .nowCountCompleted(todoBarGraphStatisticsVOList.get(0).getCount()) - .diffCount(todoBarGraphStatisticsVOList.get(0).getCount() - todoBarGraphStatisticsVOList.get(1).getCount()) - .achievementStatistics(achievementRateStatisticsVOList.stream().map(vo -> AchievementStatisticsDto.of(vo.getDate(), vo.getRate())).collect(Collectors.toList())) - .nowAchievementRate(achievementRateStatisticsVOList.get(0).getRate() - achievementRateStatisticsVOList.get(1).getRate()) - .nowCountCompletedA(achievementRateStatisticsVOList.get(0).getCount()) - .build(); - } else{ - return StatisticsResponseDto.builder() - .categoryStatistics(Collections.emptyList()) - .mostCategory("기본 카테고리") - .nowCategoryCount(0) - .beforeCategoryCount(0) - .todoStatistics(Collections.emptyList()) - .nowTotalCount(0) - .nowCountCompleted(0) - .diffCount(0) - .achievementStatistics(Collections.emptyList()) - .nowAchievementRate(0.0f) - .nowCountCompletedA(0.0f) - .build(); - } + float diffCount = calculateDiffCount(todoBarGraphStatisticsVOList); + float nowAchievementRate = calculateNowAchievementRate(achievementRateStatisticsVOList); + + return StatisticsResponseDto.builder() + .categoryStatistics(categoryStatisticsVOList.stream().map(vo -> CategoryStatisticsDto.of(vo.getCategoryName(), vo.getColor(), vo.getRate())).collect(Collectors.toList())) + .mostCategory(categoryStatisticsVOList.get(0).getCategoryName()) + .nowCategoryCount(categoryStatisticsVOList.get(0).getCount()) + .beforeCategoryCount(previousCategoryStatisticsVO.getCount()) + .todoStatistics(todoBarGraphStatisticsVOList.stream().map(vo -> TodoStatisticsDto.of(vo.getTodoDate(), vo.getCount())).collect(Collectors.toList())) + .nowTotalCount(totalCount) + .nowCountCompleted(todoBarGraphStatisticsVOList.get(0).getCount()) + .diffCount(diffCount) + .achievementStatistics(achievementRateStatisticsVOList.stream().map(vo -> AchievementStatisticsDto.of(vo.getDate(), vo.getRate())).collect(Collectors.toList())) + .nowAchievementRate(nowAchievementRate) + .nowCountCompletedA(achievementRateStatisticsVOList.get(0).getCount()) + .build(); } public static StatisticsResponseDto ofWeek(List categoryStatisticsVOList, int totalCount, PreviousCategoryStatisticsVO previousCategoryStatisticsVO, List weeklyBarGraphAndRateStatisticsVOList){ - if (!categoryStatisticsVOList.isEmpty()) { - return StatisticsResponseDto.builder() - .categoryStatistics(categoryStatisticsVOList.stream().map(vo -> CategoryStatisticsDto.of(vo.getCategoryName(), vo.getColor(), vo.getRate())).collect(Collectors.toList())) - .mostCategory(categoryStatisticsVOList.get(0).getCategoryName()) - .nowCategoryCount(categoryStatisticsVOList.get(0).getCount()) - .beforeCategoryCount(previousCategoryStatisticsVO.getCount()) - .todoStatistics(weeklyBarGraphAndRateStatisticsVOList.stream().map(vo -> TodoStatisticsDto.of(vo.getStartDate(), vo.getEndDate(), vo.getCount())).collect(Collectors.toList())) - .nowTotalCount(totalCount) - .nowCountCompleted(weeklyBarGraphAndRateStatisticsVOList.get(0).getCount()) - .diffCount(weeklyBarGraphAndRateStatisticsVOList.get(0).getCount() - weeklyBarGraphAndRateStatisticsVOList.get(1).getCount()) - .achievementStatistics(weeklyBarGraphAndRateStatisticsVOList.stream().map(vo -> AchievementStatisticsDto.of(vo.getStartDate(), vo.getEndDate(), vo.getRate())).collect(Collectors.toList())) - .nowAchievementRate(weeklyBarGraphAndRateStatisticsVOList.get(0).getRate() - weeklyBarGraphAndRateStatisticsVOList.get(1).getRate()) - .nowCountCompletedA(weeklyBarGraphAndRateStatisticsVOList.get(0).getCount()) - .build(); - } else{ - return StatisticsResponseDto.builder() - .categoryStatistics(Collections.emptyList()) - .mostCategory("기본 카테고리") - .nowCategoryCount(0) - .beforeCategoryCount(0) - .todoStatistics(Collections.emptyList()) - .nowTotalCount(0) - .nowCountCompleted(0) - .diffCount(0) - .achievementStatistics(Collections.emptyList()) - .nowAchievementRate(0.0f) - .nowCountCompletedA(0.0f) - .build(); - } + float diffCount = calculateDiffCount(weeklyBarGraphAndRateStatisticsVOList); + float nowAchievementRate = calculateNowAchievementRate(weeklyBarGraphAndRateStatisticsVOList); + + return StatisticsResponseDto.builder() + .categoryStatistics(categoryStatisticsVOList.stream().map(vo -> CategoryStatisticsDto.of(vo.getCategoryName(), vo.getColor(), vo.getRate())).collect(Collectors.toList())) + .mostCategory(categoryStatisticsVOList.get(0).getCategoryName()) + .nowCategoryCount(categoryStatisticsVOList.get(0).getCount()) + .beforeCategoryCount(previousCategoryStatisticsVO.getCount()) + .todoStatistics(weeklyBarGraphAndRateStatisticsVOList.stream().map(vo -> TodoStatisticsDto.of(vo.getStartDate(), vo.getEndDate(), vo.getCount())).collect(Collectors.toList())) + .nowTotalCount(totalCount) + .nowCountCompleted(weeklyBarGraphAndRateStatisticsVOList.get(0).getCount()) + .diffCount(diffCount) + .achievementStatistics(weeklyBarGraphAndRateStatisticsVOList.stream().map(vo -> AchievementStatisticsDto.of(vo.getStartDate(), vo.getEndDate(), vo.getRate())).collect(Collectors.toList())) + .nowAchievementRate(nowAchievementRate) + .nowCountCompletedA(weeklyBarGraphAndRateStatisticsVOList.get(0).getCount()) + .build(); } public static StatisticsResponseDto ofMonth(List categoryStatisticsVOList, int totalCount, PreviousCategoryStatisticsVO previousCategoryStatisticsVO, List monthlyBarGraphAndRateStatisticsVOList){ - if (!categoryStatisticsVOList.isEmpty()){ - return StatisticsResponseDto.builder() - .categoryStatistics(categoryStatisticsVOList.stream().map(vo -> CategoryStatisticsDto.of(vo.getCategoryName(), vo.getColor(), vo.getRate())).collect(Collectors.toList())) - .mostCategory(categoryStatisticsVOList.get(0).getCategoryName()) - .nowCategoryCount(categoryStatisticsVOList.get(0).getCount()) - .beforeCategoryCount(previousCategoryStatisticsVO.getCount()) - .todoStatistics(monthlyBarGraphAndRateStatisticsVOList.stream().map(vo -> TodoStatisticsDto.of(vo.getMonthDate(), vo.getCount())).collect(Collectors.toList())) - .nowTotalCount(totalCount) - .nowCountCompleted(monthlyBarGraphAndRateStatisticsVOList.get(0).getCount()) - .diffCount(monthlyBarGraphAndRateStatisticsVOList.get(0).getCount() - monthlyBarGraphAndRateStatisticsVOList.get(1).getCount()) - .achievementStatistics(monthlyBarGraphAndRateStatisticsVOList.stream().map(vo-> AchievementStatisticsDto.of(vo.getMonthDate(), vo.getRate())).collect(Collectors.toList())) - .nowAchievementRate(monthlyBarGraphAndRateStatisticsVOList.get(0).getRate() - monthlyBarGraphAndRateStatisticsVOList.get(1).getRate()) - .build(); - } else{ - return StatisticsResponseDto.builder() - .categoryStatistics(Collections.emptyList()) - .mostCategory("기본 카테고리") - .nowCategoryCount(0) - .beforeCategoryCount(0) - .todoStatistics(Collections.emptyList()) - .nowTotalCount(0) - .nowCountCompleted(0) - .diffCount(0) - .achievementStatistics(Collections.emptyList()) - .nowAchievementRate(0.0f) - .build(); + float diffCount = calculateDiffCount(monthlyBarGraphAndRateStatisticsVOList); + float nowAchievementRate = calculateNowAchievementRate(monthlyBarGraphAndRateStatisticsVOList); + + return StatisticsResponseDto.builder() + .categoryStatistics(categoryStatisticsVOList.stream().map(vo -> CategoryStatisticsDto.of(vo.getCategoryName(), vo.getColor(), vo.getRate())).collect(Collectors.toList())) + .mostCategory(categoryStatisticsVOList.get(0).getCategoryName()) + .nowCategoryCount(categoryStatisticsVOList.get(0).getCount()) + .beforeCategoryCount(previousCategoryStatisticsVO.getCount()) + .todoStatistics(monthlyBarGraphAndRateStatisticsVOList.stream().map(vo -> TodoStatisticsDto.of(vo.getMonthDate(), vo.getCount())).collect(Collectors.toList())) + .nowTotalCount(totalCount) + .nowCountCompleted(monthlyBarGraphAndRateStatisticsVOList.get(0).getCount()) + .diffCount(diffCount) + .achievementStatistics(monthlyBarGraphAndRateStatisticsVOList.stream().map(vo-> AchievementStatisticsDto.of(vo.getMonthDate(), vo.getRate())).collect(Collectors.toList())) + .nowAchievementRate(nowAchievementRate) + .build(); + } + + private static float calculateNowAchievementRate(List statisticsVOList) { + if (statisticsVOList.size() > 1) { + if (statisticsVOList.get(0) instanceof AchievementRateStatisticsVO) { + return ((AchievementRateStatisticsVO) statisticsVOList.get(0)).getRate() - ((AchievementRateStatisticsVO) statisticsVOList.get(1)).getRate(); + } else if (statisticsVOList.get(0) instanceof WeeklyBarGraphAndRateStatisticsVO) { + return ((WeeklyBarGraphAndRateStatisticsVO) statisticsVOList.get(0)).getRate() - ((WeeklyBarGraphAndRateStatisticsVO) statisticsVOList.get(1)).getRate(); + } else if (statisticsVOList.get(0) instanceof MonthlyBarGraphAndRateStatisticsVO) { + return ((MonthlyBarGraphAndRateStatisticsVO) statisticsVOList.get(0)).getRate() - ((MonthlyBarGraphAndRateStatisticsVO) statisticsVOList.get(1)).getRate(); + } + } + if (statisticsVOList.get(0) instanceof AchievementRateStatisticsVO) { + return ((AchievementRateStatisticsVO) statisticsVOList.get(0)).getRate(); + } else if (statisticsVOList.get(0) instanceof WeeklyBarGraphAndRateStatisticsVO) { + return ((WeeklyBarGraphAndRateStatisticsVO) statisticsVOList.get(0)).getRate(); + } else if (statisticsVOList.get(0) instanceof MonthlyBarGraphAndRateStatisticsVO) { + return ((MonthlyBarGraphAndRateStatisticsVO) statisticsVOList.get(0)).getRate(); + } + return 0; + } + + private static float calculateDiffCount(List statisticsVOList) { + if (statisticsVOList.size() > 1) { + if (statisticsVOList.get(0) instanceof TodoBarGraphStatisticsVO) { + return ((TodoBarGraphStatisticsVO) statisticsVOList.get(0)).getCount() - ((TodoBarGraphStatisticsVO) statisticsVOList.get(1)).getCount(); + } else if (statisticsVOList.get(0) instanceof WeeklyBarGraphAndRateStatisticsVO) { + return ((WeeklyBarGraphAndRateStatisticsVO) statisticsVOList.get(0)).getCount() - ((WeeklyBarGraphAndRateStatisticsVO) statisticsVOList.get(1)).getCount(); + } else if (statisticsVOList.get(0) instanceof MonthlyBarGraphAndRateStatisticsVO) { + return ((MonthlyBarGraphAndRateStatisticsVO) statisticsVOList.get(0)).getCount() - ((MonthlyBarGraphAndRateStatisticsVO) statisticsVOList.get(1)).getCount(); + } + } + if (statisticsVOList.get(0) instanceof TodoBarGraphStatisticsVO) { + return ((TodoBarGraphStatisticsVO) statisticsVOList.get(0)).getCount(); + } else if (statisticsVOList.get(0) instanceof WeeklyBarGraphAndRateStatisticsVO) { + return ((WeeklyBarGraphAndRateStatisticsVO) statisticsVOList.get(0)).getCount(); + } else if (statisticsVOList.get(0) instanceof MonthlyBarGraphAndRateStatisticsVO) { + return ((MonthlyBarGraphAndRateStatisticsVO) statisticsVOList.get(0)).getCount(); } + return 0; } } diff --git a/src/main/java/com/umc/mada/todo/service/ChartService.java b/src/main/java/com/umc/mada/todo/service/ChartService.java index ec032d8..c042291 100644 --- a/src/main/java/com/umc/mada/todo/service/ChartService.java +++ b/src/main/java/com/umc/mada/todo/service/ChartService.java @@ -27,21 +27,18 @@ public class ChartService { private final RepeatTodoRepository repeatTodoRepository; public StatisticsResponseDto dailyStatistics(Authentication authentication, LocalDate date){ -// User user = userRepository.findByAuthIdAndAccountExpired(authentication.getName()).orElseThrow(()-> new RuntimeException("올바른 유저 ID가 아닙니다.")); -// User user = userRepository.findByAuthIdAndAccountExpiredAndAccountExpired(authentication.getName(), false).orElseThrow(()-> new RuntimeException("올바른 유저 ID가 아닙니다.")); User user = userRepository.findByAuthIdAndAccountExpired(authentication.getName(), false).orElseThrow(()-> new NotFoundUserException("유저를 찾을 수 없습니다")); //카테고리 통계 List categoryStatisticsVOList = chartRepository.statisticsOnCategories(user.getId(), date, date); - PreviousCategoryStatisticsVO previousCategoryStatisticsVO; - if (!categoryStatisticsVOList.isEmpty()) { - int check = categoryStatisticsVOList.get(0).getCategoryId(); - previousCategoryStatisticsVO = chartRepository.statisticsOnPreviousCategories(user.getId(), date, date, check); - } else { - previousCategoryStatisticsVO = new DefaultPreviousCategoryStatisticsVO(); + if (categoryStatisticsVOList.isEmpty()) { + return null; } + int check = categoryStatisticsVOList.get(0).getCategoryId(); + PreviousCategoryStatisticsVO previousCategoryStatisticsVO = chartRepository.statisticsOnPreviousCategories(user.getId(), date, date, check); + //막대 그래프 통계 LocalDate startDate = date.minusDays(3); List todoBarGraphStatisticsVOList = chartRepository.dayTodoBarGraphStatistics(user.getId(), startDate, date); @@ -51,6 +48,10 @@ public StatisticsResponseDto dailyStatistics(Authentication authentication, Loca startDate = date.minusDays(5); List achievementRateStatisticsVOList = chartRepository.dayStatisticsOnAchievementRate(user.getId(), startDate, date); +// if(categoryStatisticsVOList.isEmpty() || previousCategoryStatisticsVO != null || todoBarGraphStatisticsVOList.isEmpty() || achievementRateStatisticsVOList.isEmpty()){ +// return null; +// } + return StatisticsResponseDto.ofDay(categoryStatisticsVOList, previousCategoryStatisticsVO, todoBarGraphStatisticsVOList, totalCount, achievementRateStatisticsVOList); } @@ -65,15 +66,13 @@ public StatisticsResponseDto weeklyStatistics(Authentication authentication, Loc List categoryStatisticsVOList = chartRepository.statisticsOnCategories(user.getId(), startDate, endDate); int totalCount = chartRepository.countAllByUserIdAndDateBetween(user, startDate, endDate); //이번주 생성한 투두 개수 - PreviousCategoryStatisticsVO previousCategoryStatisticsVO; - if (!categoryStatisticsVOList.isEmpty()) { - int check = categoryStatisticsVOList.get(0).getCategoryId(); - previousCategoryStatisticsVO = chartRepository.statisticsOnPreviousCategories(user.getId(), startDate, endDate, check); - } else { - // categoryStatisticsVOList가 비어 있을 때 - previousCategoryStatisticsVO = new DefaultPreviousCategoryStatisticsVO(); + if (categoryStatisticsVOList.isEmpty()) { + return null; } + int check = categoryStatisticsVOList.get(0).getCategoryId(); + PreviousCategoryStatisticsVO previousCategoryStatisticsVO = chartRepository.statisticsOnPreviousCategories(user.getId(), startDate, endDate, check); + //막대 그래프&달성률 통계 LocalDate startDate2 = date.minusWeeks(6); List weeklyBarGraphAndRateStatisticsVOList = chartRepository.weeklyTodoBarGraphAndRateStatistics(user.getId(), startDate2, endDate); @@ -92,15 +91,13 @@ public StatisticsResponseDto monthlyStatistics(Authentication authentication, Lo List categoryStatisticsVOList = chartRepository.statisticsOnCategories(user.getId(), startDate, endDate); int totalCount = chartRepository.countAllByUserIdAndDateBetween(user, startDate, endDate); //이번달 생성한 투두 개수 - PreviousCategoryStatisticsVO previousCategoryStatisticsVO; - if (!categoryStatisticsVOList.isEmpty()) { - int check = categoryStatisticsVOList.get(0).getCategoryId(); - previousCategoryStatisticsVO = chartRepository.statisticsOnPreviousCategories(user.getId(), startDate, endDate, check); - } else { - // categoryStatisticsVOList가 비어 있을 때 - previousCategoryStatisticsVO = new DefaultPreviousCategoryStatisticsVO(); + if (categoryStatisticsVOList.isEmpty()) { + return null; } + int check = categoryStatisticsVOList.get(0).getCategoryId(); + PreviousCategoryStatisticsVO previousCategoryStatisticsVO = chartRepository.statisticsOnPreviousCategories(user.getId(), startDate, endDate, check); + //막대 그래프&달성률 통계 LocalDate startDate2 = date.minusMonths(6); List monthlyBarGraphAndRateStatisticsVOList = chartRepository.monthlyTodoBarGraphAndRateStatistics(user.getId(), startDate2, endDate);