Skip to content

Commit

Permalink
#316 [feat] presentation 계층과 service 계층 DTO 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
sohyundoh committed Aug 20, 2024
1 parent 0ce2f3d commit f2760fb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.asap.server.presentation.controller.dto.response.UserTimeResponseDto;
import com.asap.server.presentation.controller.time.docs.TimeRegisterControllerDocs;
import com.asap.server.service.UserService;
import com.asap.server.service.dto.UserMeetingScheduleRegisterDto;
import com.asap.server.service.dto.UserTimeRegisterDto;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.List;
Expand All @@ -34,7 +36,7 @@ public SuccessResponse<UserMeetingTimeResponseDto> createHostTime(
) {
return SuccessResponse.success(
Success.CREATE_HOST_TIME_SUCCESS,
userService.createHostTime(meetingId, userId, requestDtoList)
userService.createHostTime(meetingId, userId, UserMeetingScheduleRegisterDto.of(requestDtoList))
);
}

Expand All @@ -46,7 +48,7 @@ public SuccessResponse<UserTimeResponseDto> createMemberTime(
) {
return SuccessResponse.success(
Success.CREATE_MEETING_TIME_SUCCESS,
userService.createUserTime(meetingId, requestDto)
userService.createUserTime(meetingId, UserTimeRegisterDto.of(requestDto))
);
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package com.asap.server.service;

import com.asap.server.common.utils.DateUtil;
import com.asap.server.persistence.domain.UserMeetingSchedule;
import com.asap.server.persistence.domain.time.UserMeetingSchedule;
import com.asap.server.persistence.repository.UserMeetingScheduleRepository;
import com.asap.server.presentation.controller.dto.request.UserMeetingTimeSaveRequestDto;
import lombok.AccessLevel;
import com.asap.server.service.dto.UserMeetingScheduleRegisterDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@RequiredArgsConstructor
public class UserMeetingScheduleService {
private final UserMeetingScheduleRepository userMeetingScheduleRepository;

@Transactional
public void createUserMeetingSchedule(final Long userId, final Long meetingId, final UserMeetingTimeSaveRequestDto userMeetingTimeSaveRequestDto) {
public void createUserMeetingSchedule(final Long userId, final Long meetingId, final UserMeetingScheduleRegisterDto registerDto) {
UserMeetingSchedule userMeetingSchedule = UserMeetingSchedule.builder()
.userId(userId).meetingId(meetingId)
.availableDate(DateUtil.transformLocalDate(userMeetingTimeSaveRequestDto.month(), userMeetingTimeSaveRequestDto.day()))
.startTimeSlot(userMeetingTimeSaveRequestDto.startTime())
.endTimeSlot(userMeetingTimeSaveRequestDto.endTime())
.userId(userId)
.meetingId(meetingId)
.availableDate(DateUtil.transformLocalDate(registerDto.month(), registerDto.day()))
.startTimeSlot(registerDto.startTime())
.endTimeSlot(registerDto.endTime())
.build();
userMeetingScheduleRepository.save(userMeetingSchedule);
}
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/asap/server/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
import com.asap.server.persistence.domain.enums.TimeSlot;
import com.asap.server.persistence.repository.meeting.MeetingRepository;
import com.asap.server.persistence.repository.user.UserRepository;
import com.asap.server.presentation.controller.dto.request.AvailableTimeRequestDto;
import com.asap.server.presentation.controller.dto.request.UserMeetingTimeSaveRequestDto;
import com.asap.server.presentation.controller.dto.request.UserRequestDto;
import com.asap.server.presentation.controller.dto.response.UserMeetingTimeResponseDto;
import com.asap.server.presentation.controller.dto.response.UserTimeResponseDto;
import com.asap.server.service.dto.UserMeetingScheduleRegisterDto;
import com.asap.server.service.dto.UserTimeRegisterDto;
import com.asap.server.service.vo.BestMeetingTimeVo;
import com.asap.server.service.vo.BestMeetingTimeWithUsersVo;
import com.asap.server.service.vo.UserVo;
Expand Down Expand Up @@ -61,7 +61,7 @@ public User createUser(final Meeting meeting,
@Transactional
public UserMeetingTimeResponseDto createHostTime(final Long meetingId,
final Long userId,
final List<UserMeetingTimeSaveRequestDto> requestDtos) {
final List<UserMeetingScheduleRegisterDto> requestDtos) {
Meeting meeting = meetingRepository.findById(meetingId)
.orElseThrow(() -> new NotFoundException(Error.MEETING_NOT_FOUND_EXCEPTION));
if (!meeting.authenticateHost(userId))
Expand All @@ -82,13 +82,13 @@ public UserMeetingTimeResponseDto createHostTime(final Long meetingId,

@Transactional
public UserTimeResponseDto createUserTime(final Long meetingId,
final AvailableTimeRequestDto requestDto) {
final UserTimeRegisterDto registerDto) {
Meeting meeting = meetingRepository.findById(meetingId)
.orElseThrow(() -> new NotFoundException(Error.MEETING_NOT_FOUND_EXCEPTION));

User user = createUser(meeting, new Name(requestDto.getName()), Role.MEMBER);
isDuplicatedDate(requestDto.getAvailableTimes());
requestDto.getAvailableTimes().forEach(availableTime -> createUserTimeBlock(meeting, user, availableTime));
User user = createUser(meeting, new Name(registerDto.name()), Role.MEMBER);
isDuplicatedDate(registerDto.availableSchedules());
registerDto.availableSchedules().forEach(availableTime -> createUserTimeBlock(meeting, user, availableTime));
return UserTimeResponseDto.builder()
.role(Role.MEMBER.getRole())
.build();
Expand All @@ -106,18 +106,18 @@ public List<String> findUserNameByMeeting(final Meeting meeting) {

private void createUserTimeBlock(final Meeting meeting,
final User user,
final UserMeetingTimeSaveRequestDto requestDto) {
AvailableDate availableDate = availableDateService.findByMeetingAndDate(meeting, requestDto.month(), requestDto.day());
TimeSlot.getTimeSlots(requestDto.startTime().ordinal(), requestDto.endTime().ordinal() - 1)
final UserMeetingScheduleRegisterDto registerDto) {
AvailableDate availableDate = availableDateService.findByMeetingAndDate(meeting, registerDto.month(), registerDto.day());
TimeSlot.getTimeSlots(registerDto.startTime().ordinal(), registerDto.endTime().ordinal() - 1)
.stream()
.map(timeSlot -> timeBlockService.searchTimeBlock(timeSlot, availableDate, requestDto.priority())).collect(Collectors.toList())
.map(timeSlot -> timeBlockService.searchTimeBlock(timeSlot, availableDate, registerDto.priority())).toList()
.forEach(timeBlock -> timeBlock.addTimeBlockUsers(timeBlockUserService.create(timeBlock, user)));
userMeetingScheduleService.createUserMeetingSchedule(meeting.getId(), user.getId(), requestDto);
userMeetingScheduleService.createUserMeetingSchedule(meeting.getId(), user.getId(), registerDto);
}

private void isDuplicatedDate(final List<UserMeetingTimeSaveRequestDto> requestDtoList) {
private void isDuplicatedDate(final List<UserMeetingScheduleRegisterDto> registerDto) {
Map<String, List<TimeSlot>> meetingTimeAvailable = new HashMap<>();
for (UserMeetingTimeSaveRequestDto requestDto : requestDtoList) {
for (UserMeetingScheduleRegisterDto requestDto : registerDto) {
String col = String.format("%s %s", requestDto.month(), requestDto.day());
List<TimeSlot> timeSlots = TimeSlot.getTimeSlots(requestDto.startTime().ordinal(), requestDto.endTime().ordinal() - 1);
if (meetingTimeAvailable.containsKey(col)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.asap.server.service.dto;

import com.asap.server.persistence.domain.enums.TimeSlot;
import com.asap.server.presentation.controller.dto.request.UserMeetingTimeSaveRequestDto;

import java.util.List;

public record UserMeetingScheduleRegisterDto(
String month,
String day,
TimeSlot startTime,
TimeSlot endTime,
int priority
) {

public static List<UserMeetingScheduleRegisterDto> of(final List<UserMeetingTimeSaveRequestDto> targets) {
return targets.stream().map(
t -> new UserMeetingScheduleRegisterDto(
t.month(),
t.day(),
t.startTime(),
t.endTime(),
t.priority()
)).toList();
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/asap/server/service/dto/UserTimeRegisterDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.asap.server.service.dto;

import com.asap.server.presentation.controller.dto.request.AvailableTimeRequestDto;

import java.util.List;

public record UserTimeRegisterDto(
String name,
List<UserMeetingScheduleRegisterDto> availableSchedules
) {
public static UserTimeRegisterDto of(final AvailableTimeRequestDto target) {
return new UserTimeRegisterDto(target.getName(), UserMeetingScheduleRegisterDto.of(target.getAvailableTimes()));
}
}

0 comments on commit f2760fb

Please sign in to comment.