Skip to content

Commit

Permalink
FEAT : valid 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
gaguriee committed Aug 9, 2024
1 parent e6613b3 commit df2dcb0
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.fledge.fledgeserver.auth.jwt.filter;

import com.fledge.fledgeserver.auth.jwt.TokenProvider;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

@Slf4j
@Component
@RequiredArgsConstructor
public class JwtFilter extends OncePerRequestFilter {
private final TokenProvider tokenProvider;

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {

String jwt = tokenProvider.resolveToken(request);

if (jwt != null) {
tokenProvider.validateToken(jwt);
setAuthentication(jwt);
}

chain.doFilter(request, response);
}

private void setAuthentication(String accessToken) {
Authentication authentication = tokenProvider.resolveToken(accessToken);
SecurityContextHolder.getContext().setAuthentication(authentication);
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.fledge.fledgeserver.canary.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Past;
import jakarta.validation.constraints.Size;
import jakarta.validation.constraints.*;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -27,6 +24,7 @@ public class CanaryProfileRequest {
@Schema(description = "전화번호", required = true, example = "010-1234-5678")
@NotBlank(message = "전화번호는 필수입니다.")
@Size(max = 255, message = "전화번호는 최대 255자까지 입력 가능합니다.")
@Pattern(regexp = "^01(?:0|1|[6-9])[.-]?(\\d{3}|\\d{4})[.-]?(\\d{4})$", message = "10 ~ 11 자리의 숫자만 입력 가능합니다.")
private String phone;

@Schema(description = "생년월일", required = true, example = "1990-01-01")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.fledge.fledgeserver.canary.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.*;
import lombok.Getter;
import lombok.Setter;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Past;
import jakarta.validation.constraints.Size;
import java.util.Date;

@Getter
Expand All @@ -23,6 +20,7 @@ public class CanaryProfileUpdateRequest {
@Schema(description = "전화번호", required = true, example = "010-1234-5678")
@NotBlank(message = "전화번호는 필수입니다.")
@Size(max = 20, message = "전화번호는 최대 20자까지 입력 가능합니다.")
@Pattern(regexp = "^01(?:0|1|[6-9])[.-]?(\\d{3}|\\d{4})[.-]?(\\d{4})$", message = "10 ~ 11 자리의 숫자만 입력 가능합니다.")
private String phone;

@Schema(description = "생년월일", required = true, example = "1990-01-01")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
import java.util.Optional;

public interface CanaryProfileRepository extends JpaRepository<CanaryProfile, Long> {
boolean existsByMember(Member member);

Optional<CanaryProfile> findByMemberId(Long memberId);

Optional<CanaryProfile> findCanaryProfileByMemberId(Long memberId);
Optional<CanaryProfile> findByMemberIdAndApprovalStatusIsTrue(Long memberId);

boolean existsByMemberAndApprovalStatusIsTrue(Member member);

boolean existsByMember(Member member);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.fledge.fledgeserver.canary.service;

import com.fledge.fledgeserver.auth.dto.OAuthUserImpl;
import com.fledge.fledgeserver.canary.dto.*;
import com.fledge.fledgeserver.canary.entity.CanaryProfile;
import com.fledge.fledgeserver.canary.repository.CanaryProfileRepository;
Expand All @@ -12,8 +11,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.fledge.fledgeserver.exception.ErrorCode.MEMBER_FORBIDDEN;


@Service
@RequiredArgsConstructor
Expand All @@ -25,8 +22,7 @@ public class CanaryProfileService {
public void createCanaryProfile(CanaryProfileRequest request) {
Member member = SecurityUtils.checkAndGetCurrentUser(request.getUserId());

boolean exists = canaryProfileRepository.existsByMember(member);
if (exists) {
if (canaryProfileRepository.existsByMember(member)){
throw new CustomException(ErrorCode.DUPLICATE_APPLICATION);
}

Expand Down Expand Up @@ -74,7 +70,11 @@ public CanaryProfileResponse getCanaryProfile(Long userId) {

@Transactional
public CanaryProfileResponse updateCanaryProfile(Long userId, CanaryProfileUpdateRequest request) {
SecurityUtils.checkAndGetCurrentUser(userId);
Member member = SecurityUtils.checkAndGetCurrentUser(userId);

if (!canaryProfileRepository.existsByMemberAndApprovalStatusIsTrue(member)){
throw new CustomException(ErrorCode.CANARY_NOT_FOUND, "인증되지 않은 자립준비청년 입니다.");
}

CanaryProfile existingProfile = canaryProfileRepository.findByMemberId(userId)
.orElseThrow(() -> new CustomException(ErrorCode.CANARY_NOT_FOUND));
Expand All @@ -90,7 +90,7 @@ public CanaryProfileResponse updateCanaryProfile(Long userId, CanaryProfileUpdat
@Transactional(readOnly = true)
public CanaryGetDeliveryInfoResponse getCanaryDeliveryInfo() {
Long userId = SecurityUtils.getCurrentUserId();
CanaryProfile canary = canaryProfileRepository.findCanaryProfileByMemberId(userId)
CanaryProfile canary = canaryProfileRepository.findByMemberIdAndApprovalStatusIsTrue(userId)
.orElseThrow(() -> new CustomException(ErrorCode.CANARY_NOT_FOUND));
return new CanaryGetDeliveryInfoResponse(
canary.getName(),
Expand All @@ -103,7 +103,7 @@ public CanaryGetDeliveryInfoResponse getCanaryDeliveryInfo() {

@Transactional(readOnly = true)
public CanaryProfileGetResponse getCanaryForSupport(Long memberId) {
CanaryProfile canaryProfile = canaryProfileRepository.findByMemberId(memberId)
CanaryProfile canaryProfile = canaryProfileRepository.findByMemberIdAndApprovalStatusIsTrue(memberId)
.orElseThrow(() -> new CustomException(ErrorCode.CANARY_NOT_FOUND));

return new CanaryProfileGetResponse(canaryProfile);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fledge.fledgeserver.challenge.service;

import com.fledge.fledgeserver.canary.repository.CanaryProfileRepository;
import com.fledge.fledgeserver.challenge.repository.ChallengeRepository;
import com.fledge.fledgeserver.challenge.Enum.Frequency;
import com.fledge.fledgeserver.challenge.dto.TopParticipantResponse;
Expand All @@ -13,7 +14,6 @@
import com.fledge.fledgeserver.exception.CustomException;
import com.fledge.fledgeserver.exception.ErrorCode;
import com.fledge.fledgeserver.member.entity.Member;
import com.fledge.fledgeserver.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
Expand All @@ -29,14 +29,18 @@ public class ChallengeParticipationService {

private final ChallengeParticipationRepository participationRepository;
private final ChallengeProofRepository proofRepository;
private final MemberRepository memberRepository;
private final CanaryProfileRepository canaryProfileRepository;
private final ChallengeRepository challengeRepository;

@Transactional
public ChallengeParticipationResponse participateInChallenge(Long memberId, Long challengeId, LocalDate startDate) {

Member member = SecurityUtils.checkAndGetCurrentUser(memberId);

if (!canaryProfileRepository.existsByMemberAndApprovalStatusIsTrue(member)){
throw new CustomException(ErrorCode.CANARY_NOT_FOUND, "인증된 자립준비 청년이 아닙니다.");
}

Challenge challenge = challengeRepository.findById(challengeId)
.orElseThrow(() -> new CustomException(ErrorCode.CHALLENGE_NOT_FOUND));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.fledge.fledgeserver.challenge.service;

import com.fledge.fledgeserver.canary.repository.CanaryProfileRepository;
import com.fledge.fledgeserver.challenge.repository.ChallengeProofRepository;
import com.fledge.fledgeserver.challenge.dto.ChallengeProofResponse;
import com.fledge.fledgeserver.challenge.entity.ChallengeProof;
import com.fledge.fledgeserver.common.utils.SecurityUtils;
import com.fledge.fledgeserver.exception.CustomException;
import com.fledge.fledgeserver.exception.ErrorCode;
import com.fledge.fledgeserver.member.entity.Member;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -16,9 +19,14 @@
public class ChallengeProofService {

private final ChallengeProofRepository proofRepository;
private final CanaryProfileRepository canaryProfileRepository;

@Transactional
public ChallengeProofResponse uploadProof(Long participationId, LocalDate proofDate, String proofImageUrl) {
Member member = SecurityUtils.getCurrentMember();
if (!canaryProfileRepository.existsByMemberAndApprovalStatusIsTrue(member)){
throw new CustomException(ErrorCode.CANARY_NOT_FOUND, "인증된 자립준비 청년이 아닙니다.");
}
ChallengeProof proof = proofRepository.findByParticipationIdAndProofDate(participationId, proofDate)
.orElseThrow(() -> new CustomException(ErrorCode.CHALLENGE_PROOF_NOT_FOUND));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public ResponseEntity<ApiResponse<MemberResponse>> getMemberDetails(
@PutMapping("/{id}/nickname")
public ResponseEntity<ApiResponse<MemberResponse>> updateNickname(
@Parameter(description = "회원 ID", required = true, example = "1") @PathVariable Long id,
@Parameter(description = "닉네임 수정 요청", required = true) @RequestBody MemberNicknameUpdateRequest request,
@AuthenticationPrincipal OAuthUserImpl oAuth2User) {
@Parameter(description = "닉네임 수정 요청", required = true) @RequestBody MemberNicknameUpdateRequest request) {
MemberResponse memberResponse = memberService.updateNickname(id, request.getNickname());
return ApiResponse.success(SuccessStatus.MEMBER_NICKNAME_UPDATE_SUCCESS, memberResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class PostCreateRequest {
private String recipientName;

@Schema(description = "전화번호", example = "010-1234-5678")
@Pattern(regexp = "^01(?:0|1|[6-9])[.-]?(\\d{3}|\\d{4})[.-]?(\\d{4})$", message = "10 ~ 11 자리의 숫자만 입력 가능합니다.")
private String phone;

@Schema(description = "주소", example = "서울특별시 노원구 공릉로232")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class PostUpdateRequest {
private String recipientName;

@Schema(description = "전화번호", example = "010-1234-5678")
@Pattern(regexp = "^01(?:0|1|[6-9])[.-]?(\\d{3}|\\d{4})[.-]?(\\d{4})$", message = "10 ~ 11 자리의 숫자만 입력 가능합니다.")
@NotBlank(message = "전화번호는 필수입니다.")
private String phone;

Expand Down

0 comments on commit df2dcb0

Please sign in to comment.