From 311a66637b6a56654e634c5200e7b2fb943a3147 Mon Sep 17 00:00:00 2001 From: kwondongwook Date: Thu, 28 Dec 2023 14:25:01 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Feat:=20=EB=82=B4=EA=B0=80=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=ED=95=9C=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/InterestArticleController.java | 7 +++ .../dto/GetMyInterestArticlesResponse.java | 49 +++++++++++++++++++ .../repository/InterestArticleRepository.java | 3 ++ .../service/InterestArticleService.java | 19 +++++++ 4 files changed, 78 insertions(+) create mode 100644 src/main/java/com/api/trip/domain/interestarticle/controller/dto/GetMyInterestArticlesResponse.java diff --git a/src/main/java/com/api/trip/domain/interestarticle/controller/InterestArticleController.java b/src/main/java/com/api/trip/domain/interestarticle/controller/InterestArticleController.java index 0aa9999..76ba35b 100644 --- a/src/main/java/com/api/trip/domain/interestarticle/controller/InterestArticleController.java +++ b/src/main/java/com/api/trip/domain/interestarticle/controller/InterestArticleController.java @@ -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; @@ -27,4 +28,10 @@ public ResponseEntity deleteInterestArticle(@PathVariable Long interestArt interestArticleService.deleteInterestArticle(interestArticleId, email); return ResponseEntity.ok().build(); } + + @GetMapping("/me") + public ResponseEntity getMyInterestArticles() { + String email = SecurityContextHolder.getContext().getAuthentication().getName(); + return ResponseEntity.ok(interestArticleService.getMyInterestArticles(email)); + } } diff --git a/src/main/java/com/api/trip/domain/interestarticle/controller/dto/GetMyInterestArticlesResponse.java b/src/main/java/com/api/trip/domain/interestarticle/controller/dto/GetMyInterestArticlesResponse.java new file mode 100644 index 0000000..8948b6e --- /dev/null +++ b/src/main/java/com/api/trip/domain/interestarticle/controller/dto/GetMyInterestArticlesResponse.java @@ -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 articles; + + public static GetMyInterestArticlesResponse of(List
articles) { + List 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(); + } + } +} diff --git a/src/main/java/com/api/trip/domain/interestarticle/repository/InterestArticleRepository.java b/src/main/java/com/api/trip/domain/interestarticle/repository/InterestArticleRepository.java index c9dfe7c..ede2795 100644 --- a/src/main/java/com/api/trip/domain/interestarticle/repository/InterestArticleRepository.java +++ b/src/main/java/com/api/trip/domain/interestarticle/repository/InterestArticleRepository.java @@ -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 { Optional findByMemberAndArticle(Member member, Article article); + + List findAllByMember(Member member); } diff --git a/src/main/java/com/api/trip/domain/interestarticle/service/InterestArticleService.java b/src/main/java/com/api/trip/domain/interestarticle/service/InterestArticleService.java index 01acf51..91c30d0 100644 --- a/src/main/java/com/api/trip/domain/interestarticle/service/InterestArticleService.java +++ b/src/main/java/com/api/trip/domain/interestarticle/service/InterestArticleService.java @@ -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; @@ -13,6 +14,8 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; + @Service @Transactional @RequiredArgsConstructor @@ -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 interestArticles = interestArticleRepository.findAllByMember(member); + + List
articles = articleRepository.findAllById( + interestArticles + .stream() + .map(interestArticle -> interestArticle.getArticle().getId()) + .toList() + ); + + return GetMyInterestArticlesResponse.of(articles); + } } From 0749b45e2ada550635ad4fa681b4b9ed98704e1e Mon Sep 17 00:00:00 2001 From: gkfktkrh153 Date: Thu, 28 Dec 2023 15:53:37 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EC=97=AC=ED=96=89=EC=83=81=ED=92=88=20?= =?UTF-8?q?=EA=B0=B1=EC=8B=A0=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/api/trip/common/init/InitData.java | 51 ------------------- .../com/api/trip/common/schedulerRunner.java | 27 +++++----- 2 files changed, 13 insertions(+), 65 deletions(-) delete mode 100644 src/main/java/com/api/trip/common/init/InitData.java diff --git a/src/main/java/com/api/trip/common/init/InitData.java b/src/main/java/com/api/trip/common/init/InitData.java deleted file mode 100644 index 7dd9d58..0000000 --- a/src/main/java/com/api/trip/common/init/InitData.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.api.trip.common.init; - -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 com.api.trip.domain.tag.service.TagService; -import lombok.RequiredArgsConstructor; -import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; -import org.springframework.stereotype.Component; - -import java.util.List; - -@Component -@RequiredArgsConstructor -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 { - - - - - for(int i = 0; i < 1; i++) { - - ShoppingRequest request = ShoppingRequest.builder() - .start(1 + i * 100) - .display(100).build(); - - ShoppingResponse searchResponse = naverClient.search(request); - List shoppingItems = naverApiService.doFilterCategory(searchResponse.getItems()); - List createItemRequests = naverApiService.toCreateItemRequest(shoppingItems); - - for (CreateItemRequest createItemRequest : createItemRequests) - itemService.createItem(createItemRequest); - - - - } - - } -} \ No newline at end of file diff --git a/src/main/java/com/api/trip/common/schedulerRunner.java b/src/main/java/com/api/trip/common/schedulerRunner.java index 5c2abbf..a9e42b3 100644 --- a/src/main/java/com/api/trip/common/schedulerRunner.java +++ b/src/main/java/com/api/trip/common/schedulerRunner.java @@ -9,11 +9,13 @@ 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; @@ -21,6 +23,7 @@ @RequiredArgsConstructor @Component @EnableScheduling +@Slf4j public class schedulerRunner { private final NaverClient naverClient; @@ -29,18 +32,15 @@ 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){ @@ -48,24 +48,23 @@ public void updateData() ShoppingRequest request = ShoppingRequest.builder() .start(1 + i * 50) .display(50).build(); - ShoppingResponse searchResponse = naverClient.search(request); List shoppingItems = naverApiService.doFilterCategory(searchResponse.getItems()); List 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("데이터 갱신 종료"); -*/ } }