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

[FIX] 질문별 조회시 답변 총 개수 반환 값 추가 및 lastIndex 생성 오류 수정 #200

Merged
merged 3 commits into from
Oct 1, 2024
Merged
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
9 changes: 9 additions & 0 deletions src/main/java/com/server/capple/config/RedisConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ public CacheManager noExpireCacheManager(RedisConnectionFactory redisConnectionF
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory).cacheDefaults(redisCacheConfiguration).build();
}

@Bean
public CacheManager oneDayExpireCacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
.entryTtl(Duration.ofDays(1));
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory).cacheDefaults(redisCacheConfiguration).build();
}

@Bean
public CacheManager apnsJwtCacheManager(RedisConnectionFactory redisCloudConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ public interface AnswerRepository extends JpaRepository<Answer, Long> {
Slice<AnswerInfoInterface> findByQuestion(@Param("questionId") Long questionId, Long lastIndex, Pageable pageable);

Slice<Answer> findByMemberAndIdIsLessThanEqual(@Param("member") Member member, Long lastIndex, Pageable pageable);

@Query("SELECT COUNT(a) FROM Answer a WHERE a.question.id = :questionId")
Integer getAnswerCountByQuestionId(Long questionId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.server.capple.domain.answer.service;

import com.server.capple.domain.answer.repository.AnswerRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

@Service
@RequiredArgsConstructor
public class AnswerCountService {
private final AnswerRepository answerRepository;

@Cacheable(value = "questionAnswer", key = "#questionId", cacheManager = "oneDayExpireCacheManager")
public Integer getQuestionAnswerCount(Long questionId) {
return answerRepository.getAnswerCountByQuestionId(questionId);
}

@Async
@CachePut(value = "questionAnswer", key = "#questionId", cacheManager = "oneDayExpireCacheManager")
public CompletableFuture<Integer> updateQuestionAnswerCount(Long questionId) {
return CompletableFuture.completedFuture(answerRepository.getAnswerCountByQuestionId(questionId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
import com.server.capple.global.common.SliceResponse;
import com.server.capple.global.exception.RestApiException;
import com.server.capple.global.exception.errorCode.AnswerErrorCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

@Service
@RequiredArgsConstructor
Expand All @@ -34,6 +39,8 @@ public class AnswerServiceImpl implements AnswerService {
private final AnswerMapper answerMapper;
private final MemberService memberService;
private final AnswerHeartRedisRepository answerHeartRedisRepository;
private final AnswerCountService answerCountService;
private final ApplicationEventPublisher applicationEventPublisher;

@Transactional
@Override
Expand All @@ -49,7 +56,7 @@ public AnswerResponse.AnswerId createAnswer(Member loginMember, Long questionId,
//답변 저장
Answer answer = answerRepository.save(answerMapper.toAnswerEntity(request, member, question));
// answer.getQuestion().increaseCommentCount();

applicationEventPublisher.publishEvent(new QuestionAnswerCountChangedEvent(questionId));
return new AnswerResponse.AnswerId(answer.getId());
}

Expand All @@ -71,13 +78,12 @@ public AnswerResponse.AnswerId updateAnswer(Member loginMember, Long answerId, A
@Transactional
public AnswerResponse.AnswerId deleteAnswer(Member loginMember, Long answerId) {
Answer answer = findAnswer(answerId);
applicationEventPublisher.publishEvent(new QuestionAnswerCountChangedEvent(answer.getQuestion().getId()));

checkPermission(loginMember, answer);
// answer.getQuestion().decreaseCommentCount();


answer.delete();

return new AnswerResponse.AnswerId(answerId);
}

Expand All @@ -104,7 +110,7 @@ public SliceResponse<AnswerInfo> getAnswerList(Long memberId, Long questionId, L
answerHeartRedisRepository.isMemberLikedAnswer(memberId, answerInfoDto.getAnswer().getId()),
answerInfoDto.getAnswer().getMember().getId().equals(memberId)
)
).toList(), lastIndex.toString(), null);
).toList(), lastIndex.toString(), answerCountService.getQuestionAnswerCount(questionId));
}

// 유저가 작성한 답변 조회
Expand Down Expand Up @@ -158,10 +164,25 @@ private Long getLastIndex(Long lastIndex) {
}

private Long getLastIndexFromAnswerInfoInterface(Long lastIndex, Slice<AnswerInfoInterface> answerInfoSliceInterface) {
return lastIndex == Long.MAX_VALUE ? answerInfoSliceInterface.stream().map(AnswerInfoInterface::getAnswer).map(Answer::getId).max(Long::compareTo).get() : lastIndex;
if(answerInfoSliceInterface.hasContent() && lastIndex == Long.MAX_VALUE)
return answerInfoSliceInterface.stream().map(AnswerInfoInterface::getAnswer).map(Answer::getId).max(Long::compareTo).get();
return lastIndex;
}

private Long getLastIndexFromAnswer(Long lastIndex, Slice<Answer> answerSlice) {
return lastIndex == Long.MAX_VALUE ? answerSlice.stream().map(Answer::getId).max(Long::compareTo).get() : lastIndex;
if (answerSlice.hasContent() && lastIndex == Long.MAX_VALUE)
return answerSlice.stream().map(Answer::getId).max(Long::compareTo).get();
return lastIndex;
}

@Getter
@AllArgsConstructor
static class QuestionAnswerCountChangedEvent {
private Long questionId;
}

@TransactionalEventListener(classes = QuestionAnswerCountChangedEvent.class, phase = TransactionPhase.AFTER_COMPLETION)
public void handleQuestionCreatedEvent(QuestionAnswerCountChangedEvent event) {
answerCountService.updateQuestionAnswerCount(event.getQuestionId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ private Long getLastIndex(Long lastIndex) {
}

private Long getLastIndexFromBoardCommentInfoInterface(Long lastIndex, Slice<BoardCommentInfoInterface> sliceBoardCommentInfos) {
return lastIndex == Long.MAX_VALUE ? sliceBoardCommentInfos.stream().map(BoardCommentInfoInterface::getBoardComment).map(BoardComment::getId).max(Long::compareTo).get() : lastIndex;
if(sliceBoardCommentInfos.hasContent() && lastIndex == Long.MAX_VALUE)
return sliceBoardCommentInfos.stream().map(BoardCommentInfoInterface::getBoardComment).map(BoardComment::getId).max(Long::compareTo).get();
return lastIndex;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ private long getLastIndex(Long lastIndex) {
}

private Long getLastIndexFromNotification(Long lastIndex, Slice<Notification> notifications) {
return lastIndex == Long.MAX_VALUE ? notifications.stream().map(Notification::getId).max(Long::compareTo).get() : lastIndex;
if(notifications.hasContent() && lastIndex == Long.MAX_VALUE)
return notifications.stream().map(Notification::getId).max(Long::compareTo).get();
return lastIndex;
}
}
Loading