Skip to content

Commit

Permalink
Merge pull request #245 from sopt-makers/yongtaek_#238
Browse files Browse the repository at this point in the history
[FIX] 테스트 중 500 에러 수정 + 추가 리팩토링
  • Loading branch information
dragontaek-lee authored Feb 28, 2024
2 parents df55d68 + 2c54d03 commit 3a1f85f
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void checkSubLectureEnded(SubLecture subLecture) {
}

private void checkMatchedCode(SubLecture subLecture, String code) {
if (subLecture.isMatchCode(code)) {
if (!subLecture.isMatchCode(code)) {
throw new SubLectureException(INVALID_CODE);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ private static DateTimeFormatter convertFormat() {
return DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
}

public static TodayLectureResponse getOnAttendanceLectureResponse(
SubAttendance subAttendance,
Lecture lecture,
LectureResponseType responseType,
String message
) {
return lecture.isFirst()
? TodayLectureResponse.of(responseType, lecture, message, Collections.emptyList())
: TodayLectureResponse.of(responseType, lecture, message, Collections.singletonList(subAttendance));
}

public static TodayLectureResponse getAttendanceLectureResponse(
List<SubAttendance> subAttendances,
SubAttendance subAttendance,
Lecture lecture,
LectureResponseType responseType,
String message
) {
return lecture.isFirst()
? TodayLectureResponse.of(responseType, lecture, message, Collections.singletonList(subAttendance))
: TodayLectureResponse.of(responseType, lecture, message, subAttendances);
}

@Builder
record LectureGetResponseVO(
AttendanceStatus status,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ public TodayLectureResponse getTodayLecture(long memberPlaygroundId) {

val subAttendances = attendance.getSubAttendances();

if (lecture.isFirst()) {
return getTodayFirstLectureResponse(subAttendances.get(0), responseType, lecture);
}

return getTodaySecondLectureResponse(subAttendances, responseType, lecture);
return getTodayLectureResponse(subAttendances, responseType, lecture);
}

private void checkAttendancesSize(List<Attendance> attendances) {
Expand All @@ -74,17 +70,31 @@ private void checkAttendancesSize(List<Attendance> attendances) {
}

private boolean checkOnAttendanceAbsence(SubLecture subLecture, SubAttendance subAttendance) {
val isOnAttendanceCheck = LocalDateTime.now().isBefore(subLecture.getStartAt().plusMinutes(10));
return isOnAttendanceCheck && subAttendance.getStatus().equals(ABSENT);
val isOnAttendanceCheck = subLecture.isEnded(valueConfig.getATTENDANCE_MINUTE());
return !isOnAttendanceCheck && subAttendance.getStatus().equals(ABSENT);
}

private Attendance getNowAttendance(List<Attendance> attendances) {
val index = getAttendanceIndex();
val index = getAttendanceIndex(attendances);
return attendances.get(index);
}

private int getAttendanceIndex() {
return (LocalDateTime.now().getHour() >= 16) ? 1 : 0;
private int getAttendanceIndex(List<Attendance> attendances) {
val isMultipleAttendance = getIsMultipleAttendance(attendances.size());
return isMultipleAttendance ? 1 : 0;
}
private boolean getIsMultipleAttendance(int lectureCount) {
return LocalDateTime.now().getHour() >= valueConfig.getHACKATHON_LECTURE_START_HOUR()
&& lectureCount == valueConfig.getMAX_LECTURE_COUNT();
}

private SubAttendance getNowSubAttendance(List<SubAttendance> subAttendances, Lecture lecture) {
val index = getSubAttendanceIndex(lecture);
return subAttendances.get(index);
}

private int getSubAttendanceIndex(Lecture lecture) {
return lecture.isFirst() ? 0 : 1;
}

private LectureResponseType getResponseType(Lecture lecture) {
Expand All @@ -100,27 +110,20 @@ private String getMessage(Attribute attribute) {
};
}

private TodayLectureResponse getTodayFirstLectureResponse(SubAttendance subAttendance, LectureResponseType responseType, Lecture lecture) {
val subLecture = subAttendance.getSubLecture();
val message = getMessage(lecture.getAttribute());
if (checkOnAttendanceAbsence(subLecture, subAttendance)) {
return TodayLectureResponse.of(responseType, lecture, message, Collections.emptyList());
}
return TodayLectureResponse.of(responseType, lecture, message, Collections.singletonList(subAttendance));
}

private TodayLectureResponse getTodaySecondLectureResponse(
List<SubAttendance> subAttendances,
LectureResponseType responseType,
Lecture lecture
private TodayLectureResponse getTodayLectureResponse(
List<SubAttendance> subAttendances,
LectureResponseType responseType,
Lecture lecture
) {
val subAttendance = subAttendances.get(1);
val subAttendance = getNowSubAttendance(subAttendances, lecture);
val subLecture = subAttendance.getSubLecture();
val message = getMessage(lecture.getAttribute());

if (checkOnAttendanceAbsence(subLecture, subAttendance)) {
return TodayLectureResponse.of(responseType, lecture, message, Collections.singletonList(subAttendances.get(0)));
return TodayLectureResponse.getOnAttendanceLectureResponse(subAttendance, lecture, responseType, message);
}
return TodayLectureResponse.of(responseType, lecture, message, subAttendances);

return TodayLectureResponse.getAttendanceLectureResponse(subAttendances, subAttendance, lecture, responseType, message);
}

@Override
Expand Down Expand Up @@ -169,19 +172,11 @@ private void checkLectureBefore(Lecture lecture) {
}

private void checkEndAttendance(SubLecture subLecture) {
if (isEndAttendance(subLecture)) {
if (subLecture.isEnded(valueConfig.getATTENDANCE_MINUTE())) {
throw new LectureException(ENDED_ATTENDANCE, subLecture.getRound());
}
}

private boolean isEndAttendance(SubLecture subLecture) {
val status = subLecture.getLecture().getLectureStatus();
if (LocalDateTime.now().isAfter(subLecture.getStartAt().plusMinutes(10))) {
return status.equals(FIRST) || status.equals(SECOND);
}
return false;
}

private void checkLectureEnd(Lecture lecture) {
if (lecture.isEnd()) {
throw new LectureException(END_LECTURE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public ResponseEntity<BaseResponse<?>> login(LoginRequest userLoginRequestDTO) {
@PatchMapping("/refresh")
public ResponseEntity<BaseResponse<?>> refresh(String refreshToken) {
val response = authService.refresh(refreshToken);
val headers = cookie.setRefreshToken(response.refreshToken());
return ApiResponseUtil.success(SUCCESS_GET_REFRESH_TOKEN, headers, response.accessToken());
return ApiResponseUtil.success(SUCCESS_GET_REFRESH_TOKEN, response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public record SignUpRequest(
Role role
) {

public Admin toEntity() {
public Admin toEntity(String encodedPassword) {
return Admin.builder()
.email(this.email)
.password(this.password)
.password(encodedPassword)
.name(this.name)
.role(this.role)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.sopt.makers.operation.web.admin.dto.response;

import lombok.AccessLevel;
import lombok.Builder;

import org.sopt.makers.operation.admin.domain.Admin;
import org.sopt.makers.operation.admin.domain.AdminStatus;
import lombok.Builder;

import static lombok.AccessLevel.PRIVATE;

@Builder(access = AccessLevel.PRIVATE)
@Builder(access = PRIVATE)
public record LoginResponse(
LoginResponseVO loginResponseVO,
String refreshToken
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import org.sopt.makers.operation.admin.domain.Admin;
import org.sopt.makers.operation.admin.domain.Role;

import lombok.AccessLevel;
import lombok.Builder;

@Builder(access = AccessLevel.PRIVATE)
import static lombok.AccessLevel.PRIVATE;

@Builder(access = PRIVATE)
public record SignUpResponse(
Long id,
long id,
String email,
String name,
Role role
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.sopt.makers.operation.web.admin.dto.response;

import lombok.Builder;

import static lombok.AccessLevel.PRIVATE;

@Builder(access = PRIVATE)
public record TokenRefreshGetResponse(
String accessToken
) {

public static TokenRefreshGetResponse of(String accessToken) {
return TokenRefreshGetResponse.builder()
.accessToken(accessToken)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import org.sopt.makers.operation.web.admin.dto.request.LoginRequest;
import org.sopt.makers.operation.web.admin.dto.request.SignUpRequest;
import org.sopt.makers.operation.web.admin.dto.response.LoginResponse;
import org.sopt.makers.operation.web.admin.dto.response.RefreshResponse;
import org.sopt.makers.operation.web.admin.dto.response.TokenRefreshGetResponse;
import org.sopt.makers.operation.web.admin.dto.response.SignUpResponse;

public interface AdminService {
SignUpResponse signUp(SignUpRequest request);
LoginResponse login(LoginRequest request);
RefreshResponse refresh(String refreshToken);
TokenRefreshGetResponse refresh(String refreshToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.sopt.makers.operation.web.admin.dto.response.SignUpResponse;
import org.sopt.makers.operation.web.admin.dto.request.LoginRequest;
import org.sopt.makers.operation.web.admin.dto.response.LoginResponse;
import org.sopt.makers.operation.web.admin.dto.response.RefreshResponse;
import org.sopt.makers.operation.web.admin.dto.response.TokenRefreshGetResponse;
import org.sopt.makers.operation.admin.repository.AdminRepository;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
Expand All @@ -34,7 +34,8 @@ public class AdminServiceImpl implements AdminService {
@Transactional
public SignUpResponse signUp(SignUpRequest request){
checkEmailDuplicated(request.email());
val admin = adminRepository.save(request.toEntity());
val adminEntity = request.toEntity(passwordEncoder.encode(request.password()));
val admin = adminRepository.save(adminEntity);
return SignUpResponse.of(admin);
}

Expand Down Expand Up @@ -72,8 +73,8 @@ private Admin findByEmail(String email) {
.orElseThrow(() -> new AdminFailureException(INVALID_EMAIL));
}

private void checkPasswordMatched(String password, Admin admin) { // TODO: admin 내부로 옮기는 게 좋지 않을까..?
if (!passwordEncoder.matches(password, admin.getPassword())) {
private void checkPasswordMatched(String password, Admin admin) {
if (!admin.checkPasswordMatched(passwordEncoder, password)) {
throw new AdminFailureException(INVALID_PASSWORD);
}
}
Expand All @@ -86,17 +87,17 @@ private void checkAdminAllowed(Admin admin) {

@Override
@Transactional
public RefreshResponse refresh(String refreshToken) {
public TokenRefreshGetResponse refresh(String refreshToken) {
val adminId = jwtTokenProvider.getId(refreshToken, JwtTokenType.REFRESH_TOKEN);
val admin = findById(adminId);
validateRefreshToken(admin, refreshToken);
val newAccessToken = generateAccessToken(admin);

return RefreshResponse.of(newAccessToken, admin);
return TokenRefreshGetResponse.of(newAccessToken);
}

public void validateRefreshToken(Admin admin, String refreshToken) {
if(!admin.getRefreshToken().equals(refreshToken)) {
if (!admin.isMatchRefreshToken(refreshToken)) {
throw new AdminFailureException(INVALID_REFRESH_TOKEN);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void doFilterInternal(HttpServletRequest request, HttpServletResponse res

private void checkJwtAvailable (String token, JwtTokenType jwtTokenType) {
if (token == null || !jwtTokenProvider.validateTokenExpiration(token, jwtTokenType)) {
throw new TokenException(EMPTY_TOKEN);
throw new TokenException(INVALID_TOKEN);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class ValueConfig {
private String playGroundToken;

private final int SUB_LECTURE_MAX_ROUND = 2;
private final int MAX_LECTURE_COUNT = 2;
private final String ETC_MESSAGE = "출석 점수가 반영되지 않아요.";
private final String SEMINAR_MESSAGE = "";
private final String EVENT_MESSAGE = "행사도 참여하고, 출석점수도 받고, 일석이조!";
Expand All @@ -43,6 +44,7 @@ public class ValueConfig {
private final int MAX_SCHEDULE_DURATION = 50;
private final int DAY_DURATION = 1;
private final int TWO_DAYS_DURATION = 2;
private final int HACKATHON_LECTURE_START_HOUR = 16;

private final List<String> APP_LINK_LIST = Arrays.asList(
"home",
Expand Down
7 changes: 2 additions & 5 deletions operation-domain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ bootJar {
dependencies {
implementation project(path: ':operation-common')

// QueryDSL
// implementation "com.querydsl:querydsl-jpa:5.0.0"
// implementation "com.querydsl:querydsl-apt:5.0.0"
// testImplementation 'junit:junit:4.13.1'
// testImplementation 'junit:junit:4.13.1'
implementation 'org.springframework.boot:spring-boot-starter-security'

implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:5.0.0:jakarta"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.sopt.makers.operation.admin.domain;

import org.springframework.security.crypto.password.PasswordEncoder;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
Expand All @@ -17,6 +19,7 @@
@Entity
@NoArgsConstructor
public class Admin {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "admin_id")
Expand Down Expand Up @@ -56,4 +59,12 @@ public void updateRefreshToken(String refreshToken) {
public boolean isNotAllowed() {
return this.status.equals(AdminStatus.NOT_CERTIFIED);
}

public boolean isMatchRefreshToken(String refreshToken) {
return this.getRefreshToken().equals(refreshToken);
}

public boolean checkPasswordMatched(PasswordEncoder passwordEncoder, String password) {
return passwordEncoder.matches(password, this.password);
}
}

0 comments on commit 3a1f85f

Please sign in to comment.