Skip to content

Commit

Permalink
Merge pull request #149 from TRIP-Side-Project/dev
Browse files Browse the repository at this point in the history
배포 환경에서의 테스트를 위한 main merge
  • Loading branch information
gkfktkrh153 authored Dec 28, 2023
2 parents af9acab + 453c36d commit ec8330e
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 65 deletions.
51 changes: 0 additions & 51 deletions src/main/java/com/api/trip/common/init/InitData.java

This file was deleted.

27 changes: 13 additions & 14 deletions src/main/java/com/api/trip/common/schedulerRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@
import com.api.trip.domain.item.controller.dto.CreateItemRequest;
import com.api.trip.domain.item.service.ItemService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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 org.springframework.web.client.HttpClientErrorException;

import java.util.List;
import java.util.Set;

@RequiredArgsConstructor
@Component
@EnableScheduling
@Slf4j
public class schedulerRunner {

private final NaverClient naverClient;
Expand All @@ -29,43 +32,39 @@ public class schedulerRunner {

/**
* @Description
* 1분에 한번씩 api를 요청하여 데이터 갱신(중단조건에 걸릴때까지 50개씩 반복요청)
* 5분에 한번씩 api를 요청하여 데이터 갱신(중단조건에 걸릴때까지 50개씩 반복요청)
* 데이터가 중복됐을 때(이미 존재하는 데이터)를 중단조건
*/
@Scheduled(fixedDelay = 60000)
@Scheduled(fixedDelay = 300000)
public void updateData()
{
/*

System.out.println("schedulerRunner");
int i = 0;
System.out.println("데이터 갱신 시작");
log.info("데이터 갱신 시작");
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)
for (CreateItemRequest createItemRequest : createItemRequests) {
itemService.createItem(createItemRequest);
}
i++;

}
}catch (DataIntegrityViolationException e)
{
System.out.println("데이터 중복");
log.info("데이터 중복 - 데이터 갱신 종료");
}
catch (HttpClientErrorException e)
{
log.info("요청 범위 초과 - 데이터 갱신 종료");
}
System.out.println("데이터 갱신 종료");

*/
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.api.trip.domain.interestarticle.controller;

import com.api.trip.domain.interestarticle.controller.dto.CreateInterestArticleRequest;
import com.api.trip.domain.interestarticle.controller.dto.GetMyInterestArticlesResponse;
import com.api.trip.domain.interestarticle.service.InterestArticleService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,4 +28,10 @@ public ResponseEntity<Void> deleteInterestArticle(@PathVariable Long interestArt
interestArticleService.deleteInterestArticle(interestArticleId, email);
return ResponseEntity.ok().build();
}

@GetMapping("/me")
public ResponseEntity<GetMyInterestArticlesResponse> getMyInterestArticles() {
String email = SecurityContextHolder.getContext().getAuthentication().getName();
return ResponseEntity.ok(interestArticleService.getMyInterestArticles(email));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.api.trip.domain.interestarticle.controller.dto;

import com.api.trip.domain.article.model.Article;
import com.api.trip.domain.member.model.Member;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.List;

@Getter
@AllArgsConstructor
public class GetMyInterestArticlesResponse {

private List<ArticleDto> articles;

public static GetMyInterestArticlesResponse of(List<Article> articles) {
List<ArticleDto> articleDtos = articles.stream()
.map(ArticleDto::of)
.toList();

return new GetMyInterestArticlesResponse(articleDtos);
}

@Getter
@Builder
private static class ArticleDto {

private Long articleId;
private String title;
private Long writerId;
private String writerNickname;
private String writerRole;
private LocalDateTime createdAt;

private static ArticleDto of(Article article) {
Member writer = article.getWriter();
return builder()
.articleId(article.getId())
.title(article.getTitle())
.writerId(writer.getId())
.writerNickname(writer.getNickname())
.writerRole(writer.getRole().name())
.createdAt(article.getCreatedAt())
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import com.api.trip.domain.member.model.Member;
import org.springframework.data.jpa.repository.JpaRepository;

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

public interface InterestArticleRepository extends JpaRepository<InterestArticle, Long> {

Optional<InterestArticle> findByMemberAndArticle(Member member, Article article);

List<InterestArticle> findAllByMember(Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.api.trip.domain.article.model.Article;
import com.api.trip.domain.article.repository.ArticleRepository;
import com.api.trip.domain.interestarticle.controller.dto.CreateInterestArticleRequest;
import com.api.trip.domain.interestarticle.controller.dto.GetMyInterestArticlesResponse;
import com.api.trip.domain.interestarticle.model.InterestArticle;
import com.api.trip.domain.interestarticle.repository.InterestArticleRepository;
import com.api.trip.domain.member.model.Member;
Expand All @@ -13,6 +14,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@RequiredArgsConstructor
Expand Down Expand Up @@ -60,4 +63,20 @@ public void deleteInterestArticle(Long interestArticleId, String email) {

interestArticleRepository.delete(interestArticle);
}

public GetMyInterestArticlesResponse getMyInterestArticles(String email) {
Member member = memberRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorCode.UNAUTHORIZED));

List<InterestArticle> interestArticles = interestArticleRepository.findAllByMember(member);

List<Article> articles = articleRepository.findAllById(
interestArticles
.stream()
.map(interestArticle -> interestArticle.getArticle().getId())
.toList()
);

return GetMyInterestArticlesResponse.of(articles);
}
}

0 comments on commit ec8330e

Please sign in to comment.