From a31240ba97ffc5e07aa4cfffd9186d2b101ab658 Mon Sep 17 00:00:00 2001 From: yeji Date: Sun, 26 Nov 2023 00:13:49 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EC=97=86?= =?UTF-8?q?=EC=9D=B4=20=ED=96=89=EC=82=AC=20=EC=A1=B0=ED=9A=8C=20=EA=B0=80?= =?UTF-8?q?=EB=8A=A5=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../program/service/ProgramService.java | 52 +++++++++++++++---- .../dhs/global/config/SecurityConfig.java | 2 +- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/efub/dhs/domain/program/service/ProgramService.java b/src/main/java/com/efub/dhs/domain/program/service/ProgramService.java index c29651f..9f740cc 100644 --- a/src/main/java/com/efub/dhs/domain/program/service/ProgramService.java +++ b/src/main/java/com/efub/dhs/domain/program/service/ProgramService.java @@ -36,6 +36,7 @@ import com.efub.dhs.domain.program.repository.ProgramRepository; import com.efub.dhs.domain.registration.entity.Registration; import com.efub.dhs.domain.registration.service.RegistrationService; +import com.efub.dhs.global.utils.SecurityUtils; import lombok.RequiredArgsConstructor; @@ -62,7 +63,7 @@ public Program getProgram(Long programId) { public ProgramDetailResponseDto findProgramById(Long programId) { Program program = getProgram(programId); - Member currentUser = memberService.getCurrentUser(); + Member currentUser = isLoggedIn(SecurityUtils.getCurrentUsername()); Integer remainingDays = calculateRemainingDays(program.getDeadline()); @@ -108,9 +109,18 @@ public ProgramDto findProgramInfo(Program program, Integer remainingDays, GoalDt } public ProgramMemberDto findProgramMemberInfo(Program program, Member member) { - Boolean hasLike = heartService.existsByMemberAndProgram(member, program); - Boolean hasRegistration = registrationService.existsByMemberAndProgram(member, program); - Boolean isHost = program.getHost().equals(member); + boolean hasLike; + boolean hasRegistration; + boolean isHost; + if (member == null) { + hasLike = false; + hasRegistration = false; + isHost = false; + } else { + hasLike = heartService.existsByMemberAndProgram(member, program); + hasRegistration = registrationService.existsByMemberAndProgram(member, program); + isHost = program.getHost().equals(member); + } return new ProgramMemberDto(hasLike, hasRegistration, isHost); } @@ -122,12 +132,21 @@ public List findSimilarPrograms(Program program, Memb public List convertToProgramOutlineResponseDtoList( List programList, Member member) { - return programList.stream().map(program -> - new ProgramOutlineResponseDto(program, - calculateRemainingDays(program.getDeadline()), - findGoalByProgram(program.getTargetNumber(), program.getRegistrantNumber()), - heartService.existsByMemberAndProgram(member, program)) - ).collect(Collectors.toList()); + if (member == null) { + return programList.stream().map(program -> + new ProgramOutlineResponseDto(program, + calculateRemainingDays(program.getDeadline()), + findGoalByProgram(program.getTargetNumber(), program.getRegistrantNumber()), + false) + ).collect(Collectors.toList()); + } else { + return programList.stream().map(program -> + new ProgramOutlineResponseDto(program, + calculateRemainingDays(program.getDeadline()), + findGoalByProgram(program.getTargetNumber(), program.getRegistrantNumber()), + heartService.existsByMemberAndProgram(member, program)) + ).collect(Collectors.toList()); + } } public Long createProgram(ProgramCreationRequestDto requestDto) { @@ -160,7 +179,8 @@ public Registration registerProgram(Long programId, ProgramRegistrationRequestDt } public ProgramListResponseDto findProgramList(int page, ProgramListRequestDto requestDto) { - Member currentUser = memberService.getCurrentUser(); + Member currentUser = isLoggedIn(SecurityUtils.getCurrentUsername()); + Page programPage = programRepository.findProgramListByFilter(requestDto, PageRequest.of(page, PAGE_SIZE)); PageInfoDto pageInfoDto = PageInfoDto.createProgramPageInfoDto(programPage); @@ -178,4 +198,14 @@ public List findProgramPopular() { false) ).collect(Collectors.toList()); } + + public Member isLoggedIn(String username) { + Member currentUser; + if (username.equals("anonymousUser")) { + currentUser = null; + } else { + currentUser = memberService.getCurrentUser(); + } + return currentUser; + } } diff --git a/src/main/java/com/efub/dhs/global/config/SecurityConfig.java b/src/main/java/com/efub/dhs/global/config/SecurityConfig.java index 96ca749..829db84 100644 --- a/src/main/java/com/efub/dhs/global/config/SecurityConfig.java +++ b/src/main/java/com/efub/dhs/global/config/SecurityConfig.java @@ -38,7 +38,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws "/programs/registered", "/programs/*/registrations", "/auth/logout").authenticated() - .antMatchers("/auth/**", "/oauth/**").permitAll() + .antMatchers("/auth/**", "/oauth/**", "/programs/**").permitAll() .antMatchers(HttpMethod.GET).permitAll() .anyRequest().authenticated() .and()