Skip to content

Commit

Permalink
✨Feat: 스크랩 데이터 제공 API
Browse files Browse the repository at this point in the history
✨Feat: 스크랩 데이터 제공 API
  • Loading branch information
MeongW authored Oct 3, 2024
2 parents 0641694 + 45ce8f8 commit 1a8288d
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/korad1004/back_end/config/RestClientConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.korad1004.back_end.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;

@Configuration
public class RestClientConfig {

@Bean
public RestClient restClient(RestClient.Builder builder) {
return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.korad1004.back_end.global.handler;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<String> handleTypeMismatchException(MethodArgumentTypeMismatchException exception) {
String errorMessage = "잘못된 요청: '" + exception.getValue() + "'의 타입이 잘못되었습니다.";
return ResponseEntity.badRequest().body(errorMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.korad1004.back_end.scrap.controller;

import com.korad1004.back_end.scrap.dto.KoradNaverBlogRecentArticleDto;
import com.korad1004.back_end.scrap.service.ScrapService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Tag(name = "scrap-controller")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/scrap/korad-naver-blog")
@Slf4j
public class KoradNaverBlogScrapController {

private final ScrapService scrapService;

@Value("${lambda.api.korad-naver-blog.article.recent}")
private String apiUrl;

@GetMapping("/article/recent/{cnt}")
public ResponseEntity<?> getKoradNaverBlogRecentArticle(@PathVariable Integer cnt) {
if (cnt <= 0) {
return ResponseEntity.badRequest().body("cnt는 0 이하의 숫자값을 가질 수 없습니다.");
}

String url = apiUrl + "?cnt=" + cnt;

return scrapService.getScrapResponse(url, KoradNaverBlogRecentArticleDto.class);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.korad1004.back_end.scrap.controller;

import com.korad1004.back_end.scrap.dto.KoradNoticeRecentArticleDto;
import com.korad1004.back_end.scrap.service.ScrapService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Tag(name = "scrap-controller")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/scrap/korad-official-website")
public class KoradNoticeScrapController {

private final ScrapService scrapService;

@Value("${lambda.api.korad-official-website.notice.recent}")
private String apiUrl;

@GetMapping("/notice/recent/{cnt}")
public ResponseEntity<?> getKoradOfficialWebsiteNoticeRecent(@PathVariable Integer cnt) {
if (cnt <= 0) {
return ResponseEntity.badRequest().body("cnt는 0 이하의 숫자값을 가질 수 없습니다.");
}

String url = apiUrl + "?cnt=" + cnt;

return scrapService.getScrapResponse(url, KoradNoticeRecentArticleDto.class);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.korad1004.back_end.scrap.controller;

import com.korad1004.back_end.scrap.dto.NationWideRadiationAverageDto;
import com.korad1004.back_end.scrap.service.ScrapService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Tag(name = "scrap-controller")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/scrap/radiation")
public class RadiationScrapController {

private final ScrapService scrapService;

@Value("${lambda.api.radiation.average}")
private String apiUrl;

@GetMapping("/average")
public ResponseEntity<?> getRadiationAverage() {

String url = apiUrl;

return scrapService.getScrapResponse(url, NationWideRadiationAverageDto.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.korad1004.back_end.scrap.dto;

import lombok.Data;

@Data
public class KoradNaverBlogRecentArticleDto {

private int idx;
private Content content;

@Data
public static class Content {
private String notice_num;
private String title;
private String content;
private String write_date;
private String url;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.korad1004.back_end.scrap.dto;

import lombok.Data;

@Data
public class KoradNoticeRecentArticleDto {
private int idx;
private KoradNaverBlogRecentArticleDto.Content content;

@Data
public static class Content {
private String notice_num;
private String title;
private String content;
private String write_date;
private String url;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.korad1004.back_end.scrap.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
public class NationWideRadiationAverageDto {
@JsonProperty(value = "지역")
private String region;
private Double avgRad;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.korad1004.back_end.scrap.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class ScrapService {
private final RestClient restClient;

public <T> ResponseEntity<?> getScrapResponse(String url, Class<T> dto) {
try {
ResponseEntity<List<T>> responseEntity = restClient.get()
.uri(url)
.retrieve()
.toEntity(new ParameterizedTypeReference<>() {});


if (!responseEntity.getStatusCode().is2xxSuccessful()) {
return ResponseEntity.status(responseEntity.getStatusCode()).body(responseEntity.getBody());
}

List<?> data = responseEntity.getBody();
if (data == null || data.isEmpty()) {
log.error("Response 에러: {}", data);
return ResponseEntity.internalServerError().body("스크랩 API 서버의 요청에 문제가 발생하였습니다.");
}

return ResponseEntity.ok(data);
} catch (Exception e) {
log.error("getScrapResponse 에러 : {}", e.getMessage());
return ResponseEntity.status(500).body("스크랩 API 서버와의 통신에 실패하였습니다.");
}
}
}

0 comments on commit 1a8288d

Please sign in to comment.