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

[feat] 거래처리 #79

Merged
merged 1 commit into from
Dec 15, 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
Expand Up @@ -142,6 +142,14 @@ public BaseResponse<PostsResponse.NoRentalPeriodsResponse> blockPostController(@
return new BaseResponse<>(postsService.ViewNoRentalPeriodsService(userId,postId));

}

@Operation(summary="거래처리",description = "해당 게시물에 대한 거래처리를 합니다.")
@PostMapping("/payment")
public BaseResponse<PostsResponse.BillAcceptResponse> 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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ public static PostsResponse.saveSearchListResponse toUserSearhList(List<PostsRes
public static String toRecommandSearch(SearchKeywordStatsJpaEntity searchKeywordStats){
return searchKeywordStats.getKeyword();
}
public static BorrowHistJpaEntity toBorrowHist(ItemsJpaEntity item,UserJpaEntity user,PostsRequest.BillAcceptRequest request){
return BorrowHistJpaEntity.builder()
.item(item)
.borrower(user)
.startedAt(request.getStartDate())
.endedAt(request.getEndDate())
.build();
}
public static ItemsReviewJpaEntity toItemsReview(UserJpaEntity user,ItemsJpaEntity item,PostsRequest.ReviewRequest request ,String reviewId){
return ItemsReviewJpaEntity.builder()
.id(reviewId)
Expand All @@ -136,6 +144,11 @@ public static PostsResponse.ReviewIdResponse toReviewIdResponse(ItemsJpaEntity i
.reviewId(review.getId())
.build();
}
public static PostsResponse.BillAcceptResponse toBillAcceptResponse(Long billId){
return PostsResponse.BillAcceptResponse.builder()
.bilId(billId)
.build();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ public static class NoRentalPeriod{
private LocalDate startDate;
private LocalDate endDate;
}

@Getter
@Setter
@Builder
public static class BillAcceptRequest{
private String postId;
private LocalDate startDate;
private LocalDate endDate;
}
@Getter
@Setter
@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public static class Post{
}
@Builder
@Getter
public static class BillAcceptResponse{
private Long bilId;
}
@Builder
@Getter
public static class ViewPostResponse{
private String postId;
private String title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public interface PostsService {

PostsResponse.NoRentalPeriodsResponse ViewNoRentalPeriodsService(String userId, String postId);

PostsResponse.BillAcceptResponse DoBillAcceptService(String userId, PostsRequest.BillAcceptRequest request);

}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public PostsResponse.ViewAllResultResponse ViewSearchPostService(String userId,
return PostsConverter.toViewAllList(items);
}


public PostsResponse.saveSearchListResponse findSearchService(String userId){
UserJpaEntity user = userRepository.findById(userId).orElse(null);
List<UserSearchHistJpaEntity> searchHists=userSearchHistRepository.findByUserAndDelYnOrderByCreatedAtDesc(user,false);
Expand All @@ -203,7 +204,7 @@ public List<String> 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);
Expand All @@ -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);
Expand All @@ -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<ItemsBorrowStatusJpaEntity> itemsBorrowStatus = itemsBorrowStatusRepository.findAllByItemIdAndBorrowStatusCode(postId, "RENTAL_NOT_POSSIBLE");
// 상태 코드 리스트 정의
List<String> statusCodes = List.of("RENTAL_NOT_POSSIBLE", "RENTAL_POSSIBLE");
List<ItemsBorrowStatusJpaEntity> itemsBorrowStatus = itemsBorrowStatusRepository.findAllByItemIdAndBorrowStatusCodeIn(postId, statusCodes);

List<PostsResponse.NoRentalPeriodResponse> ownerNoRentalPeriod=itemsBorrowStatus.stream().map(itemStatus->{
return PostsConverter.toOwnerNoRentalPeriod(itemStatus);
Expand All @@ -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());

}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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,Long> {
BorrowHistJapEntity findBorrowHistByBorrower(UserJpaEntity borrower);
public interface BorrowHistRepository extends JpaRepository<BorrowHistJpaEntity,Long> {
BorrowHistJpaEntity findBorrowHistByBorrower(UserJpaEntity borrower);
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public List<BorrowHistoryResponse> 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<BorrowHistoryResponse> qb = queryFactory.select(
Projections.constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

public interface ItemsBorrowStatusRepository extends JpaRepository<ItemsBorrowStatusJpaEntity,Long> {
List<ItemsBorrowStatusJpaEntity> findAllByItemIdAndBorrowStatusCode(String itemId,String borrowStatusCode);
List<ItemsBorrowStatusJpaEntity> findAllByItemIdAndBorrowStatusCodeIn(String itemId, List<String> statusCodes);
}
Loading