Skip to content

Commit

Permalink
feat : 3일내에 만든 토론중에, 가장 조회수가 높은걸 반환하도록 함
Browse files Browse the repository at this point in the history
  • Loading branch information
minseok1015 committed Aug 18, 2024
1 parent ce081d5 commit d96f7b3
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/store/itpick/backend/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(jwtAuthenticationInterceptor)
.order(1)
.addPathPatterns("/**")
.excludePathPatterns("/auth/login", "/auth/signup", "/auth/refresh","/auth/emails/**", "/rank/**","/auth/email/check","/auth/nickname/check","/favicon.ico","/keyword/**","/debate/keyword", "/test/**");
.excludePathPatterns("/auth/login", "/auth/signup", "/auth/refresh","/auth/emails/**", "/rank/**","/auth/email/check","/auth/nickname/check","/favicon.ico","/keyword/**","/debate/keyword", "/test/**","/debate/trend");
//인터셉터 적용 범위 수정
registry.addInterceptor(getJwtInterceptor)
.addPathPatterns("/user/email");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,12 @@ public BaseResponse<?> deleteDebate(@PreAuthorize long userId, @PathVariable Lon
debateService.deleteDebate(debateId,userId);
return new BaseResponse<>(null);
}


@GetMapping("/trend")
public BaseResponse<List<DebateByKeywordDTO>> getTrendDebate(){
List<DebateByKeywordDTO> debateResponse = debateService.updateHotDebate();

return new BaseResponse<>(debateResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Getter
public class DebateByKeywordDTO {
String title;
String content;
Long debateId;
String mediaUrl;
Long hit;
Long comment;
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/store/itpick/backend/model/TrendDebate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package store.itpick.backend.model;


import jakarta.persistence.*;
import lombok.*;

import java.sql.Timestamp;

@Entity
@Table(name = "trend_debate")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class TrendDebate {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "trend_debate_id")
private Long trendDebateId;

@OneToOne
@JoinColumn(name = "debate_id", nullable = false)
private Debate debate;

@Column(name = "update_at", nullable = false)
private Timestamp updateAt;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package store.itpick.backend.repository;

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -8,6 +9,7 @@
import store.itpick.backend.model.Debate;
import store.itpick.backend.model.User;

import java.sql.Timestamp;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -38,5 +40,10 @@ public interface DebateRepository extends JpaRepository<Debate, Long> {

Optional<Debate> getDebateByDebateId(Long debateId);

@Query("SELECT d FROM Debate d " +
"WHERE d.createAt >= :startTime " +
"ORDER BY d.hits DESC")
List<Debate> findTop3DebatesCreatedInLast3Days(@Param("startTime") Timestamp startTime, Pageable pageable);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package store.itpick.backend.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import store.itpick.backend.model.TrendDebate;

@Repository
public interface TrendDebateRepository extends JpaRepository<TrendDebate, Long> {
void deleteAll();
}
37 changes: 34 additions & 3 deletions src/main/java/store/itpick/backend/service/DebateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import store.itpick.backend.common.exception.DebateException;
import store.itpick.backend.common.exception.AuthException;
Expand Down Expand Up @@ -41,6 +42,7 @@ public class DebateService {
private final JwtProvider jwtProvider;
private final S3ImageBucketService s3ImageBucketService;
private final RecentViewedDebateRepository recentViewedDebateRepository;
private final TrendDebateRepository trendDebateRepository;

@Transactional
public PostDebateResponse createDebate(PostDebateRequest postDebateRequest, long userId) {
Expand Down Expand Up @@ -230,11 +232,11 @@ public List<DebateByKeywordDTO> GetDebatesByKeyword(Long keywordID, String sort)
for (Debate debate : debates) {
if(debate.getStatus().equals("active")){
String title= debate.getTitle();
String content =debate.getContent();
Long debateId =debate.getDebateId();
String mediaUrl =debate.getImageUrl();
Long hit = debate.getHits();
Long comment = (long) debate.getComment().size();
debateList.add(new DebateByKeywordDTO(title,content,mediaUrl,hit,comment));
debateList.add(new DebateByKeywordDTO(title,debateId,mediaUrl,hit,comment));
}
}

Expand Down Expand Up @@ -267,7 +269,7 @@ public List<DebateByKeywordDTO> getRecentViewedDebate(long userId){
// Debate를 DTO로 변환
return debates.stream()
.filter(debate -> "active".equals(debate.getStatus()))
.map(debate -> new DebateByKeywordDTO(debate.getTitle(), debate.getContent(), debate.getImageUrl(),debate.getHits(), (long) debate.getComment().size()))
.map(debate -> new DebateByKeywordDTO(debate.getTitle(), debate.getDebateId(), debate.getImageUrl(),debate.getHits(), (long) debate.getComment().size()))
.collect(Collectors.toList());

}
Expand All @@ -294,4 +296,33 @@ public void deleteDebate(Long debateId, long userId) {

}

@Transactional
public List<DebateByKeywordDTO> updateHotDebate() {
// 현재 시간 및 48시간 전 시간 계산
Timestamp endTime = new Timestamp(System.currentTimeMillis());
Timestamp startTime = new Timestamp(endTime.getTime() - 3 * 24 * 60 * 60 * 1000); // 3일 전 시간
PageRequest pageRequest = PageRequest.of(0, 3); // 상위 3개만 가져오기

// 기존의 TrendDebate 삭제
trendDebateRepository.deleteAllInBatch();

// 48시간 동안 조회수가 가장 많이 오른 상위 3개의 Debate 조회
List<Debate> debateList = debateRepository.findTop3DebatesCreatedInLast3Days(startTime, pageRequest);
List<DebateByKeywordDTO> debates = new ArrayList<>();

// 새로운 TrendDebate 엔트리 삽입
for (Debate debate : debateList) {
TrendDebate trendDebate = TrendDebate.builder()
.debate(debate)
.updateAt(endTime) // 업데이트된 시간 저장
.build();

trendDebateRepository.save(trendDebate);

debates.add(new DebateByKeywordDTO(debate.getTitle(), debate.getDebateId(), debate.getImageUrl(), debate.getHits(), (long) debate.getComment().size()));
}

return debates;
}

}

0 comments on commit d96f7b3

Please sign in to comment.