Skip to content

Commit

Permalink
chore: 중복된 keyword count 증가 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimin0304 committed Aug 17, 2023
1 parent 7300218 commit e4d9dfa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/mz/ihangbot/keyWord/domain/KeyWord.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
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;
import org.springframework.data.repository.query.Param;

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

public interface KeyWordRepository extends JpaRepository<KeyWord, Long> {

@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<KeyWordReportResponseDTO> 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<KeyWord> 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<KeyWord> findByUsernameAndDateAndKeyWord(@Param("username") String username, @Param("aWeekAgo") Date aWeekAgo, @Param("keyword") String keyword);

}
28 changes: 20 additions & 8 deletions src/main/java/com/mz/ihangbot/keyWord/service/KeyWordService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<KeyWord> 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
Expand Down

0 comments on commit e4d9dfa

Please sign in to comment.