-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
238d1d3
commit bccc31f
Showing
9 changed files
with
197 additions
and
137 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
67 changes: 67 additions & 0 deletions
67
src/main/java/com/catcher/batch/core/service/MovieService.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,67 @@ | ||
package com.catcher.batch.core.service; | ||
|
||
import com.catcher.batch.core.database.CatcherItemRepository; | ||
import com.catcher.batch.core.database.CategoryRepository; | ||
import com.catcher.batch.core.domain.entity.CatcherItem; | ||
import com.catcher.batch.core.domain.entity.Category; | ||
import com.catcher.batch.core.dto.MovieApiResponse; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDate; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.catcher.batch.common.utils.HashCodeGenerator.hashString; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class MovieService { | ||
private final CatcherItemRepository catcherItemRepository; | ||
private final CategoryRepository categoryRepository; | ||
public static final String CATEGORY_NAME = "movie"; | ||
|
||
@Transactional | ||
public void batch(List<MovieApiResponse.MovieItem> movies){ | ||
|
||
Category category = categoryRepository.findByName(CATEGORY_NAME) | ||
.orElseGet(()->categoryRepository.save(Category.create(CATEGORY_NAME))); | ||
|
||
Map<String, CatcherItem> itemHashKeyToDBItem = catcherItemRepository.findByCategory(category).stream() | ||
.collect(Collectors.toMap(CatcherItem::getItemHashValue, Function.identity())); | ||
|
||
|
||
//TODO: 썸네일URL, 개봉일 변경 여부 확인 로직 필요 | ||
|
||
// Map<String, MovieApiResponse.MovieItem> itemHashKeyToApiItem = movies.stream() | ||
// .collect(Collectors.toMap( | ||
// movieItem -> hashString(CATEGORY_NAME, String.valueOf(movieItem.getId())), | ||
// movieItem -> movieItem | ||
// )); | ||
|
||
// List<CatcherItem> catcherItems = itemHashKeyToApiItem.entrySet().stream() | ||
// .map(entry -> { | ||
// DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE; | ||
// LocalDate localDate = LocalDate.parse(entry.getValue().getReleaseDate(), dateFormatter); | ||
// | ||
// ZonedDateTime releaseDate = localDate.atStartOfDay(ZoneId.systemDefault()); | ||
// | ||
// return CatcherItem.builder() | ||
// .category(category) | ||
// .title(entry.getValue().getTitle()) | ||
// .itemHashValue(entry.getKey()) | ||
// .thumbnailUrl(entry.getValue().getPosterPath()) | ||
// .startAt(releaseDate) | ||
// .build(); | ||
// }) | ||
// .collect(Collectors.toList()); | ||
//DB에 추가 | ||
// catcherItemRepository.saveAll(catcherItems); | ||
} | ||
} |
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
43 changes: 24 additions & 19 deletions
43
src/main/java/com/catcher/batch/infrastructure/properties/MovieProperties.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,52 +1,57 @@ | ||
package com.catcher.batch.infrastructure.properties; | ||
|
||
import com.catcher.batch.core.dto.MovieApiResponse; | ||
import com.catcher.batch.core.properties.HeaderSupport; | ||
import com.catcher.batch.core.properties.PropertyBase; | ||
import com.catcher.batch.infrastructure.utils.KmsUtils; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import java.net.URI; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Map; | ||
|
||
@Component | ||
public class MovieProperties extends PropertyBase { | ||
public class MovieProperties extends PropertyBase implements HeaderSupport { | ||
|
||
@Value("${movie.key}") | ||
private String serviceKey; | ||
|
||
private String targetDate; | ||
|
||
public MovieProperties(@Value("${movie.baseUrl}") String endPoint) { | ||
super(endPoint); | ||
} | ||
|
||
@Override | ||
public boolean support(Class<?> clazz) { | ||
if (clazz.isAssignableFrom(MovieApiResponse.class)) { | ||
initCurrentDate(); | ||
return true; | ||
} | ||
return false; | ||
return clazz.isAssignableFrom(MovieApiResponse.class); | ||
} | ||
|
||
@Override | ||
public URI getURI() { | ||
return UriComponentsBuilder | ||
UriComponentsBuilder uriBuilder = UriComponentsBuilder | ||
.fromUriString(this.getEndPoint()) | ||
.queryParam("key", KmsUtils.decrypt(serviceKey)) | ||
.queryParam("targetDt", targetDate) | ||
.queryParam("language", "ko-KR"); | ||
|
||
return this.addParams(uriBuilder) | ||
.build().toUri(); | ||
} | ||
|
||
private void initCurrentDate() { | ||
ZonedDateTime currentDateTime = ZonedDateTime.now(); | ||
ZonedDateTime previousDate = currentDateTime.minus(1, ChronoUnit.DAYS); | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); | ||
private UriComponentsBuilder addParams(UriComponentsBuilder uriComponentsBuilder ) { | ||
Map<String, Object> params = getParams(); | ||
for (String key : params.keySet()) { | ||
uriComponentsBuilder | ||
.queryParam(key, params.get(key)); | ||
} | ||
return uriComponentsBuilder; | ||
} | ||
|
||
targetDate = previousDate.format(formatter); | ||
@Override | ||
public HttpHeaders addHeaders() { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("Authorization", "Bearer " + KmsUtils.decrypt(serviceKey)); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
return headers; | ||
} | ||
} |
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
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
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
Oops, something went wrong.