Skip to content

Commit

Permalink
fix: userId가 null일때 에러가 발생하던 부분 수정
Browse files Browse the repository at this point in the history
fix: userId가 null일때 에러가 발생하던 부분 수정
  • Loading branch information
tjdwns5063 authored Feb 21, 2024
2 parents ab75f28 + e6f9039 commit 313132f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/strcat/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class SecurityConfig {
"favicon.ico",
"v3/api-docs/**",
"swagger-ui/**",
"boards/*",
"boards/{boardId}",
"boards/*/summaries",
"boards/*/contents",
"boards/*/contents/pictures",
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/strcat/controller/BoardController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatusCode;
import org.springframework.security.core.Authentication;
Expand Down Expand Up @@ -84,7 +85,8 @@ public String createPicture(@PathVariable(name = "boardId") String encryptedBoar
@Operation(summary = "보드 조회", description = "보드에 대한 모든 정보와 보드 소유자 여부를 반환합니다.")
public ReadBoardResDto readBoard(Authentication authentication,
@PathVariable(name = "boardId") String encryptedBoardId) {
Long userId = (Long) authentication.getPrincipal();
Long userId = authentication != null ? (Long) authentication.getPrincipal() : null;

return boardService.readBoard(encryptedBoardId, userId);
}

Expand All @@ -101,7 +103,9 @@ public ReadBoardSummaryResDto readSummary(@PathVariable(name = "boardId") String
@Content(examples = {@ExampleObject("인증 실패")})})
public ReadBoardResDto deleteContents(@PathVariable(name = "boardId") String encryptedBoardId, @RequestBody
DeleteContentReqDto dto, Authentication authentication) {
if (authentication == null) throw new ResponseStatusException(HttpStatusCode.valueOf(401));
if (authentication == null) {
throw new ResponseStatusException(HttpStatusCode.valueOf(401));
}

User user = (User) authentication.getCredentials();

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/strcat/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ public String createBoard(CreateBoardReqDto dto, Long userId) {
public ReadBoardResDto readBoard(String encryptedBoardId, Long userId) {
Board board = boardRepository.findByEncryptedId(encryptedBoardId)
.orElseThrow(() -> new NotAcceptableException("존재하지 않는 보드입니다."));

try {
if (userId != null) {
recordHistoryUseCase.write(userId,
List.of(new HistoryItem(encryptedBoardId, board.getTitle(), LocalDateTime.now())));

Boolean isOwner = userId.equals(board.getUser().getId());
return board.toReadBoardResDto(isOwner);
} catch (NotAcceptableException e) {
return board.toReadBoardResDto(false);
}
return board.toReadBoardResDto(false);
}

public ReadBoardSummaryResDto readSummary(String encryptedBoardId) {
Expand Down

0 comments on commit 313132f

Please sign in to comment.