Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Be/fix/#166] 옥션비즈, 롤 수정 #167

Merged
merged 3 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,35 +30,37 @@ public AuctionBidController(AuctionBidService auctionBidService) {

@Operation(summary = "경매 입찰 생성", description = "경매 입찰 생성")
@PostMapping(value = "/auction-bids", consumes = "multipart/form-data")
public ResponseEntity<AuctionBidResponse> createAuctionBid(@RequestPart("request") @Valid AuctionBidRequest request,
@RequestPart("files") List<MultipartFile> files) throws IOException {
AuctionBidResponse bidresponse = auctionBidService.createAuctionBid(request, files);
public ResponseEntity<AuctionBidResponse> createAuctionBid( @Parameter(hidden = true) @RequestHeader("Authorization") String token,
@RequestPart("request") @Valid AuctionBidRequest request,
@RequestPart("files") List<MultipartFile> files) {
AuctionBidResponse bidresponse = auctionBidService.createAuctionBid(token, request, files);
return ResponseEntity.created(URI.create("/auction-bids/" + bidresponse.getAuctionBidId()))
.body(bidresponse);
}


@Operation(summary = "경매 입찰 수정", description = "경매 입찰 수정")
@PutMapping(value = "/auction-bids/{auctionBidId}", consumes = "multipart/form-data")
public ResponseEntity<AuctionBidResponse> updateAuctionBid(@PathVariable Long auctionBidId,
@RequestPart("request") @Valid AuctionBidRequest request,
@RequestPart("files") List<MultipartFile> files) throws IOException {
AuctionBidResponse response = auctionBidService.updateAuctionBid(auctionBidId, request, files);
public ResponseEntity<AuctionBidResponse> updateAuctionBid( @Parameter(hidden = true) @RequestHeader("Authorization") String token,
@PathVariable Long auctionBidId,
@RequestPart("request") @Valid AuctionBidRequest request,
@RequestPart("files") List<MultipartFile> files) {
AuctionBidResponse response = auctionBidService.updateAuctionBid(token, auctionBidId, request, files);
return ResponseEntity.ok(response);
}


@Operation(summary = "경매 입찰 삭제", description = "경매 입찰 삭제")
@DeleteMapping("/auction-bids/{auctionBidId}")
public ResponseEntity<ResponseMessage<String>> deleteAuctionBid(@PathVariable Long auctionBidId) {
public ResponseEntity<ResponseMessage<String>> deleteAuctionBid( @PathVariable Long auctionBidId) {
auctionBidService.deleteAuctionBid(auctionBidId);
ResponseMessage<String> response = new ResponseMessage<>(HttpStatus.OK.value(), "입찰 삭제 성공", "auctionBidId/" + auctionBidId);
return ResponseEntity.ok(response);
}

@Operation(summary = "경매 입찰 조회", description = "경매 입찰 조회")
@GetMapping("/auction-bids/{auctionBidId}")
public ResponseEntity<AuctionBidResponse> getAuctionBid(@PathVariable Long auctionBidId) {
public ResponseEntity<AuctionBidResponse> getAuctionBid( @PathVariable Long auctionBidId) {
AuctionBidResponse response = auctionBidService.findAuctionBidById(auctionBidId);
return ResponseEntity.ok(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> materialName;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,45 +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.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 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<MultipartFile> files) throws IOException {
public AuctionBidResponse createAuctionBid(String token, AuctionBidRequest auctionBidRequest, List<MultipartFile> files) {
if (authTokenGenerator.extractRole(token.substring(7)).equals("1"))
throw new IllegalArgumentException("사업자만 입찰할 수 있습니다.");
Auction auction = auctionRepository.findById(auctionBidRequest.getAuctionId())
.orElseThrow(() -> new IllegalStateException("해당 경매가 존재하지 않습니다."));
Business business = businessRepository.findById(auctionBidRequest.getBusinessId())
.orElseThrow(() -> new IllegalStateException("해당 사업자가 존재하지 않습니다."));
.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())
);

List<String> photoUrls = new ArrayList<>();
for (MultipartFile file : files) {
String photoUrl = s3Service.photoUploadToS3("auction-bid", file);
photoUrls.add(photoUrl);
}

AuctionBid auctionBid = buildAuctionBid(auctionBidRequest, auction, business, photoUrls);
AuctionBid savedBid = auctionBidRepository.save(auctionBid);
return toDto(savedBid);
}
Expand All @@ -75,17 +76,28 @@ private AuctionBid buildAuctionBid(AuctionBidRequest auctionBidRequest, Auction
.build();
}

public AuctionBidResponse updateAuctionBid(Long id, AuctionBidRequest request, List<MultipartFile> files) throws IOException {
AuctionBid auctionBid = auctionBidRepository.findById(id)
public AuctionBidResponse updateAuctionBid(String token, Long auctionBidId, AuctionBidRequest request, List<MultipartFile> files) {
String role = authTokenGenerator.extractRole(token);
AuctionBid auctionBid = auctionBidRepository.findById(auctionBidId)
.orElseThrow(AuctionBidNotFoundException::new);

List<String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading