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

[배포] 홈 화면 배너 (조회수, 좋아요) 추가 및 이미지 서버 이전, Redis 설정 추가 및 홈 화면 배너 조회(2개) 글로벌 캐시 적용 #181

Merged
merged 21 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6cbcfe7
Merge pull request #177 from TravelLaboratory/main
k9want Aug 26, 2024
f9fa4dd
Merge pull request #178 from TravelLaboratory/main
k9want Aug 26, 2024
a1d09c2
modified: 홈 배너 조회 API에서 여행 및 리뷰 리스트 제거
k9want Aug 26, 2024
a51ad2e
update: Transactional(readOnly=true) 추가 및 에러 메시지 수정 (AUTH_DUPLICATED_…
k9want Aug 28, 2024
54a992d
modified: N+1 문제 해결 및 북마크 개수 기준(최근 7일 이내)으로 변경된 로직 반영 및 홈 배너에서 북마크 개수 제거
k9want Aug 28, 2024
1b386c3
modified: name -> writer 변경
k9want Aug 28, 2024
a4202ed
rename: Bookmark -> BookmarkEntity 이름 변경
k9want Aug 28, 2024
d0f014b
feat: ArticleViews 관련 엔티티, 레포지토리 생성
k9want Aug 28, 2024
64ef906
refactor: 메서드 추출 및 articleId로 검색 로직 변경
k9want Aug 28, 2024
e3b9d20
feat: 여행 일정 상세 조회 시 조회 수 증가 로직 추가
k9want Aug 28, 2024
1870c94
feat: 여행 일정 상세 조회 시 조회 수 증가 로직에 updatedAt을 기준으로 조회 및 재조회 시 updatedAt …
k9want Aug 28, 2024
7d07e83
modified: updatedAt 기준 검색을 createdAt 기준으로 수정
k9want Aug 28, 2024
9698c57
update: S3 이미지 저장 시 파일 독립성을 위해 (날짜와 시간, UUID)를 파일명으로 변경 후 저장하도록 업데이트
k9want Aug 28, 2024
ab8c675
feat: 최근 핫한 여행 계획 (feat. 조회 수 기준 3일 이내), 좋아요 누른 시점을 기준으로 잡기로 결정 (crea…
k9want Aug 29, 2024
8f7b691
style: 주석 내용 변경
k9want Aug 29, 2024
3ddd64d
chore: redis 의존성 추가 및 yml 설정
k9want Aug 29, 2024
b5cf09f
feat: 홈 배너 여행 계획 리스트 (좋아요, 조회수) 조회 - 글로벌 캐시 레디스 적용
k9want Aug 29, 2024
f5fbecf
test: S3 저장 시 이미지명 변경 로직 적용으로 인한 테스트 수정
k9want Aug 29, 2024
f9c69bd
Merge pull request #179 from TravelLaboratory/feature/home_banner_glo…
k9want Aug 29, 2024
af03809
chore: redis 환경 설정 추가
k9want Aug 29, 2024
50a6247
Merge pull request #180 from TravelLaboratory/feature/home_banner_glo…
k9want Aug 29, 2024
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
2 changes: 2 additions & 0 deletions .github/workflows/main-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ jobs:
spring.datasource.url: ${{ secrets.DB_URL }}
spring.datasource.username: ${{ secrets.DB_USERNAME }}
spring.datasource.password: ${{ secrets.DB_PASSWORD }}
spring.data.redis.host: ${{ secrets.REDIS_HOST }}
spring.data.redis.port: ${{ secrets.REDIS_PORT }}
jwt.secret-key: ${{ secrets.JWT_SECRET_KEY }}
jwt.access-token.plus-hour: ${{ secrets.JWT_ACCESS_TOKEN_PLUS_HOUR }}
jwt.refresh-token.plus-hour: ${{ secrets.JWT_REFRESH_TOKEN_PLUS_HOUR }}
Expand Down
12 changes: 5 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ repositories {

dependencies {
implementation 'org.jetbrains:annotations:24.0.0'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

// https://mvnrepository.com/artifact/org.springframework.security/spring-security-core
implementation 'org.springframework.security:spring-security-core'

implementation 'org.springframework.boot:spring-boot-starter-validation'

implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'


// https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-api
implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.5'
// https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-impl
Expand All @@ -49,7 +48,6 @@ dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'

runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'com.h2database:h2' // 테스트 실행 - H2 (임시)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.springframework.transaction.annotation.Transactional;
import site.travellaboratory.be.article.domain.enums.ArticleStatus;
import site.travellaboratory.be.article.infrastructure.persistence.entity.ArticleEntity;
import site.travellaboratory.be.article.infrastructure.persistence.entity.Bookmark;
import site.travellaboratory.be.article.infrastructure.persistence.entity.BookmarkEntity;
import site.travellaboratory.be.article.infrastructure.persistence.repository.ArticleJpaRepository;
import site.travellaboratory.be.article.infrastructure.persistence.repository.BookmarkRepository;
import site.travellaboratory.be.article.presentation.response.like.BookmarkSaveResponse;
Expand All @@ -33,17 +33,17 @@ public BookmarkSaveResponse saveBookmark(final Long userId, final Long articleId
final ArticleEntity articleEntity = articleJpaRepository.findByIdAndStatus(articleId, ArticleStatus.ACTIVE)
.orElseThrow(() -> new BeApplicationException(ErrorCodes.ARTICLE_NOT_FOUND, HttpStatus.NOT_FOUND));

Bookmark bookmark = bookmarkRepository.findByArticleEntityAndUserEntity(articleEntity,
BookmarkEntity bookmarkEntity = bookmarkRepository.findByArticleEntityAndUserEntity(articleEntity,
userEntity).orElse(null);

if (bookmark != null) {
bookmark.toggleStatus();
if (bookmarkEntity != null) {
bookmarkEntity.toggleStatus();
} else {
bookmark = Bookmark.of(userEntity, articleEntity);
bookmarkEntity = BookmarkEntity.of(userEntity, articleEntity);
}

final Bookmark newBookmark = bookmarkRepository.save(bookmark);
return BookmarkSaveResponse.from(newBookmark);
final BookmarkEntity newBookmarkEntity = bookmarkRepository.save(bookmarkEntity);
return BookmarkSaveResponse.from(newBookmarkEntity);
}
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package site.travellaboratory.be.article.application.service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
Expand All @@ -12,20 +14,22 @@
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.travellaboratory.be.common.exception.BeApplicationException;
import site.travellaboratory.be.common.error.ErrorCodes;
import site.travellaboratory.be.article.infrastructure.persistence.repository.ArticleJpaRepository;
import site.travellaboratory.be.article.infrastructure.persistence.entity.ArticleEntity;
import site.travellaboratory.be.article.domain.enums.ArticleStatus;
import site.travellaboratory.be.article.infrastructure.persistence.repository.BookmarkRepository;
import site.travellaboratory.be.article.infrastructure.persistence.entity.Bookmark;
import site.travellaboratory.be.article.domain.enums.BookmarkStatus;
import site.travellaboratory.be.user.infrastructure.persistence.repository.UserJpaRepository;
import site.travellaboratory.be.user.infrastructure.persistence.entity.UserEntity;
import site.travellaboratory.be.user.domain.enums.UserStatus;
import site.travellaboratory.be.article.infrastructure.persistence.entity.ArticleEntity;
import site.travellaboratory.be.article.infrastructure.persistence.entity.BookmarkEntity;
import site.travellaboratory.be.article.infrastructure.persistence.repository.ArticleJpaRepository;
import site.travellaboratory.be.article.infrastructure.persistence.repository.ArticleViewsJpaRepository;
import site.travellaboratory.be.article.infrastructure.persistence.repository.BookmarkRepository;
import site.travellaboratory.be.article.presentation.response.like.BookmarkResponse;
import site.travellaboratory.be.article.presentation.response.reader.ArticleOneResponse;
import site.travellaboratory.be.article.presentation.response.reader.ArticleTotalResponse;
import site.travellaboratory.be.article.presentation.response.like.BookmarkResponse;
import site.travellaboratory.be.article.presentation.response.reader.BannerArticlesResponse;
import site.travellaboratory.be.common.error.ErrorCodes;
import site.travellaboratory.be.common.exception.BeApplicationException;
import site.travellaboratory.be.user.domain.enums.UserStatus;
import site.travellaboratory.be.user.infrastructure.persistence.entity.UserEntity;
import site.travellaboratory.be.user.infrastructure.persistence.repository.UserJpaRepository;

@Service
@RequiredArgsConstructor
Expand All @@ -34,6 +38,7 @@ public class ArticleReaderService {
private final ArticleJpaRepository articleJpaRepository;
private final UserJpaRepository userJpaRepository;
private final BookmarkRepository bookmarkRepository;
private final ArticleViewsJpaRepository articleViewsJpaRepository;

// 내 초기 여행 계획 전체 조회
@Transactional
Expand Down Expand Up @@ -133,48 +138,50 @@ public Page<ArticleTotalResponse> searchArticlesByKeyWord(
return new PageImpl<>(articleResponses, pageable, newArticles.getTotalElements());
}

@Transactional
public List<ArticleTotalResponse> getBannerNotUserArticles() {
List<ArticleEntity> articleJpaEntities = articleJpaRepository.findAllByStatus(ArticleStatus.ACTIVE);
@Transactional(readOnly = true)
@Cacheable(cacheNames = "weeklyLikes", key = "'getBannerWeeklyLikes'")
public List<BannerArticlesResponse> readBannerArticlesByWeeklyLikes() {
// 일 주일 전 계산
LocalDateTime oneWeekAgo = LocalDateTime.now().minusWeeks(1);

return articleJpaEntities.stream()
.map(article -> {
Long bookmarkCount = bookmarkRepository.countByArticleIdAndStatus(article.getId(),
BookmarkStatus.ACTIVE);
boolean isBookmarked = false;
boolean isEditable = false;
// 좋아요 수 기준으로 상위 12개의 articleId 가져오기
Pageable pageable = PageRequest.of(0, 12);
List<Long> topArticleIds = bookmarkRepository.findTopArticleIdsByLikeCount(oneWeekAgo, pageable);

return ArticleTotalResponse.of(article, bookmarkCount, isBookmarked, isEditable);
})
.sorted((a1, a2) -> a2.bookmarkCount().compareTo(a1.bookmarkCount())) // 북마크 수 기준으로 내림차순 정렬
.limit(4) // 상위 4개 아티클만 가져옴
.collect(Collectors.toList());
// 해당 articleId 리스트로 게시글 조회
List<ArticleEntity> articles = articleJpaRepository.findActiveArticlesWithUserByIds(topArticleIds);
articleJpaRepository.findActiveArticlesWithLocationsByIds(topArticleIds);
articleJpaRepository.findActiveArticlesWithTravelStylesByIds(topArticleIds);


return articles.stream()
.map(BannerArticlesResponse::of)
.collect(Collectors.toList());
}

@Transactional
public List<ArticleTotalResponse> getBannerUserArticles(final Long userId) {
userJpaRepository.findByIdAndStatus(userId, UserStatus.ACTIVE)
.orElseThrow(() -> new BeApplicationException(ErrorCodes.USER_NOT_FOUND, HttpStatus.NOT_FOUND));

List<ArticleEntity> articleJpaEntities = articleJpaRepository.findAllByStatus(ArticleStatus.ACTIVE);
@Transactional(readOnly = true)
@Cacheable(cacheNames = "hourlyViews", key = "'getBannerHourlyViews'")
public List<BannerArticlesResponse> readBannerArticlesByHourlyViews() {
// 3일 전부터 시간 계산
LocalDateTime threeDaysAgo = LocalDateTime.now().minusDays(3);

return articleJpaEntities.stream()
.map(article -> {
Long bookmarkCount = bookmarkRepository.countByArticleIdAndStatus(article.getId(),
BookmarkStatus.ACTIVE);
boolean isBookmarked = bookmarkRepository.existsByUserEntityIdAndArticleEntityIdAndStatus(userId,
article.getId(),
BookmarkStatus.ACTIVE);
boolean isEditable = userId.equals(article.getUserEntity().getId());
// 조회수 기준으로 상위 12개의 articleId 가져오기
Pageable pageable = PageRequest.of(0, 12);
List<Long> topArticleIdsByViewsCount = articleViewsJpaRepository.findTopArticleIdsByViewsCount(
threeDaysAgo, pageable);

return ArticleTotalResponse.of(article, bookmarkCount, isBookmarked, isEditable);
})
.sorted((a1, a2) -> a2.bookmarkCount().compareTo(a1.bookmarkCount())) // 북마크 수 기준으로 내림차순 정렬
.limit(4) // 상위 4개 아티클만 가져옴
.collect(Collectors.toList());
}
// 해당 articleId 리스트로 게시글 조회
List<ArticleEntity> articles = articleJpaRepository.findActiveArticlesWithUserByIds(topArticleIdsByViewsCount);
articleJpaRepository.findActiveArticlesWithLocationsByIds(topArticleIdsByViewsCount);
articleJpaRepository.findActiveArticlesWithTravelStylesByIds(topArticleIdsByViewsCount);


return articles.stream()
.map(BannerArticlesResponse::of)
.collect(Collectors.toList());
}

@Transactional
public Page<BookmarkResponse> findAllBookmarkByUser(final Long loginId, final Long userId, Pageable pageable) {
final UserEntity loginUserEntity = userJpaRepository.findByIdAndStatus(loginId, UserStatus.ACTIVE)
Expand All @@ -185,7 +192,7 @@ public Page<BookmarkResponse> findAllBookmarkByUser(final Long loginId, final Lo
.orElseThrow(() -> new BeApplicationException(ErrorCodes.USER_NOT_FOUND,
HttpStatus.NOT_FOUND));

final Page<Bookmark> bookmarks = bookmarkRepository.findByUserEntityAndStatus(
final Page<BookmarkEntity> bookmarks = bookmarkRepository.findByUserEntityAndStatus(
userEntity, BookmarkStatus.ACTIVE, pageable)
.orElseThrow(() -> new BeApplicationException(ErrorCodes.BOOKMARK_NOT_FOUND, HttpStatus.NOT_FOUND));

Expand Down
Loading
Loading