Skip to content
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

fix: userId가 null일때 에러가 발생하던 부분 수정 #195

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading