Skip to content

Commit

Permalink
refactor: infrastructure 패키지 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
geneaky committed Sep 18, 2024
1 parent 3cc9492 commit c23b596
Show file tree
Hide file tree
Showing 24 changed files with 372 additions and 375 deletions.
10 changes: 5 additions & 5 deletions src/main/java/toy/bookchat/bookchat/BookChatApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import toy.bookchat.bookchat.config.token.OAuth2Properties;
import toy.bookchat.bookchat.config.web.BookSearchProperties;
import toy.bookchat.bookchat.config.websocket.ExternalBrokerProperties;
import toy.bookchat.bookchat.infrastructure.aws.StorageProperties;
import toy.bookchat.bookchat.infrastructure.s3.StorageProperties;

@SpringBootApplication
@EnableConfigurationProperties({JwtTokenProperties.class, OAuth2Properties.class,
StorageProperties.class, ExternalBrokerProperties.class, BookSearchProperties.class})
public class BookChatApplication {
public static void main(String[] args) {
SpringApplication.run(BookChatApplication.class, args);
}

public static void main(String[] args) {
SpringApplication.run(BookChatApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package toy.bookchat.bookchat.domain.chat.service;

import static toy.bookchat.bookchat.infrastructure.push.PushType.CHAT;
import static toy.bookchat.bookchat.infrastructure.fcm.PushType.CHAT;

import java.util.List;
import org.springframework.data.domain.Pageable;
Expand All @@ -16,11 +16,11 @@
import toy.bookchat.bookchat.domain.participant.service.ParticipantValidator;
import toy.bookchat.bookchat.domain.user.User;
import toy.bookchat.bookchat.domain.user.service.UserReader;
import toy.bookchat.bookchat.infrastructure.broker.MessagePublisher;
import toy.bookchat.bookchat.infrastructure.broker.message.CommonMessage;
import toy.bookchat.bookchat.infrastructure.push.ChatMessageBody;
import toy.bookchat.bookchat.infrastructure.push.PushMessageBody;
import toy.bookchat.bookchat.infrastructure.push.service.PushService;
import toy.bookchat.bookchat.infrastructure.rabbitmq.MessagePublisher;
import toy.bookchat.bookchat.infrastructure.rabbitmq.message.CommonMessage;
import toy.bookchat.bookchat.infrastructure.fcm.ChatMessageBody;
import toy.bookchat.bookchat.infrastructure.fcm.PushMessageBody;
import toy.bookchat.bookchat.infrastructure.fcm.service.PushService;

@Service
public class ChatService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
import toy.bookchat.bookchat.domain.participant.service.ParticipantCleaner;
import toy.bookchat.bookchat.domain.participant.service.ParticipantReader;
import toy.bookchat.bookchat.domain.participant.service.ParticipantValidator;
import toy.bookchat.bookchat.domain.storage.StorageService;
import toy.bookchat.bookchat.infrastructure.s3.StorageService;
import toy.bookchat.bookchat.domain.user.User;
import toy.bookchat.bookchat.domain.user.service.UserReader;
import toy.bookchat.bookchat.infrastructure.broker.MessagePublisher;
import toy.bookchat.bookchat.infrastructure.broker.message.NotificationMessage;
import toy.bookchat.bookchat.infrastructure.rabbitmq.MessagePublisher;
import toy.bookchat.bookchat.infrastructure.rabbitmq.message.NotificationMessage;

@Service
public class ChatRoomService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import toy.bookchat.bookchat.domain.participant.ParticipantAdmin;
import toy.bookchat.bookchat.domain.participant.ParticipantStatus;
import toy.bookchat.bookchat.domain.participant.ParticipantWithChatRoom;
import toy.bookchat.bookchat.infrastructure.broker.MessagePublisher;
import toy.bookchat.bookchat.infrastructure.broker.message.NotificationMessage;
import toy.bookchat.bookchat.infrastructure.rabbitmq.MessagePublisher;
import toy.bookchat.bookchat.infrastructure.rabbitmq.message.NotificationMessage;

@Service
public class ParticipantService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package toy.bookchat.bookchat.domain.user.service;

import static toy.bookchat.bookchat.infrastructure.push.PushType.LOGIN;
import static toy.bookchat.bookchat.infrastructure.fcm.PushType.LOGIN;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Expand All @@ -14,16 +14,16 @@
import toy.bookchat.bookchat.db_module.device.repository.DeviceRepository;
import toy.bookchat.bookchat.db_module.user.UserEntity;
import toy.bookchat.bookchat.db_module.user.repository.UserRepository;
import toy.bookchat.bookchat.domain.storage.StorageService;
import toy.bookchat.bookchat.infrastructure.s3.StorageService;
import toy.bookchat.bookchat.domain.user.UserProfile;
import toy.bookchat.bookchat.domain.user.api.v1.request.ChangeUserNicknameRequest;
import toy.bookchat.bookchat.domain.user.api.v1.request.UserSignInRequest;
import toy.bookchat.bookchat.domain.user.api.v1.request.UserSignUpRequest;
import toy.bookchat.bookchat.domain.user.api.v1.response.MemberProfileResponse;
import toy.bookchat.bookchat.exception.badrequest.user.UserAlreadySignUpException;
import toy.bookchat.bookchat.exception.conflict.device.DeviceAlreadyRegisteredException;
import toy.bookchat.bookchat.infrastructure.push.PushMessageBody;
import toy.bookchat.bookchat.infrastructure.push.service.PushService;
import toy.bookchat.bookchat.infrastructure.fcm.PushMessageBody;
import toy.bookchat.bookchat.infrastructure.fcm.service.PushService;

@Service
public class UserService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package toy.bookchat.bookchat.infrastructure.push;
package toy.bookchat.bookchat.infrastructure.fcm;

import lombok.Builder;
import lombok.Getter;

@Getter
public class ChatMessageBody {

private Long chatId;
private Long chatRoomId;
private Long chatId;
private Long chatRoomId;

@Builder
private ChatMessageBody(Long chatId, Long chatRoomId) {
this.chatId = chatId;
this.chatRoomId = chatRoomId;
}
@Builder
private ChatMessageBody(Long chatId, Long chatRoomId) {
this.chatId = chatId;
this.chatRoomId = chatRoomId;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package toy.bookchat.bookchat.infrastructure.push;
package toy.bookchat.bookchat.infrastructure.fcm;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
Expand All @@ -13,19 +13,19 @@
@Configuration
public class FCMConfig {

@Bean
public FirebaseMessaging firebaseMessaging() throws IOException {
ClassPathResource resource = new ClassPathResource("firebase/bookchat-firebase-private.json");
InputStream refreshToken = resource.getInputStream();
@Bean
public FirebaseMessaging firebaseMessaging() throws IOException {
ClassPathResource resource = new ClassPathResource("firebase/bookchat-firebase-private.json");
InputStream refreshToken = resource.getInputStream();

FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(refreshToken))
.build();
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(refreshToken))
.build();

if (FirebaseApp.getApps().isEmpty()) {
return FirebaseMessaging.getInstance(FirebaseApp.initializeApp(options));
} else {
return FirebaseMessaging.getInstance(FirebaseApp.getInstance());
}
if (FirebaseApp.getApps().isEmpty()) {
return FirebaseMessaging.getInstance(FirebaseApp.initializeApp(options));
} else {
return FirebaseMessaging.getInstance(FirebaseApp.getInstance());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package toy.bookchat.bookchat.infrastructure.push;
package toy.bookchat.bookchat.infrastructure.fcm;

import lombok.Getter;

@Getter
public class PushMessageBody {

private PushType pushType;
private Object body;
private PushType pushType;
private Object body;

private PushMessageBody(PushType pushType, Object body) {
this.pushType = pushType;
this.body = body;
}
private PushMessageBody(PushType pushType, Object body) {
this.pushType = pushType;
this.body = body;
}

public static PushMessageBody of(PushType pushType, Object body) {
return new PushMessageBody(pushType, body);
}
public static PushMessageBody of(PushType pushType, Object body) {
return new PushMessageBody(pushType, body);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package toy.bookchat.bookchat.infrastructure.push;
package toy.bookchat.bookchat.infrastructure.fcm;

public enum PushType {

LOGIN,
CHAT;
LOGIN,
CHAT;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package toy.bookchat.bookchat.infrastructure.push.service;
package toy.bookchat.bookchat.infrastructure.fcm.service;

import static com.google.firebase.ErrorCode.NOT_FOUND;

Expand All @@ -16,46 +16,46 @@
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import toy.bookchat.bookchat.exception.serviceunavailable.push.PushServiceCallException;
import toy.bookchat.bookchat.infrastructure.push.PushMessageBody;
import toy.bookchat.bookchat.infrastructure.fcm.PushMessageBody;

@Service
public class FcmService implements PushService {

private final String BOOK_CHAT = "Book Chat";
private final FirebaseMessaging firebaseMessaging;
private final ObjectMapper objectMapper;

public FcmService(FirebaseMessaging firebaseMessaging, ObjectMapper objectMapper) {
this.firebaseMessaging = firebaseMessaging;
this.objectMapper = objectMapper;
}

@Async
@Retryable(value = PushServiceCallException.class, maxAttempts = 5, backoff = @Backoff(delay = 2000L, multiplier = 2.0))
@Override
public void send(String fcmToken, PushMessageBody messageBody) {
try {
Map<String, String> data = new HashMap<>();
data.put("title", BOOK_CHAT);
data.put("body", objectMapper.writeValueAsString(messageBody));

Message message = Message.builder()
.setToken(fcmToken)
.setAndroidConfig(AndroidConfig.builder()
.setDirectBootOk(true)
.setPriority(Priority.HIGH)
.build())
.putAllData(data)
.build();

firebaseMessaging.send(message);
} catch (FirebaseMessagingException e) {
if (e.getErrorCode() == NOT_FOUND) {
return;
}
throw new PushServiceCallException();
} catch (JsonProcessingException e) {
throw new IllegalArgumentException(e);
}
private final String BOOK_CHAT = "Book Chat";
private final FirebaseMessaging firebaseMessaging;
private final ObjectMapper objectMapper;

public FcmService(FirebaseMessaging firebaseMessaging, ObjectMapper objectMapper) {
this.firebaseMessaging = firebaseMessaging;
this.objectMapper = objectMapper;
}

@Async
@Retryable(value = PushServiceCallException.class, maxAttempts = 5, backoff = @Backoff(delay = 2000L, multiplier = 2.0))
@Override
public void send(String fcmToken, PushMessageBody messageBody) {
try {
Map<String, String> data = new HashMap<>();
data.put("title", BOOK_CHAT);
data.put("body", objectMapper.writeValueAsString(messageBody));

Message message = Message.builder()
.setToken(fcmToken)
.setAndroidConfig(AndroidConfig.builder()
.setDirectBootOk(true)
.setPriority(Priority.HIGH)
.build())
.putAllData(data)
.build();

firebaseMessaging.send(message);
} catch (FirebaseMessagingException e) {
if (e.getErrorCode() == NOT_FOUND) {
return;
}
throw new PushServiceCallException();
} catch (JsonProcessingException e) {
throw new IllegalArgumentException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package toy.bookchat.bookchat.infrastructure.push.service;
package toy.bookchat.bookchat.infrastructure.fcm.service;

import toy.bookchat.bookchat.infrastructure.push.PushMessageBody;
import toy.bookchat.bookchat.infrastructure.fcm.PushMessageBody;

public interface PushService {

void send(String fcmToken, PushMessageBody pushMessageBody);
void send(String fcmToken, PushMessageBody pushMessageBody);
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package toy.bookchat.bookchat.infrastructure.broker;
package toy.bookchat.bookchat.infrastructure.rabbitmq;

import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;
import toy.bookchat.bookchat.infrastructure.broker.message.CommonMessage;
import toy.bookchat.bookchat.infrastructure.broker.message.NotificationMessage;
import toy.bookchat.bookchat.infrastructure.rabbitmq.message.CommonMessage;
import toy.bookchat.bookchat.infrastructure.rabbitmq.message.NotificationMessage;

@Component
public class MessagePublisher {

private final String DESTINATION_PREFIX = "/topic/";
private final String DESTINATION_PREFIX = "/topic/";

private final SimpMessagingTemplate messagingTemplate;
private final SimpMessagingTemplate messagingTemplate;

public MessagePublisher(SimpMessagingTemplate messagingTemplate) {
this.messagingTemplate = messagingTemplate;
}
public MessagePublisher(SimpMessagingTemplate messagingTemplate) {
this.messagingTemplate = messagingTemplate;
}

public void sendCommonMessage(String roomSid, CommonMessage commonMessage) {
messagingTemplate.convertAndSend(DESTINATION_PREFIX + roomSid, commonMessage);
}
public void sendCommonMessage(String roomSid, CommonMessage commonMessage) {
messagingTemplate.convertAndSend(DESTINATION_PREFIX + roomSid, commonMessage);
}

public void sendNotificationMessage(String roomSid, NotificationMessage notificationMessage) {
messagingTemplate.convertAndSend(DESTINATION_PREFIX + roomSid, notificationMessage);
}
public void sendNotificationMessage(String roomSid, NotificationMessage notificationMessage) {
messagingTemplate.convertAndSend(DESTINATION_PREFIX + roomSid, notificationMessage);
}
}
Loading

0 comments on commit c23b596

Please sign in to comment.