Skip to content

Commit

Permalink
Merge pull request #123 from minseok1015/dev
Browse files Browse the repository at this point in the history
feat : 최근본 유저와 토론을 저장할 Model(테이블 생성) 및 토론상세페이지 요청 시 로직 추가
  • Loading branch information
minseok1015 authored Aug 18, 2024
2 parents ed21d02 + abc37ff commit 9b09708
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,14 @@ public BaseResponse<List<DebateByKeywordDTO>> getDebateByKeyword( @RequestParam

return new BaseResponse<>(debates);
}

@GetMapping("/recent")
public BaseResponse<List<DebateByKeywordDTO>> getRecentViewedDebate(@RequestHeader("Authorization") String token) {

String jwtToken = token.substring(7);

List<DebateByKeywordDTO> debateResponse = debateService.getRecentViewedDebate(jwtToken);

return new BaseResponse<>(debateResponse);
}
}
30 changes: 30 additions & 0 deletions src/main/java/store/itpick/backend/model/RecentViewedDebate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package store.itpick.backend.model;

import jakarta.persistence.*;
import lombok.*;
import java.sql.Timestamp;


@Entity
@Table(name = "recent_viewed_debate")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class RecentViewedDebate {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "recent_viewed_debate_id")
private Long id;

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private User user;

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

@Column(name = "viewed_at", nullable = false)
private Timestamp viewedAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package store.itpick.backend.repository;

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

import java.util.List;

@Repository
public interface RecentViewedDebateRepository extends JpaRepository<RecentViewedDebate, Long> {
List<RecentViewedDebate> findByUser_UserIdOrderByViewedAtDesc(Long userId);

}

44 changes: 43 additions & 1 deletion src/main/java/store/itpick/backend/service/DebateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static store.itpick.backend.common.response.status.BaseExceptionResponseStatus.*;

Expand All @@ -39,6 +40,7 @@ public class DebateService {
private final VoteService voteService;
private final JwtProvider jwtProvider;
private final S3ImageBucketService s3ImageBucketService;
private final RecentViewedDebateRepository recentViewedDebateRepository;

@Transactional
public PostDebateResponse createDebate(PostDebateRequest postDebateRequest) {
Expand Down Expand Up @@ -148,6 +150,11 @@ public GetDebateResponse getDebate(Long debateId, String token) {
Debate debate = debateRepository.findById(debateId)
.orElseThrow(() -> new DebateException(DEBATE_NOT_FOUND));


// 최근 본 토론 기록 생성 및 저장
saveRecentViewedDebate(userId, debate);


debate.setHits(debate.getHits() + 1);
debateRepository.save(debate);

Expand Down Expand Up @@ -224,7 +231,7 @@ public List<DebateByKeywordDTO> GetDebatesByKeyword(Long keywordID, String sort)
for (Debate debate : debates) {
String title= debate.getTitle();
String content =debate.getContent();
String mediaUrl =null;
String mediaUrl =debate.getImageUrl();
Long hit = debate.getHits();
Long comment = (long) debate.getComment().size();
debateList.add(new DebateByKeywordDTO(title,content,mediaUrl,hit,comment));
Expand All @@ -233,4 +240,39 @@ public List<DebateByKeywordDTO> GetDebatesByKeyword(Long keywordID, String sort)
return debateList;

}
private void saveRecentViewedDebate(Long userId, Debate debate) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new UserException(USER_NOT_FOUND));

RecentViewedDebate recentViewedDebate = new RecentViewedDebate();
recentViewedDebate.setUser(user);
recentViewedDebate.setDebate(debate);
recentViewedDebate.setViewedAt(new Timestamp(System.currentTimeMillis())); // 시청 시간을 저장

recentViewedDebateRepository.save(recentViewedDebate);
}

public List<DebateByKeywordDTO> getRecentViewedDebate(String token){
if (jwtProvider.isExpiredToken(token)) {
throw new JwtUnauthorizedTokenException(INVALID_TOKEN);
}

Long userId = jwtProvider.getUserIdFromToken(token);

List<RecentViewedDebate> recentViewedDebates = recentViewedDebateRepository.findByUser_UserIdOrderByViewedAtDesc(userId);

// Debate ID를 통해 Debate 엔티티를 조회
List<Long> debateIds = recentViewedDebates.stream()
.map(recentViewedDebate -> recentViewedDebate.getDebate().getDebateId())
.collect(Collectors.toList());

List<Debate> debates = debateRepository.findAllById(debateIds);

// Debate를 DTO로 변환
return debates.stream()
.map(debate -> new DebateByKeywordDTO(debate.getTitle(), debate.getContent(), debate.getImageUrl(),debate.getHits(), (long) debate.getComment().size()))
.collect(Collectors.toList());

}

}

0 comments on commit 9b09708

Please sign in to comment.