From e4d9dfa93e88f30ce07085641475c9e9a866fcfa Mon Sep 17 00:00:00 2001 From: Jimin0304 Date: Thu, 17 Aug 2023 19:40:19 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20=EC=A4=91=EB=B3=B5=EB=90=9C=20keyword?= =?UTF-8?q?=20count=20=EC=A6=9D=EA=B0=80=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mz/ihangbot/keyWord/domain/KeyWord.java | 4 +++ .../keyWord/repository/KeyWordRepository.java | 7 ++++- .../keyWord/service/KeyWordService.java | 28 +++++++++++++------ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/mz/ihangbot/keyWord/domain/KeyWord.java b/src/main/java/com/mz/ihangbot/keyWord/domain/KeyWord.java index f415ad4..bf5a19a 100644 --- a/src/main/java/com/mz/ihangbot/keyWord/domain/KeyWord.java +++ b/src/main/java/com/mz/ihangbot/keyWord/domain/KeyWord.java @@ -41,4 +41,8 @@ public KeyWord(Member member, String keyword, int count, Date date) { this.count = count; this.date = date; } + + public void updateCount(int count) { + this.count += count; + } } diff --git a/src/main/java/com/mz/ihangbot/keyWord/repository/KeyWordRepository.java b/src/main/java/com/mz/ihangbot/keyWord/repository/KeyWordRepository.java index c5dd8d3..a52a697 100644 --- a/src/main/java/com/mz/ihangbot/keyWord/repository/KeyWordRepository.java +++ b/src/main/java/com/mz/ihangbot/keyWord/repository/KeyWordRepository.java @@ -1,6 +1,7 @@ package com.mz.ihangbot.keyWord.repository; import com.mz.ihangbot.keyWord.domain.KeyWord; +import com.mz.ihangbot.member.domain.Member; import com.mz.ihangbot.report.dto.KeyWordReportResponseDTO; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; @@ -8,13 +9,17 @@ import java.util.Date; import java.util.List; +import java.util.Optional; public interface KeyWordRepository extends JpaRepository { @Query(value = "select new com.mz.ihangbot.report.dto.KeyWordReportResponseDTO(result.kw, result.cnt) from (select k.keyword as kw, SUM(k.count) as cnt from KeyWord k where :username = k.member.username and :aWeekAgo <= k.date group by k.keyword) as result order by result.cnt DESC limit 5") List getKeyWords(@Param("username") String username, @Param("aWeekAgo") Date aWeekAgo); - @Query(value = "select k from KeyWord k where :username = k.member.username and k.date >= :aWeekAgo") List findAllByUsernameAndDate(@Param("username") String username, @Param("aWeekAgo") Date aWeekAgo); + + @Query(value = "select k from KeyWord k where :username = k.member.username and k.date >= :aWeekAgo and k.keyword = :keyword") + Optional findByUsernameAndDateAndKeyWord(@Param("username") String username, @Param("aWeekAgo") Date aWeekAgo, @Param("keyword") String keyword); + } diff --git a/src/main/java/com/mz/ihangbot/keyWord/service/KeyWordService.java b/src/main/java/com/mz/ihangbot/keyWord/service/KeyWordService.java index fcd79c6..1c0166a 100644 --- a/src/main/java/com/mz/ihangbot/keyWord/service/KeyWordService.java +++ b/src/main/java/com/mz/ihangbot/keyWord/service/KeyWordService.java @@ -16,6 +16,7 @@ import java.util.Calendar; import java.util.Date; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @RequiredArgsConstructor @@ -31,15 +32,26 @@ public KeyWordResponseDTO addKeyWord(String keyword, String username, int count, Member member = memberRepository.findByUserName(username) .orElseThrow(() -> new EntityNotFoundException(ErrorCode.MEMBER_NOT_FOUND)); - KeyWord keyWord = KeyWord.builder() - .keyword(keyword) - .member(member) - .count(count) - .date(date) - .build(); + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date()); + cal.add(Calendar.DATE, -7); + + Optional word = keyWordRepository.findByUsernameAndDateAndKeyWord(username, cal.getTime(), keyword); - KeyWord saved = keyWordRepository.save(keyWord); - return KeyWordResponseDTO.from(saved); + if (word.isEmpty()) { + KeyWord keyWord = KeyWord.builder() + .keyword(keyword) + .member(member) + .count(count) + .date(date) + .build(); + KeyWord saved = keyWordRepository.save(keyWord); + return KeyWordResponseDTO.from(saved); + } + else { + word.get().updateCount(count); + } + return KeyWordResponseDTO.from(word.get()); } @Transactional