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

프로덕션 코드 리팩토링 #206

Merged
merged 18 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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 @@ -13,18 +13,21 @@
@RequiredArgsConstructor
public class AdminQuery {

private static final String ID_VALIDATION_ERROR_MESSAGE = "유효하지 않은 관리자 아이디(%s)입니다.";
private static final String PASSWORD_VALIDATION_ERROR_MESSAGE = "유효하지 않은 관리자 비밀번호(%s)입니다.";

private final AdminRepository adminRepository;

public void checkIdAndPassword(String id, String password) {
Admin admin = adminRepository.findById(id)
.orElseThrow(() -> new NotFoundException(VALIDATION_ERROR,
String.format("유효하지 않은 관리자 아이디(%s)입니다.", id)));
String.format(ID_VALIDATION_ERROR_MESSAGE, id)));

if (admin.isCorrectPassword(password)) {
return;
}
throw new NotFoundException(VALIDATION_ERROR,
String.format("유효하지 않은 관리자 비밀번호(%s)입니다.", password));
String.format(PASSWORD_VALIDATION_ERROR_MESSAGE, password));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@
import coffeemeet.server.admin.presentation.dto.AdminCustomPage;
import coffeemeet.server.admin.presentation.dto.AdminCustomSlice;
import coffeemeet.server.admin.presentation.dto.AdminLoginHTTP;
import coffeemeet.server.admin.presentation.dto.FindGroupReportsParam;
import coffeemeet.server.admin.presentation.dto.GroupReportHTTP;
import coffeemeet.server.admin.presentation.dto.ReportDeletionHTTP;
import coffeemeet.server.admin.presentation.dto.ReportDetailHTTP;
import coffeemeet.server.admin.presentation.dto.UserPunishmentHTTP;
import coffeemeet.server.admin.service.AdminService;
import coffeemeet.server.certification.service.CertificationService;
import coffeemeet.server.certification.service.dto.PendingCertification;
import coffeemeet.server.certification.service.dto.PendingCertificationDto;
import coffeemeet.server.certification.service.dto.PendingCertificationPageDto;
import coffeemeet.server.common.annotation.PerformanceMeasurement;
import coffeemeet.server.inquiry.presentation.dto.InquiryDetailHTTP;
import coffeemeet.server.inquiry.service.InquiryService;
import coffeemeet.server.inquiry.service.dto.InquiryDetailDto;
import coffeemeet.server.inquiry.service.dto.InquirySearchResponse;
import coffeemeet.server.inquiry.service.dto.InquirySearchResponse.InquirySummary;
import coffeemeet.server.report.presentation.dto.FindGroupReports;
import coffeemeet.server.report.presentation.dto.GroupReportHTTP;
import coffeemeet.server.report.presentation.dto.ReportDetailHTTP;
import coffeemeet.server.report.presentation.dto.ReportListHTTP;
import coffeemeet.server.report.presentation.dto.ReportListHTTP.Response;
import coffeemeet.server.inquiry.service.dto.InquirySearchDto;
import coffeemeet.server.inquiry.service.dto.InquirySummaryDto;
import coffeemeet.server.report.service.ReportService;
import coffeemeet.server.report.service.dto.GroupReportDto;
import coffeemeet.server.report.service.dto.ReportDetailDto;
import coffeemeet.server.report.service.dto.ReportDto;
import coffeemeet.server.report.service.dto.ReportListDto;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
Expand Down Expand Up @@ -128,7 +127,7 @@ public ResponseEntity<Void> dismissReport(
}

@GetMapping("/reports")
public ResponseEntity<AdminCustomSlice<ReportListHTTP.Response>> findAllReports(
public ResponseEntity<AdminCustomSlice<ReportDto>> findAllReports(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 왜 Dto 인가여 HTTP.Response

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AdminCustomSlice는 HTTP 대신 Slice로 반환하고 있습니다.

public record AdminCustomSlice<T>(
    List<T> contents,
    boolean hasNext
)

단일 dto는
Dto -> HTTP 로
복수 dto는
Dto 여러개가 -> Slice로
반환 되기 때문에 통일성을 주고자 이렇게 했습니다.

@SessionAttribute(name = ADMIN_SESSION_ATTRIBUTE, required = false) String adminId,
@RequestParam(defaultValue = "0") Long lastReportId,
@RequestParam(defaultValue = "10") int pageSize
Expand All @@ -137,22 +136,20 @@ public ResponseEntity<AdminCustomSlice<ReportListHTTP.Response>> findAllReports(
// throw new InvalidAuthException(NOT_AUTHORIZED, REQUEST_WITHOUT_SESSION_MESSAGE);
// }
ReportListDto reportListDto = reportService.findAllReports(lastReportId, pageSize);
List<ReportListHTTP.Response> responses = reportListDto.contents().stream()
.map(Response::from)
.toList();
return ResponseEntity.ok(AdminCustomSlice.of(responses, reportListDto.hasNext()));
return ResponseEntity.ok(
AdminCustomSlice.of(reportListDto.contents(), reportListDto.hasNext()));
}

@GetMapping("/reports/group")
public ResponseEntity<GroupReportHTTP.Response> findReportByTargetIdAndChattingRoomId(
@SessionAttribute(name = ADMIN_SESSION_ATTRIBUTE, required = false) String adminId,
@ModelAttribute FindGroupReports findGroupReports
@ModelAttribute FindGroupReportsParam findGroupReportsParam
) {
// if (adminId == null) {
// throw new InvalidAuthException(NOT_AUTHORIZED, REQUEST_WITHOUT_SESSION_MESSAGE);
// }
List<GroupReportDto> responses = reportService.findReportByTargetIdAndChattingRoomId(
findGroupReports.targetedId(), findGroupReports.chattingRoomId());
findGroupReportsParam.targetedId(), findGroupReportsParam.chattingRoomId());
return ResponseEntity.ok(GroupReportHTTP.Response.from(responses));
}

Expand All @@ -169,14 +166,14 @@ public ResponseEntity<ReportDetailHTTP.Response> findReport(
}

@GetMapping("/inquiries")
public ResponseEntity<AdminCustomSlice<InquirySummary>> searchInquiries(
public ResponseEntity<AdminCustomSlice<InquirySummaryDto>> searchInquiries(
@SessionAttribute(name = ADMIN_SESSION_ATTRIBUTE, required = false) String adminId,
@RequestParam(defaultValue = "0") Long lastInquiryId,
@RequestParam(defaultValue = "10") int pageSize) {
// if (adminId == null) {
// throw new InvalidAuthException(NOT_AUTHORIZED, REQUEST_WITHOUT_SESSION_MESSAGE);
// }
InquirySearchResponse inquiries = inquiryService.searchInquiries(lastInquiryId, pageSize);
InquirySearchDto inquiries = inquiryService.searchInquiries(lastInquiryId, pageSize);
return ResponseEntity.ok(AdminCustomSlice.of(inquiries.contents(), inquiries.hasNext()));
}

Expand Down Expand Up @@ -206,7 +203,7 @@ public ResponseEntity<Void> checkInquiry(
// TODO: 11/27/23 임시로 페이징(옵셋 기반) 처리, 개선 필요
@PerformanceMeasurement
@GetMapping("/certifications/pending")
public ResponseEntity<AdminCustomPage<PendingCertification>> getPendingCertifications(
public ResponseEntity<AdminCustomPage<PendingCertificationDto>> getPendingCertifications(
@SessionAttribute(name = ADMIN_SESSION_ATTRIBUTE, required = false) String adminId,
@RequestParam(defaultValue = "0") int offset,
@RequestParam(defaultValue = "10") int size
Expand All @@ -220,7 +217,7 @@ public ResponseEntity<AdminCustomPage<PendingCertification>> getPendingCertifica
PendingCertificationPageDto uncertifiedUserRequests = certificationService.getUncertifiedUserRequests(
pageable);

Page<PendingCertification> page = uncertifiedUserRequests.page();
Page<PendingCertificationDto> page = uncertifiedUserRequests.page();
return ResponseEntity.ok(
AdminCustomPage.of(page.getContent(), page.hasNext())
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package coffeemeet.server.report.presentation.dto;
package coffeemeet.server.admin.presentation.dto;

import jakarta.validation.constraints.NotNull;

public record FindGroupReports(
public record FindGroupReportsParam(
@NotNull
Long chattingRoomId,
@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package coffeemeet.server.admin.presentation.dto;

import static lombok.AccessLevel.PRIVATE;

import coffeemeet.server.report.service.dto.GroupReportDto;
import java.util.List;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = PRIVATE)
public final class GroupReportHTTP {

public record Response(List<GroupReportDto> groupReports) {

public static Response from(List<GroupReportDto> responses) {
return new Response(responses);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package coffeemeet.server.report.presentation.dto;
package coffeemeet.server.admin.presentation.dto;

import static lombok.AccessLevel.PRIVATE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class AuthTokensGenerator {

private static final String BEARER_TYPE = "Bearer ";

private final JwtTokenProvider jwtTokenProvider;
private final Long accessTokenExpireTime;
private final Long refreshTokenExpireTime;
private final JwtTokenProvider jwtTokenProvider;
private final RefreshTokenCommand refreshTokenCommand;

public AuthTokensGenerator(JwtTokenProvider jwtTokenProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import coffeemeet.server.certification.implement.CertificationQuery;
import coffeemeet.server.certification.implement.EmailVerificationCommand;
import coffeemeet.server.certification.implement.EmailVerificationQuery;
import coffeemeet.server.certification.service.dto.PendingCertification;
import coffeemeet.server.certification.service.dto.PendingCertificationDto;
import coffeemeet.server.certification.service.dto.PendingCertificationPageDto;
import coffeemeet.server.common.execption.InvalidInputException;
import coffeemeet.server.common.implement.EmailSender;
Expand Down Expand Up @@ -40,17 +40,17 @@ public class CertificationService {
private final EmailVerificationCommand emailVerificationCommand;
private final EmailVerificationQuery emailVerificationQuery;

public void registerCertification(long userId, String companyName, String email,
public void registerCertification(Long userId, String companyName, String email,
String departmentName, File businessCardImage) {
processCertification(userId, companyName, email, departmentName, businessCardImage, false);
}

public void updateCertification(long userId, String companyName, String email,
public void updateCertification(Long userId, String companyName, String email,
String departmentName, File businessCardImage) {
processCertification(userId, companyName, email, departmentName, businessCardImage, true);
}

private void processCertification(long userId, String companyName, String email,
private void processCertification(Long userId, String companyName, String email,
String departmentName, File businessCardImage, boolean isUpdate) {
String key = mediaManager.generateKey(BUSINESS_CARD);
uploadBusinessCard(userId, key, businessCardImage);
Expand All @@ -67,7 +67,7 @@ private void processCertification(long userId, String companyName, String email,
businessCardUrl);
}

private void uploadBusinessCard(long userId, String key, File businessCardUrl) {
private void uploadBusinessCard(Long userId, String key, File businessCardUrl) {
certificationCommand.applyIfCertifiedUser(userId, certification -> {
String oldKey = mediaManager.extractKey(certification.getBusinessCardUrl(), BUSINESS_CARD);
mediaManager.delete(oldKey);
Expand Down Expand Up @@ -101,9 +101,9 @@ public void compareCode(Long userId, String verificationCode) {
public PendingCertificationPageDto getUncertifiedUserRequests(Pageable pageable) {
Page<Certification> pendingCertification =
certificationQuery.getPendingCertification(pageable);
Page<PendingCertification> pendingCertificationPage = pendingCertification.map(
PendingCertification::from);
return new PendingCertificationPageDto(pendingCertificationPage);
Page<PendingCertificationDto> pendingCertificationPage = pendingCertification.map(
PendingCertificationDto::from);
return PendingCertificationPageDto.from(pendingCertificationPage);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import coffeemeet.server.certification.domain.Certification;

public record PendingCertification(
public record PendingCertificationDto(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이건 Dto을 제거하는게 맞아보는데요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이것도 위에서 커멘트에서 작성한 것과 같이 같은 의미로 통일성을 주고자 dto를 붙였습니다.

Long certificationId,
String nickname,
String companyName,
Expand All @@ -11,8 +11,8 @@ public record PendingCertification(
String department
) {

public static PendingCertification from(Certification certification) {
return new PendingCertification(
public static PendingCertificationDto from(Certification certification) {
return new PendingCertificationDto(
certification.getId(),
certification.getUser().getProfile().getNickname(),
certification.getCompanyName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import org.springframework.data.domain.Page;

public record PendingCertificationPageDto(
Copy link
Collaborator

@smartandhandsome smartandhandsome Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

얘가 Dto이기 떄문에

Page 안에 값은 Dto보단 PendingCertification인 VO로 그대로 두는게 적절해 보입니다

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전달 객체는 PendingCertificationPageDto 이고 PendingCertificationDto는 전달 객체가 아니기 때문에 PendingCertification로 하는게 적절해 보입니다

Copy link
Collaborator Author

@yumyeonghan yumyeonghan Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PendingCertificationPageDto
PendingCertificationDto
이거 둘다 직접/간접적으로 전달되는거라
서비스에서 컨트롤러로 전달되는 객체에 Dto라는 네이밍을 쓰기로했으면 통일? 하는것도 나쁘지 않아보이는데 어떻게 생각하시나요

Page<PendingCertification> page
Page<PendingCertificationDto> page
) {

public static PendingCertificationPageDto from(Page<PendingCertificationDto> page) {
return new PendingCertificationPageDto(page);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void onDisconnect(SessionDisconnectEvent event) {

@MessageMapping("/chatting/messages")
public void message(@Valid ChatStomp.Request request, SimpMessageHeaderAccessor accessor) {
ChattingDto.Response response = chattingMessageService.chatting(
ChattingDto response = chattingMessageService.chatting(
accessor.getSessionId(),
request.roomId(),
request.content()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import coffeemeet.server.chatting.current.presentation.dto.ChatsHTTP;
import coffeemeet.server.chatting.current.service.ChattingRoomService;
import coffeemeet.server.chatting.current.service.dto.ChatRoomStatusDto;
import coffeemeet.server.chatting.current.service.dto.ChattingDto.Response;
import java.util.List;
import coffeemeet.server.chatting.current.service.dto.ChattingListDto;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -27,7 +26,8 @@ public ResponseEntity<ChatsHTTP.Response> viewChattingRoomMessages(
@PathVariable Long roomId,
@RequestParam(defaultValue = "0") Long firstMessageId,
@RequestParam(defaultValue = "50") int pageSize) {
List<Response> responses = chattingRoomService.searchMessages(roomId, firstMessageId, pageSize);
ChattingListDto responses = chattingRoomService.searchMessages(roomId, firstMessageId,
pageSize);
return ResponseEntity.ok(ChatsHTTP.Response.from(responses));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public record Response(
LocalDateTime createdAt
) {

public static ChatStomp.Response from(ChattingDto.Response response) {
public static ChatStomp.Response from(ChattingDto response) {
return new ChatStomp.Response(
response.userId(),
response.messageId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,20 @@
import static lombok.AccessLevel.PRIVATE;

import coffeemeet.server.chatting.current.service.dto.ChattingDto;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;
import coffeemeet.server.chatting.current.service.dto.ChattingListDto;
import java.util.List;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = PRIVATE)
public final class ChatsHTTP {

public record Chat(
Long userId,
Long messageId,
String nickname,
String content,
String profileImageUrl,
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
LocalDateTime createdAt
) {

public static Chat from(ChattingDto.Response response) {
return new Chat(
response.userId(),
response.messageId(),
response.nickname(),
response.content(),
response.profileImageUrl(),
response.createdAt()
);
}

}

public record Response(
List<Chat> chats
List<ChattingDto> chats,
boolean hasNext
) {

public static Response from(List<ChattingDto.Response> responses) {
List<Chat> chatList = responses.stream()
.map(Chat::from)
.toList();
return new Response(chatList);
public static Response from(ChattingListDto chattingListDto) {
return new Response(chattingListDto.contents(), chattingListDto.hasNext());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public class ChattingMessageService {
private final UserCommand userCommand;
private final FCMNotificationSender fcmNotificationSender;

public ChattingDto.Response chatting(String sessionId, Long roomId, String content) {
public ChattingDto chatting(String sessionId, Long roomId, String content) {
Long userId = chattingSessionQuery.getUserIdById(sessionId);
ChattingRoom room = chattingRoomQuery.getChattingRoomById(roomId);
List<User> users = userQuery.getUsersByRoom(room);
User user = userQuery.getUserById(userId);
sendChattingAlarm(user.getProfile().getNickname(), content, users);
ChattingMessage chattingMessage = chattingMessageCommand.createChattingMessage(content,
room, user);
return ChattingDto.Response.of(user, chattingMessage);
return ChattingDto.of(user, chattingMessage);
}

private void sendChattingAlarm(String chattingUserNickname, String content, List<User> users) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import coffeemeet.server.chatting.current.implement.ChattingRoomQuery;
import coffeemeet.server.chatting.current.service.dto.ChatRoomStatusDto;
import coffeemeet.server.chatting.current.service.dto.ChattingDto;
import coffeemeet.server.chatting.current.service.dto.ChattingDto.Response;
import coffeemeet.server.chatting.current.service.dto.ChattingListDto;
import coffeemeet.server.chatting.history.domain.ChattingMessageHistory;
import coffeemeet.server.chatting.history.domain.ChattingRoomHistory;
import coffeemeet.server.chatting.history.domain.UserChattingHistory;
Expand Down Expand Up @@ -45,10 +45,15 @@ public Long createChattingRoom() {
}

@Transactional
public List<Response> searchMessages(Long roomId, Long firstMessageId, int pageSize) {
public ChattingListDto searchMessages(Long roomId, Long firstMessageId, int pageSize) {
ChattingRoom chattingRoom = chattingRoomQuery.getChattingRoomById(roomId);
return chattingMessageQuery.findMessages(chattingRoom, firstMessageId, pageSize).stream()
.map(message -> ChattingDto.Response.of(message.getUser(), message)).toList();
List<ChattingMessage> chattingMessages = chattingMessageQuery.findMessages(chattingRoom,
firstMessageId,
pageSize);
boolean hasNext = chattingMessages.size() >= pageSize;
List<ChattingDto> chattingDtoList = chattingMessages.stream()
.map(message -> ChattingDto.of(message.getUser(), message)).toList();
return ChattingListDto.of(chattingDtoList, hasNext);
}

@Transactional
Expand Down
Loading