|
1 | 1 | package com.backend.domain.bid.controller; |
2 | 2 |
|
3 | 3 | import com.backend.domain.bid.dto.*; |
| 4 | +import com.backend.domain.bid.service.BidPaymentService; |
4 | 5 | import com.backend.domain.bid.service.BidService; |
5 | 6 | import com.backend.domain.member.repository.MemberRepository; |
6 | 7 | import com.backend.global.response.RsData; |
|
26 | 27 | public class ApiV1BidController { |
27 | 28 |
|
28 | 29 | private final BidService bidService; |
| 30 | + private final BidPaymentService bidPaymentService; |
29 | 31 | private final MemberRepository memberRepository; |
30 | 32 |
|
31 | 33 | @Operation(summary = "입찰 생성", description = "특정 상품에 대해 입찰 생성.") |
32 | 34 | @ApiResponses(value = { |
33 | | - @ApiResponse(responseCode = "200", description = "입찰 생성 성공", |
34 | | - content = @Content(schema = @Schema(implementation = RsData.class))), |
35 | | - @ApiResponse(responseCode = "400", description = "잘못된 요청", |
36 | | - content = @Content(schema = @Schema(implementation = RsData.class))), |
37 | | - @ApiResponse(responseCode = "401", description = "인증 실패", |
38 | | - content = @Content(schema = @Schema(implementation = RsData.class))) |
| 35 | + @ApiResponse(responseCode = "201", description = "입찰 생성 성공", |
| 36 | + content = @Content(schema = @Schema(implementation = RsData.class))), |
| 37 | + @ApiResponse(responseCode = "400", description = "잘못된 요청", |
| 38 | + content = @Content(schema = @Schema(implementation = RsData.class))), |
| 39 | + @ApiResponse(responseCode = "401", description = "인증 실패", |
| 40 | + content = @Content(schema = @Schema(implementation = RsData.class))) |
39 | 41 | }) |
40 | 42 | @PostMapping("/products/{productId}") |
41 | 43 | public RsData<BidResponseDto> createBid( |
42 | 44 | @Parameter(description = "상품 ID", required = true) @PathVariable Long productId, |
43 | 45 | @Parameter(description = "입찰 요청 정보", required = true) @Valid @RequestBody BidRequestDto request, |
44 | 46 | @Parameter(hidden = true) @AuthenticationPrincipal User user) { |
45 | | - // TODO: JWT 토큰에서 사용자 추출로직으로 대체 |
46 | | - Long bidderId; |
47 | | - if (user != null) { |
48 | | - bidderId = Long.parseLong(user.getUsername()); |
49 | | - } else { |
50 | | - // 테스트용: 인증이 없으면 첫 번째 사용자 사용 |
51 | | - bidderId = 1L; |
52 | | - } |
| 47 | + Long bidderId = extractMemberId(user); |
53 | 48 | return bidService.createBid(productId, bidderId, request); |
54 | 49 | } |
55 | 50 |
|
56 | 51 | @Operation(summary = "입찰 현황 조회", description = "특정 상품의 현재 입찰 현황 조회.") |
57 | 52 | @ApiResponses(value = { |
58 | | - @ApiResponse(responseCode = "200", description = "입찰 현황 조회 성공", |
59 | | - content = @Content(schema = @Schema(implementation = RsData.class))), |
60 | | - @ApiResponse(responseCode = "404", description = "상품을 찾을 수 없음", |
61 | | - content = @Content(schema = @Schema(implementation = RsData.class))) |
| 53 | + @ApiResponse(responseCode = "200", description = "입찰 현황 조회 성공", |
| 54 | + content = @Content(schema = @Schema(implementation = RsData.class))), |
| 55 | + @ApiResponse(responseCode = "404", description = "상품을 찾을 수 없음", |
| 56 | + content = @Content(schema = @Schema(implementation = RsData.class))) |
62 | 57 | }) |
63 | 58 | @GetMapping("/products/{productId}") |
64 | 59 | public RsData<BidCurrentResponseDto> getBidStatus( |
65 | | - @Parameter(description = "상품 ID", required = true) @PathVariable Long productId |
66 | | - ){ |
| 60 | + @Parameter(description = "상품 ID", required = true) @PathVariable Long productId) { |
67 | 61 | return bidService.getBidStatus(productId); |
68 | 62 | } |
69 | 63 |
|
70 | 64 | @Operation(summary = "내 입찰 내역 조회", description = "현재 사용자의 입찰 내역을 페이지네이션으로 조회.") |
71 | 65 | @ApiResponses(value = { |
72 | | - @ApiResponse(responseCode = "200", description = "내 입찰 내역 조회 성공", |
73 | | - content = @Content(schema = @Schema(implementation = RsData.class))), |
74 | | - @ApiResponse(responseCode = "401", description = "인증 실패", |
75 | | - content = @Content(schema = @Schema(implementation = RsData.class))) |
| 66 | + @ApiResponse(responseCode = "200", description = "내 입찰 내역 조회 성공", |
| 67 | + content = @Content(schema = @Schema(implementation = RsData.class))), |
| 68 | + @ApiResponse(responseCode = "401", description = "인증 실패", |
| 69 | + content = @Content(schema = @Schema(implementation = RsData.class))) |
76 | 70 | }) |
77 | 71 | @GetMapping("/me") |
78 | 72 | public RsData<MyBidResponseDto> getMyBids( |
79 | 73 | @Parameter(description = "페이지 번호 (0부터 시작)", example = "0") @RequestParam(defaultValue = "0") int page, |
80 | 74 | @Parameter(description = "페이지 크기", example = "10") @RequestParam(defaultValue = "10") int size, |
81 | | - @Parameter(hidden = true) @AuthenticationPrincipal User user |
82 | | - ){ |
83 | | - // TODO: JWT 토큰에서 사용자 추출로직으로 대체 |
84 | | - Long memberId = Long.parseLong(user.getUsername()); |
85 | | - return bidService.getMyBids(memberId,page,size); |
| 75 | + @Parameter(hidden = true) @AuthenticationPrincipal User user) { |
| 76 | + Long memberId = extractMemberId(user); |
| 77 | + return bidService.getMyBids(memberId, page, size); |
86 | 78 | } |
87 | 79 |
|
88 | 80 | @Operation(summary = "낙찰 결제", description = "내가 낙찰한 입찰 건에 대해 지갑에서 출금하고 결제 완료로 표시합니다.") |
| 81 | + @ApiResponses(value = { |
| 82 | + @ApiResponse(responseCode = "200", description = "결제 성공", |
| 83 | + content = @Content(schema = @Schema(implementation = RsData.class))), |
| 84 | + @ApiResponse(responseCode = "401", description = "인증 실패", |
| 85 | + content = @Content(schema = @Schema(implementation = RsData.class))), |
| 86 | + @ApiResponse(responseCode = "403", description = "권한 없음", |
| 87 | + content = @Content(schema = @Schema(implementation = RsData.class))) |
| 88 | + }) |
89 | 89 | @PostMapping("/{bidId}/pay") |
90 | 90 | public RsData<BidPayResponseDto> payBid( |
91 | | - @PathVariable Long bidId, |
92 | | - @AuthenticationPrincipal User user |
93 | | - ) { |
| 91 | + @Parameter(description = "입찰 ID", required = true) @PathVariable Long bidId, |
| 92 | + @Parameter(hidden = true) @AuthenticationPrincipal User user) { |
94 | 93 | if (user == null) { |
95 | 94 | throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "로그인이 필요합니다."); |
96 | 95 | } |
97 | 96 |
|
98 | | - Long memberId; |
| 97 | + Long memberId = extractMemberId(user); |
| 98 | + return bidPaymentService.payForBid(memberId, bidId); |
| 99 | + } |
| 100 | + |
| 101 | + // ======================================= helper methods ======================================= // |
| 102 | + private Long extractMemberId(User user) { |
| 103 | + if (user == null) { |
| 104 | + // 테스트용: 인증이 없으면 첫 번째 사용자 사용 |
| 105 | + return 1L; |
| 106 | + } |
| 107 | + |
99 | 108 | String username = user.getUsername(); |
100 | 109 | try { |
101 | | - memberId = Long.parseLong(username); |
| 110 | + return Long.parseLong(username); |
102 | 111 | } catch (NumberFormatException e) { |
103 | | - var me = memberRepository.findByEmail(username) |
104 | | - .orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "유효하지 않은 인증 정보입니다.")); |
105 | | - memberId = me.getId(); |
| 112 | + var member = memberRepository.findByEmail(username) |
| 113 | + .orElseThrow(() -> new ResponseStatusException( |
| 114 | + HttpStatus.UNAUTHORIZED, |
| 115 | + "유효하지 않은 인증 정보입니다." |
| 116 | + )); |
| 117 | + return member.getId(); |
106 | 118 | } |
107 | | - |
108 | | - return bidService.payForBid(memberId, bidId); |
109 | 119 | } |
110 | 120 | } |
0 commit comments