From 2fae40caf01210d515f3eb74a151e8c18f938d81 Mon Sep 17 00:00:00 2001 From: emost22 Date: Wed, 15 Mar 2023 01:09:30 +0900 Subject: [PATCH] [Nexters/www-be#76] Refactor save timetable and place list --- .../wwwbe/service/MeetingService.java | 102 ++++++++---------- 1 file changed, 42 insertions(+), 60 deletions(-) diff --git a/src/main/java/com/promise8/wwwbe/service/MeetingService.java b/src/main/java/com/promise8/wwwbe/service/MeetingService.java index b3dabb6..711e072 100644 --- a/src/main/java/com/promise8/wwwbe/service/MeetingService.java +++ b/src/main/java/com/promise8/wwwbe/service/MeetingService.java @@ -55,40 +55,8 @@ public MeetingCreateResDtoV1 createMeeting(MeetingCreateReqDtoV1 meetingCreateRe .build() ); - // TODO Refactoring - List meetingUserTimetableEntityList = new ArrayList<>(); - - for (UserPromiseTimeReqDtoV1 userPromiseTimeReqDto : meetingCreateReqDto.getPromiseDateTimeList()) { - LocalDate promiseDate = userPromiseTimeReqDto.getPromiseDate(); - PromiseTime promiseTime = userPromiseTimeReqDto.getPromiseTime(); - - MeetingUserTimetableEntityV1 meetingUserTimetableEntity = MeetingUserTimetableEntityV1.builder() - .promiseDate(promiseDate) - .promiseTime(promiseTime) - .isConfirmed(false) - .meetingUserEntity(meetingUserEntity) - .build(); - meetingUserTimetableEntityList.add(meetingUserTimetableEntity); - } - meetingUserTimetableRepository.saveAll(meetingUserTimetableEntityList); - - // TODO Refactoring - HashSet isExistPlaceHashSet = new HashSet<>(); - List meetingPlaceEntityList = new ArrayList<>(); - for (String promisePlace : meetingCreateReqDto.getPromisePlaceList()) { - if (isExistPlaceHashSet.contains(promisePlace)) { - continue; - } else { - isExistPlaceHashSet.add(promisePlace); - } - - meetingPlaceEntityList.add(MeetingPlaceEntityV1.builder() - .promisePlace(promisePlace) - .isConfirmed(false) - .meetingUserEntity(meetingUserEntity) - .build()); - } - meetingPlaceRepository.saveAll(meetingPlaceEntityList); + saveMeetingUserTimetableList(meetingCreateReqDto.getPromiseDateTimeList(), meetingUserEntity); + saveMeetingPlaceList(meetingCreateReqDto.getPromisePlaceList(), meetingUserEntity, new HashSet<>()); return MeetingCreateResDtoV1.of(meetingCode, shortLink); } @@ -263,17 +231,7 @@ public Long joinMeetingAndGetMeetingUserId(long userId, long meetingId, JoinMeet MeetingUserEntityV1 meetingUserEntity = meetingEntity.addMeetingUser(newMeetingUserEntity); meetingUserRepository.save(meetingUserEntity); - List meetingUserTimetableEntityList = new ArrayList<>(); - for (UserPromiseTimeReqDtoV1 userPromiseTimeReq : joinMeetingReqDto.getUserPromiseTimeList()) { - meetingUserTimetableEntityList.add(MeetingUserTimetableEntityV1.builder() - .meetingUserEntity(meetingUserEntity) - .promiseDate(userPromiseTimeReq.getPromiseDate()) - .promiseTime(userPromiseTimeReq.getPromiseTime()) - .isConfirmed(false) - .build()); - } - - meetingUserTimetableRepository.saveAll(meetingUserTimetableEntityList); + saveMeetingUserTimetableList(joinMeetingReqDto.getUserPromiseTimeList(), meetingUserEntity); HashSet existPromisePlaceHashSet = new HashSet<>(); if (meetingEntity.getMeetingUserEntityList() != null) { @@ -282,20 +240,7 @@ public Long joinMeetingAndGetMeetingUserId(long userId, long meetingId, JoinMeet }); } - List meetingPlaceEntityList = new ArrayList<>(); - joinMeetingReqDto.getPromisePlaceList().forEach(req -> { - if (existPromisePlaceHashSet.contains(req)) { - return; - } else { - existPromisePlaceHashSet.add(req); - } - - meetingPlaceEntityList.add(meetingPlaceRepository.save(MeetingPlaceEntityV1.builder() - .meetingUserEntity(meetingUserEntity) - .promisePlace(req) - .isConfirmed(false) - .build())); - }); + saveMeetingPlaceList(joinMeetingReqDto.getPromisePlaceList(), meetingUserEntity, existPromisePlaceHashSet); // int currentUserCount = meetingEntity.getMeetingUserEntityList().size(); // @@ -313,10 +258,47 @@ public Long joinMeetingAndGetMeetingUserId(long userId, long meetingId, JoinMeet // } // } - meetingPlaceRepository.saveAll(meetingPlaceEntityList); return meetingUserEntity.getMeetingUserId(); } + private void saveMeetingUserTimetableList( + List promiseDateTimeList, MeetingUserEntityV1 meetingUserEntity) { + List meetingUserTimetableEntityList = new ArrayList<>(); + + for (UserPromiseTimeReqDtoV1 userPromiseTimeReqDto : promiseDateTimeList) { + LocalDate promiseDate = userPromiseTimeReqDto.getPromiseDate(); + PromiseTime promiseTime = userPromiseTimeReqDto.getPromiseTime(); + + MeetingUserTimetableEntityV1 meetingUserTimetableEntity = MeetingUserTimetableEntityV1.builder() + .promiseDate(promiseDate) + .promiseTime(promiseTime) + .isConfirmed(false) + .meetingUserEntity(meetingUserEntity) + .build(); + meetingUserTimetableEntityList.add(meetingUserTimetableEntity); + } + meetingUserTimetableRepository.saveAll(meetingUserTimetableEntityList); + } + + private void saveMeetingPlaceList( + List promisePlaceList, MeetingUserEntityV1 meetingUserEntity, HashSet isExistPlaceHashSet) { + List meetingPlaceEntityList = new ArrayList<>(); + for (String promisePlace : promisePlaceList) { + if (isExistPlaceHashSet.contains(promisePlace)) { + continue; + } else { + isExistPlaceHashSet.add(promisePlace); + } + + meetingPlaceEntityList.add(MeetingPlaceEntityV1.builder() + .promisePlace(promisePlace) + .isConfirmed(false) + .meetingUserEntity(meetingUserEntity) + .build()); + } + meetingPlaceRepository.saveAll(meetingPlaceEntityList); + } + public List getVoteNotiNeedMeetingList() { return meetingRepository.findVotedMeetingByDateTime(LocalDate.now().minusDays(1).atStartOfDay()); }