Skip to content

Commit

Permalink
refactor : response flow
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Diger committed Feb 11, 2024
1 parent a52a19d commit f607d20
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
package usw.suwiki.domain.admin.service;

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.successCapitalFlag;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import usw.suwiki.domain.admin.controller.dto.UserAdminRequestDto.EvaluatePostBlacklistForm;
import usw.suwiki.domain.admin.controller.dto.UserAdminRequestDto.EvaluatePostNoProblemForm;
import usw.suwiki.domain.admin.controller.dto.UserAdminRequestDto.EvaluatePostRestrictForm;
import usw.suwiki.domain.admin.controller.dto.UserAdminRequestDto.ExamPostBlacklistForm;
import usw.suwiki.domain.admin.controller.dto.UserAdminRequestDto.ExamPostNoProblemForm;
import usw.suwiki.domain.admin.controller.dto.UserAdminRequestDto.ExamPostRestrictForm;
import usw.suwiki.domain.admin.controller.dto.UserAdminRequestDto.*;
import usw.suwiki.domain.admin.controller.dto.UserAdminResponseDto.LoadAllReportedPostForm;
import usw.suwiki.domain.blacklistdomain.service.BlacklistDomainCRUDService;
import usw.suwiki.domain.evaluatepost.domain.EvaluatePost;
Expand All @@ -27,14 +15,21 @@
import usw.suwiki.domain.postreport.ExamPostReport;
import usw.suwiki.domain.postreport.service.ReportPostService;
import usw.suwiki.domain.restrictinguser.service.RestrictingUserService;
import usw.suwiki.domain.user.user.Role;
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.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
@Transactional
@RequiredArgsConstructor
Expand All @@ -56,16 +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(Role.ADMIN.getKey())) {

if (user.isAdmin()) {
final long userCount = userCRUDService.countAllUsers();
final long userIsolationCount = userIsolationCRUDService.countAllIsolatedUsers();
final long totalUserCount = userCount + userIsolationCount;

return new HashMap<>() {{
put("AccessToken", jwtAgent.createAccessToken(user));
put("UserCount", String.valueOf(totalUserCount));
}};
return adminLoginResponseForm(
jwtAgent.createAccessToken(user),
String.valueOf(totalUserCount)
);
}
throw new AccountException(USER_RESTRICTED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,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;
}
}
}
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
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 f607d20

Please sign in to comment.