-
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
Showing
22 changed files
with
446 additions
and
64 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
9 changes: 9 additions & 0 deletions
9
src/main/java/com/catcher/batch/common/utils/HashCodeGenerator.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,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); | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
src/main/java/com/catcher/batch/core/database/CatcherItemRepository.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,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); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/catcher/batch/core/database/CategoryRepository.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.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); | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/catcher/batch/core/domain/command/RegisterRestaurantDataCommand.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,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; | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
src/main/java/com/catcher/batch/core/dto/RestaurantApiResponse.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,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; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/catcher/batch/core/properties/HeaderPropertyProxy.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,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(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/catcher/batch/core/properties/HeaderSupport.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,7 @@ | ||
package com.catcher.batch.core.properties; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
|
||
public interface HeaderSupport { | ||
HttpHeaders addHeaders(); | ||
} |
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
68 changes: 68 additions & 0 deletions
68
src/main/java/com/catcher/batch/core/service/RestaurantService.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,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(); | ||
} | ||
} |
Oops, something went wrong.