Skip to content

Commit

Permalink
Merge pull request #158 from TRIP-Side-Project/dev
Browse files Browse the repository at this point in the history
main merge
  • Loading branch information
don9m1n authored Dec 28, 2023
2 parents b8cd6dc + 9ac9122 commit 652921d
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 13 deletions.
37 changes: 35 additions & 2 deletions src/main/java/com/api/trip/common/scheduler/SchedulerRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
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.common.sse.SseNotificationResponse;
import com.api.trip.common.sse.emitter.SseEmitterMap;
import com.api.trip.domain.item.controller.dto.CreateItemRequest;
import com.api.trip.domain.item.model.Item;
import com.api.trip.domain.item.service.ItemService;
import com.api.trip.domain.notification.service.NotificationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
Expand All @@ -27,7 +31,8 @@ public class SchedulerRunner {
private final NaverClient naverClient;
private final NaverApiService naverApiService;
private final ItemService itemService;

private final NotificationService notificationService;
private final SseEmitterMap sseEmitterMap;
/**
* @Description
* 5분에 한번씩 api를 요청하여 데이터 갱신(중단조건에 걸릴때까지 50개씩 반복요청)
Expand All @@ -49,7 +54,33 @@ public void updateData()
List<ShoppingItem> shoppingItems = naverApiService.doFilterCategory(searchResponse.getItems());
List<CreateItemRequest> createItemRequests = naverApiService.toCreateItemRequest(shoppingItems);
for (CreateItemRequest createItemRequest : createItemRequests) {
itemService.createItem(createItemRequest);
Item item = itemService.createItem(createItemRequest);
notificationService.createNotification(item, createItemRequest.getTagNames());
sseEmitterMap.sendToAll("notification",new SseNotificationResponse(item.getId(), createItemRequest.getTagNames()));
/**
*
* 알림이 가져야할 데이터가 itemId, memberId
* SSE보낼 때는 Object로 태그 넘기기
*/
/**
* 백
* 1. 아이템의 태그를 다 뽑아옴
* 2. 태그를 구독한 member를 다 뽑아오는 로직
* 3. 회원이 10000 -> "제주"
*
* 알림들의 id를 넘겨줄텐데
*
* server ||
*
* member1:"제주" -> 1
* member2:"제주" -> 2
* member3:"제주" -> 3
* member4:"제주" -> 4
*
* member1브라우저 || member1:"제주" -> tagNames = ["제주","서울], ids = [ 1,2,3,4]
* member2브라우저 || member1:"제주" -> 1,2,3,4
*/

}
i++;

Expand All @@ -63,5 +94,7 @@ public void updateData()
log.info("요청 범위 초과 - 데이터 갱신 종료");
}



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.IOException;
import java.util.Optional;
Expand Down Expand Up @@ -58,12 +59,16 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
// OAuth2User 객체에서 권한 가져옴
JwtToken jwtToken = jwtTokenProvider.createJwtToken(member.getEmail(), member.getRole().getValue());

response.addHeader(HttpHeaders.SET_COOKIE, createCookie("accessToken", jwtToken.getAccessToken()));
response.addHeader(HttpHeaders.SET_COOKIE, createCookie("refreshToken", jwtToken.getRefreshToken()));
response.addHeader(HttpHeaders.SET_COOKIE, createCookie("memberId", String.valueOf(member.getId())));
response.addHeader(HttpHeaders.SET_COOKIE, createCookie("profileImgUrl", member.getProfileImg()));

response.sendRedirect("https://dkoqktaeu3tic.cloudfront.net/home");
String targetUrl = UriComponentsBuilder.fromUriString("https://dkoqktaeu3tic.cloudfront.net/home")
.queryParam("accessToken", jwtToken.getAccessToken())
.queryParam("refreshToken", jwtToken.getRefreshToken())
.queryParam("memberId", String.valueOf(member.getId()))
.queryParam("profileImgUrl", member.getProfileImg())
.build().toUriString();

getRedirectStrategy().sendRedirect(request, response, targetUrl);


}

private static String createCookie(String name, String value) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/api/trip/common/sse/SseNotificationResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.api.trip.common.sse;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

import java.util.List;

@AllArgsConstructor
@Getter
public class SseNotificationResponse {

private Long itemId;
private List<String> tagNames;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.api.trip.domain.notification.emitter;
package com.api.trip.common.sse.emitter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.api.trip.domain.interesttag.respository;

import com.api.trip.domain.interesttag.model.InterestTag;
import com.api.trip.domain.member.model.Member;

import java.util.List;

public interface InterestTagRepositoryCustom {

List<String> findInterestTags(Member member);
List<InterestTag> findInterestTagsByTagNames(List<String> tagNames);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.api.trip.domain.interesttag.respository;

import com.api.trip.domain.interesttag.model.InterestTag;
import com.api.trip.domain.interesttag.model.QInterestTag;
import com.api.trip.domain.member.model.Member;
import com.api.trip.domain.tag.model.QTag;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;

Expand All @@ -25,4 +27,14 @@ public List<String> findInterestTags(Member member) {
.fetch();
}

@Override
public List<InterestTag> findInterestTagsByTagNames(List<String> tagNames) {
return jpaQueryFactory.select(interestTag)
.from(interestTag)
.innerJoin(interestTag.tag, QTag.tag).fetchJoin()
.where(interestTag.tag.name.in(tagNames))
.fetch();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ public void createTag(Member member, List<String> tagNames) {
public List<String> getInterestTag(Member member) {
return interestTagRepository.findInterestTags(member);
}

@Transactional(readOnly = true)
public List<Member> getMemberByTags(List<String> tagNames){
List<InterestTag> interestTags = interestTagRepository.findInterestTagsByTagNames(tagNames);
return interestTags.stream().map(InterestTag::getMember).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public Long createItemByDirect(CreateItemRequest itemRequest) {

return itemRepository.save(item).getId();
}
public Long createItem(CreateItemRequest itemRequest){
public Item createItem(CreateItemRequest itemRequest){
Item item = itemRequest.toEntity();
itemTagService.createItemTag(item, itemRequest.getTagNames());
return itemRepository.save(item);

return itemRepository.save(item).getId();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.api.trip.common.exception.ErrorCode;
import com.api.trip.domain.member.repository.MemberRepository;
import com.api.trip.domain.notification.controller.dto.GetMyNotificationsResponse;
import com.api.trip.domain.notification.emitter.SseEmitterMap;
import com.api.trip.common.sse.emitter.SseEmitterMap;
import com.api.trip.domain.notification.service.NotificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
Expand All @@ -31,7 +31,7 @@ public ResponseEntity<SseEmitter> connect() {
.orElseThrow(() -> new CustomException(ErrorCode.UNAUTHORIZED))
.getId();

SseEmitter sseEmitter = new SseEmitter();
SseEmitter sseEmitter = new SseEmitter(3600000L);
sseEmitterMap.put(memberId, sseEmitter);
sseEmitterMap.send(memberId, "connect", LocalDateTime.now());
return ResponseEntity.ok(sseEmitter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.api.trip.common.exception.CustomException;
import com.api.trip.common.exception.ErrorCode;
import com.api.trip.domain.interesttag.service.InterestTagService;
import com.api.trip.domain.item.model.Item;
import com.api.trip.domain.itemtag.model.ItemTag;
import com.api.trip.domain.member.model.Member;
import com.api.trip.domain.member.repository.MemberRepository;
Expand All @@ -21,6 +23,20 @@ public class NotificationService {

private final MemberRepository memberRepository;
private final NotificationRepository notificationRepository;
private final InterestTagService interestTagService;

public void createNotification(Item item, List<String> tagNames){

List<Member> receivers = interestTagService.getMemberByTags(tagNames);

receivers.stream().forEach(member -> {
notificationRepository.save(Notification.builder()
.item(item)
.member(member).build());
});


}

@Transactional(readOnly = true)
public GetMyNotificationsResponse getMyNotifications(String email) {
Expand Down

0 comments on commit 652921d

Please sign in to comment.