Skip to content

Commit

Permalink
Merge pull request #145 from TRIP-Side-Project/dev
Browse files Browse the repository at this point in the history
12.28 main merge
  • Loading branch information
don9m1n authored Dec 28, 2023
2 parents 1e83d2f + 7370e2f commit af9acab
Show file tree
Hide file tree
Showing 23 changed files with 662 additions and 66 deletions.
147 changes: 147 additions & 0 deletions src/main/java/com/api/trip/common/init/DevInitData.java
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);
}
}
}
29 changes: 19 additions & 10 deletions src/main/java/com/api/trip/common/init/InitData.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
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 com.api.trip.domain.tag.service.TagService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
Expand All @@ -19,24 +21,31 @@ public class InitData implements ApplicationRunner {

private final NaverClient naverClient;
private final NaverApiService naverApiService;
private final ItemService itemService;
private final TagService tagService;

@Override
public void run(ApplicationArguments args) throws Exception {

/*

ShoppingRequest request = ShoppingRequest.builder()
.start(1)
.display(100).build();

ShoppingResponse searchResponse = naverClient.search(request);
List<ShoppingItem> shoppingItems = naverApiService.doFilterCategory(searchResponse.getItems());
List<CreateItemRequest> createItemRequests = naverApiService.toCreateItemRequest(shoppingItems);

for(CreateItemRequest createItemRequest :createItemRequests)
System.out.println(createItemRequest.toString());
for(int i = 0; i < 1; i++) {

*/
ShoppingRequest request = ShoppingRequest.builder()
.start(1 + i * 100)
.display(100).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);



}

}
}
71 changes: 71 additions & 0 deletions src/main/java/com/api/trip/common/schedulerRunner.java
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("데이터 갱신 종료");
*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic

String registrationId = userRequest.getClientRegistration().getRegistrationId();
String userNameAttributeName = userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUserNameAttributeName();
String accessToken = userRequest.getAccessToken().getTokenValue();

OAuth2Attribute oAuth2Attribute = OAuth2Attribute.of(registrationId, userNameAttributeName, oAuth2User.getAttributes());
OAuth2Attribute oAuth2Attribute = OAuth2Attribute.of(registrationId, accessToken, userNameAttributeName, oAuth2User.getAttributes());
Map<String, Object> memberAttribute = oAuth2Attribute.convertToMap();

return new DefaultOAuth2User(Collections.singleton(new SimpleGrantedAuthority("ROLE_MEMBER")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.api.trip.common.exception.ErrorCode;
import com.api.trip.common.exception.custom_exception.NotFoundException;
import com.api.trip.domain.member.model.SocialCode;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -22,18 +23,20 @@ public class OAuth2Attribute {
private String name; // 이름
private String picture; // 프로필 사진
private String provider; // 플랫폼
private SocialCode socialCode;
private String accessToken; // 소셜 accessToken

static OAuth2Attribute of(String provider, String attributeKey, Map<String, Object> attributes) {
static OAuth2Attribute of(String provider, String accessToken, String attributeKey, Map<String, Object> attributes) {
// 각 플랫폼 별로 제공해주는 데이터가 조금씩 다르기 때문에 분기 처리함.
return switch (provider) {
case "google" -> google(provider, attributeKey, attributes);
case "kakao" -> kakao(provider, attributeKey, attributes);
case "naver" -> naver(provider, attributeKey, attributes);
case "google" -> google(provider, accessToken, attributeKey, attributes);
case "kakao" -> kakao(provider, accessToken, attributeKey, attributes);
case "naver" -> naver(provider, accessToken, attributeKey, attributes);
default -> throw new NotFoundException(ErrorCode.NOT_FOUND_PROVIDER);
};
}

private static OAuth2Attribute google(String provider, String attributeKey, Map<String, Object> attributes) {
private static OAuth2Attribute google(String provider, String accessToken, String attributeKey, Map<String, Object> attributes) {
log.debug("google: {}", attributes);
return OAuth2Attribute.builder()
.email((String) attributes.get("email"))
Expand All @@ -42,10 +45,12 @@ private static OAuth2Attribute google(String provider, String attributeKey, Map<
.attributes(attributes)
.attributeKey(attributeKey)
.provider(provider)
.socialCode(SocialCode.GOOGLE)
.accessToken(accessToken)
.build();
}

private static OAuth2Attribute kakao(String provider, String attributeKey, Map<String, Object> attributes) {
private static OAuth2Attribute kakao(String provider, String accessToken, String attributeKey, Map<String, Object> attributes) {
Map<String, Object> kakaoAccount = (Map<String, Object>) attributes.get("kakao_account");
Map<String, Object> kakaoProfile = (Map<String, Object>) kakaoAccount.get("profile");

Expand All @@ -56,10 +61,12 @@ private static OAuth2Attribute kakao(String provider, String attributeKey, Map<S
.attributes(kakaoAccount)
.attributeKey(attributeKey)
.provider(provider)
.socialCode(SocialCode.KAKAO)
.accessToken(accessToken)
.build();
}

private static OAuth2Attribute naver(String provider, String attributeKey, Map<String, Object> attributes) {
private static OAuth2Attribute naver(String provider, String accessToken, String attributeKey, Map<String, Object> attributes) {
Map<String, Object> response = (Map<String, Object>) attributes.get("response");

return OAuth2Attribute.builder()
Expand All @@ -69,6 +76,8 @@ private static OAuth2Attribute naver(String provider, String attributeKey, Map<S
.attributes(response)
.attributeKey(attributeKey)
.provider(provider)
.socialCode(SocialCode.NAVER)
.accessToken(accessToken)
.build();
}

Expand All @@ -82,6 +91,8 @@ public Map<String, Object> convertToMap() {
map.put("name", name);
map.put("picture", picture);
map.put("provider", provider);
map.put("accessToken", accessToken);
map.put("socialCode", socialCode);

return map;
}
Expand Down
Loading

0 comments on commit af9acab

Please sign in to comment.