-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from TRIP-Side-Project/dev
12.28 main merge
- Loading branch information
Showing
23 changed files
with
662 additions
and
66 deletions.
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
src/main/java/com/api/trip/common/init/DevInitData.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,147 @@ | ||
package com.api.trip.common.init; | ||
|
||
import com.api.trip.domain.article.model.Article; | ||
import com.api.trip.domain.article.repository.ArticleRepository; | ||
import com.api.trip.domain.comment.model.Comment; | ||
import com.api.trip.domain.comment.repository.CommentRepository; | ||
import com.api.trip.domain.interestarticle.model.InterestArticle; | ||
import com.api.trip.domain.interestarticle.repository.InterestArticleRepository; | ||
import com.api.trip.domain.interestitem.model.InterestItem; | ||
import com.api.trip.domain.interestitem.repository.InterestItemRepository; | ||
import com.api.trip.domain.interesttag.model.InterestTag; | ||
import com.api.trip.domain.interesttag.respository.InterestTagRepository; | ||
import com.api.trip.domain.item.model.Item; | ||
import com.api.trip.domain.item.repository.ItemRepository; | ||
import com.api.trip.domain.member.model.Member; | ||
import com.api.trip.domain.member.model.SocialCode; | ||
import com.api.trip.domain.member.repository.MemberRepository; | ||
import com.api.trip.domain.tag.model.Tag; | ||
import com.api.trip.domain.tag.repository.TagRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
import java.util.ArrayList; | ||
|
||
@Configuration | ||
@Profile("dev") | ||
@RequiredArgsConstructor | ||
public class DevInitData { | ||
|
||
private final MemberRepository memberRepository; | ||
private final PasswordEncoder passwordEncoder; | ||
private final ArticleRepository articleRepository; | ||
private final CommentRepository commentRepository; | ||
private final ItemRepository itemRepository; | ||
private final InterestItemRepository interestItemRepository; | ||
private final TagRepository tagRepository; | ||
private final InterestTagRepository interestTagRepository; | ||
|
||
@Value("${cloud.aws.default-image}") | ||
private String defaultProfileImg; | ||
|
||
@Bean | ||
public CommandLineRunner init() { | ||
return args -> { | ||
Member member1 = memberRepository.save(createMember("[email protected]", "trip123", passwordEncoder.encode("trip1234"))); | ||
Member member2 = memberRepository.save(createMember("[email protected]", "admin", passwordEncoder.encode("trip1234"))); | ||
|
||
Article article1 = articleRepository.save(createArticle("title1", "content1", member1)); | ||
Article article2 = articleRepository.save(createArticle("title2", "content2", member1)); | ||
Article article3 = articleRepository.save(createArticle("title3", "content3", member1)); | ||
|
||
Comment comment1 = commentRepository.save(createComment(article1, member1, "댓글1", null)); | ||
Comment comment2 = commentRepository.save(createComment(article1, member1, "댓글2", comment1)); | ||
Comment comment3 = commentRepository.save(createComment(article1, member1, "댓글3", null)); | ||
commentRepository.save(createComment(article1, member1, "댓글4", comment3)); | ||
|
||
// 2번 회원이 상품 2개 추가 | ||
Item item1 = itemRepository.save(createItem(1L, "서울/경기도 투어", member2)); | ||
Item item2 = itemRepository.save(createItem(2L, "부산 해산물 투어", member2)); | ||
|
||
// 1번 회원이 상품 2개 좋아요 | ||
interestItemRepository.save(createInterestItem(item1, member1)); | ||
interestItemRepository.save(createInterestItem(item2, member1)); | ||
|
||
// 태그 생성 | ||
createTag(); | ||
|
||
// 관심 태그 설정 | ||
saveInterestTag(member1, "부산"); | ||
saveInterestTag(member1, "요트"); | ||
saveInterestTag(member1, "경주"); | ||
}; | ||
} | ||
|
||
private void saveInterestTag(Member member, String name) { | ||
InterestTag interestTag1 = InterestTag.builder() | ||
.tag(tagRepository.findByName(name)) | ||
.member(member) | ||
.build(); | ||
|
||
interestTagRepository.save(interestTag1); | ||
} | ||
|
||
private Member createMember(String email, String nickname, String password) { | ||
return Member.builder() | ||
.email(email) | ||
.nickname(nickname) | ||
.password(password) | ||
.profileImg(defaultProfileImg) | ||
.socialCode(SocialCode.NORMAL) | ||
.build(); | ||
} | ||
|
||
private Article createArticle(String title, String content, Member member) { | ||
return Article.builder() | ||
.title(title) | ||
.content(content) | ||
.writer(member) | ||
.build(); | ||
} | ||
|
||
private Comment createComment(Article article, Member member, String content, Comment parent) { | ||
return Comment.builder() | ||
.article(article) | ||
.writer(member) | ||
.content(content) | ||
.parent(parent) | ||
.build(); | ||
} | ||
|
||
private Item createItem(Long productId, String title, Member member) { | ||
return Item.builder() | ||
.productId(productId) | ||
.title(title) | ||
.shopName("trip") | ||
.buyUrl("buyUrl") | ||
.maxPrice(10000) | ||
.minPrice(100) | ||
.imageUrl("imageUrl") | ||
.writer(member) | ||
.build(); | ||
} | ||
|
||
private InterestItem createInterestItem(Item item, Member member) { | ||
return InterestItem.builder() | ||
.item(item) | ||
.member(member) | ||
.build(); | ||
} | ||
|
||
private void createTag() { | ||
String[] tags = {"부산", "요트", "경주", "눈꽃여행", "기차"}; | ||
|
||
for (String name : tags) { | ||
Tag tag = Tag.builder() | ||
.name(name) | ||
.build(); | ||
|
||
tagRepository.save(tag); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.api.trip.common; | ||
|
||
|
||
import com.api.trip.common.naverapi.NaverApiService; | ||
import com.api.trip.common.naverapi.NaverClient; | ||
import com.api.trip.common.naverapi.dto.ShoppingItem; | ||
import com.api.trip.common.naverapi.dto.ShoppingRequest; | ||
import com.api.trip.common.naverapi.dto.ShoppingResponse; | ||
import com.api.trip.domain.item.controller.dto.CreateItemRequest; | ||
import com.api.trip.domain.item.service.ItemService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.dao.DataIntegrityViolationException; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
@EnableScheduling | ||
public class schedulerRunner { | ||
|
||
private final NaverClient naverClient; | ||
private final NaverApiService naverApiService; | ||
private final ItemService itemService; | ||
|
||
/** | ||
* @Description | ||
* 1분에 한번씩 api를 요청하여 데이터 갱신(중단조건에 걸릴때까지 50개씩 반복요청) | ||
* 데이터가 중복됐을 때(이미 존재하는 데이터)를 중단조건 | ||
*/ | ||
@Scheduled(fixedDelay = 60000) | ||
public void updateData() | ||
{ | ||
/* | ||
System.out.println("schedulerRunner"); | ||
int i = 0; | ||
System.out.println("데이터 갱신 시작"); | ||
try { | ||
while(true){ | ||
System.out.println("=========================="); | ||
ShoppingRequest request = ShoppingRequest.builder() | ||
.start(1 + i * 50) | ||
.display(50).build(); | ||
ShoppingResponse searchResponse = naverClient.search(request); | ||
List<ShoppingItem> shoppingItems = naverApiService.doFilterCategory(searchResponse.getItems()); | ||
List<CreateItemRequest> createItemRequests = naverApiService.toCreateItemRequest(shoppingItems); | ||
for (CreateItemRequest createItemRequest : createItemRequests) | ||
itemService.createItem(createItemRequest); | ||
i++; | ||
} | ||
}catch (DataIntegrityViolationException e) | ||
{ | ||
System.out.println("데이터 중복"); | ||
} | ||
System.out.println("데이터 갱신 종료"); | ||
*/ | ||
} | ||
} |
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.