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

[refactor #175] 크레딧 저장 내역 함수 통합 #176

Merged
merged 5 commits into from
Dec 27, 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.dnd.gongmuin.common.dto.PageResponse;
import com.dnd.gongmuin.common.exception.runtime.NotFoundException;
import com.dnd.gongmuin.common.exception.runtime.ValidationException;
import com.dnd.gongmuin.credit_history.domain.CreditType;
import com.dnd.gongmuin.credit_history.service.CreditHistoryService;
import com.dnd.gongmuin.member.domain.Member;
import com.dnd.gongmuin.notification.dto.NotificationEvent;
Expand Down Expand Up @@ -104,7 +105,11 @@ private void chooseAnswer(QuestionPost questionPost, Answer answer) {
questionPost.updateIsChosen(answer);
questionPost.updateStatus(QuestionPostStatus.CHOSEN_COMPLETED);
answer.getMember().increaseCredit(questionPost.getReward());
creditHistoryService.saveChosenCreditHistory(questionPost, answer);
creditHistoryService.saveCreditHistory(
CreditType.CHOSEN,
questionPost.getReward(),
questionPost.getMember()
);
}

private void validateIfQuestionPostExists(Long questionPostId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public CreateChatInquiryResponse createChatInquiry(CreateChatInquiryRequest requ
ChatInquiryMapper.toChatInquiry(questionPost, inquirer, answerer, request.inquiryMessage())
);
memberRepository.save(inquirer);
creditHistoryService.saveChatCreditHistory(CreditType.CHAT_REQUEST, inquirer);
creditHistoryService.saveCreditHistory(CreditType.CHAT_REQUEST, CHAT_REWARD, inquirer);
eventPublisher.publishEvent(
new NotificationEvent(NotificationType.CHAT_REQUEST, chatInquiry.getId(), inquirer.getId(), answerer)
);
Expand Down Expand Up @@ -96,7 +96,7 @@ public AcceptChatResponse acceptChat(Long chatInquiryId, Member answerer) {
ChatInquiry chatInquiry = getChatInquiryById(chatInquiryId);
validateIfAnswerer(answerer, chatInquiry);
chatInquiry.updateStatusAccepted();
creditHistoryService.saveChatCreditHistory(CreditType.CHAT_ACCEPT, answerer);
creditHistoryService.saveCreditHistory(CreditType.CHAT_ACCEPT, CHAT_REWARD, answerer);

ChatRoom chatRoom = chatRoomRepository.save(
ChatRoomMapper.toChatRoom(chatInquiry.getQuestionPost(), chatInquiry.getInquirer(), answerer)
Expand All @@ -118,7 +118,7 @@ public RejectChatResponse rejectChat(Long chatInquiryId, Member answerer) {

validateIfAnswerer(answerer, chatInquiry);
chatInquiry.updateStatusRejected();
creditHistoryService.saveChatCreditHistory(CreditType.CHAT_REFUND, chatInquiry.getInquirer());
creditHistoryService.saveCreditHistory(CreditType.CHAT_REFUND, CHAT_REWARD, chatInquiry.getInquirer());
eventPublisher.publishEvent(
new NotificationEvent(NotificationType.CHAT_REJECT, chatInquiry.getId(), answerer.getId(),
chatInquiry.getInquirer())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.servers.Server;

@OpenAPIDefinition(
info = @Info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dnd.gongmuin.answer.domain.Answer;
import com.dnd.gongmuin.credit_history.domain.CreditHistory;
import com.dnd.gongmuin.credit_history.domain.CreditType;
import com.dnd.gongmuin.credit_history.dto.CreditHistoryMapper;
import com.dnd.gongmuin.credit_history.repository.CreditHistoryRepository;
import com.dnd.gongmuin.member.domain.Member;
import com.dnd.gongmuin.member.repository.MemberRepository;
import com.dnd.gongmuin.question_post.domain.QuestionPost;

import lombok.RequiredArgsConstructor;

Expand All @@ -23,18 +20,9 @@ public class CreditHistoryService {
private final CreditHistoryRepository creditHistoryRepository;
private final MemberRepository memberRepository;

@Transactional
public void saveChosenCreditHistory(QuestionPost questionPost, Answer answer) {
public void saveCreditHistory(CreditType creditType, int credit, Member member) {
creditHistoryRepository.save(
CreditHistoryMapper.toCreditHistory(CreditType.CHOSEN, questionPost.getReward(), answer.getMember())
);
}

@Transactional
public void saveChatCreditHistory(CreditType creditType, Member member) {
int chatCredit = 2000;
creditHistoryRepository.save(
CreditHistoryMapper.toCreditHistory(creditType, chatCredit, member)
CreditHistoryMapper.toCreditHistory(creditType, credit, member)
);
}

Expand All @@ -45,16 +33,4 @@ public void saveCreditHistoryInMemberIds(List<Long> memberIds, CreditType type,
.toList();
creditHistoryRepository.saveAll(histories);
}

public void saveQuestionPostCreditHistory(int reward, Member member) {
creditHistoryRepository.save(
CreditHistoryMapper.toCreditHistory(CreditType.WRITE_QUESTION_POST, reward, member)
);
}

public void saveRefundQuestionPostCreditHistory(int reward, Member member) {
creditHistoryRepository.save(
CreditHistoryMapper.toCreditHistory(CreditType.REFUND_QUESTION_POST, reward, member)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.dnd.gongmuin.common.dto.PageMapper;
import com.dnd.gongmuin.common.dto.PageResponse;
import com.dnd.gongmuin.common.exception.runtime.NotFoundException;
import com.dnd.gongmuin.credit_history.domain.CreditType;
import com.dnd.gongmuin.credit_history.service.CreditHistoryService;
import com.dnd.gongmuin.member.domain.JobGroup;
import com.dnd.gongmuin.member.domain.Member;
Expand Down Expand Up @@ -61,7 +62,7 @@ public RegisterQuestionPostResponse registerQuestionPost(
Member member
) {
decreaseMemberCredit(request, member);
creditHistoryService.saveQuestionPostCreditHistory(request.reward(), member);
creditHistoryService.saveCreditHistory(CreditType.WRITE_QUESTION_POST, request.reward(), member);

QuestionPost questionPost = QuestionPostMapper.toQuestionPost(request, member);
return QuestionPostMapper.toRegisterQuestionPostResponse(
Expand Down Expand Up @@ -157,7 +158,8 @@ private void refundQuestionPostCredit() {
refundQuestionPostDto.member().increaseCredit(refundQuestionPostDto.reward());
memberRepository.save(refundQuestionPostDto.member());

creditHistoryService.saveRefundQuestionPostCreditHistory(
creditHistoryService.saveCreditHistory(
CreditType.REFUND_QUESTION_POST,
refundQuestionPostDto.reward(),
refundQuestionPostDto.member()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.dnd.gongmuin.answer.domain.Answer;
import com.dnd.gongmuin.common.fixture.AnswerFixture;
import com.dnd.gongmuin.common.fixture.CreditHistoryFixture;
import com.dnd.gongmuin.common.fixture.MemberFixture;
import com.dnd.gongmuin.common.fixture.QuestionPostFixture;
import com.dnd.gongmuin.credit_history.domain.CreditHistory;
import com.dnd.gongmuin.credit_history.domain.CreditType;
import com.dnd.gongmuin.credit_history.repository.CreditHistoryRepository;
import com.dnd.gongmuin.member.domain.Member;
import com.dnd.gongmuin.member.repository.MemberRepository;
import com.dnd.gongmuin.question_post.domain.QuestionPost;

@DisplayName("[AnswerService 테스트]")
@DisplayName("[CreditHistoryService 테스트]")
@ExtendWith(MockitoExtension.class)
class CreditHistoryServiceTest {

Expand All @@ -37,20 +33,9 @@ class CreditHistoryServiceTest {
@InjectMocks
private CreditHistoryService creditHistoryService;

@DisplayName("[크레딧 내역을 저장할 수 있다.]")
@Test
void saveChosenCreditHistory() {
QuestionPost questionPost = QuestionPostFixture.questionPost(MemberFixture.member(1L));
Answer answer = AnswerFixture.answer(questionPost.getId(), MemberFixture.member(2L));

given(creditHistoryRepository.save(any(CreditHistory.class))).willReturn(any(CreditHistory.class));

creditHistoryService.saveChosenCreditHistory(questionPost, answer);
}

@DisplayName("회원 아이디 리스트에 속하는 회원에 대한 크레딧을 모두 저장할 수 있다.")
@Test
void test() {
void saveCreditHistoryInMemberIds() {
//given
List<Long> memberIds = List.of(1L, 2L);
Member member1 = MemberFixture.member(memberIds.get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.dnd.gongmuin.common.fixture.InteractionCountFixture;
import com.dnd.gongmuin.common.fixture.MemberFixture;
import com.dnd.gongmuin.common.fixture.QuestionPostFixture;
import com.dnd.gongmuin.credit_history.domain.CreditType;
import com.dnd.gongmuin.credit_history.service.CreditHistoryService;
import com.dnd.gongmuin.member.domain.Member;
import com.dnd.gongmuin.member.repository.MemberRepository;
Expand Down Expand Up @@ -80,7 +81,11 @@ void registerQuestionPost() {

given(questionPostRepository.save(any(QuestionPost.class))).willReturn(questionPost);
given(memberRepository.save(any(Member.class))).willReturn(member);
doNothing().when(creditHistoryService).saveQuestionPostCreditHistory(anyInt(), any(Member.class));
doNothing().when(creditHistoryService).saveCreditHistory(
CreditType.WRITE_QUESTION_POST,
questionPost.getReward(),
member
);

//when
RegisterQuestionPostResponse response = questionPostService.registerQuestionPost(request, member);
Expand Down
Loading