Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] 아티클 조회수 기능을 구현한다. #1070

Open
wants to merge 6 commits into
base: dev-be
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,19 @@ public class Article extends BaseEntity {

private String thumbnail;

private Long viewCount;

public Article(String title, String content, String keyword, String summary, String thumbnail) {
this.title = title;
this.content = content;
this.keyword = keyword;
this.summary = summary;
this.thumbnail = thumbnail;
this.viewCount = 0L;
}

public void increaseViewCount() {
viewCount++;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.time.LocalDateTime;

public record ArticleResponse(Long articleId, String title, String content, String keyword, String summary,
String thumbnail, LocalDateTime createdAt) {
String thumbnail, LocalDateTime createdAt, Long viewCount) {

public static ArticleResponse from(Article article) {
return new ArticleResponse(
Expand All @@ -14,7 +14,8 @@ public static ArticleResponse from(Article article) {
article.getKeyword(),
article.getSummary(),
article.getThumbnail(),
article.getCreatedAt()
article.getCreatedAt(),
article.getViewCount()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
import java.time.LocalDateTime;

public record ArticlesResponse(Long articleId, String title, String keyword, String summary,
LocalDateTime createdAt) {
LocalDateTime createdAt, Long viewCount) {

public static ArticlesResponse from(Article article) {
return new ArticlesResponse(
article.getId(),
article.getTitle(),
article.getKeyword(),
article.getSummary(),
article.getCreatedAt()
article.getCreatedAt(),
article.getViewCount()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
import com.bang_ggood.article.dto.response.ArticlesResponses;
import com.bang_ggood.article.repository.ArticleRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

import static com.bang_ggood.global.config.cache.CacheName.ARTICLE;

@RequiredArgsConstructor
@Service
public class ArticleService {
Expand All @@ -28,10 +24,10 @@ public long createArticle(ArticleCreateRequest request) {
return article.getId();
}

@Cacheable(cacheNames = ARTICLE, key = "#id")
@Transactional(readOnly = true)
@Transactional
public ArticleResponse readArticle(Long id) {
Article article = articleRepository.getById(id);
article.increaseViewCount();
return ArticleResponse.from(article);
}

Expand All @@ -43,7 +39,6 @@ public ArticlesResponses readArticles() {
return new ArticlesResponses(articles);
}

@CacheEvict(cacheNames = ARTICLE, key = "#id")
@Transactional
public void deleteArticle(Long id) {
articleRepository.deleteById(id);
Expand Down
1 change: 1 addition & 0 deletions backend/bang-ggood/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ CREATE TABLE article
keyword VARCHAR(255),
summary VARCHAR(255),
thumbnail VARCHAR(2500),
view_count BIGINT default 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

머지되면 스키마 변경사항 prod db에도 반영해주세요~

created_at TIMESTAMP(6),
modified_at TIMESTAMP(6),
deleted BOOLEAN
Expand Down
Loading
Loading