-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
343 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/com/example/SWExhibition/api/BoxOfficeApiController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.example.SWExhibition.api; | ||
|
||
import com.example.SWExhibition.service.BoxOfficeService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.json.simple.parser.ParseException; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
public class BoxOfficeApiController { | ||
|
||
private final BoxOfficeService boxOfficeService; | ||
|
||
@GetMapping("/api/get/dailyBoxOffice") | ||
public List<?> dailyBoxOffice() throws ParseException { | ||
boxOfficeService.deleteAll(); // 초기화 | ||
boxOfficeService.save(); // 전날 박스오피스 정보 삽입 | ||
return boxOfficeService.returnBoxOffice(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/com/example/SWExhibition/dto/BoxOfficeDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.example.SWExhibition.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class BoxOfficeDto { | ||
|
||
private Long id; | ||
private String movieCd; // 영화 코드 | ||
private String rank; // 순위 | ||
private String audiCnt; // 해당일 관객수 | ||
private String audiAcc; // 누적관객수 | ||
|
||
@Builder | ||
public BoxOfficeDto(Long id, String movieCd, String rank, String audiCnt, String audiAcc) { | ||
this.id = id; | ||
this.movieCd = movieCd; | ||
this.rank = rank; | ||
this.audiCnt = audiCnt; | ||
this.audiAcc = audiAcc; | ||
} | ||
} |
26 changes: 17 additions & 9 deletions
26
src/main/java/com/example/SWExhibition/entity/BoxOffice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,37 @@ | ||
package com.example.SWExhibition.entity; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
import lombok.*; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@Table(name = "Box-Office") | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@ToString | ||
@Getter | ||
public class BoxOffice { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private Long id; | ||
private Long id; // 객체 id | ||
|
||
@OneToOne | ||
@JoinColumn(name = "movies_movieCd") | ||
private String movieCd; // 영화 코드 | ||
private Movies movies; // 영화 객체 | ||
|
||
@Column(nullable = false) | ||
private String rank; // 순위 | ||
|
||
@Column | ||
private String audiAcc; // 관객수 | ||
private String audiCnt; // 해당일 관객수 | ||
|
||
@Column | ||
private String audiAcc; // 누적관객수 | ||
|
||
@Builder | ||
public BoxOffice(Long id, Movies movies, String rank, String audiCnt, String audiAcc) { | ||
this.id = id; | ||
this.movies = movies; | ||
this.rank = rank; | ||
this.audiCnt = audiCnt; | ||
this.audiAcc = audiAcc; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/com/example/SWExhibition/repository/BoxOfficeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.example.SWExhibition.repository; | ||
|
||
import com.example.SWExhibition.entity.BoxOffice; | ||
import com.example.SWExhibition.entity.Movies; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface BoxOfficeRepository extends JpaRepository<BoxOffice, Long> { | ||
boolean existsByMovies(Movies movies); // DB에서 해당하는 movie 찾기 | ||
} |
142 changes: 142 additions & 0 deletions
142
src/main/java/com/example/SWExhibition/service/BoxOfficeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package com.example.SWExhibition.service; | ||
|
||
import com.example.SWExhibition.dto.BoxOfficeDto; | ||
import com.example.SWExhibition.entity.BoxOffice; | ||
import com.example.SWExhibition.entity.Movies; | ||
import com.example.SWExhibition.repository.BoxOfficeRepository; | ||
import com.example.SWExhibition.repository.MoviesRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.json.simple.JSONArray; | ||
import org.json.simple.JSONObject; | ||
import org.json.simple.parser.JSONParser; | ||
import org.json.simple.parser.ParseException; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Calendar; | ||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
@Slf4j | ||
public class BoxOfficeService { | ||
|
||
private final MoviesService moviesService; | ||
private final MoviesRepository moviesRepository; | ||
private final BoxOfficeRepository boxOfficeRepository; | ||
private final RestTemplate restTemplate; // rest 방식 api 호출 | ||
|
||
private final String boxOffieUrl = "http://kobis.or.kr/kobisopenapi/webservice/rest/boxoffice/searchDailyBoxOfficeList.json?"; // 영화 진흥 위원회 일별 박스오피스 api url | ||
private final String apiKey = "key=4801347d5095e67e89bbba73e7650760"; // api 키 값 | ||
|
||
private final String targetDt = yesterday(); // 어제 날짜 | ||
|
||
|
||
// 어제 날짜 구하기 yyyymmdd | ||
public String yesterday() { | ||
SimpleDateFormat Format = new SimpleDateFormat("yyyyMMdd"); | ||
Calendar cal = Calendar.getInstance(); | ||
|
||
cal.add(Calendar.DATE, -1); | ||
|
||
return Format.format(cal.getTime()); | ||
} | ||
|
||
// 영화 정보 불러오기 | ||
public List<BoxOfficeDto> dailyBoxOfficeInfo() throws ParseException { | ||
// 받아온 영화 이름을 url에 넣음 | ||
String url = new StringBuilder(boxOffieUrl) | ||
.append(apiKey) | ||
.append("&targetDt=") | ||
.append(this.targetDt) | ||
.toString(); | ||
|
||
// 영화 목록 api를 결과를 String 형태로 받음 | ||
String data = restTemplate.getForObject(url, String.class); | ||
log.info(data); | ||
|
||
// 응답 값 파싱 | ||
JSONParser jsonParser = new JSONParser(); | ||
JSONObject jsonObject = (JSONObject) jsonParser.parse(data); | ||
|
||
// 가장 큰 객체인 boxOfficeResult 파싱 | ||
JSONObject jsonBoxOfficeResult = (JSONObject) jsonObject.get("boxOfficeResult"); | ||
|
||
// 일별 박스오피스 정보를 배열로 담은 dailyBoxOfficeList 파싱 | ||
JSONArray jsonDailyBoxOfficeList = (JSONArray) jsonBoxOfficeResult.get("dailyBoxOfficeList"); | ||
log.info(jsonDailyBoxOfficeList.toString()); | ||
|
||
List<BoxOfficeDto> boxOfficeDtoList = new ArrayList<>(); // 필요한 데이터만 담은 Dto List | ||
|
||
// JSON 객체를 Dto로 변환하고 List에 추가 | ||
for (Object o : jsonDailyBoxOfficeList) { | ||
JSONObject item = (JSONObject) o; | ||
boxOfficeDtoList.add(toDto(item)); | ||
log.info(toDto(item).toString()); | ||
} | ||
|
||
return boxOfficeDtoList; | ||
} | ||
|
||
// 박스 오피스 DB에 저장 | ||
public void save() throws ParseException { | ||
List<BoxOfficeDto> boxOfficeDtoList = dailyBoxOfficeInfo(); | ||
|
||
// DB에 없으면 저장 | ||
for (BoxOfficeDto dto : boxOfficeDtoList) { | ||
// DB에 영화가 저장되어 있지 않으면 영화와 박스오피스 둘 다 저장 | ||
if (!moviesRepository.existsByMovieCd(dto.getMovieCd())) { | ||
Movies movies = moviesService.searchForBoxOffice(dto.getMovieCd()); | ||
log.info(movies.toString()); | ||
|
||
BoxOffice entity = toEntity(dto); | ||
log.info(entity.toString()); | ||
|
||
// 저장 | ||
boxOfficeRepository.save(entity); | ||
} | ||
// 박스 오피스만 저장 | ||
else { | ||
BoxOffice entity = toEntity(dto); | ||
log.info(entity.toString()); | ||
|
||
// 저장 | ||
boxOfficeRepository.save(entity); | ||
} | ||
} | ||
} | ||
|
||
// DB에 저장되어 있는 일별 박스오피스 값 주기 | ||
public List<BoxOffice> returnBoxOffice() throws ParseException { | ||
return boxOfficeRepository.findAll(); | ||
} | ||
|
||
// DB에 값 전부 삭제 하루마다 리셋 | ||
public void deleteAll() { | ||
boxOfficeRepository.deleteAll(); | ||
} | ||
|
||
// JSONObject -> Dto | ||
private BoxOfficeDto toDto(JSONObject item) { | ||
return BoxOfficeDto.builder() | ||
.audiAcc((String) item.get("audiAcc")) | ||
.audiCnt((String) item.get("audiCnt")) | ||
.movieCd((String) item.get("movieCd")) | ||
.rank((String) item.get("rank")) | ||
.build(); | ||
} | ||
|
||
// Dto -> Entity | ||
public BoxOffice toEntity(BoxOfficeDto dto) { | ||
return BoxOffice.builder() | ||
.audiAcc(dto.getAudiAcc()) | ||
.rank(dto.getRank()) | ||
.audiCnt(dto.getAudiCnt()) | ||
.id(dto.getId()) | ||
.movies(moviesRepository.findByMovieCd(dto.getMovieCd())) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.