Skip to content

Commit

Permalink
Merge pull request #324 from ASAP-as-soon-as-possible/feat/#317
Browse files Browse the repository at this point in the history
#317 [feat] 엔티티 변경으로 인한 종합 일정 시간표 로직 리팩토링
  • Loading branch information
sohyundoh authored Aug 24, 2024
2 parents ccc4e7c + 2402bcb commit 5e5f6c7
Show file tree
Hide file tree
Showing 17 changed files with 267 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.asap.server.presentation.controller.dto.request;

import com.asap.server.service.time.dto.UserTimeRegisterDto;
import com.asap.server.service.time.dto.register.UserTimeRegisterDto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.asap.server.presentation.controller.dto.request;

import com.asap.server.persistence.domain.enums.TimeSlot;
import com.asap.server.service.time.dto.UserMeetingScheduleRegisterDto;
import com.asap.server.service.time.dto.register.UserMeetingScheduleRegisterDto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package com.asap.server.presentation.controller.dto.response;

import lombok.AllArgsConstructor;
import com.asap.server.service.time.dto.retrieve.AvailableDatesRetrieveDto;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;

import java.util.List;

@Getter
@ToString
@AllArgsConstructor
@Builder
public class AvailableDatesDto {
private String month;
private String day;
private String dayOfWeek;
private List<TimeSlotDto> timeSlots;
public record AvailableDatesDto(
String month,
String day,
String dayOfWeek,
List<TimeSlotDto> timeSlots
) {
public static List<AvailableDatesDto> of(final List<AvailableDatesRetrieveDto> retrieveDtos) {
return retrieveDtos.stream().map(
dto -> new AvailableDatesDto(
dto.month(),
dto.day(),
dto.dayOfWeek(),
TimeSlotDto.of(dto.timeSlots())
)).toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
package com.asap.server.presentation.controller.dto.response;

import lombok.AllArgsConstructor;
import com.asap.server.service.time.dto.retrieve.TimeBlockRetrieveDto;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;

import java.util.List;
@Getter
@ToString
@AllArgsConstructor
@Builder
public class TimeSlotDto {
private String time;
private List<String> userNames;
private int colorLevel;

public void setColorLevel(final int memberCount) {
double ratio = (double) userNames.size() / memberCount;

if (ratio <= 0.2) {
this.colorLevel = 1;
} else if (ratio <= 0.4) {
this.colorLevel = 2;
} else if (ratio <= 0.6) {
this.colorLevel = 3;
} else if (ratio <= 0.8) {
this.colorLevel = 4;
} else if (ratio <= 1.0) {
this.colorLevel = 5;
} else {
this.colorLevel = 0;
}
@Builder
public record TimeSlotDto (
String time,
List<String> userNames,
int colorLevel
) {
public static List<TimeSlotDto> of(List<TimeBlockRetrieveDto> retrieveDtos) {
return retrieveDtos.stream().map(
dto -> new TimeSlotDto(dto.time(), dto.userNames(), dto.colorLevel())
).toList();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.asap.server.presentation.controller.dto.response;

import lombok.AllArgsConstructor;
import com.asap.server.service.time.dto.retrieve.TimeTableRetrieveDto;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;

import java.util.List;
@Getter

@Builder
@ToString
@AllArgsConstructor
public class TimeTableResponseDto {
public int memberCount;
public List<String> totalUserNames;
private List<AvailableDatesDto> availableDateTimes;
public record TimeTableResponseDto(
int memberCount,
List<String> totalUserNames,
List<AvailableDatesDto> availableDateTimes
) {
public static TimeTableResponseDto of(final TimeTableRetrieveDto retrieveDto) {
return new TimeTableResponseDto(
retrieveDto.memberCount(),
retrieveDto.totalUserNames(),
AvailableDatesDto.of(retrieveDto.availableDateTimes()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public SuccessResponse<TimeTableResponseDto> getTimeTable(
@MeetingPathVariable final Long meetingId,
@UserId final Long userId
) {
return SuccessResponse.success(Success.FIND_TIME_TABLE_SUCCESS, meetingService.getTimeTable(userId, meetingId));
return SuccessResponse.success(
Success.FIND_TIME_TABLE_SUCCESS,
TimeTableResponseDto.of(meetingRetrieveService.getTimeTable(userId, meetingId))
);
}

@GetMapping("/{meetingId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public TimeSlotDto getTimeSlotDto(final TimeBlock timeBlock, final int memberCou
.time(timeBlock.getTimeSlot().getTime())
.userNames(findUsersByTimeBlock(timeBlock).stream().map(User::getName).collect(Collectors.toList()))
.build();
timeSlotDto.setColorLevel(memberCount);
return timeSlotDto;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/asap/server/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import com.asap.server.presentation.controller.dto.response.UserMeetingTimeResponseDto;
import com.asap.server.presentation.controller.dto.response.UserTimeResponseDto;
import com.asap.server.service.time.UserMeetingScheduleService;
import com.asap.server.service.time.dto.UserMeetingScheduleRegisterDto;
import com.asap.server.service.time.dto.UserTimeRegisterDto;
import com.asap.server.service.time.dto.register.UserMeetingScheduleRegisterDto;
import com.asap.server.service.time.dto.register.UserTimeRegisterDto;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.asap.server.service.meeting;

import static com.asap.server.common.exception.Error.MEETING_VALIDATION_FAILED_EXCEPTION;

import com.asap.server.common.exception.Error;
import com.asap.server.common.exception.model.ConflictException;
import com.asap.server.common.exception.model.NotFoundException;
Expand All @@ -13,14 +11,24 @@
import com.asap.server.service.meeting.dto.UserDto;
import com.asap.server.service.time.MeetingTimeRecommendService;
import com.asap.server.service.time.UserMeetingScheduleService;
import com.asap.server.service.time.dto.retrieve.AvailableDatesRetrieveDto;
import com.asap.server.service.time.dto.retrieve.TimeBlockRetrieveDto;
import com.asap.server.service.time.dto.retrieve.TimeTableRetrieveDto;
import com.asap.server.service.time.vo.BestMeetingTimeVo;
import com.asap.server.service.time.vo.BestMeetingTimeWithUsers;
import com.asap.server.service.time.vo.TimeBlockVo;
import com.asap.server.service.user.UserRetrieveService;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.asap.server.common.exception.Error.MEETING_VALIDATION_FAILED_EXCEPTION;
import static java.util.function.Predicate.isEqual;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -81,4 +89,72 @@ private BestMeetingTimeWithUsers mapToBestMeetingTimeWithUsers(
userDtos
);
}

@Transactional(readOnly = true)
public TimeTableRetrieveDto getTimeTable(final Long userId, final Long meetingId) {
Meeting meeting = meetingRepository.findById(meetingId)
.orElseThrow(() -> new NotFoundException(Error.MEETING_NOT_FOUND_EXCEPTION));

if (!meeting.authenticateHost(userId)) {
throw new UnauthorizedException(Error.INVALID_MEETING_HOST_EXCEPTION);
}
if (meeting.isConfirmedMeeting()) {
throw new ConflictException(MEETING_VALIDATION_FAILED_EXCEPTION);
}
Map<Long, User> userIdToUserMap = userRetrieveService.getUserIdToUserMap(meetingId);

List<String> userNames = userIdToUserMap.keySet().stream()
.sorted()
.map(id -> userIdToUserMap.get(id).getName())
.toList();

return TimeTableRetrieveDto.of(userNames, getAvailableDatesDto(meetingId, userNames.size(), userIdToUserMap));

}

private List<AvailableDatesRetrieveDto> getAvailableDatesDto(final Long meetingId, final int totalUserCount, final Map<Long, User> userIdToUserMap) {
List<TimeBlockVo> timeBlockVos = userMeetingScheduleService.getTimeBlocks(meetingId);
Map<LocalDate, List<TimeBlockRetrieveDto>> timeSlotDtoMappedByDate = getTimeTableMapFromTimeBlockVo(timeBlockVos, totalUserCount, userIdToUserMap);
return timeSlotDtoMappedByDate.keySet().stream().map(
date -> AvailableDatesRetrieveDto.of(
date,
timeSlotDtoMappedByDate.get(date)
)
).toList();
}

private Map<LocalDate, List<TimeBlockRetrieveDto>> getTimeTableMapFromTimeBlockVo(final List<TimeBlockVo> timeBlockVo, final int totalUserCount, final Map<Long, User> userIdToUserMap) {
return timeBlockVo.stream()
.collect(Collectors.groupingBy(
TimeBlockVo::availableDate,
Collectors.mapping(t -> new TimeBlockRetrieveDto(
t.timeSlot().getTime(),
t.userIds().stream()
.filter(userIdToUserMap::containsKey)
.map(id -> userIdToUserMap.get(id).getName())
.toList(),
setColorLevel(totalUserCount, t.userIds().size())
),
Collectors.toList()
)
));
}

private int setColorLevel(final int memberCount, final int availableUserCount) {
double ratio = (double) availableUserCount / memberCount;

if (ratio <= 0.2) {
return 1;
} else if (ratio <= 0.4) {
return 2;
} else if (ratio <= 0.6) {
return 3;
} else if (ratio <= 0.8) {
return 4;
} else if (ratio <= 1.0) {
return 5;
} else {
return 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
import com.asap.server.persistence.domain.enums.TimeSlot;
import com.asap.server.persistence.domain.time.UserMeetingSchedule;
import com.asap.server.persistence.repository.UserMeetingScheduleRepository;
import com.asap.server.service.time.dto.UserMeetingScheduleRegisterDto;
import com.asap.server.service.time.dto.register.UserMeetingScheduleRegisterDto;
import com.asap.server.service.time.vo.TimeBlockVo;
import com.asap.server.service.time.vo.UserScheduleByTimeSlotVo;
import com.asap.server.service.time.vo.UserScheduleByTimeSlotVo.CompositeKey;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand All @@ -33,6 +34,7 @@ public void createUserMeetingSchedule(
.availableDate(DateUtil.transformLocalDate(registerDto.month(), registerDto.day()))
.startTimeSlot(registerDto.startTime())
.endTimeSlot(registerDto.endTime())
.weight(registerDto.priority())
.build();
userMeetingScheduleRepository.save(userMeetingSchedule);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.asap.server.service.time.dto;
package com.asap.server.service.time.dto.register;

import com.asap.server.persistence.domain.enums.TimeSlot;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.asap.server.service.time.dto;
package com.asap.server.service.time.dto.register;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.asap.server.service.time.dto.retrieve;

import com.asap.server.common.utils.DateUtil;

import java.time.LocalDate;
import java.util.List;

public record AvailableDatesRetrieveDto(
String month,
String day,
String dayOfWeek,
List<TimeBlockRetrieveDto> timeSlots
) {
public static AvailableDatesRetrieveDto of(final LocalDate date, final List<TimeBlockRetrieveDto> timeSlots) {
return new AvailableDatesRetrieveDto(
DateUtil.getMonth(date),
DateUtil.getDay(date),
DateUtil.getDayOfWeek(date),
timeSlots);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.asap.server.service.time.dto.retrieve;

import java.util.List;

public record TimeBlockRetrieveDto(
String time,
List<String> userNames,
int colorLevel
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.asap.server.service.time.dto.retrieve;


import java.util.List;

public record TimeTableRetrieveDto(
int memberCount,
List<String> totalUserNames,
List<AvailableDatesRetrieveDto> availableDateTimes
) {
public static TimeTableRetrieveDto of(final List<String> totalUserNames, final List<AvailableDatesRetrieveDto> availableDateTimes) {
return new TimeTableRetrieveDto(totalUserNames.size(), totalUserNames, availableDateTimes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.asap.server.persistence.domain.Meeting;
import com.asap.server.persistence.domain.user.User;
import com.asap.server.persistence.repository.user.UserRepository;

import java.util.Map;
import java.util.List;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand Down
Loading

0 comments on commit 5e5f6c7

Please sign in to comment.