Skip to content

Commit ef76dc5

Browse files
committed
✨ feat: 일정 조회 예외처리
1 parent 6f41c74 commit ef76dc5

File tree

7 files changed

+60
-16
lines changed

7 files changed

+60
-16
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/service/ScheduleCommandService.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,17 +465,19 @@ public void voteMiddleLocation(Schedule schedule, ScheduleMember scheduleMember,
465465
Vote vote = VoteMiddleLocationDto.fromDto(dto);
466466
voteCommandRepository.save(vote);
467467

468+
468469
if (scheduleMemberNumber - voteCount == 0) {
470+
Long winnerLocationId = 0L;
469471
int winner = 0;
470-
Long winnerLid = null;
471472
for (Location l : locationList) {
472473
if (winner <= l.getVoteCount()) {
473474
winner = l.getVoteCount();
474-
winnerLid = l.getId();
475+
winnerLocationId = l.getId();
476+
log.info("winnerLocationId: {}", winnerLocationId);
475477
}
476478
}
477479

478-
Optional<Location> winnerLocation = locationQueryRepository.findById(winnerLid);
480+
Optional<Location> winnerLocation = locationQueryRepository.findById(winnerLocationId);
479481
winnerLocation.get().setStatus(VoteStatus.WINNER);
480482
}
481483
}

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) {
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+
}

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

Lines changed: 12 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,25 @@
2021
@Order(1)
2122
public class ScheduleExceptionAdvice {
2223

24+
@ExceptionHandler(NotScheduleMemberException.class)
25+
public ResponseEntity<ApiResponse<String>> notScheduleMemberExHandler(
26+
NotScheduleMemberException ex) {
27+
28+
return ResponseEntity.status(HttpStatus.FORBIDDEN)
29+
.body(ApiResponse.error(ScheduleErrorCode.NOT_SCHEDULE_MEMBER));
30+
}
31+
2332
@ExceptionHandler(WorkSpaceNotFoundException.class)
2433
public ResponseEntity<ApiResponse<String>> workspaceNotFoundExHandler(
25-
UserNotFoundException ex) {
34+
WorkSpaceNotFoundException ex) {
2635

2736
return ResponseEntity.status(ResponseCode.NOT_FOUND.status())
2837
.body(ApiResponse.error(ScheduleErrorCode.SCHEDULE_MEMBER_NOT_FOUND));
2938
}
3039

3140
@ExceptionHandler(ScheduleMemberNotFoundException.class)
3241
public ResponseEntity<ApiResponse<String>> scheduleMemberNotFoundExHandler(
33-
UserNotFoundException ex) {
42+
ScheduleMemberNotFoundException ex) {
3443

3544
return ResponseEntity.status(ResponseCode.NOT_FOUND.status())
3645
.body(ApiResponse.error(ScheduleErrorCode.SCHEDULE_MEMBER_NOT_FOUND));
@@ -46,7 +55,7 @@ public ResponseEntity<ApiResponse<String>> userNotFoundExHandler(
4655

4756
@ExceptionHandler(LocationNotFoundException.class)
4857
public ResponseEntity<ApiResponse<String>> locationNotFoundExHandler(
49-
ScheduleNotFoundException ex) {
58+
LocationNotFoundException ex) {
5059

5160
return ResponseEntity.status(ResponseCode.NOT_FOUND.status())
5261
.body(ApiResponse.error(GroupErrorCode.SCHEDULE_NOT_FOUND));

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ 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,"일정에 포함된 멤버만 조회할 수 있습니다.");
1112

1213
private final String code;
1314
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)