Skip to content

Commit

Permalink
fix: #199 질문별 조회시 답변 총 개수 반환 값 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jaewonLeeKOR committed Oct 1, 2024
1 parent 458b08e commit ea5f175
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
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 @@ -164,4 +170,15 @@ private Long getLastIndexFromAnswerInfoInterface(Long lastIndex, Slice<AnswerInf
private Long getLastIndexFromAnswer(Long lastIndex, Slice<Answer> answerSlice) {
return (!answerSlice.getContent().isEmpty() && lastIndex == Long.MAX_VALUE) ? answerSlice.stream().map(Answer::getId).max(Long::compareTo).get() : 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());
}
}

0 comments on commit ea5f175

Please sign in to comment.