Skip to content

Commit d57e703

Browse files
authored
Merge pull request #231 from prgrms-web-devcourse-final-project/lkh/schedule/refactor
🐛 fix: 일정 - 중간장소 투표 결과 WINNER 반영되지 않는 이슈 해결
2 parents 6f41c74 + f5edb49 commit d57e703

File tree

10 files changed

+103
-88
lines changed

10 files changed

+103
-88
lines changed

src/main/java/com/grepp/spring/app/controller/api/schedule/ScheduleController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ private Schedule validSchedule(Long scheduleId) {
5858
@Operation(summary = "일정 조회", description = "일정을 조회합니다.")
5959
@GetMapping("/show/{scheduleId}")
6060
public ResponseEntity<ApiResponse<ShowScheduleResponse>> showSchedules(
61-
@PathVariable Long scheduleId) {
61+
@PathVariable Long scheduleId, @CurrentUser String userId) {
6262

6363
Schedule schedule = validSchedule(scheduleId);
64-
ShowScheduleResponse response = scheduleQueryService.showSchedule(schedule);
64+
ShowScheduleResponse response = scheduleQueryService.showSchedule(schedule, userId);
6565

6666
return ResponseEntity.ok(ApiResponse.success(response));
6767
}

src/main/java/com/grepp/spring/app/model/schedule/repository/LocationQueryRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Optional;
77
import org.springframework.data.jpa.repository.JpaRepository;
88
import org.springframework.data.jpa.repository.Lock;
9+
import org.springframework.data.jpa.repository.Modifying;
910
import org.springframework.data.jpa.repository.Query;
1011
import org.springframework.data.repository.query.Param;
1112

@@ -17,4 +18,5 @@ public interface LocationQueryRepository extends JpaRepository<Location, Long> {
1718
@Lock(LockModeType.PESSIMISTIC_WRITE)
1819
@Query("select l from Location l where l.id = :id")
1920
Optional<Location> findByIdWithPessimisticLock(@Param("id") Long id);
21+
2022
}

src/main/java/com/grepp/spring/app/model/schedule/service/ScheduleCommandService.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.grepp.spring.app.model.schedule.repository.WorkspaceCommandRepository;
5353
import com.grepp.spring.app.model.schedule.repository.WorkspaceQueryRepository;
5454
import com.grepp.spring.infra.error.exceptions.schedule.EventNotActivatedException;
55+
import com.grepp.spring.infra.error.exceptions.schedule.VoteAlreadyProgressException;
5556
import com.grepp.spring.infra.utils.RandomPicker;
5657
import com.grepp.spring.infra.error.exceptions.NotFoundException;
5758
import com.grepp.spring.infra.error.exceptions.group.UserNotFoundException;
@@ -452,30 +453,39 @@ public void voteMiddleLocation(Schedule schedule, ScheduleMember scheduleMember,
452453
// 파라미터를 id로 받으면 더 좋을 것 같음
453454
Location location = locationQueryRepository.findByIdWithPessimisticLock(lid.getId())
454455
.orElseThrow(() -> new IllegalArgumentException("장소를 찾을 수 없습니다."));
456+
// log.info("location = {}", location.toString());
457+
455458

456459
// 락 이후 voteCnt 증가시키기
457460
location.setVoteCount(location.getVoteCount() + 1);
458461

459462
List<Location> locationList = locationQueryRepository.findByScheduleId(schedule.getId());
463+
// log.info("locationList = {}", locationList.toString());
464+
460465
int scheduleMemberNumber = scheduleMemberQueryRepository.findByScheduleId(schedule.getId()).size();
461-
int voteCount = voteQueryRepository.findByScheduleId(schedule.getId()).size();
462466

463467
// vote 저장 시점을 뒤로 미뤄서, Location 락과의 교착을 방지
464468
VoteMiddleLocationDto dto = VoteMiddleLocationDto.toDto(scheduleMember, lid, schedule);
465469
Vote vote = VoteMiddleLocationDto.fromDto(dto);
466470
voteCommandRepository.save(vote);
467471

472+
int voteCount = voteQueryRepository.findByScheduleId(schedule.getId()).size();
473+
log.info("voteCount = {}", voteCount);
474+
475+
468476
if (scheduleMemberNumber - voteCount == 0) {
477+
Long winnerLocationId = 0L;
469478
int winner = 0;
470-
Long winnerLid = null;
471479
for (Location l : locationList) {
472480
if (winner <= l.getVoteCount()) {
473481
winner = l.getVoteCount();
474-
winnerLid = l.getId();
482+
winnerLocationId = l.getId();
483+
log.info("winnerLocationId: {}", winnerLocationId);
475484
}
476485
}
477486

478-
Optional<Location> winnerLocation = locationQueryRepository.findById(winnerLid);
487+
Optional<Location> winnerLocation = locationQueryRepository.findById(winnerLocationId);
488+
log.info("winnerLocation: {}", winnerLocation);
479489
winnerLocation.get().setStatus(VoteStatus.WINNER);
480490
}
481491
}
@@ -555,7 +565,7 @@ public void WriteSuggestedLocation(Schedule schedule, WriteSuggestedLocationRequ
555565
}
556566
}
557567
else {
558-
throw new RuntimeException("투표중입니다! 투표중에는 후보장소를 등록할 수 없습니다.");
568+
throw new VoteAlreadyProgressException(ScheduleErrorCode.VOTE_ALREADY_PROGRESS);
559569
}
560570

561571
}

src/main/java/com/grepp/spring/app/model/schedule/service/ScheduleQueryService.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.grepp.spring.infra.error.exceptions.event.EventNotFoundException;
2626
import com.grepp.spring.infra.error.exceptions.group.ScheduleNotFoundException;
2727
import com.grepp.spring.infra.error.exceptions.schedule.LocationNotFoundException;
28+
import com.grepp.spring.infra.error.exceptions.schedule.NotScheduleMemberException;
2829
import com.grepp.spring.infra.error.exceptions.schedule.ScheduleMemberNotFoundException;
2930
import com.grepp.spring.infra.error.exceptions.schedule.WorkSpaceNotFoundException;
3031
import com.grepp.spring.infra.response.EventErrorCode;
@@ -51,22 +52,31 @@ public class ScheduleQueryService {
5152
private final MetroTransferQueryRepository metroTransferQueryRepository;
5253

5354
@Transactional
54-
public ShowScheduleResponse showSchedule(Schedule schedule) {
55+
public ShowScheduleResponse showSchedule(Schedule schedule, String userId) {
5556

5657
Long eventId = schedule.getEvent().getId();
5758

5859
List<ScheduleMember> scheduleMembers = scheduleMemberQueryRepository.findByScheduleId(
5960
schedule.getId());
61+
Boolean bool = false;
62+
for (ScheduleMember scheduleMember : scheduleMembers) {
63+
if (scheduleMember.getMember().getId().equals(userId)) {
64+
bool = true;
65+
}
66+
}
6067

61-
List<Workspace> workspaces = workspaceQueryRepository.findAllByScheduleId(schedule.getId());
68+
if (bool) {
69+
List<Workspace> workspaces = workspaceQueryRepository.findAllByScheduleId(schedule.getId());
6270

63-
MeetingType meetingType = eventRepository.findById(eventId).get().getMeetingType();
71+
MeetingType meetingType = eventRepository.findById(eventId).get().getMeetingType();
6472

73+
ShowScheduleDto dto = ShowScheduleDto.fromEntity(meetingType, eventId, schedule,
74+
scheduleMembers, workspaces);
6575

66-
ShowScheduleDto dto = ShowScheduleDto.fromEntity(meetingType, eventId, schedule,
67-
scheduleMembers, workspaces);
76+
return ShowScheduleDto.fromDto(dto);
77+
}
6878

69-
return ShowScheduleDto.fromDto(dto);
79+
throw new NotScheduleMemberException(ScheduleErrorCode.NOT_SCHEDULE_MEMBER);
7080
}
7181

7282
public ShowSuggestedLocationsResponse showSuggestedLocation(Long scheduleId) {

src/main/java/com/grepp/spring/infra/error/RestApiExceptionAdvice.java

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.grepp.spring.infra.error.exceptions.schedule;
2+
3+
import com.grepp.spring.infra.response.ScheduleErrorCode;
4+
import lombok.extern.slf4j.Slf4j;
5+
6+
@Slf4j
7+
public class NotScheduleMemberException extends RuntimeException {
8+
private final ScheduleErrorCode code;
9+
10+
public NotScheduleMemberException(ScheduleErrorCode code) {
11+
this.code = code;
12+
}
13+
14+
public NotScheduleMemberException(ScheduleErrorCode code, Exception e) {
15+
this.code = code;
16+
log.error(e.getMessage(), e);
17+
}
18+
19+
public ScheduleErrorCode code() {
20+
return code;
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.grepp.spring.infra.error.exceptions.schedule;
2+
3+
import com.grepp.spring.infra.response.ScheduleErrorCode;
4+
import lombok.extern.slf4j.Slf4j;
5+
6+
@Slf4j
7+
public class VoteAlreadyProgressException extends RuntimeException {
8+
private final ScheduleErrorCode code;
9+
10+
public VoteAlreadyProgressException(ScheduleErrorCode code) {
11+
this.code = code;
12+
}
13+
14+
public VoteAlreadyProgressException(ScheduleErrorCode code, Exception e) {
15+
this.code = code;
16+
log.error(e.getMessage(), e);
17+
}
18+
19+
public ScheduleErrorCode code() {
20+
return code;
21+
}
22+
}

src/main/java/com/grepp/spring/infra/error/scheduleAdvuce/ScheduleExceptionAdvice.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.grepp.spring.infra.response.ScheduleErrorCode;
1212
import lombok.extern.slf4j.Slf4j;
1313
import org.springframework.core.annotation.Order;
14+
import org.springframework.http.HttpStatus;
1415
import org.springframework.http.ResponseEntity;
1516
import org.springframework.web.bind.annotation.ExceptionHandler;
1617
import org.springframework.web.bind.annotation.RestControllerAdvice;
@@ -20,17 +21,33 @@
2021
@Order(1)
2122
public class ScheduleExceptionAdvice {
2223

24+
@ExceptionHandler(VoteAlreadyProgressException.class)
25+
public ResponseEntity<ApiResponse<String>> notScheduleMemberExHandler(
26+
VoteAlreadyProgressException ex) {
27+
28+
return ResponseEntity.status(HttpStatus.CONFLICT)
29+
.body(ApiResponse.error(ScheduleErrorCode.VOTE_ALREADY_PROGRESS));
30+
}
31+
32+
@ExceptionHandler(NotScheduleMemberException.class)
33+
public ResponseEntity<ApiResponse<String>> notScheduleMemberExHandler(
34+
NotScheduleMemberException ex) {
35+
36+
return ResponseEntity.status(HttpStatus.FORBIDDEN)
37+
.body(ApiResponse.error(ScheduleErrorCode.NOT_SCHEDULE_MEMBER));
38+
}
39+
2340
@ExceptionHandler(WorkSpaceNotFoundException.class)
2441
public ResponseEntity<ApiResponse<String>> workspaceNotFoundExHandler(
25-
UserNotFoundException ex) {
42+
WorkSpaceNotFoundException ex) {
2643

2744
return ResponseEntity.status(ResponseCode.NOT_FOUND.status())
2845
.body(ApiResponse.error(ScheduleErrorCode.SCHEDULE_MEMBER_NOT_FOUND));
2946
}
3047

3148
@ExceptionHandler(ScheduleMemberNotFoundException.class)
3249
public ResponseEntity<ApiResponse<String>> scheduleMemberNotFoundExHandler(
33-
UserNotFoundException ex) {
50+
ScheduleMemberNotFoundException ex) {
3451

3552
return ResponseEntity.status(ResponseCode.NOT_FOUND.status())
3653
.body(ApiResponse.error(ScheduleErrorCode.SCHEDULE_MEMBER_NOT_FOUND));
@@ -46,7 +63,7 @@ public ResponseEntity<ApiResponse<String>> userNotFoundExHandler(
4663

4764
@ExceptionHandler(LocationNotFoundException.class)
4865
public ResponseEntity<ApiResponse<String>> locationNotFoundExHandler(
49-
ScheduleNotFoundException ex) {
66+
LocationNotFoundException ex) {
5067

5168
return ResponseEntity.status(ResponseCode.NOT_FOUND.status())
5269
.body(ApiResponse.error(GroupErrorCode.SCHEDULE_NOT_FOUND));

src/main/java/com/grepp/spring/infra/response/ScheduleErrorCode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ public enum ScheduleErrorCode {
77
LOCATION_NOT_FOUND("404",HttpStatus.NOT_FOUND, "해당 투표리스트(장소)를 찾을 수 없습니다. locationId를 확인해주세요."),
88
SCHEDULE_MEMBER_NOT_FOUND("404",HttpStatus.NOT_FOUND, "스케줄에서 회원을 찾을 수 없습니다."),
99
WORKSPACE_NOT_FOUND("404",HttpStatus.NOT_FOUND,"워크스페이스를 찾을 수 없습니다."),
10-
EVENT_NOT_ACTIVATED("409", HttpStatus.CONFLICT, "이미 일정이 생성된 이벤트입니다.");
10+
EVENT_NOT_ACTIVATED("409", HttpStatus.CONFLICT, "이미 일정이 생성된 이벤트입니다."),
11+
NOT_SCHEDULE_MEMBER("403",HttpStatus.FORBIDDEN,"일정에 포함된 멤버만 조회할 수 있습니다."),
12+
VOTE_ALREADY_PROGRESS("409",HttpStatus.CONFLICT,"투표중입니다! 투표중에는 후보장소를 등록할 수 없습니다.");
1113

1214
private final String code;
1315
private final HttpStatus status;

src/test/java/com/grepp/spring/app/model/schedule/ScheduleQueryServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ void showScheduleTest() {
257257

258258

259259
// When
260-
var response = scheduleQueryService.showSchedule(dummySchedule1);
260+
var response = scheduleQueryService.showSchedule(dummySchedule1,"GOOGLE_123");
261261

262262
// Then
263263
assertNotNull(response);

0 commit comments

Comments
 (0)