Skip to content

[hotfix] 회원가입 진행 시 빠진 검증 기능 추가 #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 24, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ public class GroupRequestFacade {
private final UserService userService;
private final LocationValidator locationValidator;
private final TitleValidator titleValidator;
private final IntroductionValidator introductionValidator;

public void validateRegisterRequest(Long userId, RegisterGongbaekRequest dto) {
TimeValidator.isTimeValid(dto.startTime(), dto.endTime());
isDateValid(dto);
isWeekDateRight(dto);
titleValidator.isGroupTitleValid(dto.groupTitle());
locationValidator.isLocationValid(dto.location());
introductionValidator.isIntroductionValid(dto.introduction());
IntroductionValidator.isIntroductionValid(dto.introduction());
isValidCoverImg(dto);
isValidMaxPeople(dto);

Expand Down
14 changes: 3 additions & 11 deletions src/main/java/com/ggang/be/api/facade/SignupFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
import com.ggang.be.api.exception.GongBaekException;
import com.ggang.be.api.lectureTimeSlot.service.LectureTimeSlotService;
import com.ggang.be.api.school.service.SchoolService;
import com.ggang.be.api.user.NicknameValidator;
import com.ggang.be.api.user.dto.SignupRequest;
import com.ggang.be.api.user.dto.SignupResponse;
import com.ggang.be.api.user.service.UserService;
import com.ggang.be.api.user.vo.TimeTableVo;
import com.ggang.be.domain.group.IntroductionValidator;
import com.ggang.be.domain.school.SchoolEntity;
import com.ggang.be.domain.timslot.lectureTimeSlot.vo.LectureTimeSlotVo;
import com.ggang.be.domain.user.UserEntity;
import com.ggang.be.domain.user.dto.SaveUserSignUp;
import com.ggang.be.global.jwt.JwtService;
import com.ggang.be.global.util.TimeValidator;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -34,9 +37,6 @@ public void duplicateCheckNickname(final String nickname) {

@Transactional
public SignupResponse signup(SignupRequest request) {

duplicateUserCheck(request);

SchoolEntity schoolEntityByName = schoolService.findSchoolEntityByName(request.schoolName());

SaveUserSignUp saveUserSignUp = SignupRequest.toSaveUserSignUp(request, schoolEntityByName);
Expand All @@ -52,14 +52,6 @@ public SignupResponse signup(SignupRequest request) {
return madeSignupResponse(userEntity);
}

private void duplicateUserCheck(SignupRequest request) {
try{
duplicateCheckNickname(request.nickname());
} catch (GongBaekException e) {
throw new GongBaekException(ResponseError.USERNAME_ALREADY_EXISTS);
}
}

private SignupResponse madeSignupResponse(final UserEntity userEntity) {
Long userId = userEntity.getId();
String accessToken = jwtService.createAccessToken(userId);
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/ggang/be/api/facade/SignupRequestFacade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.ggang.be.api.facade;

import com.ggang.be.api.common.ResponseError;
import com.ggang.be.api.exception.GongBaekException;
import com.ggang.be.api.user.NicknameValidator;
import com.ggang.be.api.user.dto.SignupRequest;
import com.ggang.be.api.user.service.UserService;
import com.ggang.be.domain.group.IntroductionValidator;
import com.ggang.be.global.annotation.Facade;
import com.ggang.be.global.util.TimeValidator;
import lombok.RequiredArgsConstructor;

@Facade
@RequiredArgsConstructor
public class SignupRequestFacade {

private final UserService userService;

public void validateSignupRequest(SignupRequest request) {
userService.duplicateCheckNickname(request.nickname());
IntroductionValidator.isIntroductionValid(request.introduction());
NicknameValidator.validate(request.nickname());
TimeValidator.hasDuplicateInfo(request.timeTable());
TimeValidator.isTimeVoValidTime(request.timeTable());

isValidSchoolGrade(request.schoolGrade());
TimeValidator.isYearAfterNow(request.enterYear());
}

private void isValidSchoolGrade(Integer schoolGrade) {
if (schoolGrade < 1 || schoolGrade > 4) {
throw new GongBaekException(ResponseError.BAD_REQUEST);
}
}


}
4 changes: 2 additions & 2 deletions src/main/java/com/ggang/be/api/user/NicknameValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class NicknameValidator {

public static void validate(String nickname){
if(!LengthValidator.rangelengthCheck(nickname, MIN_LENGTH, MAX_LENGTH))
throw new GongBaekException(ResponseError.INVALID_INPUT_LENGTH);
throw new GongBaekException(ResponseError.BAD_REQUEST);
if(!koreanPattern.matcher(nickname).find())
throw new GongBaekException(ResponseError.INVALID_INPUT_NICKNAME);
throw new GongBaekException(ResponseError.BAD_REQUEST);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.ggang.be.api.common.ResponseSuccess;
import com.ggang.be.api.exception.GongBaekException;
import com.ggang.be.api.facade.SignupFacade;
import com.ggang.be.api.facade.SignupRequestFacade;
import com.ggang.be.api.user.NicknameValidator;
import com.ggang.be.api.user.dto.SignupRequest;
import com.ggang.be.api.user.dto.SignupResponse;
Expand All @@ -28,6 +29,7 @@
public class UserController {
private final UserService userService;
private final SignupFacade signupFacade;
private final SignupRequestFacade signupRequestFacade;
private final JwtService jwtService;

private final static int INTRODUCTION_MIN_LENGTH = 20;
Expand All @@ -49,6 +51,7 @@ public ResponseEntity<ApiResponse<Void>> validateNickname(@RequestParam final St

@PostMapping("/user/signup")
public ResponseEntity<ApiResponse<SignupResponse>> signup(@RequestBody final SignupRequest request){
signupRequestFacade.validateSignupRequest(request);
return ResponseBuilder.created(signupFacade.signup(request));
}

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/ggang/be/api/user/vo/TimeTableVo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.ggang.be.domain.constant.WeekDay;
import com.ggang.be.domain.timslot.lectureTimeSlot.vo.LectureTimeSlotVo;
import java.util.Objects;
import lombok.EqualsAndHashCode;

public record TimeTableVo(WeekDay weekDay,
Double startTime,
Expand All @@ -14,4 +16,21 @@ public static LectureTimeSlotVo toLectureTimeSlotVo(final TimeTableVo timeTableV
timeTableVo.endTime()
);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof TimeTableVo that)) {
return false;
}
return Objects.equals(endTime, that.endTime) && weekDay == that.weekDay
&& Objects.equals(startTime, that.startTime);
}

@Override
public int hashCode() {
return Objects.hash(weekDay, startTime, endTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class IntroductionValidator {
public void isIntroductionValid(String introduction) {
public static void isIntroductionValid(String introduction) {
log.info("now value is : {}", introduction);
if(!LengthValidator.rangelengthCheck(introduction, 20, 100)) {
log.error("소개글 길이 검증에 실패하였습니다.");
Expand Down
46 changes: 36 additions & 10 deletions src/main/java/com/ggang/be/global/util/TimeValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,69 @@

import com.ggang.be.api.common.ResponseError;
import com.ggang.be.api.exception.GongBaekException;
import com.ggang.be.api.user.vo.TimeTableVo;
import com.ggang.be.domain.constant.WeekDay;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.util.List;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TimeValidator {
public static void isWeekDateRight(WeekDay writeWeekDay, LocalDate writeDate) {

public static void isWeekDateRight(WeekDay writeWeekDay, LocalDate writeDate) {
WeekDay realWeekDay = WeekDay.fromDayOfWeek(writeDate.getDayOfWeek());
if(!realWeekDay.equals(writeWeekDay)) {
if (!realWeekDay.equals(writeWeekDay)) {
log.error("weekDate and weekDay is not same");
throw new GongBaekException(ResponseError.BAD_REQUEST);
}
}

public static void isTimeValid(double startTime, double endTime){
if((startTime < 9 || startTime >= 18) && (endTime > 18 || endTime <= 9)) {
public static void isTimeValid(double startTime, double endTime) {
if ((startTime < 9 || startTime >= 18) || (endTime > 18 || endTime <= 9)) {
log.error("startTime {} or endTime {} is not valid", startTime, endTime);
throw new GongBaekException(ResponseError.BAD_REQUEST);
}
if(startTime >= endTime) {
if (startTime >= endTime) {
log.error("startTime or endTime is not valid");
throw new GongBaekException(ResponseError.BAD_REQUEST);
}
}

public static void isDateBeforeNow(LocalDate writeDate){
if(writeDate.isBefore(LocalDate.now())) {
public static void isYearAfterNow(int year) {
if (year > LocalDate.now().getYear()) {
log.error("year {} is after now {}", year, LocalDate.now().getYear());
throw new GongBaekException(ResponseError.BAD_REQUEST);
}
}

public static void isDateBeforeNow(LocalDate writeDate) {
if (writeDate.isBefore(LocalDate.now())) {
log.error("writeDate {} is before now {}", writeDate, LocalDate.now());
throw new GongBaekException(ResponseError.BAD_REQUEST);
}
}

public static void isSameDate(LocalDate date, LocalDate otherDate) {
if(!date.equals(otherDate)) {
if (!date.equals(otherDate)) {
log.error("localDate {} and otherDate {} is not same", date, otherDate);
throw new GongBaekException(ResponseError.BAD_REQUEST);
}
}

public static void hasDuplicateInfo(List<TimeTableVo> timeTableVos) {
long size = timeTableVos.size();
long count = timeTableVos.stream().distinct().count();

if (size != count) {
log.error("timeTableVo has duplicate info");
throw new GongBaekException(ResponseError.BAD_REQUEST);
}
}

public static void isTimeVoValidTime(List<TimeTableVo> timeTableVos) {
timeTableVos.forEach(
timeTableVo -> isTimeValid(timeTableVo.startTime(),
timeTableVo.endTime())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class NicknameValidatorTest {
//when && then
Assertions.assertThatThrownBy(() -> NicknameValidator.validate(name))
.isInstanceOf(GongBaekException.class)
.hasMessageContaining("닉네임은 한글로만 입력 가능합니다.")
.hasFieldOrPropertyWithValue("responseError", ResponseError.INVALID_INPUT_NICKNAME);
.hasMessageContaining("유효하지 않은 요청입니다.")
.hasFieldOrPropertyWithValue("responseError", ResponseError.BAD_REQUEST);
}


Expand All @@ -28,8 +28,8 @@ class NicknameValidatorTest {
//when && then
Assertions.assertThatThrownBy(() -> NicknameValidator.validate(name))
.isInstanceOf(GongBaekException.class)
.hasMessageContaining("입력된 글자수가 허용된 범위를 벗어났습니다.")
.hasFieldOrPropertyWithValue("responseError", ResponseError.INVALID_INPUT_LENGTH);
.hasMessageContaining("유효하지 않은 요청입니다.")
.hasFieldOrPropertyWithValue("responseError", ResponseError.BAD_REQUEST);
}

@ParameterizedTest
Expand Down
Loading