Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

모범 음식점 가게 데이터 추가 로직 개선 (#19) #20

Merged
merged 11 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions .github/workflows/action-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ on:
# 단위 테스트 결과를 발행하기 위해 쓰기 권한을 주어야 합니다.
permissions: write-all

env:
GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}

jobs:
test: # 테스트를 수행합니다.
runs-on: ubuntu-latest # 실행 환경 지정
Expand All @@ -34,18 +37,6 @@ jobs:
java-version: '17'
distribution: 'corretto' # OpenJDK 배포사 corretto, temurin

- name: Copy secrets to application
env:
GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}
OCCUPY_SECRET_TEST_DIR: ./src/test/resources # 레포지토리 내 빈 env.yml의 위치 (test)
OCCUPY_SECRET_DIR: ./src/main/resources # 레포지토리 내 빈 env.yml의 위치 (main)
OCCUPY_SECRET_DIR_FILE_NAME: env.yml # 파일 이름

# base64 디코딩하여 파일에 secrets 값 복사
run: echo $OCCUPY_SECRET | base64 --decode > $OCCUPY_SECRET_DIR/$OCCUPY_SECRET_DIR_FILE_NAME &&
echo $OCCUPY_SECRET | base64 --decode > $OCCUPY_SECRET_TEST_DIR/$OCCUPY_SECRET_DIR_FILE_NAME


# github action 에서 Gradle dependency 캐시 사용
- name: Cache Gradle packages
uses: actions/cache@v3
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ out/
.DS_Store

###
/src/main/resources/images/
/src/main/resources/images/

env.yml
5 changes: 3 additions & 2 deletions src/main/java/com/nainga/nainga/NaingaApplication.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.nainga.nainga;

import com.nainga.nainga.domain.store.application.GoogleMapStoreService;
import com.nainga.nainga.domain.store.application.MobeomDataParser;
import com.nainga.nainga.global.config.EnvConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;

@SpringBootApplication
@PropertySource(value = {"classpath:env.yaml"}, factory = EnvConfig.class)
public class NaingaApplication {

public static void main(String[] args) {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/nainga/nainga/domain/store/api/StoreApi.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.nainga.nainga.domain.store.api;

import com.nainga.nainga.domain.store.application.GoogleMapStoreService;
import com.nainga.nainga.domain.store.dto.CreateDividedMobeomStoresResponse;
import com.nainga.nainga.global.util.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand All @@ -12,9 +14,15 @@ public class StoreApi {
private final GoogleMapStoreService googleMapStoreService;

@GetMapping("api/v1/store/mobeom")
public Result<String> createAllMobeomStores() {
googleMapStoreService.createAllMobeomStores();
public Result<String> createAllMobeomStores(@RequestParam(value = "fileName") String fileName) {
googleMapStoreService.createAllMobeomStores(fileName);
return new Result<>(Result.CODE_SUCCESS, Result.MESSAGE_OK, null);
}

@GetMapping("api/v1/store/dividedMobeom")
public Result<CreateDividedMobeomStoresResponse> createDividedMobeomStores(@RequestParam(value = "fileName") String fileName, @RequestParam(value = "dollars") double dollars, @RequestParam(value = "startIndex") int startIndex) {
CreateDividedMobeomStoresResponse response = googleMapStoreService.createDividedMobeomStores(fileName, dollars, startIndex);
System.out.println("response = " + response); //편하게 콘솔 로그에서 확인하기 위한 용도
return new Result<>(Result.CODE_SUCCESS, Result.MESSAGE_OK, response);
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,22 @@ public Optional<Store> findById(Long id) {
return result.stream().findAny();
}

public Optional<Store> findByDisplayName(String displayName) {
List<Store> result = em.createQuery("select s from Store s where s.displayName = :displayName", Store.class)
.setParameter("displayName", displayName)
.getResultList();
return result.stream().findAny();
}

public Optional<Store> findByGooglePlaceId(String googlePlaceId) {
List<Store> result = em.createQuery("select s from Store s where s.googlePlaceId = :googlePlaceId", Store.class)
.setParameter("googlePlaceId", googlePlaceId)
.getResultList();
return result.stream().findAny();
}

public List<Store> findAll() {
return em.createQuery("select s from Store s", Store.class)
.getResultList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.nainga.nainga.domain.store.dto;

import lombok.Data;

@Data
public class CreateDividedMobeomStoresResponse {
private Double dollars; //API Call하고 남은 달러 리턴
private int nextIndex; //조회가 모두 끝났으면 -1 리턴, 아직 안끝났으면 다음에 시작해야할 인덱스 리턴
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ public List<StoreCertification> findAll() {
return em.createQuery("select sc from StoreCertification sc", StoreCertification.class)
.getResultList();
}

public Optional<StoreCertification> findByStoreIdCertificationId(Long storeId, Long certificationId) {
List<StoreCertification> result = em.createQuery("select sc from StoreCertification sc where sc.store.id = :storeId and sc.certification.id = :certificationId", StoreCertification.class)
.setParameter("storeId", storeId)
.setParameter("certificationId", certificationId)
.getResultList();
return result.stream().findAny();
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/nainga/nainga/global/config/EnvConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.nainga.nainga.global.config;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;

import java.io.IOException;
import java.util.Objects;
import java.util.Properties;

public class EnvConfig implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
factoryBean.setResources(resource.getResource());
Properties properties = factoryBean.getObject();
assert properties != null;
return new PropertiesPropertySource(Objects.requireNonNull(resource.getResource().getFilename()), properties);
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ spring:
# show_sql: true #밑에 org.hivernate.SQL과 겹치는데 얘는 System.out으로 찍는 거고 밑에는 로그로 찍는거. 얘를 안쓰는 걸 추천
format_sql: true
default_batch_fetch_size: 1000 # 배치 사이즈 글로벌 적용 (1+n) 문제 해결
profiles:
include: env

logging: #로그 레벨을 정하는 것
level:
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ spring:
# show_sql: true #밑에 org.hivernate.SQL과 겹치는데 얘는 System.out으로 찍는 거고 밑에는 로그로 찍는거. 얘를 안쓰는 걸 추천
format_sql: true
default_batch_fetch_size: 1000 # 배치 사이즈 글로벌 적용 (1+n) 문제 해결
profiles:
include: env

logging: #로그 레벨을 정하는 것
level:
Expand Down
Binary file modified src/main/resources/data/mobeom/mobeom_test.xlsx
Binary file not shown.
1 change: 1 addition & 0 deletions src/main/resources/env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GOOGLE_API_KEY: AIzaSyBIXhFd1Ya05ZoqhpVxpc7YysdWAo3z1EM
2 changes: 2 additions & 0 deletions src/test/java/com/nainga/nainga/NaingaApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;

@TestPropertySource(properties = {"GOOGLE_API_KEY"})
@SpringBootTest
class NaingaApplicationTests {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.nainga.nainga.domain.store.application;

import com.nainga.nainga.domain.certification.dao.CertificationRepository;
import com.nainga.nainga.domain.certification.domain.Certification;
import com.nainga.nainga.domain.store.dao.StoreRepository;
import com.nainga.nainga.domain.store.domain.Store;
import com.nainga.nainga.domain.store.dto.CreateDividedMobeomStoresResponse;
import com.nainga.nainga.domain.storecertification.dao.StoreCertificationRepository;
import com.nainga.nainga.domain.storecertification.domain.StoreCertification;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

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

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@Transactional //테스트가 끝난 뒤 롤백하기 위해
class GoogleMapStoreServiceTest {

@Autowired
GoogleMapStoreService googleMapStoreService;

@Autowired
StoreRepository storeRepository;

@Autowired
CertificationRepository certificationRepository;

@Autowired
StoreCertificationRepository storeCertificationRepository;

@Test
void createAllMobeomStores() {
//given
//테스트용 mobeom_test.xlsx가 주어졌을 때!
//이 테스트 파일은 중복된 가게, 폐업된 가게, 모범 음식점에 선정되었다가 취소된 가게 등의 케이스를 모두 포함하고 있다.
googleMapStoreService.createAllMobeomStores("mobeom_test.xlsx");

//when
List<Store> stores = storeRepository.findAll();
List<Certification> certifications = certificationRepository.findAll();
List<StoreCertification> storeCertifications = storeCertificationRepository.findAll();
Optional<Store> cheongjinok = storeRepository.findByDisplayName("청진옥");
Optional<Store> maknae = storeRepository.findByDisplayName("막내회집 광교점");
Optional<Store> onejo = storeRepository.findByDisplayName("원조할머니 낙지센타");
Optional<Store> hamhueng = storeRepository.findByDisplayName("함흥곰보냉면");

//then
assertThat(stores.size()).isEqualTo(2);
assertThat(certifications.size()).isEqualTo(1);
assertThat(storeCertifications.size()).isEqualTo(2);
assertThat(cheongjinok).isEmpty();
assertThat(maknae).isPresent();
assertThat(onejo).isEmpty();
assertThat(hamhueng).isPresent();
}

@Test
void createDividedMobeomStores() {
//given
//테스트용 mobeom_test.xlsx가 주어졌을 때!
//이 테스트 파일은 중복된 가게, 폐업된 가게, 모범 음식점에 선정되었다가 취소된 가게 등의 케이스를 모두 포함하고 있다.
CreateDividedMobeomStoresResponse result = googleMapStoreService.createDividedMobeomStores("mobeom_test.xlsx", 1000, 0);

//when

//then
assertThat(Math.floor(result.getDollars() * 10000) / 10000).isEqualTo(999.9460); //부동 소수점 계산 오차 때문에 소수점 넷째자리까지만 표현
assertThat(result.getNextIndex()).isEqualTo(-1); //한 싸이클 모두 조회가 되어야 함

//그 뒤로 바로 이어서 동일한 메서드를 추가 호출했을 때, 이미 DB에 등록된 상태이므로 API call이 나가지 않아야 함
CreateDividedMobeomStoresResponse result2 = googleMapStoreService.createDividedMobeomStores("mobeom_test.xlsx", 1000, 0);
assertThat(result2.getDollars()).isEqualTo(1000); //API call이 나가지 않아서 비용 그대로!
assertThat(result2.getNextIndex()).isEqualTo(-1); //한 싸이클은 전부 조회하므로 -1 리턴
}
}
2 changes: 1 addition & 1 deletion src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ spring:
properties:
hibernate:
format_sql: true

logging:
level:
org.hibernate.SQL: debug
1 change: 1 addition & 0 deletions src/test/resources/env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GOOGLE_API_KEY: AIzaSyBIXhFd1Ya05ZoqhpVxpc7YysdWAo3z1EM