From df4a43187a0e88ad877b9886b0be44146a52c204 Mon Sep 17 00:00:00 2001 From: kalsteve Date: Mon, 3 Jun 2024 20:56:42 +0900 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20AuctionBidController=20role=20?= =?UTF-8?q?=EC=97=B0=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AuctionBidController.java | 17 ++++++++------- .../service/AuctionBidService.java | 21 ++++++++++++------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/backend/ready/src/main/java/project/one/auctionbids/controller/AuctionBidController.java b/backend/ready/src/main/java/project/one/auctionbids/controller/AuctionBidController.java index cadd803..6c4d357 100644 --- a/backend/ready/src/main/java/project/one/auctionbids/controller/AuctionBidController.java +++ b/backend/ready/src/main/java/project/one/auctionbids/controller/AuctionBidController.java @@ -1,6 +1,7 @@ package project.one.auctionbids.controller; import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -29,8 +30,9 @@ public AuctionBidController(AuctionBidService auctionBidService) { @Operation(summary = "경매 입찰 생성", description = "경매 입찰 생성") @PostMapping(value = "/auction-bids", consumes = "multipart/form-data") - public ResponseEntity createAuctionBid(@RequestPart("request") @Valid AuctionBidRequest request, - @RequestPart("files") List files) throws IOException { + public ResponseEntity createAuctionBid( @Parameter(hidden = true) @RequestHeader("Authorization") String token, + @RequestPart("request") @Valid AuctionBidRequest request, + @RequestPart("files") List files) { AuctionBidResponse bidresponse = auctionBidService.createAuctionBid(request, files); return ResponseEntity.created(URI.create("/auction-bids/" + bidresponse.getAuctionBidId())) .body(bidresponse); @@ -39,9 +41,9 @@ public ResponseEntity createAuctionBid(@RequestPart("request @Operation(summary = "경매 입찰 수정", description = "경매 입찰 수정") @PutMapping(value = "/auction-bids/{auctionBidId}", consumes = "multipart/form-data") - public ResponseEntity updateAuctionBid(@PathVariable Long auctionBidId, - @RequestPart("request") @Valid AuctionBidRequest request, - @RequestPart("files") List files) throws IOException { + public ResponseEntity updateAuctionBid( @Parameter(hidden = true) @RequestHeader("Authorization") String token,@PathVariable Long auctionBidId, + @RequestPart("request") @Valid AuctionBidRequest request, + @RequestPart("files") List files) { AuctionBidResponse response = auctionBidService.updateAuctionBid(auctionBidId, request, files); return ResponseEntity.ok(response); } @@ -49,7 +51,8 @@ public ResponseEntity updateAuctionBid(@PathVariable Long au @Operation(summary = "경매 입찰 삭제", description = "경매 입찰 삭제") @DeleteMapping("/auction-bids/{auctionBidId}") - public ResponseEntity> deleteAuctionBid(@PathVariable Long auctionBidId) { + public ResponseEntity> deleteAuctionBid( @Parameter(hidden = true) @RequestHeader("Authorization") String token, + @PathVariable Long auctionBidId) { auctionBidService.deleteAuctionBid(auctionBidId); ResponseMessage response = new ResponseMessage<>(HttpStatus.OK.value(), "입찰 삭제 성공", "auctionBidId/" + auctionBidId); return ResponseEntity.ok(response); @@ -57,7 +60,7 @@ public ResponseEntity> deleteAuctionBid(@PathVariable Lo @Operation(summary = "경매 입찰 조회", description = "경매 입찰 조회") @GetMapping("/auction-bids/{auctionBidId}") - public ResponseEntity getAuctionBid(@PathVariable Long auctionBidId) { + public ResponseEntity getAuctionBid( @PathVariable Long auctionBidId) { AuctionBidResponse response = auctionBidService.findAuctionBidById(auctionBidId); return ResponseEntity.ok(response); } diff --git a/backend/ready/src/main/java/project/one/auctionbids/service/AuctionBidService.java b/backend/ready/src/main/java/project/one/auctionbids/service/AuctionBidService.java index f47fbde..1592636 100644 --- a/backend/ready/src/main/java/project/one/auctionbids/service/AuctionBidService.java +++ b/backend/ready/src/main/java/project/one/auctionbids/service/AuctionBidService.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Optional; @Service @Transactional @@ -39,16 +40,20 @@ public AuctionBidService(AuctionBidRepository auctionBidRepository, } - public AuctionBidResponse createAuctionBid(AuctionBidRequest auctionBidRequest, List files) throws IOException { - Auction auction = auctionRepository.findById(auctionBidRequest.getAuctionId()) - .orElseThrow(() -> new IllegalStateException("해당 경매가 존재하지 않습니다.")); - Business business = businessRepository.findById(auctionBidRequest.getBusinessId()) - .orElseThrow(() -> new IllegalStateException("해당 사업자가 존재하지 않습니다.")); + public AuctionBidResponse createAuctionBid(AuctionBidRequest auctionBidRequest, List files) { + Optional auction = auctionRepository.findById(auctionBidRequest.getAuctionId()); + Optional business = businessRepository.findById(auctionBidRequest.getBusinessId()); + + auction. List photoUrls = new ArrayList<>(); for (MultipartFile file : files) { - String photoUrl = s3Service.photoUploadToS3("auction-bid", file); - photoUrls.add(photoUrl); + try { + String photoUrl = s3Service.photoUploadToS3("auction-bid", file); + photoUrls.add(photoUrl); + } catch (IOException e) { + throw new IllegalStateException("파일 업로드 중 오류가 발생했습니다."); + } } AuctionBid auctionBid = buildAuctionBid(auctionBidRequest, auction, business, photoUrls); @@ -75,7 +80,7 @@ private AuctionBid buildAuctionBid(AuctionBidRequest auctionBidRequest, Auction .build(); } - public AuctionBidResponse updateAuctionBid(Long id, AuctionBidRequest request, List files) throws IOException { + public AuctionBidResponse updateAuctionBid(Long id, AuctionBidRequest request, List files) { AuctionBid auctionBid = auctionBidRepository.findById(id) .orElseThrow(AuctionBidNotFoundException::new); From 6e7cda8511d4fba1eb3f235c51702f640b475677 Mon Sep 17 00:00:00 2001 From: key Date: Mon, 3 Jun 2024 22:56:03 +0900 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20businessId=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AuctionBidController.java | 10 +-- .../auctionbids/dto/AuctionBidRequest.java | 1 - .../service/AuctionBidService.java | 85 ++++++++++--------- 3 files changed, 51 insertions(+), 45 deletions(-) diff --git a/backend/ready/src/main/java/project/one/auctionbids/controller/AuctionBidController.java b/backend/ready/src/main/java/project/one/auctionbids/controller/AuctionBidController.java index 6c4d357..77c9d47 100644 --- a/backend/ready/src/main/java/project/one/auctionbids/controller/AuctionBidController.java +++ b/backend/ready/src/main/java/project/one/auctionbids/controller/AuctionBidController.java @@ -33,7 +33,7 @@ public AuctionBidController(AuctionBidService auctionBidService) { public ResponseEntity createAuctionBid( @Parameter(hidden = true) @RequestHeader("Authorization") String token, @RequestPart("request") @Valid AuctionBidRequest request, @RequestPart("files") List files) { - AuctionBidResponse bidresponse = auctionBidService.createAuctionBid(request, files); + AuctionBidResponse bidresponse = auctionBidService.createAuctionBid(token, request, files); return ResponseEntity.created(URI.create("/auction-bids/" + bidresponse.getAuctionBidId())) .body(bidresponse); } @@ -41,18 +41,18 @@ public ResponseEntity createAuctionBid( @Parameter(hidden = @Operation(summary = "경매 입찰 수정", description = "경매 입찰 수정") @PutMapping(value = "/auction-bids/{auctionBidId}", consumes = "multipart/form-data") - public ResponseEntity updateAuctionBid( @Parameter(hidden = true) @RequestHeader("Authorization") String token,@PathVariable Long auctionBidId, + public ResponseEntity updateAuctionBid( @Parameter(hidden = true) @RequestHeader("Authorization") String token, + @PathVariable Long auctionBidId, @RequestPart("request") @Valid AuctionBidRequest request, @RequestPart("files") List files) { - AuctionBidResponse response = auctionBidService.updateAuctionBid(auctionBidId, request, files); + AuctionBidResponse response = auctionBidService.updateAuctionBid(token, auctionBidId, request, files); return ResponseEntity.ok(response); } @Operation(summary = "경매 입찰 삭제", description = "경매 입찰 삭제") @DeleteMapping("/auction-bids/{auctionBidId}") - public ResponseEntity> deleteAuctionBid( @Parameter(hidden = true) @RequestHeader("Authorization") String token, - @PathVariable Long auctionBidId) { + public ResponseEntity> deleteAuctionBid( @PathVariable Long auctionBidId) { auctionBidService.deleteAuctionBid(auctionBidId); ResponseMessage response = new ResponseMessage<>(HttpStatus.OK.value(), "입찰 삭제 성공", "auctionBidId/" + auctionBidId); return ResponseEntity.ok(response); diff --git a/backend/ready/src/main/java/project/one/auctionbids/dto/AuctionBidRequest.java b/backend/ready/src/main/java/project/one/auctionbids/dto/AuctionBidRequest.java index e92713d..ffb85a7 100644 --- a/backend/ready/src/main/java/project/one/auctionbids/dto/AuctionBidRequest.java +++ b/backend/ready/src/main/java/project/one/auctionbids/dto/AuctionBidRequest.java @@ -12,7 +12,6 @@ @Setter public class AuctionBidRequest { private Long auctionId; - private Long businessId; private String content; @Schema(description = "List of material names", example = "[\"string1\", \"string2\", \"string3\"]") private List materialName; diff --git a/backend/ready/src/main/java/project/one/auctionbids/service/AuctionBidService.java b/backend/ready/src/main/java/project/one/auctionbids/service/AuctionBidService.java index 1592636..7d9d495 100644 --- a/backend/ready/src/main/java/project/one/auctionbids/service/AuctionBidService.java +++ b/backend/ready/src/main/java/project/one/auctionbids/service/AuctionBidService.java @@ -1,5 +1,6 @@ package project.one.auctionbids.service; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -13,50 +14,45 @@ import project.one.auctionbids.exception.AuctionBidNotFoundException; import project.one.business.domain.Business; import project.one.business.domain.BusinessRepository; +import project.one.global.auth.jwt.AuthTokenGenerator; import java.io.IOException; -import java.util.ArrayList; import java.util.List; -import java.util.Optional; +import java.util.stream.Collectors; @Service @Transactional +@RequiredArgsConstructor public class AuctionBidService { private final AuctionBidRepository auctionBidRepository; private final AuctionRepository auctionRepository; private final BusinessRepository businessRepository; private final S3Service s3Service; + private final AuthTokenGenerator authTokenGenerator; + + + public AuctionBidResponse createAuctionBid(String token, AuctionBidRequest auctionBidRequest, List files) { + if (authTokenGenerator.extractRole(token.substring(7)).equals("1")) + throw new IllegalArgumentException("사업자만 입찰할 수 있습니다."); + Auction auction = auctionRepository.findById(auctionBidRequest.getAuctionId()) + .orElseThrow( () -> new IllegalArgumentException("해당 경매가 존재하지 않습니다.")); + Business business = businessRepository.findById(Long.valueOf(authTokenGenerator.extractUserId(token.substring(7)))) + .orElseThrow( () -> new IllegalArgumentException("해당 사업자가 존재하지 않습니다.")); + + + AuctionBid auctionBid = buildAuctionBid(auctionBidRequest, auction, business, + files.stream() + .map(file -> { + try { + return s3Service.photoUploadToS3("auction-bid", file); + } catch (IOException e) { + throw new IllegalStateException("파일 업로드 중 오류가 발생했습니다.", e); + } + }) + .collect(Collectors.toList()) + ); - - public AuctionBidService(AuctionBidRepository auctionBidRepository, - AuctionRepository auctionRepository, - BusinessRepository businessRepository, - S3Service s3Service) { - this.auctionBidRepository = auctionBidRepository; - this.auctionRepository = auctionRepository; - this.businessRepository = businessRepository; - this.s3Service = s3Service; - } - - - public AuctionBidResponse createAuctionBid(AuctionBidRequest auctionBidRequest, List files) { - Optional auction = auctionRepository.findById(auctionBidRequest.getAuctionId()); - Optional business = businessRepository.findById(auctionBidRequest.getBusinessId()); - - auction. - - List photoUrls = new ArrayList<>(); - for (MultipartFile file : files) { - try { - String photoUrl = s3Service.photoUploadToS3("auction-bid", file); - photoUrls.add(photoUrl); - } catch (IOException e) { - throw new IllegalStateException("파일 업로드 중 오류가 발생했습니다."); - } - } - - AuctionBid auctionBid = buildAuctionBid(auctionBidRequest, auction, business, photoUrls); AuctionBid savedBid = auctionBidRepository.save(auctionBid); return toDto(savedBid); } @@ -80,17 +76,28 @@ private AuctionBid buildAuctionBid(AuctionBidRequest auctionBidRequest, Auction .build(); } - public AuctionBidResponse updateAuctionBid(Long id, AuctionBidRequest request, List files) { - AuctionBid auctionBid = auctionBidRepository.findById(id) + public AuctionBidResponse updateAuctionBid(String token, Long auctionBidId, AuctionBidRequest request, List files) { + String role = authTokenGenerator.extractRole(token); + AuctionBid auctionBid = auctionBidRepository.findById(auctionBidId) .orElseThrow(AuctionBidNotFoundException::new); - List photoUrls = new ArrayList<>(); - for (MultipartFile file : files) { - String photoUrl = s3Service.photoUploadToS3("auction-bid", file); - photoUrls.add(photoUrl); - } + if (role.equals("1")) + throw new IllegalArgumentException("사업자만 수정할 수 있습니다."); + + updateAuctionBids( + auctionBid, + request, + files.stream() + .map(file -> { + try { + return s3Service.photoUploadToS3("auction-bid", file); + } catch (IOException e) { + throw new IllegalStateException("파일 업로드 중 오류가 발생했습니다.", e); + } + }) + .collect(Collectors.toList()) + ); - updateAuctionBids(auctionBid, request, photoUrls); auctionBidRepository.save(auctionBid); return toDto(auctionBid); } From 9a634a9754b187cbb899966ebded178bd6966b37 Mon Sep 17 00:00:00 2001 From: key Date: Mon, 3 Jun 2024 22:56:21 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20role=20=EB=AC=B8=EC=9E=90=EC=97=B4?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/project/one/global/auth/jwt/AuthTokenGenerator.java | 2 +- .../java/project/one/global/auth/jwt/JwtTokenProvider.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/ready/src/main/java/project/one/global/auth/jwt/AuthTokenGenerator.java b/backend/ready/src/main/java/project/one/global/auth/jwt/AuthTokenGenerator.java index ed6f92e..a251c1d 100644 --- a/backend/ready/src/main/java/project/one/global/auth/jwt/AuthTokenGenerator.java +++ b/backend/ready/src/main/java/project/one/global/auth/jwt/AuthTokenGenerator.java @@ -48,6 +48,6 @@ public AuthToken generate(String userId) { } public String extractUserId(String token) { return jwtTokenProvider.extractSubject(token);} - public int extractRole(String token) { return jwtTokenProvider.extractRole(token);} + public String extractRole(String token) { return jwtTokenProvider.extractRole(token);} public boolean validateToken(String token) { return jwtTokenProvider.validateToken(token);} } diff --git a/backend/ready/src/main/java/project/one/global/auth/jwt/JwtTokenProvider.java b/backend/ready/src/main/java/project/one/global/auth/jwt/JwtTokenProvider.java index f88bf22..652fc4f 100644 --- a/backend/ready/src/main/java/project/one/global/auth/jwt/JwtTokenProvider.java +++ b/backend/ready/src/main/java/project/one/global/auth/jwt/JwtTokenProvider.java @@ -38,9 +38,9 @@ public String extractSubject(String token) { return claims.getSubject(); } - public int extractRole(String token) { + public String extractRole(String token) { Claims claims = parseClaims(token); - return claims.get("role", Integer.class); + return claims.get("role", String.class); } public boolean validateToken(String token) {