Skip to content

Commit

Permalink
RAC-351 fix : 중복코드 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
ywj9811 committed Apr 28, 2024
1 parent 5d06c68 commit c016d3d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,75 +1,35 @@
package com.postgraduate.global.bizppurio.usecase;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.postgraduate.domain.senior.domain.entity.Senior;
import com.postgraduate.domain.user.domain.entity.User;
import com.postgraduate.global.bizppurio.dto.req.CommonRequest;
import com.postgraduate.global.bizppurio.dto.res.MessageResponse;
import com.postgraduate.global.bizppurio.mapper.BizppurioMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.function.Supplier;

import static org.springframework.http.MediaType.APPLICATION_JSON;

@RequiredArgsConstructor
@Component
@Slf4j
public class BizppurioJuniorMessage {
private final WebClient webClient;
private final BizppurioAuth bizppurioAuth;
private final ObjectMapper objectMapper;
private final BizppurioMapper mapper;

@Value("${bizppurio.message}")
private String messageUrl;
private final BizppurioSend bizppurioSend;

public void mentoringApply(User user) {
sendMessageWithExceptionHandling(() -> mapper.mapToJuniorApplyMessage(user));
bizppurioSend.sendMessageWithExceptionHandling(() -> mapper.mapToJuniorApplyMessage(user));
}

public void mentoringAccept(User user, Senior senior, String time) {
sendMessageWithExceptionHandling(() -> {
bizppurioSend.sendMessageWithExceptionHandling(() -> {
String chatLink = senior.getProfile().getChatLink();
return mapper.mapToJuniorAcceptMessage(user, chatLink, time);
});
}

public void mentoringRefuse(User user) {
sendMessageWithExceptionHandling(() -> mapper.mapToJuniorRefuseMessage(user));
bizppurioSend.sendMessageWithExceptionHandling(() -> mapper.mapToJuniorRefuseMessage(user));
}

public void mentoringFinish(User user) {
sendMessageWithExceptionHandling(() -> mapper.mapToJuniorFinish(user));
}

private void sendMessageWithExceptionHandling(Supplier<CommonRequest> messageSupplier) {
try {
CommonRequest commonRequest = messageSupplier.get();
String accessToken = bizppurioAuth.getAuth();
String request = objectMapper.writeValueAsString(commonRequest);
webClient.post()
.uri(messageUrl)
.headers(h -> h.setContentType(APPLICATION_JSON))
.headers(h -> h.setBearerAuth(accessToken))
.bodyValue(request)
.retrieve()
.bodyToMono(MessageResponse.class)
.subscribe(this::check);
} catch (Exception ex) {
log.error("알림톡 전송 예외 발생: {}", ex.getMessage());
}
}

private void check(MessageResponse response) {
if (response.code() != 1000) {
log.error("전송실패 errorCode : {} errorMessage : {}", response.code(), response.description());
return;
}
log.info("알림톡 전송에 성공하였습니다.");
bizppurioSend.sendMessageWithExceptionHandling(() -> mapper.mapToJuniorFinish(user));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.postgraduate.global.bizppurio.usecase;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.postgraduate.global.bizppurio.dto.req.CommonRequest;
import com.postgraduate.global.bizppurio.dto.res.MessageResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.function.Supplier;

import static org.springframework.http.MediaType.APPLICATION_JSON;

@RequiredArgsConstructor
@Slf4j
@Component
public class BizppurioSend {
private final BizppurioAuth bizppurioAuth;
private final ObjectMapper objectMapper;
private final WebClient webClient;

@Value("${bizppurio.message}")
private String messageUrl;

protected void sendMessageWithExceptionHandling(Supplier<CommonRequest> messageSupplier) {
try {
CommonRequest commonRequest = messageSupplier.get();
String accessToken = bizppurioAuth.getAuth();
String request = objectMapper.writeValueAsString(commonRequest);
webClient.post()
.uri(messageUrl)
.headers(h -> h.setContentType(APPLICATION_JSON))
.headers(h -> h.setBearerAuth(accessToken))
.bodyValue(request)
.retrieve()
.bodyToMono(MessageResponse.class)
.subscribe(this::check);
} catch (Exception ex) {
log.error("알림톡 전송 예외 발생: {}", ex.getMessage());
}
}

private void check(MessageResponse response) {
if (response.code() != 1000) {
log.error("전송실패 errorCode : {} errorMessage : {}", response.code(), response.description());
return;
}
log.info("알림톡 전송에 성공하였습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -1,83 +1,44 @@
package com.postgraduate.global.bizppurio.usecase;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.postgraduate.domain.senior.domain.entity.Senior;
import com.postgraduate.domain.user.domain.entity.User;
import com.postgraduate.global.bizppurio.dto.req.CommonRequest;
import com.postgraduate.global.bizppurio.dto.res.MessageResponse;
import com.postgraduate.global.bizppurio.mapper.BizppurioMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

import static org.springframework.http.MediaType.APPLICATION_JSON;
import java.util.function.Supplier;

@RequiredArgsConstructor
@Component
@Slf4j
public class BizppurioSeniorMessage {
private final WebClient webClient;
private final BizppurioMapper mapper;
private final BizppurioAuth bizppurioAuth;
private final ObjectMapper objectMapper;

@Value("${bizppurio.message}")
private String messageUrl;
private final BizppurioSend bizppurioSend;

public void signUp(User user) {
sendMessageWithExceptionHandling(() -> mapper.mapToSeniorSignUpMessage(user));
bizppurioSend.sendMessageWithExceptionHandling(() -> mapper.mapToSeniorSignUpMessage(user));
}

public void mentoringApply(User user) {
sendMessageWithExceptionHandling(() -> mapper.mapToSeniorApplyMessage(user));
bizppurioSend.sendMessageWithExceptionHandling(() -> mapper.mapToSeniorApplyMessage(user));
}

public void mentoringAccept(Senior senior, String time) {
sendMessageWithExceptionHandling(() -> {
bizppurioSend.sendMessageWithExceptionHandling(() -> {
User user = senior.getUser();
String chatLink = senior.getProfile().getChatLink();
return mapper.mapToSeniorAcceptMessage(user, chatLink, time);
});
}

public void certificationApprove(User user) {
sendMessageWithExceptionHandling(() -> mapper.mapToCertificationApprove(user));
bizppurioSend.sendMessageWithExceptionHandling(() -> mapper.mapToCertificationApprove(user));
}

public void certificationDenied(User user) {
sendMessageWithExceptionHandling(() -> mapper.mapToCertificationDenied(user));
bizppurioSend.sendMessageWithExceptionHandling(() -> mapper.mapToCertificationDenied(user));
}

public void mentoringFinish(User user) {
sendMessageWithExceptionHandling(() -> mapper.mapToSeniorFinish(user));
}

private void sendMessageWithExceptionHandling(Supplier<CommonRequest> messageSupplier) {
try {
CommonRequest commonRequest = messageSupplier.get();
String accessToken = bizppurioAuth.getAuth();
String request = objectMapper.writeValueAsString(commonRequest);
webClient.post()
.uri(messageUrl)
.headers(h -> h.setContentType(APPLICATION_JSON))
.headers(h -> h.setBearerAuth(accessToken))
.bodyValue(request)
.retrieve()
.bodyToMono(MessageResponse.class)
.subscribe(this::check);
} catch (Exception ex) {
log.error("알림톡 전송 예외 발생: {}", ex.getMessage());
}
}

private void check(MessageResponse response) {
if (response.code() != 1000) {
log.error("전송실패 errorCode : {} errorMessage : {}", response.code(), response.description());
return;
}
log.info("알림톡 전송에 성공하였습니다.");
bizppurioSend.sendMessageWithExceptionHandling(() -> mapper.mapToSeniorFinish(user));
}
}

0 comments on commit c016d3d

Please sign in to comment.