Skip to content

Commit

Permalink
resolve conflict error
Browse files Browse the repository at this point in the history
  • Loading branch information
cheolwon1994 committed Nov 15, 2023
2 parents 0b652b9 + 24d160d commit a10ff4a
Show file tree
Hide file tree
Showing 22 changed files with 446 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.catcher.batch.core.converter.CatcherConverter;
import com.catcher.batch.core.properties.PropertyBase;
import com.catcher.batch.core.properties.HeaderSupport;
import com.catcher.batch.resource.external.ExternalFeign;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -40,6 +42,13 @@ public <T> T parseService(Map<String, Object> params, Class<T> requestType) {
property.setParams(params);
URI uri = property.getURI();

// 헤더가 있는 경우
HttpHeaders headers;
if (property instanceof HeaderSupport headerSupport) {
headers = headerSupport.addHeaders();
return catcherConverter.parse(externalFeign.getInfoWithHeader(headers.getFirst("Authorization"), uri), requestType);
}

return catcherConverter.parse(externalFeign.getInfo(uri), requestType);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.catcher.batch.common.utils;

import org.apache.commons.codec.digest.DigestUtils;

public class HashCodeGenerator {
public static String hashString(String category, String input) {
return DigestUtils.sha256Hex(category + "-" + input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.catcher.batch.annotation.CatcherJson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -33,6 +34,8 @@ private <T> String getPath(Class<T> responseType) {
private JSONObject getJsonObject(String json, String jsonPath) {
if(jsonPath == null) {
throw new IllegalStateException();
} else if (StringUtils.isBlank(jsonPath)) {
return new JSONObject(json);
}

JSONObject jsonObject = new JSONObject(json);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.catcher.batch.core.database;

import com.catcher.batch.core.domain.entity.CatcherItem;
import com.catcher.batch.core.domain.entity.Category;

import java.util.List;
import java.util.Optional;

public interface CatcherItemRepository {
void saveAll(List<CatcherItem> catcherItems);

void save(CatcherItem catcherItem);

Optional<CatcherItem> findByItemHashValue(String hashKey);

List<CatcherItem> findByCategory(Category category);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.catcher.batch.core.database;

import com.catcher.batch.core.domain.entity.Category;

import java.util.Optional;

public interface CategoryRepository {
Optional<Category> findByName(String name);

Category save(Category category);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.catcher.batch.core.domain.command;

import com.catcher.batch.core.dto.RestaurantApiResponse;
import com.catcher.batch.core.service.RestaurantService;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class RegisterRestaurantDataCommand implements Command<Void> {
private final RestaurantService restaurantService;
private final RestaurantApiResponse restaurantApiResponse;

@Override
public Void execute() {
restaurantService.batch(restaurantApiResponse);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Where(clause = "delete_at = 'N'")
@Where(clause = "deleted_at IS NULL")
@Table(name = "catcher_item")
public class CatcherItem extends BaseTimeEntity {
@Id
Expand Down Expand Up @@ -44,6 +44,6 @@ public class CatcherItem extends BaseTimeEntity {
@Column(name = "end_at")
private ZonedDateTime endAt;

@Column(name = "delete_at")
@Column(name = "deleted_at")
private ZonedDateTime deletedAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.catcher.batch.core.dto;

import com.catcher.batch.annotation.CatcherJson;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;

import java.util.List;

@Getter
@JsonIgnoreProperties(ignoreUnknown = true)
@CatcherJson(path = "")
public class RestaurantApiResponse {

@JsonProperty("meta")
private Meta meta;

@JsonProperty("documents")
private List<RestaurantItem> items;

@Getter
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Meta {

@JsonProperty("pageable_count")
private int pageableCount;

@JsonProperty("total_count")
private int totalCount;

@JsonProperty("is_end")
private boolean isEnd;
}

@Getter
@JsonIgnoreProperties(ignoreUnknown = true)
public static class RestaurantItem {

@JsonProperty("id")
private String key;

@JsonProperty("place_name")
private String name;

@JsonProperty("place_url")
private String resourceUrl;

@JsonProperty("address_name")
private String address;

@JsonProperty("x")
private String latitude;

@JsonProperty("y")
private String longitude;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.catcher.batch.core.properties;

import org.springframework.http.HttpHeaders;

import java.net.URI;
import java.util.Map;

public class HeaderPropertyProxy extends PropertyBase {
private final PropertyBase property;

public HeaderPropertyProxy(PropertyBase property) {
super(property.getEndPoint());
this.property = property;
}

@Override
public boolean support(Class<?> clazz) {
return property.support(clazz);
}

@Override
public URI getURI() {
return property.getURI();
}

@Override
public void setParams(Map<String, Object> params) {
property.setParams(params);
}

public HttpHeaders addHeaders() {
if (property instanceof HeaderSupport) {
return ((HeaderSupport) property).addHeaders();
}
return new HttpHeaders();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.catcher.batch.core.properties;

import org.springframework.http.HttpHeaders;

public interface HeaderSupport {
HttpHeaders addHeaders();
}
59 changes: 26 additions & 33 deletions src/main/java/com/catcher/batch/core/service/CampingService.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
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.database.LocationRepository;
import com.catcher.batch.core.domain.entity.CatcherItem;
import com.catcher.batch.core.domain.entity.Category;
import com.catcher.batch.core.domain.entity.Location;
import com.catcher.batch.core.dto.CampingApiResponse;
import com.catcher.batch.datasource.CategoryRepository;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.catcher.batch.common.utils.HashCodeGenerator.hashString;

@Service
@RequiredArgsConstructor
Expand All @@ -28,33 +30,33 @@ public void batch(CampingApiResponse campingApiResponse) {
Category category = categoryRepository.findByName(CATEGORY_NAME)
.orElseGet(() -> categoryRepository.save(Category.create(CATEGORY_NAME)));

List<CampingApiResponse.CampingItem> campingItems = campingApiResponse.getItems().getItem();
List<CatcherItem> catcherItems = new ArrayList<>();
Map<String, String> itemMap = catcherItemRepository.findByCategory(category).stream()
.collect(Collectors.toMap(CatcherItem::getItemHashValue, CatcherItem::getTitle));

for (CampingApiResponse.CampingItem campingItem : campingItems) {
Location location = getLocationByDescription(campingItem.getProvince(), campingItem.getCity());
List<CampingApiResponse.CampingItem> campingItems = campingApiResponse.getItems().getItem();

String hashKey = hashString(campingItem.getKey());
List<CatcherItem> catcherItems = campingItems.stream()
.filter(campingItem -> !itemMap.containsKey(hashString(CATEGORY_NAME, campingItem.getKey())))
.map(campingItem -> {
Location location = getLocationByDescription(campingItem.getProvince(), campingItem.getCity());
String hashKey = hashString(CATEGORY_NAME, campingItem.getKey());

// 중복 해시값 체크
if (isDuplicateHashValue(hashKey)) {
System.out.println("중복됨");
continue;
}
itemMap.put(hashKey, campingItem.getName());

CatcherItem catcherItem = CatcherItem.builder()
.category(category)
.location(location)
.title(campingItem.getName())
.description(campingItem.getDescription())
.thumbnailUrl(campingItem.getThumbnailUrl())
.itemHashValue(hashKey)
.build();
return CatcherItem.builder()
.category(category)
.location(location)
.title(campingItem.getName())
.description(campingItem.getDescription())
.thumbnailUrl(campingItem.getThumbnailUrl())
.itemHashValue(hashKey)
.build();
})
.collect(Collectors.toList());

catcherItems.add(catcherItem);
if (!catcherItems.isEmpty()) {
catcherItemRepository.saveAll(catcherItems);
}

catcherItemRepository.saveAll(catcherItems);
}

private Location getLocationByDescription(String province, String city) {
Expand All @@ -63,13 +65,4 @@ private Location getLocationByDescription(String province, String city) {
return locationRepository.findByDescription(withoutDo, city)
.orElseThrow();
}

// TODO : 중복체크가 안됨 ㅜ.ㅜ
private boolean isDuplicateHashValue(String hashKey) {
return catcherItemRepository.findByItemHashValue(hashKey).isPresent();
}

private String hashString(String input) {
return DigestUtils.sha256Hex(input);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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.database.LocationRepository;
import com.catcher.batch.core.domain.entity.CatcherItem;
import com.catcher.batch.core.domain.entity.Category;
import com.catcher.batch.core.domain.entity.Location;
import com.catcher.batch.core.dto.RestaurantApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.catcher.batch.common.utils.HashCodeGenerator.hashString;

@Service
@RequiredArgsConstructor
public class RestaurantService {
private final CatcherItemRepository catcherItemRepository;
private final CategoryRepository categoryRepository;
private final LocationRepository locationRepository;
public static final String CATEGORY_NAME = "restaurant";

@Transactional
public void batch(RestaurantApiResponse restaurantApiResponse) {
Category category = categoryRepository.findByName(CATEGORY_NAME)
.orElseGet(() -> categoryRepository.save(Category.create(CATEGORY_NAME)));

Map<String, String> itemMap = catcherItemRepository.findByCategory(category).stream()
.collect(Collectors.toMap(CatcherItem::getItemHashValue, CatcherItem::getTitle));

List<CatcherItem> catcherItems = restaurantApiResponse.getItems().stream()
.filter(item -> !itemMap.containsKey(hashString(CATEGORY_NAME, item.getKey())))
.map(item -> {
Location location = getLocation(item.getAddress());
String hashKey = hashString(CATEGORY_NAME, item.getKey());

itemMap.put(hashKey, item.getName());

return CatcherItem.builder()
.category(category)
.location(location)
.title(item.getName())
.resourceUrl(item.getResourceUrl())
.itemHashValue(hashKey)
.build();
})
.collect(Collectors.toList());

if (!catcherItems.isEmpty()) {
catcherItemRepository.saveAll(catcherItems);
}
}

private Location getLocation(String address) {
String[] parts = address.split("\\s+");

String province = parts[0];
String city = parts[1];

return locationRepository.findByDescription(province, city)
.orElseThrow();
}
}
Loading

0 comments on commit a10ff4a

Please sign in to comment.