diff --git a/src/main/java/site/billbill/apiserver/api/borrowPosts/controller/PostsController.java b/src/main/java/site/billbill/apiserver/api/borrowPosts/controller/PostsController.java index 9f613c2..1799d73 100644 --- a/src/main/java/site/billbill/apiserver/api/borrowPosts/controller/PostsController.java +++ b/src/main/java/site/billbill/apiserver/api/borrowPosts/controller/PostsController.java @@ -142,6 +142,14 @@ public BaseResponse blockPostController(@ return new BaseResponse<>(postsService.ViewNoRentalPeriodsService(userId,postId)); } - + @Operation(summary="거래처리",description = "해당 게시물에 대한 거래처리를 합니다.") + @PostMapping("/payment") + public BaseResponse BillAcceptController(@RequestBody @Valid PostsRequest.BillAcceptRequest request){ + String userId = ""; + if(MDC.get(JWTUtil.MDC_USER_ID) != null) { + userId= MDC.get(JWTUtil.MDC_USER_ID).toString(); + } + return new BaseResponse<>(postsService.DoBillAcceptService(userId,request)); + } } diff --git a/src/main/java/site/billbill/apiserver/api/borrowPosts/converter/PostsConverter.java b/src/main/java/site/billbill/apiserver/api/borrowPosts/converter/PostsConverter.java index 57df653..38b4ffb 100644 --- a/src/main/java/site/billbill/apiserver/api/borrowPosts/converter/PostsConverter.java +++ b/src/main/java/site/billbill/apiserver/api/borrowPosts/converter/PostsConverter.java @@ -120,6 +120,14 @@ public static PostsResponse.saveSearchListResponse toUserSearhList(List searchHists=userSearchHistRepository.findByUserAndDelYnOrderByCreatedAtDesc(user,false); @@ -203,7 +204,7 @@ public List findRecommandService(){ public PostsResponse.ReviewIdResponse DoReviewPostService(String postId,String userId,PostsRequest.ReviewRequest request){ UserJpaEntity user = userRepository.findById(userId).orElse(null); ItemsJpaEntity item=itemsRepository.findById(postId).orElse(null); - BorrowHistJapEntity borrwHist = borrowHistRepository.findBorrowHistByBorrower(user); + BorrowHistJpaEntity borrowHist = borrowHistRepository.findBorrowHistByBorrower(user); String postsId = ULIDUtil.generatorULID("REVIEW"); if (item == null) { throw new CustomException(ErrorCode.BadRequest, "올바른 게시물 아이디가 아닙니다.", HttpStatus.BAD_REQUEST); @@ -214,7 +215,7 @@ public PostsResponse.ReviewIdResponse DoReviewPostService(String postId,String u if(user == item.getOwner()){ throw new CustomException(ErrorCode.BadRequest, "자기 자신의 게시물에는 리뷰작성이 안됩니다.", HttpStatus.BAD_REQUEST); } - if(borrwHist==null){ + if(borrowHist==null){ throw new CustomException(ErrorCode.BadRequest,"해당 게시물에 대한 대여 기록이 없습니다.", HttpStatus.BAD_REQUEST); } ItemsReviewJpaEntity review=PostsConverter.toItemsReview(user,item,request,postsId); @@ -227,7 +228,9 @@ public PostsResponse.ReviewIdResponse DoReviewPostService(String postId,String u public PostsResponse.NoRentalPeriodsResponse ViewNoRentalPeriodsService(String userId,String postId){ UserJpaEntity user = userRepository.findById(userId).orElse(null); ItemsJpaEntity item=itemsRepository.findById(postId).orElse(null); - List itemsBorrowStatus = itemsBorrowStatusRepository.findAllByItemIdAndBorrowStatusCode(postId, "RENTAL_NOT_POSSIBLE"); + // 상태 코드 리스트 정의 + List statusCodes = List.of("RENTAL_NOT_POSSIBLE", "RENTAL_POSSIBLE"); + List itemsBorrowStatus = itemsBorrowStatusRepository.findAllByItemIdAndBorrowStatusCodeIn(postId, statusCodes); List ownerNoRentalPeriod=itemsBorrowStatus.stream().map(itemStatus->{ return PostsConverter.toOwnerNoRentalPeriod(itemStatus); @@ -242,7 +245,29 @@ public PostsResponse.NoRentalPeriodsResponse ViewNoRentalPeriodsService(String u } + public PostsResponse.BillAcceptResponse DoBillAcceptService(String userId, PostsRequest.BillAcceptRequest request){ + UserJpaEntity user = userRepository.findById(userId).orElse(null); + ItemsJpaEntity item = itemsRepository.findById(request.getPostId()).orElse(null); + if (item == null) { + throw new CustomException(ErrorCode.BadRequest, "올바른 게시물 아이디가 아닙니다.", HttpStatus.BAD_REQUEST); + } + if(user == item.getOwner()){ + throw new CustomException(ErrorCode.BadRequest, "자기 자신의 게시물을 거리할 수는 없습니다.", HttpStatus.BAD_REQUEST); + } + BorrowHistJpaEntity borrowHist = PostsConverter.toBorrowHist(item,user,request); + BorrowHistJpaEntity savedBorrowHist=borrowHistRepository.save(borrowHist); + + PostsRequest.NoRentalPeriod noRentalPeriod= PostsRequest.NoRentalPeriod.builder() + .startDate(request.getStartDate()) + .endDate(request.getEndDate()) + .build(); + ItemsBorrowStatusJpaEntity itemsBorrowStatus= PostsConverter.toItemBorrowStatus(item,"Renting",noRentalPeriod); + itemsBorrowStatusRepository.save(itemsBorrowStatus); + + return PostsConverter.toBillAcceptResponse(savedBorrowHist.getBorrowSeq()); + + } diff --git a/src/main/java/site/billbill/apiserver/model/post/BorrowHistJapEntity.java b/src/main/java/site/billbill/apiserver/model/post/BorrowHistJpaEntity.java similarity index 86% rename from src/main/java/site/billbill/apiserver/model/post/BorrowHistJapEntity.java rename to src/main/java/site/billbill/apiserver/model/post/BorrowHistJpaEntity.java index 397857d..f697590 100644 --- a/src/main/java/site/billbill/apiserver/model/post/BorrowHistJapEntity.java +++ b/src/main/java/site/billbill/apiserver/model/post/BorrowHistJpaEntity.java @@ -2,11 +2,11 @@ import jakarta.persistence.*; import lombok.*; -import org.hibernate.annotations.CreationTimestamp; import site.billbill.apiserver.common.converter.BooleanConverter; import site.billbill.apiserver.model.BaseTime; import site.billbill.apiserver.model.user.UserJpaEntity; +import java.time.LocalDate; import java.time.OffsetDateTime; @Entity @@ -15,7 +15,7 @@ @Getter @Setter @NoArgsConstructor @AllArgsConstructor -public class BorrowHistJapEntity extends BaseTime { +public class BorrowHistJpaEntity extends BaseTime { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "borrow_seq", nullable = false) private long borrowSeq; @@ -26,9 +26,9 @@ public class BorrowHistJapEntity extends BaseTime { @JoinColumn(name = "borrower_id", nullable = false) private UserJpaEntity borrower; @Column(name = "started_at", nullable = false) - private OffsetDateTime startedAt; + private LocalDate startedAt; @Column(name = "ended_at", nullable = false) - private OffsetDateTime endedAt; + private LocalDate endedAt; @Convert(converter = BooleanConverter.class) @Column(name = "use_yn", nullable = false) private boolean useYn = true; diff --git a/src/main/java/site/billbill/apiserver/repository/borrowPosts/BorrowHistRepository.java b/src/main/java/site/billbill/apiserver/repository/borrowPosts/BorrowHistRepository.java index 070f494..3509254 100644 --- a/src/main/java/site/billbill/apiserver/repository/borrowPosts/BorrowHistRepository.java +++ b/src/main/java/site/billbill/apiserver/repository/borrowPosts/BorrowHistRepository.java @@ -1,9 +1,9 @@ package site.billbill.apiserver.repository.borrowPosts; import org.springframework.data.jpa.repository.JpaRepository; -import site.billbill.apiserver.model.post.BorrowHistJapEntity; +import site.billbill.apiserver.model.post.BorrowHistJpaEntity; import site.billbill.apiserver.model.user.UserJpaEntity; -public interface BorrowHistRepository extends JpaRepository { - BorrowHistJapEntity findBorrowHistByBorrower(UserJpaEntity borrower); +public interface BorrowHistRepository extends JpaRepository { + BorrowHistJpaEntity findBorrowHistByBorrower(UserJpaEntity borrower); } diff --git a/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemDslRepositoryImpl.java b/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemDslRepositoryImpl.java index 02e824b..d76dd4a 100644 --- a/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemDslRepositoryImpl.java +++ b/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemDslRepositoryImpl.java @@ -176,7 +176,7 @@ public List getBorrowHistory(String userId, Pageable page QItemsJpaEntity qItems = QItemsJpaEntity.itemsJpaEntity; QitemsLikeJpaEntity qLike = QitemsLikeJpaEntity.itemsLikeJpaEntity; QChatChannelJpaEntity qChatChannel = QChatChannelJpaEntity.chatChannelJpaEntity; - QBorrowHistJapEntity qBorrowHist = QBorrowHistJapEntity.borrowHistJapEntity; + QBorrowHistJpaEntity qBorrowHist = QBorrowHistJpaEntity.borrowHistJpaEntity; JPAQuery qb = queryFactory.select( Projections.constructor( diff --git a/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemsBorrowStatusRepository.java b/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemsBorrowStatusRepository.java index 6ad114c..b89d466 100644 --- a/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemsBorrowStatusRepository.java +++ b/src/main/java/site/billbill/apiserver/repository/borrowPosts/ItemsBorrowStatusRepository.java @@ -7,4 +7,5 @@ public interface ItemsBorrowStatusRepository extends JpaRepository { List findAllByItemIdAndBorrowStatusCode(String itemId,String borrowStatusCode); + List findAllByItemIdAndBorrowStatusCodeIn(String itemId, List statusCodes); }