From 73c59c083db749740342ebf44e0706d69aa73e02 Mon Sep 17 00:00:00 2001 From: seongjae6751 Date: Thu, 19 Sep 2024 14:06:48 +0900 Subject: [PATCH 1/2] =?UTF-8?q?chore:=20=EC=A0=9C=EC=95=BD=20=EC=A1=B0?= =?UTF-8?q?=EA=B1=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../article/model/ArticleSearchKeywordIpMap.java | 4 +++- ...__alter_search_keywords_and_ip_map_unique_cascade.sql | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/db/migration/V60__alter_search_keywords_and_ip_map_unique_cascade.sql diff --git a/src/main/java/in/koreatech/koin/domain/community/article/model/ArticleSearchKeywordIpMap.java b/src/main/java/in/koreatech/koin/domain/community/article/model/ArticleSearchKeywordIpMap.java index b901ee098..77e214d53 100644 --- a/src/main/java/in/koreatech/koin/domain/community/article/model/ArticleSearchKeywordIpMap.java +++ b/src/main/java/in/koreatech/koin/domain/community/article/model/ArticleSearchKeywordIpMap.java @@ -12,6 +12,7 @@ import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; +import jakarta.persistence.UniqueConstraint; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -19,7 +20,8 @@ @Getter @Entity @Table(name = "article_search_keyword_ip_map", indexes = { - @Index(name = "idx_ip_address", columnList = "ipAddress") + @Index(name = "idx_ip_address", columnList = "ipAddress")}, uniqueConstraints = { + @UniqueConstraint(name = "ux_keyword_ip", columnNames = {"keyword_id", "ipAddress"}) }) @NoArgsConstructor(access = PROTECTED) public class ArticleSearchKeywordIpMap extends BaseEntity { diff --git a/src/main/resources/db/migration/V60__alter_search_keywords_and_ip_map_unique_cascade.sql b/src/main/resources/db/migration/V60__alter_search_keywords_and_ip_map_unique_cascade.sql new file mode 100644 index 000000000..b2965a323 --- /dev/null +++ b/src/main/resources/db/migration/V60__alter_search_keywords_and_ip_map_unique_cascade.sql @@ -0,0 +1,9 @@ +ALTER TABLE `article_search_keywords` + MODIFY COLUMN `keyword` VARCHAR(255) NOT NULL UNIQUE; + +ALTER TABLE article_search_keyword_ip_map + ADD CONSTRAINT unique_keyword_ip UNIQUE (keyword_id, ip_address), + ADD CONSTRAINT fk_keyword_id + FOREIGN KEY (keyword_id) + REFERENCES article_search_keywords (id) + ON DELETE CASCADE; From 46a37fc1639ed7ef5e370a95955f0998d558732f Mon Sep 17 00:00:00 2001 From: seongjae6751 Date: Thu, 19 Sep 2024 18:12:04 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=EB=A1=9C=EC=A7=81=20=EC=83=81=20?= =?UTF-8?q?=EA=B2=B0=ED=95=A8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../community/article/model/ArticleSearchKeyword.java | 1 - .../repository/ArticleSearchKeywordIpMapRepository.java | 2 +- .../article/repository/ArticleSearchKeywordRepository.java | 2 +- .../domain/community/article/service/ArticleService.java | 6 ++---- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main/java/in/koreatech/koin/domain/community/article/model/ArticleSearchKeyword.java b/src/main/java/in/koreatech/koin/domain/community/article/model/ArticleSearchKeyword.java index 373702666..17d7cd394 100644 --- a/src/main/java/in/koreatech/koin/domain/community/article/model/ArticleSearchKeyword.java +++ b/src/main/java/in/koreatech/koin/domain/community/article/model/ArticleSearchKeyword.java @@ -57,7 +57,6 @@ public void updateWeight(double newWeight) { public void resetWeight() { this.weight = 1.0; - this.lastSearchedAt = LocalDateTime.now(); } public void incrementTotalSearch() { diff --git a/src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleSearchKeywordIpMapRepository.java b/src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleSearchKeywordIpMapRepository.java index 173023dce..0d3ab84f5 100644 --- a/src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleSearchKeywordIpMapRepository.java +++ b/src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleSearchKeywordIpMapRepository.java @@ -15,5 +15,5 @@ public interface ArticleSearchKeywordIpMapRepository extends Repository findByArticleSearchKeywordAndIpAddress(ArticleSearchKeyword keyword, String ipAddress); - List findByCreatedAtBetween(LocalDateTime fiveHoursThirtyMinutesAgo, LocalDateTime now); + List findByUpdatedAtBetween(LocalDateTime before, LocalDateTime now); } diff --git a/src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleSearchKeywordRepository.java b/src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleSearchKeywordRepository.java index 227623c90..a526d1b2e 100644 --- a/src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleSearchKeywordRepository.java +++ b/src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleSearchKeywordRepository.java @@ -31,5 +31,5 @@ public interface ArticleSearchKeywordRepository extends Repository findTopKeywordsByLatest(Pageable pageable); - List findByCreatedAtBetween(LocalDateTime startTime, LocalDateTime endTime); + List findByUpdatedAtBetween(LocalDateTime startTime, LocalDateTime endTime); } diff --git a/src/main/java/in/koreatech/koin/domain/community/article/service/ArticleService.java b/src/main/java/in/koreatech/koin/domain/community/article/service/ArticleService.java index 64ccc18c9..1996d03c6 100644 --- a/src/main/java/in/koreatech/koin/domain/community/article/service/ArticleService.java +++ b/src/main/java/in/koreatech/koin/domain/community/article/service/ArticleService.java @@ -211,8 +211,6 @@ private void updateKeywordWeightAndCount(ArticleSearchKeyword keyword, ArticleSe if (map.getSearchCount() <= 10) { keyword.updateWeight(keyword.getWeight() + additionalWeight); } - - keyword.updateWeight(keyword.getWeight() + additionalWeight); articleSearchKeywordRepository.save(keyword); } @@ -221,9 +219,9 @@ public void resetWeightsAndCounts() { LocalDateTime now = LocalDateTime.now(); LocalDateTime before = now.minusHours(6).minusMinutes(30); - List keywordsToUpdate = articleSearchKeywordRepository.findByCreatedAtBetween( + List keywordsToUpdate = articleSearchKeywordRepository.findByUpdatedAtBetween( before, now); - List ipMapsToUpdate = articleSearchKeywordIpMapRepository.findByCreatedAtBetween( + List ipMapsToUpdate = articleSearchKeywordIpMapRepository.findByUpdatedAtBetween( before, now); for (ArticleSearchKeyword keyword : keywordsToUpdate) {