Skip to content

Commit

Permalink
Merge pull request #13 from Team3-NEWNEEK-Clone-Coding/KWON
Browse files Browse the repository at this point in the history
Kwon
  • Loading branch information
NHclub committed Jul 25, 2023
2 parents c7fcf35 + 83a721d commit ad9a0e4
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
http.authorizeHttpRequests((authorizeHttpRequests) ->
authorizeHttpRequests
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() // resources(정적리소스) 접근 허용
.requestMatchers("/auth/**").permitAll()
.requestMatchers("/api/auth/**").permitAll()
.requestMatchers(HttpMethod.GET,"/api/news/**").permitAll()
.anyRequest().authenticated() // 그 외 모든 요청은 인증처리
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class JwtAuthenticationFilter extends UsernamePasswordAuthenticationFilte

public JwtAuthenticationFilter(JwtUtil jwtUtil) {
this.jwtUtil = jwtUtil;
setFilterProcessesUrl("/auth/login");
setFilterProcessesUrl("/api/auth/login");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.newnique.exception;

public class CategoryNotFoundException extends RuntimeException {
public CategoryNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,25 @@ public ResponseEntity<Map<String, Object>> handleEmailAlreadyExistsException(Ema
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(NewsNotFoundException.class)
public ResponseEntity<Map<String, Object>> handleNewsNotFoundException(NewsNotFoundException e) {
Map<String, Object> response = new HashMap<>();
response.put("message", e.getMessage());
response.put("statusCode", HttpStatus.BAD_REQUEST.value());

return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(CategoryNotFoundException.class)
public ResponseEntity<Map<String, Object>> handleCategoryNotFoundException(CategoryNotFoundException e) {
Map<String, Object> response = new HashMap<>();
response.put("message", e.getMessage());
response.put("statusCode", HttpStatus.BAD_REQUEST.value());

return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.newnique.exception;

public class NewsNotFoundException extends RuntimeException {
public NewsNotFoundException(String message) {
super(message);
}
}
12 changes: 10 additions & 2 deletions src/main/java/com/example/newnique/news/service/NewsService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.example.newnique.news.service;


import com.example.newnique.exception.CategoryNotFoundException;
import com.example.newnique.exception.NewsNotFoundException;
import com.example.newnique.news.dto.NewsDetailsResponseDto;
import com.example.newnique.news.dto.NewsHeartResponseDto;
import com.example.newnique.news.dto.NewsResponseDto;
Expand Down Expand Up @@ -55,7 +58,7 @@ public Map<String, Object> getNews(int page, int size,
public NewsDetailsResponseDto getNewsDetails(Long newsId) {

News news = newsRepository.findById(newsId).orElseThrow(() ->
new IllegalArgumentException("존재하지 않는 기사 입니다.")
new NewsNotFoundException("존재하지 않는 뉴스 입니다.")
);
return new NewsDetailsResponseDto(news);
}
Expand All @@ -70,6 +73,11 @@ public Map<String, Object> getNewsByCategory(String category, int page, int size
Page<News> newsListByCategory = newsRepository.findAllByCategory(category, pageable);


if(newsListByCategory.getContent().isEmpty()){
throw new CategoryNotFoundException("존재하지 않는 카테고리입니다.");
}


Map<String, Object> response = new HashMap<>();
List<NewsResponseDto> newsResponseDto = newsListByCategory.stream().map(NewsResponseDto::new).collect(Collectors.toList());

Expand Down Expand Up @@ -133,7 +141,7 @@ public NewsHeartResponseDto getNewsHeart(Long newsId, String userEmail) {
User loginUser = userRepository.findByUserEmail(userEmail).orElseThrow(
() -> new IllegalArgumentException("존재하지 않는 사용자입니다."));
News news = newsRepository.findById(newsId).orElseThrow(() ->
new IllegalArgumentException("존재하지 않는 뉴스입니다.")
new NewsNotFoundException("존재하지 않는 뉴스입니다.")
);

NewsHeart existHeart = newsHeartRepository.findByHeartUserAndHeartNews(loginUser, news);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

@RestController
@Slf4j(topic = "UserController")
@RequestMapping("/auth")
@RequestMapping("/api/auth")
public class UserController {
private final UserService userService;
private final KakaoService kakaoService;
Expand Down

0 comments on commit ad9a0e4

Please sign in to comment.