Skip to content

Commit

Permalink
[nexters/www-be#noissue] code refactor (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdcks12 authored Feb 16, 2023
1 parent 8d26f63 commit a68a552
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.promise8.wwwbe.config.security;

import com.promise8.wwwbe.model.entity.UserEntity;
import com.promise8.wwwbe.model.exception.BizException;
import com.promise8.wwwbe.model.http.BaseErrorCode;
import com.promise8.wwwbe.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
Expand All @@ -16,7 +18,7 @@ public class CustomUserDetailsService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String deviceId) throws UsernameNotFoundException {
UserEntity user = userRepository.findByDeviceId(deviceId)
.orElseThrow(() -> new UsernameNotFoundException("User not found with deviceId : " + deviceId));
.orElseThrow(() -> new BizException(BaseErrorCode.NOT_EXIST_USER));

return UserPrincipal.create(user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public enum BaseErrorCode {
FCM_SEND_ERROR(2000, "FCM Push Error"),
ALREADY_PARTICIPATED_MEETING(3001, "Already Participated Meeting"),
NOT_EXIST_MEETING(4000, "Not Exist Meeting"),
NOT_EXIST_USER(4001, "Not Exist User"),
NOT_EXIST_MEETING_USER(4002, "Not Exist Meeting User"),
ALREADY_VOTING_MEETING(5000, "Already Voting Meeting"),
INVALID_REQUEST(9000, "Invalid Request");

Expand Down
14 changes: 5 additions & 9 deletions src/main/java/com/promise8/wwwbe/model/mobile/PushMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
import lombok.AllArgsConstructor;
import lombok.Data;

import java.time.LocalDateTime;

@Data
@AllArgsConstructor
public class PushMessage {
private PushType pushType;
private String body;
private LocalDateTime createdAt;
private ContentType contentType;
private Long contentId;
private String text;

public enum PushType {
DEFAULT,
NOTI,
EVENT
public enum ContentType {
MEETING;
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/promise8/wwwbe/service/MeetingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public MeetingCreateResDto createMeeting(MeetingCreateReqDto meetingCreateReqDto
@Transactional
public void putMeetingStatus(long meetingId, MeetingStatus meetingStatus) {
MeetingEntity meetingEntity = meetingRepository.findById(meetingId).orElseThrow(() -> {
throw new BizException(BaseErrorCode.INVALID_REQUEST, "not exist meeting");
throw new BizException(BaseErrorCode.NOT_EXIST_MEETING, "not exist meeting");
});

if (MeetingStatus.WAITING.equals(meetingStatus) || MeetingStatus.DONE.equals(meetingStatus)) {
Expand Down Expand Up @@ -172,9 +172,9 @@ public Long joinMeetingAndGetMeetingUserId(long userId, long meetingId, JoinMeet
throw new BizException(BaseErrorCode.ALREADY_PARTICIPATED_MEETING);
}
MeetingEntity meetingEntity =
meetingRepository.findById(meetingId).orElseThrow(() -> new BizException(BaseErrorCode.INVALID_REQUEST, "not exist meeting"));
meetingRepository.findById(meetingId).orElseThrow(() -> new BizException(BaseErrorCode.NOT_EXIST_MEETING));
UserEntity userEntity =
userRepository.findById(userId).orElseThrow(() -> new BizException(BaseErrorCode.INVALID_REQUEST, "not exist user"));
userRepository.findById(userId).orElseThrow(() -> new BizException(BaseErrorCode.NOT_EXIST_USER));


MeetingUserEntity meetingUserEntity = meetingUserRepository.save(MeetingUserEntity.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,14 @@ public void vote(long meetingId, long userId, PlaceVoteReqDto placeVoteReqDto) {
}

private MeetingUserEntity getMeetingUserEntity(MeetingEntity meetingEntity, UserEntity userEntity) {
MeetingUserEntity meetingUserEntity = meetingUserRepository.findByMeetingEntityAndUserEntity(meetingEntity,
userEntity).orElseThrow(() -> {
throw new BizException(BaseErrorCode.INVALID_REQUEST, "not exist meeting user");
MeetingUserEntity meetingUserEntity = meetingUserRepository.findByMeetingEntityAndUserEntity(meetingEntity, userEntity).orElseThrow(() -> {
throw new BizException(BaseErrorCode.NOT_EXIST_MEETING_USER);
});
return meetingUserEntity;
}

private UserEntity getUserEntity(long userId) {
UserEntity userEntity = userRepository.findById(userId).orElseThrow(() -> {
throw new BizException(BaseErrorCode.INVALID_REQUEST, "not exist user");
});
UserEntity userEntity = userRepository.findById(userId).orElseThrow(() -> new BizException(BaseErrorCode.NOT_EXIST_USER));
return userEntity;
}

Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/promise8/wwwbe/service/PushService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ public class PushService {
private final FirebaseMessaging fcm;
private final ObjectMapper objectMapper;

/**
* TODO: user join시에 token 정보 받아야 할 듯(deviceId와 다른 값으로 보임)
* TODO: 통합 테스트 필요
*
* @param token device 별로 고유한 토큰 (즉, 유저)
* @param body 보낼 메세지 body
* @return
*/
public String send(String token, String body) {
return send(token, new PushMessage(PushMessage.PushType.DEFAULT, body, LocalDateTime.now()));
}

public String send(String token, PushMessage pushMessage) {
try {
String jsonStr = objectMapper.writeValueAsString(pushMessage);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/promise8/wwwbe/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.promise8.wwwbe.service;

import com.promise8.wwwbe.model.entity.UserEntity;
import com.promise8.wwwbe.model.exception.BizException;
import com.promise8.wwwbe.model.http.BaseErrorCode;
import com.promise8.wwwbe.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -14,8 +15,7 @@ public class UserService {

@Transactional
public void setAlarm(long userId, boolean isAlarmOn) {
UserEntity userEntity = userRepository.findById(userId)
.orElseThrow(() -> new UsernameNotFoundException("User not found with userId : " + userId));
UserEntity userEntity = userRepository.findById(userId).orElseThrow(() -> new BizException(BaseErrorCode.NOT_EXIST_USER));
userEntity.setIsAlarmOn(isAlarmOn);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.promise8.wwwbe.service;

import com.promise8.wwwbe.model.mobile.PushMessage;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -17,6 +18,6 @@ class IntegrationPushServiceTest {
@Disabled
void testSend() throws Exception {
String token = "cORc8O22SDmqneB0GJxlOE:APA91bHHd0pyh0ogt65JDrOZI8HNyc08UXeL93wOTs1PbDeAzz2vTo-md8JevYwZEHsPJ5p3qqt21LooGZ5PMmzh06AVh1hEavTojNwHZ9OTKVu23L_p1QCmHaLd4qSQebeHqsV-2Opy";
pushService.send(token, "testYo");
pushService.send(token, new PushMessage(PushMessage.ContentType.MEETING, 2L, "test"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.Message;
import com.promise8.wwwbe.model.mobile.PushMessage;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -39,7 +40,7 @@ void testSend() throws Exception {
when(fcm.send(any(Message.class))).thenReturn("1");

// when
String id = pushService.send("token", "testBody");
String id = pushService.send("token", new PushMessage(PushMessage.ContentType.MEETING, 1L, "testBody"));

// then
Assertions.assertEquals("1", id);
Expand Down

0 comments on commit a68a552

Please sign in to comment.