Skip to content

Commit

Permalink
Merge pull request #84 from Team-baebae/feature/reaction_count/#83
Browse files Browse the repository at this point in the history
Feature/reaction count/#83
  • Loading branch information
jihyo-j authored May 17, 2024
2 parents 3203df3 + 9f7dbdb commit 5de99e3
Show file tree
Hide file tree
Showing 20 changed files with 559 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,4 @@ public ResponseEntity<Map<ReactionValue, Boolean>> hasReacted(@PathVariable Long
return ResponseEntity.ok(hasReacted);
}

@GetMapping("/{answerId}/reactionsCount")
public ResponseEntity<ReactionResponse.CountReactionInformationDto> getReactionCounts(@PathVariable Long answerId) {
ReactionResponse.CountReactionInformationDto reactionCounts = answerService.getReactionCounts(answerId);
return ResponseEntity.ok(reactionCounts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,5 @@ ResponseEntity<Map<ReactionValue, Boolean>> hasReacted(
@PathVariable Long answerId,
@RequestParam Long memberId);

@Operation(
summary = "특정 답변의 반응 개수 조회",
description = "특정 답변에 대한 하트, 궁금해요, 슬퍼요, 통했당 반응의 개수를 조회합니다."
)

@ApiResponse(responseCode = "200", description = "반응 개수 조회 성공",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ReactionResponse.CountReactionInformationDto.class)))
@GetMapping("/{answerId}/reactionsCount")
ResponseEntity<ReactionResponse.CountReactionInformationDto> getReactionCounts(
@PathVariable Long answerId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.web.baebaeBE.domain.music.entity.Music;
import com.web.baebaeBE.domain.question.entity.Question;
import com.web.baebaeBE.domain.reaction.entity.ReactionValue;
import com.web.baebaeBE.domain.reactioncount.entity.ReactionCount;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.OnDelete;
Expand Down Expand Up @@ -62,33 +63,23 @@ public class Answer {
@Column(name = "image_url")
private String imageUrl;

@Column(name = "heart_count", nullable = false)
private int heartCount;

@Column(name = "curious_count", nullable = false)
private int curiousCount;

@Column(name = "sad_count", nullable = false)
private int sadCount;

@Column(name = "connect_count", nullable = false)
private int connectCount;

@Column(name = "created_date", nullable = false)
private LocalDateTime createdDate;

@OneToMany(mappedBy = "answer", cascade = CascadeType.REMOVE)
private List<CategorizedAnswer> categorizedAnswers;

@OneToOne(mappedBy = "answer", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private ReactionCount reactionCount;

@Column(name = "profile_on_off", nullable = false)
private boolean profileOnOff;

public static Answer of(Long id, Question question, Category category, Member member, String nickname,String content,
List<String> imageFiles, Music music, String linkAttachments, String imageUrl, int heartCount,
int curiousCount, int sadCount, int connectCount, LocalDateTime createdDate, boolean profileOnOff) {

return new Answer(id, question, category, member, nickname, imageFiles, content, music, linkAttachments,imageUrl, heartCount,
curiousCount, sadCount, connectCount, createdDate,null, profileOnOff);
public static Answer of(Long id, Question question, Category category, Member member, String nickname, String content,
List<String> imageFiles, Music music, String linkAttachments, String imageUrl, LocalDateTime createdDate,
ReactionCount reactionCount, boolean profileOnOff) {
return new Answer(id, question, category, member, nickname, imageFiles, content, music, linkAttachments, imageUrl, createdDate, null, reactionCount, profileOnOff);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ public Answer toEntity(AnswerCreateRequest request, Question question, Member me
.linkAttachments(request.getLinkAttachments())
.profileOnOff(request.getProfileOnOff())
.createdDate(LocalDateTime.now())
.heartCount(0)
.curiousCount(0)
.sadCount(0)
.connectCount(0)
.music(music)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
import com.web.baebaeBE.domain.notification.dto.NotificationRequest;
import com.web.baebaeBE.domain.notification.service.NotificationService;
import com.web.baebaeBE.domain.question.repository.QuestionRepository;
import com.web.baebaeBE.domain.reaction.dto.ReactionResponse;
import com.web.baebaeBE.domain.reactioncount.dto.ReactionResponse;
import com.web.baebaeBE.domain.reaction.entity.ReactionValue;
import com.web.baebaeBE.domain.reaction.repository.MemberAnswerReactionRepository;
import com.web.baebaeBE.domain.reactioncount.entity.ReactionCount;
import com.web.baebaeBE.global.error.exception.BusinessException;
import com.web.baebaeBE.global.image.s3.S3ImageStorageService;
import com.web.baebaeBE.domain.answer.entity.Answer;
Expand Down Expand Up @@ -138,25 +139,6 @@ public void deleteAnswer(Long answerId) {
answerRepository.delete(answer);
}

@Transactional
public void updateReactionCounts(Long answerId, int heartCount, int curiousCount, int sadCount) {
Answer answer = answerRepository.findByAnswerId(answerId)
.orElseThrow(() -> new BusinessException(AnswerError.NO_EXIST_ANSWER));
answer.setHeartCount(heartCount);
answer.setCuriousCount(curiousCount);
answer.setSadCount(sadCount);
answerRepository.save(answer);

// 알림 생성 및 전송
NotificationRequest.create notificationDto = new NotificationRequest.create(
answer.getMember().getId(),
"답변에 새로운 반응이 있습니다!",
String.format("하트: %d, 궁금해요: %d, 슬퍼요: %d", heartCount, curiousCount, sadCount),
NotificationRequest.EventType.REACTION,
"updated"
);
notificationService.createNotification(notificationDto);
}

@Transactional
public Map<ReactionValue, Boolean> hasReacted(Long answerId, Long memberId) {
Expand All @@ -173,11 +155,5 @@ public Map<ReactionValue, Boolean> hasReacted(Long answerId, Long memberId) {
return reactionStatus;
}

@Transactional
public ReactionResponse.CountReactionInformationDto getReactionCounts(Long answerId) {
Answer answer = answerRepository.findByAnswerId(answerId)
.orElseThrow(() -> new BusinessException(AnswerError.NO_EXIST_ANSWER));

return ReactionResponse.CountReactionInformationDto.of(answer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
@RequestMapping("/api/questions")
public class QuestionController implements QuestionApi {
private final QuestionService questionService;

public QuestionController(QuestionService questionService) {
this.questionService = questionService;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.web.baebaeBE.domain.reaction.controller.api.MemberAnswerReactionApi;
import com.web.baebaeBE.domain.reaction.dto.ReactionRequest;
import com.web.baebaeBE.domain.reaction.dto.ReactionResponse;
import com.web.baebaeBE.domain.reactioncount.dto.ReactionResponse;
import com.web.baebaeBE.domain.reaction.entity.MemberAnswerReaction;
import com.web.baebaeBE.domain.reaction.service.MemberAnswerReactionService;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.web.baebaeBE.domain.reaction.controller.api;

import com.web.baebaeBE.domain.reaction.dto.ReactionRequest;
import com.web.baebaeBE.domain.reaction.dto.ReactionResponse;
import com.web.baebaeBE.domain.reactioncount.dto.ReactionResponse;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,48 @@

public class ReactionResponse {

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class CountReactionInformationDto {
private Integer heartCount;
private Integer curiousCount;
private Integer sadCount;
private Integer connectCount;

public static CountReactionInformationDto of(Answer answer) {
return CountReactionInformationDto.builder()
.heartCount(answer.getHeartCount())
.curiousCount(answer.getCuriousCount())
.sadCount(answer.getSadCount())
.connectCount(answer.getConnectCount())
.build();
}
}
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class ReactionInformationDto {
private boolean isClicked;
private int heartCount;
private int curiousCount;
private int sadCount;
private int connectCount;

public static ReactionInformationDto of(Answer answer, boolean isClicked) {
return ReactionInformationDto.builder()
.isClicked(isClicked)
.heartCount(answer.getHeartCount())
.curiousCount(answer.getCuriousCount())
.sadCount(answer.getSadCount())
.connectCount(answer.getConnectCount())
.build();
}
}
// @Getter
// @Setter
// @Builder
// @NoArgsConstructor
// @AllArgsConstructor
// public static class CountReactionInformationDto {
// private Integer heartCount;
// private Integer curiousCount;
// private Integer sadCount;
// private Integer connectCount;
//
// public static CountReactionInformationDto of(Answer answer) {
// return CountReactionInformationDto.builder()
// .heartCount(answer.getHeartCount())
// .curiousCount(answer.getCuriousCount())
// .sadCount(answer.getSadCount())
// .connectCount(answer.getConnectCount())
// .build();
// }
// }
// @Getter
// @Setter
// @Builder
// @NoArgsConstructor
// @AllArgsConstructor
// public static class ReactionInformationDto {
// private boolean isClicked;
// private int heartCount;
// private int curiousCount;
// private int sadCount;
// private int connectCount;
//
// public static ReactionInformationDto of(Answer answer, boolean isClicked) {
// return ReactionInformationDto.builder()
// .isClicked(isClicked)
// .heartCount(answer.getHeartCount())
// .curiousCount(answer.getCuriousCount())
// .sadCount(answer.getSadCount())
// .connectCount(answer.getConnectCount())
// .build();
// }
// }

/*@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import com.web.baebaeBE.domain.login.exception.LoginException;
import com.web.baebaeBE.domain.member.entity.Member;
import com.web.baebaeBE.domain.member.repository.MemberRepository;
import com.web.baebaeBE.domain.reaction.dto.ReactionResponse;
import com.web.baebaeBE.domain.reactioncount.dto.ReactionResponse;
import com.web.baebaeBE.domain.reaction.entity.MemberAnswerReaction;
import com.web.baebaeBE.domain.reaction.entity.ReactionValue;
import com.web.baebaeBE.domain.reaction.exception.ReactionException;
import com.web.baebaeBE.domain.reaction.repository.MemberAnswerReactionRepository;
import com.web.baebaeBE.domain.reactioncount.entity.ReactionCount;
import com.web.baebaeBE.domain.reactioncount.repository.ReactionCountJpaRepository;
import com.web.baebaeBE.global.error.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -26,6 +28,7 @@ public class MemberAnswerReactionService {

private final ReactionUpdateService reactionUpdateService;
private final MemberAnswerReactionRepository memberAnswerReactionRepository;
private final ReactionCountJpaRepository reactionCountJpaRepository;
private final MemberRepository memberRepository;
private final AnswerRepository answerRepository;

Expand All @@ -36,14 +39,20 @@ public ReactionResponse.ReactionInformationDto createReaction(Long memberId, Lon
.orElseThrow(() -> new BusinessException(AnswerError.NO_EXIST_ANSWER));
boolean isClicked = false;

// ReactionCount 엔터티를 가져옴
ReactionCount reactionCount = reactionCountJpaRepository.findByAnswerId(answerId);
if (reactionCount == null) {
throw new BusinessException(AnswerError.NO_EXIST_ANSWER);
}

// 이미 해당 반응이 있는지 확인
Optional<MemberAnswerReaction> existingReactionOpt = memberAnswerReactionRepository.findByMemberAndAnswerAndReaction(member, answer, reaction);

if (existingReactionOpt.isPresent()) {
// 이미 해당 반응이 있다면 반응을 삭제
MemberAnswerReaction existingReaction = existingReactionOpt.get();
memberAnswerReactionRepository.delete(existingReaction);
reactionUpdateService.decreaseReactionCount(answer, reaction);
reactionUpdateService.decreaseReactionCount(reactionCount, reaction);
isClicked = false;
} else {
// 해당 반응이 없다면 새로운 반응을 저장
Expand All @@ -54,11 +63,11 @@ public ReactionResponse.ReactionInformationDto createReaction(Long memberId, Lon
.build();

memberAnswerReactionRepository.save(memberAnswerReaction);
reactionUpdateService.increaseReactionCount(answer, reaction);
reactionUpdateService.increaseReactionCount(reactionCount, reaction);
isClicked = true;
}

return ReactionResponse.ReactionInformationDto.of(answer,isClicked);
return ReactionResponse.ReactionInformationDto.of(reactionCount, isClicked);
}

/*// 피드 주인이 아닌 다른 사람이 통했당 신청
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.web.baebaeBE.domain.answer.entity.Answer;
import com.web.baebaeBE.domain.reaction.entity.ReactionValue;
import com.web.baebaeBE.domain.reactioncount.entity.ReactionCount;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -11,36 +12,36 @@
@RequiredArgsConstructor
public class ReactionUpdateService {

public void increaseReactionCount(Answer answer, ReactionValue reaction) {
public void increaseReactionCount(ReactionCount reactionCount, ReactionValue reaction) {
switch (reaction) {
case HEART: // 좋아요
answer.setHeartCount(answer.getHeartCount() + 1);
reactionCount.setHeartCount(reactionCount.getHeartCount() + 1);
break;
case CURIOUS: // 궁금해요
answer.setCuriousCount(answer.getCuriousCount() + 1);
reactionCount.setCuriousCount(reactionCount.getCuriousCount() + 1);
break;
case SAD: // 슬퍼요
answer.setSadCount(answer.getSadCount() + 1);
reactionCount.setSadCount(reactionCount.getSadCount() + 1);
break;
case CONNECT: // 통했당
answer.setConnectCount(answer.getConnectCount() + 1);
reactionCount.setConnectCount(reactionCount.getConnectCount() + 1);
break;
}
}

public void decreaseReactionCount(Answer answer, ReactionValue reaction) {
public void decreaseReactionCount(ReactionCount reactionCount, ReactionValue reaction) {
switch (reaction) {
case HEART: // 좋아요
answer.setHeartCount(answer.getHeartCount() - 1);
reactionCount.setHeartCount(reactionCount.getHeartCount() - 1);
break;
case CURIOUS: // 궁금해요
answer.setCuriousCount(answer.getCuriousCount() - 1);
reactionCount.setCuriousCount(reactionCount.getCuriousCount() - 1);
break;
case SAD: // 슬퍼요
answer.setSadCount(answer.getSadCount() - 1);
reactionCount.setSadCount(reactionCount.getSadCount() - 1);
break;
case CONNECT: // 통했당
answer.setConnectCount(answer.getConnectCount() - 1);
reactionCount.setConnectCount(reactionCount.getConnectCount() - 1);
break;
}
}
Expand Down
Loading

0 comments on commit 5de99e3

Please sign in to comment.