Skip to content

Commit c56eb39

Browse files
authored
Merge pull request #362 from GTable/refactor#361-getMyWaiting
Refactor : 내 웨이팅 정보 조회 로직 리팩토링
2 parents cbca76a + d8132ac commit c56eb39

14 files changed

Lines changed: 392 additions & 70 deletions

File tree

nowait-app-user-api/src/main/java/com/nowait/applicationuser/exception/GlobalExceptionHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.nowait.applicationuser.security.exception.ResourceNotFoundException;
2323
import com.nowait.applicationuser.security.exception.UnauthorizedException;
24+
import com.nowait.applicationuser.waiting.exception.WorkInProgressException;
2425
import com.nowait.common.exception.ErrorMessage;
2526
import com.nowait.common.exception.ErrorResponse;
2627
import com.nowait.discord.service.DiscordAlarmService;
@@ -292,6 +293,15 @@ public ErrorResponse handleReservationAddUnauthorizedException(
292293
return new ErrorResponse(e.getMessage(), RESERVATION_ADD_UNAUTHORIZED.getCode());
293294
}
294295

296+
@ResponseStatus(CONFLICT)
297+
@ExceptionHandler(WorkInProgressException.class)
298+
public ErrorResponse handleWorkInProgressException(
299+
WorkInProgressException e, WebRequest request) {
300+
alarm(e, request);
301+
log.error("handleWorkInProgressException", e);
302+
return new ErrorResponse(e.getMessage(), WORK_IN_PROGRESS.getCode());
303+
}
304+
295305
// 공통 에러 Map 생성
296306
private static Map<String, String> getErrors(MethodArgumentNotValidException e) {
297307
return e.getBindingResult()

nowait-app-user-api/src/main/java/com/nowait/applicationuser/waiting/controller/WaitingController.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.nowait.applicationuser.waiting.controller;
22

3+
import java.util.List;
4+
35
import org.springframework.http.ResponseEntity;
46
import org.springframework.security.core.annotation.AuthenticationPrincipal;
57
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -12,6 +14,7 @@
1214

1315
import com.nowait.applicationuser.waiting.dto.CancelWaitingRequest;
1416
import com.nowait.applicationuser.waiting.dto.CancelWaitingResponse;
17+
import com.nowait.applicationuser.waiting.dto.GetMyWaitingInfoResponse;
1518
import com.nowait.applicationuser.waiting.dto.GetWaitingSizeResponse;
1619
import com.nowait.applicationuser.waiting.dto.RegisterWaitingRequest;
1720
import com.nowait.applicationuser.waiting.dto.RegisterWaitingResponse;
@@ -34,6 +37,24 @@ public class WaitingController {
3437
/**
3538
* 대기열 리팩토링용 API
3639
*/
40+
@GetMapping()
41+
@Operation(summary = "대기열 리팩토링용 API", description = "전체 대기 목록 조회")
42+
public ResponseEntity<?> getMyWaitingInfo(
43+
@AuthenticationPrincipal CustomOAuth2User customOAuth2User
44+
) {
45+
List<GetMyWaitingInfoResponse> response = waitingService.getMyWaitingInfo(
46+
customOAuth2User
47+
);
48+
49+
return ResponseEntity
50+
.ok()
51+
.body(
52+
ApiUtils.success(
53+
response
54+
)
55+
);
56+
}
57+
3758
@PostMapping("/{publicCode}")
3859
@Operation(summary = "대기열 리팩토링용 API", description = "대기열 리팩토링용 API")
3960
public ResponseEntity<?> registerWaiting(
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.nowait.applicationuser.waiting.dto;
2+
3+
import java.time.LocalDateTime;
4+
5+
import lombok.Builder;
6+
import lombok.Getter;
7+
8+
@Getter
9+
@Builder
10+
public class GetMyWaitingInfoResponse {
11+
private String reservationId;
12+
private Long storeId;
13+
private String storeName;
14+
private String departmentName;
15+
private Integer rank;
16+
private Integer teamsAhead;
17+
private Integer partySize;
18+
private String status;
19+
private LocalDateTime registeredAt;
20+
private String location;
21+
private String profileImageUrl;
22+
private String bannerImageUrl;
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.nowait.applicationuser.waiting.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
7+
@Getter
8+
@NoArgsConstructor
9+
@AllArgsConstructor
10+
public class IdempotencyResponse {
11+
private String state;
12+
private Object response;
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.nowait.applicationuser.waiting.exception;
2+
3+
import com.nowait.common.exception.ErrorMessage;
4+
5+
public class WorkInProgressException extends RuntimeException {
6+
public WorkInProgressException() { super(ErrorMessage.WORK_IN_PROGRESS.getMessage()); }
7+
}

nowait-app-user-api/src/main/java/com/nowait/applicationuser/waiting/redis/WaitingIdempotencyRepository.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
import org.springframework.stereotype.Repository;
88

99
import com.fasterxml.jackson.databind.ObjectMapper;
10-
import com.nowait.applicationuser.waiting.dto.CancelWaitingResponse;
11-
import com.nowait.applicationuser.waiting.dto.RegisterWaitingResponse;
10+
import com.nowait.applicationuser.waiting.dto.IdempotencyResponse;
1211
import com.nowait.applicationuser.waiting.dto.WaitingCancelIdempotencyValue;
1312
import com.nowait.applicationuser.waiting.dto.WaitingIdempotencyValue;
1413

1514
import lombok.RequiredArgsConstructor;
15+
import lombok.extern.slf4j.Slf4j;
1616

1717
@Repository
1818
@RequiredArgsConstructor
19+
@Slf4j
1920
public class WaitingIdempotencyRepository {
2021

2122
private final RedisTemplate<String, String> redisTemplate;
@@ -24,64 +25,61 @@ public class WaitingIdempotencyRepository {
2425
private static final Duration TTL = Duration.ofMinutes(10);
2526

2627
// 멱등키 조회 메서드
27-
public Optional<WaitingIdempotencyValue> findByKey(String key) {
28+
public Optional<WaitingIdempotencyValue> findByRegisterKey(String key) {
2829
String idempotencyValue = redisTemplate.opsForValue().get(key);
2930

3031
if (idempotencyValue == null) {
3132
return Optional.empty();
3233
}
3334

3435
try {
35-
return Optional.of(
36-
objectMapper.readValue(idempotencyValue, WaitingIdempotencyValue.class)
37-
);
36+
log.info("Idempotency value found in Redis for key {}: {}", key, idempotencyValue);
37+
return Optional.of(objectMapper.readValue(idempotencyValue, WaitingIdempotencyValue.class));
3838
} catch (Exception e) {
3939
throw new IllegalArgumentException("Failed to deserialize value from Redis", e);
4040
}
4141
}
4242

4343
// 멱등키 조회 메서드
44-
public Optional<WaitingCancelIdempotencyValue> findByCancelKey(String key) {
44+
public WaitingCancelIdempotencyValue findByCancelKey(String key) {
4545
String idempotencyValue = redisTemplate.opsForValue().get(key);
4646

4747
if (idempotencyValue == null) {
48-
return Optional.empty();
48+
return null;
4949
}
5050

5151
try {
52-
return Optional.of(
53-
objectMapper.readValue(idempotencyValue, WaitingCancelIdempotencyValue.class)
54-
);
52+
log.info("Idempotency value found in Redis for key {}: {}", key, idempotencyValue);
53+
return objectMapper.readValue(idempotencyValue, WaitingCancelIdempotencyValue.class);
5554
} catch (Exception e) {
5655
throw new IllegalArgumentException("Failed to deserialize value from Redis", e);
5756
}
5857
}
5958

6059

61-
// 멱등키 저장 메서드 - 대기 등록
62-
public void saveIdempotencyValue(String key, RegisterWaitingResponse response) {
63-
WaitingIdempotencyValue waitingIdempotencyValue = new WaitingIdempotencyValue(
60+
// 멱등키 저장 메서드
61+
public void saveIdempotencyResponse(String key, Object response) {
62+
IdempotencyResponse idempotencyResponse = new IdempotencyResponse(
6463
"COMPLETED",
6564
response
6665
);
6766

6867
try {
69-
String jsonValue = objectMapper.writeValueAsString(waitingIdempotencyValue);
68+
String jsonValue = objectMapper.writeValueAsString(idempotencyResponse);
7069
redisTemplate.opsForValue().set(key, jsonValue, TTL);
7170
} catch (Exception e) {
7271
throw new IllegalArgumentException("Failed to serialize value for Redis", e);
7372
}
7473
}
7574

76-
// 멱등키 저장 메서드 - 대기 취소
77-
public void saveCancelIdempotencyValue(String key, CancelWaitingResponse response) {
78-
WaitingCancelIdempotencyValue waitingIdempotencyValue = new WaitingCancelIdempotencyValue(
79-
"COMPLETED",
80-
response
75+
public void saveIdempotencyInProgress(String key) {
76+
IdempotencyResponse idempotencyResponse = new IdempotencyResponse(
77+
"IN-PROGRESS",
78+
null
8179
);
8280

8381
try {
84-
String jsonValue = objectMapper.writeValueAsString(waitingIdempotencyValue);
82+
String jsonValue = objectMapper.writeValueAsString(idempotencyResponse);
8583
redisTemplate.opsForValue().set(key, jsonValue, TTL);
8684
} catch (Exception e) {
8785
throw new IllegalArgumentException("Failed to serialize value for Redis", e);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.nowait.applicationuser.waiting.service;
2+
3+
import java.util.Optional;
4+
5+
import org.springframework.stereotype.Service;
6+
7+
import com.nowait.applicationuser.waiting.dto.WaitingCancelIdempotencyValue;
8+
import com.nowait.applicationuser.waiting.dto.WaitingIdempotencyValue;
9+
import com.nowait.applicationuser.waiting.redis.WaitingIdempotencyRepository;
10+
11+
import jakarta.servlet.http.HttpServletRequest;
12+
import lombok.RequiredArgsConstructor;
13+
import lombok.extern.slf4j.Slf4j;
14+
15+
@Service
16+
@RequiredArgsConstructor
17+
@Slf4j
18+
public class IdempotencyService {
19+
20+
private final WaitingIdempotencyRepository waitingIdempotencyRepository;
21+
22+
// 멱등키 검증 메서드
23+
// TODO : 공통 멱등키 검증 메서드로 리팩토링 필요!!!!!!!!!
24+
public Optional<WaitingIdempotencyValue> validateIdempotency(HttpServletRequest httpServletRequest) {
25+
String idempotentKey = httpServletRequest.getHeader("Idempotency-Key");
26+
27+
return waitingIdempotencyRepository.findByRegisterKey(idempotentKey);
28+
}
29+
30+
public WaitingCancelIdempotencyValue validateIdempotencyCancel(HttpServletRequest httpServletRequest) {
31+
String idempotentKey = httpServletRequest.getHeader("Idempotency-Key");
32+
33+
return waitingIdempotencyRepository.findByCancelKey(idempotentKey);
34+
}
35+
36+
// 멱등키 응답 저장 메서드
37+
public void saveIdempotencyResponse(String idempotentKey, Object response) {
38+
if (idempotentKey != null && !idempotentKey.isBlank()) {
39+
waitingIdempotencyRepository.saveIdempotencyResponse(idempotentKey, response);
40+
}
41+
}
42+
43+
// 멱등키 PROCESSING 상태로 최초 저장
44+
public void saveIdempotencyKeyInProgress(String idempotentKey) {
45+
if (idempotentKey != null && !idempotentKey.isBlank()) {
46+
log.info("Saving idempotency key in progress: {}", idempotentKey);
47+
waitingIdempotencyRepository.saveIdempotencyInProgress(idempotentKey);
48+
}
49+
}
50+
51+
}

0 commit comments

Comments
 (0)