Skip to content

Commit

Permalink
Merge branch 'develop' into hotfix/lecture_schedule_bulk_apply_obmission
Browse files Browse the repository at this point in the history
  • Loading branch information
wonslee committed Feb 13, 2024
2 parents 9fa706c + f607d20 commit cffb599
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
import usw.suwiki.domain.user.user.User;
import usw.suwiki.domain.user.user.controller.dto.UserRequestDto.LoginForm;
import usw.suwiki.domain.user.user.service.UserCRUDService;
import usw.suwiki.domain.user.userIsolation.service.UserIsolationCRUDService;
import usw.suwiki.global.exception.errortype.AccountException;
import usw.suwiki.global.jwt.JwtAgent;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static usw.suwiki.global.exception.ExceptionType.PASSWORD_ERROR;
import static usw.suwiki.global.exception.ExceptionType.USER_RESTRICTED;
import static usw.suwiki.global.util.apiresponse.ApiResponseFactory.adminLoginResponseForm;
import static usw.suwiki.global.util.apiresponse.ApiResponseFactory.successCapitalFlag;

@Service
Expand All @@ -36,6 +37,7 @@ public class AdminBusinessService {

private final BlacklistDomainCRUDService blacklistDomainCRUDService;
private final UserCRUDService userCRUDService;
private final UserIsolationCRUDService userIsolationCRUDService;
private final ReportPostService reportPostService;
private final EvaluatePostCRUDService evaluatePostCRUDService;
private final ExamPostCRUDService examPostCRUDService;
Expand All @@ -49,11 +51,15 @@ public class AdminBusinessService {
public Map<String, String> executeAdminLogin(LoginForm loginForm) {
User user = userCRUDService.loadUserFromLoginId(loginForm.loginId());
if (user.validatePassword(bCryptPasswordEncoder, loginForm.password())) {
if (user.getRole().getKey().equals("ADMIN")) {
return new HashMap<>() {{
put("AccessToken", jwtAgent.createAccessToken(user));
put("UserCount", String.valueOf(userCRUDService.findAllUsersSize()));
}};
if (user.isAdmin()) {
final long userCount = userCRUDService.countAllUsers();
final long userIsolationCount = userIsolationCRUDService.countAllIsolatedUsers();
final long totalUserCount = userCount + userIsolationCount;

return adminLoginResponseForm(
jwtAgent.createAccessToken(user),
String.valueOf(totalUserCount)
);
}
throw new AccountException(USER_RESTRICTED);
}
Expand Down Expand Up @@ -194,4 +200,4 @@ private void plusReportingUserPoint(Long reportingUserIdx) {
User user = userCRUDService.loadUserFromUserIdx(reportingUserIdx);
user.increasePointByReporting();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import usw.suwiki.domain.apilogger.ApiLogger;
Expand All @@ -24,6 +25,7 @@ public class ApiLoggerService {
private final String userOption = "user";
private final String noticeOption = "notice";

@Async
@Transactional
public void logApi(LocalDate today, Long currentProcessTime, String option) {
Optional<ApiLogger> apiLogger = apiLoggerRepository.findByCallDate(today);
Expand All @@ -43,46 +45,27 @@ private ApiLogger makeNewApiStatistics(
LocalDate today, Long currentProcessTime, String option
) {
ApiLogger newApiLogger = new ApiLogger();
switch (option) {
case lecturePostsOption:
newApiLogger = newApiLogger.saveNewLectureStatistics(today, currentProcessTime);
break;
case evaluatePostsOption:
newApiLogger = newApiLogger.saveNewEvaluatePostsStatistics(today, currentProcessTime);
break;
case examPostsOption:
newApiLogger = newApiLogger.saveNewExamPostsStatistics(today, currentProcessTime);
break;
case userOption:
newApiLogger = newApiLogger.saveNewUserStatistics(today, currentProcessTime);
break;
case noticeOption:
newApiLogger = newApiLogger.saveNewNoticeStatistics(today, currentProcessTime);
break;
}
newApiLogger = switch (option) {
case lecturePostsOption -> newApiLogger.saveNewLectureStatistics(today, currentProcessTime);
case evaluatePostsOption -> newApiLogger.saveNewEvaluatePostsStatistics(today, currentProcessTime);
case examPostsOption -> newApiLogger.saveNewExamPostsStatistics(today, currentProcessTime);
case userOption -> newApiLogger.saveNewUserStatistics(today, currentProcessTime);
case noticeOption -> newApiLogger.saveNewNoticeStatistics(today, currentProcessTime);
default -> newApiLogger;
};
return newApiLogger;
}

private ApiLogger makeOldApiStatistics(
ApiLogger apiLogger, Long currentProcessTime, String option
) {
switch (option) {
case lecturePostsOption:
apiLogger.calculateLectureApiStatistics(currentProcessTime);
break;
case evaluatePostsOption:
apiLogger.calculateEvaluatePostsApiStatistics(currentProcessTime);
break;
case examPostsOption:
apiLogger.calculateExamPostsStatistics(currentProcessTime);
break;
case userOption:
apiLogger.calculateUserApiStatistics(currentProcessTime);
break;
case noticeOption:
apiLogger.calculateNoticeApiStatistics(currentProcessTime);
break;
case lecturePostsOption -> apiLogger.calculateLectureApiStatistics(currentProcessTime);
case evaluatePostsOption -> apiLogger.calculateEvaluatePostsApiStatistics(currentProcessTime);
case examPostsOption -> apiLogger.calculateExamPostsStatistics(currentProcessTime);
case userOption -> apiLogger.calculateUserApiStatistics(currentProcessTime);
case noticeOption -> apiLogger.calculateNoticeApiStatistics(currentProcessTime);
}
return apiLogger;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public List<EvaluatePostResponseByUserIdxDto> readEvaluatePostsByUserId(
}

public boolean verifyIsUserCanWriteEvaluatePost(Long userIdx, Long lectureId) {
Lecture lecture = lectureService.findLectureById(lectureId);;
Lecture lecture = lectureService.findLectureById(lectureId);
User user = userCRUDService.loadUserFromUserIdx(userIdx);
return evaluatePostCRUDService.isAlreadyWritten(user, lecture);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/usw/suwiki/domain/lecture/domain/Lecture.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public class Lecture extends BaseTimeEntity {
private int postsCount = 0;

@OneToMany(mappedBy = "lecture", cascade = CascadeType.ALL, orphanRemoval = true)
private List<LectureSchedule> scheduleList = new ArrayList<>();
private final List<LectureSchedule> scheduleList = new ArrayList<>();

@OneToMany(mappedBy = "lecture", cascade = CascadeType.ALL, orphanRemoval = true)
private List<EvaluatePost> evaluatePostList = new ArrayList<>();
private final List<EvaluatePost> evaluatePostList = new ArrayList<>();

@Builder
public Lecture(
Expand Down Expand Up @@ -174,4 +174,4 @@ private static String buildAddedSingleSemester(String semester) {
return ", " + semester;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class Timetable extends BaseTimeEntity {
private Semester semester;

@OneToMany(mappedBy = "timetable", cascade = CascadeType.ALL, orphanRemoval = true)
private List<TimetableCell> cellList = new ArrayList<>();
private final List<TimetableCell> cellList = new ArrayList<>();

@Builder
public Timetable(String name, Integer year, Semester semester) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/usw/suwiki/domain/user/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public void activateUser() {
this.role = Role.USER;
}

public boolean isAdmin() {
return this.role.getKey().equals(Role.ADMIN);
}

/**
* Auth
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package usw.suwiki.domain.user.user.service;

import static usw.suwiki.global.exception.ExceptionType.USER_NOT_EXISTS;
import static usw.suwiki.global.exception.ExceptionType.USER_NOT_FOUND;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import usw.suwiki.domain.user.user.User;
import usw.suwiki.domain.user.user.repository.UserRepository;
import usw.suwiki.global.exception.errortype.AccountException;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import static usw.suwiki.global.exception.ExceptionType.USER_NOT_EXISTS;
import static usw.suwiki.global.exception.ExceptionType.USER_NOT_FOUND;

@Service
@RequiredArgsConstructor
@Transactional
Expand Down Expand Up @@ -73,8 +72,8 @@ private User convertOptionalUserToDomainUser(Optional<User> optionalUser) {
}

@Transactional(readOnly = true)
public int findAllUsersSize() {
return userRepository.findAll().size();
public long countAllUsers() {
return userRepository.count();
}

public void deleteFromUserIdx(Long userIdx) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package usw.suwiki.domain.user.userIsolation.service;

import static usw.suwiki.global.exception.ExceptionType.USER_NOT_EXISTS;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import usw.suwiki.domain.user.user.User;
import usw.suwiki.domain.user.userIsolation.UserIsolation;
import usw.suwiki.domain.user.userIsolation.repository.UserIsolationRepository;
import usw.suwiki.global.exception.errortype.AccountException;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static usw.suwiki.global.exception.ExceptionType.USER_NOT_EXISTS;

@Service
@RequiredArgsConstructor
@Transactional
Expand Down Expand Up @@ -61,6 +58,11 @@ public UserIsolation loadUserFromEmail(String email) {
return convertOptionalUserToDomainUser(userIsolationRepository.findByEmail(email));
}

@Transactional(readOnly = true)
public long countAllIsolatedUsers() {
return userIsolationRepository.count();
}

public void deleteByUserIdx(Long userIdx) {
userIsolationRepository.deleteByUserIdx(userIdx);
}
Expand All @@ -75,4 +77,4 @@ private UserIsolation convertOptionalUserToDomainUser(Optional<UserIsolation> op
}
throw new AccountException(USER_NOT_EXISTS);
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/usw/suwiki/global/config/AsyncConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package usw.suwiki.global.config;

import java.util.concurrent.Executor;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync(proxyTargetClass = true)
public class AsyncConfig implements AsyncConfigurer {

@Override
public Executor getAsyncExecutor() {
final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(3);
executor.setMaxPoolSize(30);
executor.setQueueCapacity(90);
executor.setThreadNamePrefix("ASYNC-");
executor.initialize();

return executor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@

public class ApiResponseFactory {

public static Map<String, String> adminLoginResponseForm(
final String accessToken,
final String userCount
) {
return new HashMap<>() {{
put("AccessToken", accessToken);
put("UserCount", userCount);
}};
}

public static Map<String, Boolean> successFlag() {
return new HashMap<>() {{
put("success", true);
Expand Down

0 comments on commit cffb599

Please sign in to comment.