Skip to content

Commit

Permalink
[fix] 게시물 불가능한 날짜 조회 분리 및 리뷰작성 에러 핸들링 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jainefer committed Dec 14, 2024
1 parent 0d6b5a4 commit 693461b
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public BaseResponse<PostsResponse.ViewAllResultResponse> getPostsController(
@RequestParam(value="sortBy",required = true,defaultValue = "accuracy") String sortBy
){


Sort.Direction direction = "asc".equalsIgnoreCase(order) ? Sort.Direction.ASC : Sort.Direction.DESC;
return new BaseResponse<>(postsService.ViewAllPostService(category,page,direction,sortBy));
}
Expand Down Expand Up @@ -127,13 +126,22 @@ public BaseResponse<String> updatePostController(@PathVariable(value="postId",re
@PostMapping("/reviews/{postId}")
public BaseResponse<PostsResponse.ReviewIdResponse> reviewPostController(@PathVariable(value="postId",required = true)String postId,
@RequestBody @Valid PostsRequest.ReviewRequest request){

String userId = "";
if(MDC.get(JWTUtil.MDC_USER_ID) != null) {
userId= MDC.get(JWTUtil.MDC_USER_ID).toString();
}
return new BaseResponse<>(postsService.DoReviewPostService(postId,userId,request));
}
@Operation(summary="해당 게시물 불가능한 날짜 조회",description = "해당 게시물의 불가능한 날짜 조회를 합니다. owner : 게시물을 등록할 때 작성한 게시물 대여 불가 기간입니다. user : 해당 채팅방에서, 논의중인 대여기간입니다. ")
@GetMapping("/{postId}/blocked")
public BaseResponse<PostsResponse.NoRentalPeriodsResponse> blockPostController(@PathVariable(value = "postId",required = true)String postId){
String userId = "";
if(MDC.get(JWTUtil.MDC_USER_ID) != null) {
userId= MDC.get(JWTUtil.MDC_USER_ID).toString();
}
return new BaseResponse<>(postsService.ViewNoRentalPeriodsService(userId,postId));

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import site.billbill.apiserver.api.borrowPosts.dto.request.PostsRequest;
import site.billbill.apiserver.api.borrowPosts.dto.response.PostsResponse;
import site.billbill.apiserver.model.chat.ChatChannelJpaEntity;
import site.billbill.apiserver.model.post.*;
import site.billbill.apiserver.model.user.UserJpaEntity;
import site.billbill.apiserver.model.user.UserSearchHistJpaEntity;
import site.billbill.apiserver.repository.borrowPosts.SearchKeywordStatRepository;

import java.time.format.DateTimeFormatter;
import java.util.List;
Expand Down Expand Up @@ -66,7 +66,7 @@ public static PostsResponse.Post toPost(ItemsJpaEntity item,ItemsBorrowJpaEntity
public static PostsResponse.ViewAllResultResponse toViewAllList(List<PostsResponse.Post> posts){
return PostsResponse.ViewAllResultResponse.builder().result(posts).build();
}
public static PostsResponse.ViewPostResponse toViewPost(ItemsJpaEntity item, ItemsBorrowJpaEntity borrowItem, List<PostsResponse.NoRentalPeriodResponse> noRental, String status,UserJpaEntity user){
public static PostsResponse.ViewPostResponse toViewPost(ItemsJpaEntity item, ItemsBorrowJpaEntity borrowItem, String status,UserJpaEntity user){
return PostsResponse.ViewPostResponse.builder()
.postId(item.getId())
.title(item.getTitle())
Expand All @@ -75,20 +75,31 @@ public static PostsResponse.ViewPostResponse toViewPost(ItemsJpaEntity item, Ite
.price(borrowItem.getPrice())
.priceStandard(borrowItem.getPriceStandard())
.deposit(borrowItem.getDeposit())
.noRentalPeriod(noRental)
.itemStatus(status)
.categoryId(item.getCategory().getId())
.categoryName(item.getCategory().getName())
.userId(user.getUserId())
.userName(user.getNickname())
.build();
}
public static PostsResponse.NoRentalPeriodResponse toNoRentalPeriod(ItemsBorrowStatusJpaEntity borrowStatus){
public static PostsResponse.NoRentalPeriodResponse toOwnerNoRentalPeriod(ItemsBorrowStatusJpaEntity borrowStatus){
return PostsResponse.NoRentalPeriodResponse.builder()
.startDate(borrowStatus.getStartDate().format(DATE_FORMATTER))
.endDate(borrowStatus.getEndDate().format(DATE_FORMATTER))
.build();
}
public static PostsResponse.NoRentalPeriodResponse toContactNoRentalPeriod(ChatChannelJpaEntity chat){
return PostsResponse.NoRentalPeriodResponse.builder()
.startDate(chat.getStartedAt().format(DATE_FORMATTER))
.endDate(chat.getEndedAt().format(DATE_FORMATTER))
.build();
}
public static PostsResponse.NoRentalPeriodsResponse toNoRentalPeriods(List<PostsResponse.NoRentalPeriodResponse> owner, List<PostsResponse.NoRentalPeriodResponse> user){
return PostsResponse.NoRentalPeriodsResponse.builder()
.owner(owner)
.user(user)
.build();
}
public static UserSearchHistJpaEntity toUserSearch(UserJpaEntity user,String keyword){
return UserSearchHistJpaEntity.builder()
.keyword(keyword)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public static class ViewPostResponse{
private int price;
private PriceStandard priceStandard;
private int deposit;
private List<NoRentalPeriodResponse> noRentalPeriod;
private String categoryId;
private String categoryName;
private String userId;
Expand Down Expand Up @@ -76,6 +75,14 @@ public static class NoRentalPeriodResponse{
@Getter
@Setter
@Builder
public static class NoRentalPeriodsResponse{

private List<PostsResponse.NoRentalPeriodResponse> owner;
private List<PostsResponse.NoRentalPeriodResponse> user;
}
@Getter
@Setter
@Builder
public static class ReviewIdResponse{
private String itemId;
private String reviewId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public interface PostsService {
PostsResponse.ReviewIdResponse DoReviewPostService(String postId,String userId,PostsRequest.ReviewRequest request);
List<String> findRecommandService();

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import site.billbill.apiserver.common.enums.exception.ErrorCode;
import site.billbill.apiserver.common.utils.ULID.ULIDUtil;
import site.billbill.apiserver.exception.CustomException;
import site.billbill.apiserver.model.chat.ChatChannelJpaEntity;
import site.billbill.apiserver.model.post.*;
import site.billbill.apiserver.model.user.UserJpaEntity;
import site.billbill.apiserver.model.user.UserSearchHistJpaEntity;
import site.billbill.apiserver.repository.borrowPosts.*;
import site.billbill.apiserver.repository.chat.ChatRepository;
import site.billbill.apiserver.repository.user.UserRepository;
import site.billbill.apiserver.repository.user.UserSearchHistRepository;

Expand All @@ -38,6 +40,8 @@ public class PostsServiceImpl implements PostsService {
private final UserSearchHistRepository userSearchHistRepository;
private final SearchKeywordStatRepository searchKeywordStatRepository;
private final ItemsReivewRepository itemsReivewRepository;
private final ChatRepository chatRepository;
private final BorrowHistRepository borrowHistRepository;
public PostsResponse.UploadResponse uploadPostService(PostsRequest.UploadRequest request,String userId){
//먼저 item 생성,
Optional<UserJpaEntity> isUser=userRepository.findById(userId);
Expand Down Expand Up @@ -81,8 +85,8 @@ public PostsResponse.ViewPostResponse ViewPostService(String postId,String userI
ItemsBorrowJpaEntity borrowItem=itemsBorrowRepository.findById(postId).orElse(null);
UserJpaEntity user = userRepository.findById(userId).orElse(null);

List<ItemsBorrowStatusJpaEntity> borrowStatus=itemsBorrowStatusRepository.findAllByItemIdAndBorrowStatusCode(postId,"RENTAL_NOT_POSSIBLE");
List<PostsResponse.NoRentalPeriodResponse> noRentalPeriods=borrowStatus.stream().map(PostsConverter::toNoRentalPeriod).toList();


if(item==null){
throw new CustomException(ErrorCode.BadRequest, "올바른 게시물 아이디가 아닙니다.", HttpStatus.BAD_REQUEST);
}
Expand All @@ -105,7 +109,7 @@ public PostsResponse.ViewPostResponse ViewPostService(String postId,String userI
break;

}
return PostsConverter.toViewPost(item,borrowItem,noRentalPeriods,status,user);
return PostsConverter.toViewPost(item,borrowItem,status,user);

}
@Transactional
Expand Down Expand Up @@ -199,19 +203,54 @@ 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);
String postsId = ULIDUtil.generatorULID("REVIEW");
if (item == null) {
throw new CustomException(ErrorCode.BadRequest, "올바른 게시물 아이디가 아닙니다.", HttpStatus.BAD_REQUEST);
}else if(request.getRating()>=6||request.getRating()<=0){
}
if(request.getRating()>=6||request.getRating()<=0){
throw new CustomException(ErrorCode.BadRequest, "평점이 올바르지 않습니다. 1~5 사이로 입력해주셔야합니다.", HttpStatus.BAD_REQUEST);
}
if(user == item.getOwner()){
throw new CustomException(ErrorCode.BadRequest, "자기 자신의 게시물에는 리뷰작성이 안됩니다.", HttpStatus.BAD_REQUEST);
}
if(borrwHist==null){
throw new CustomException(ErrorCode.BadRequest,"해당 게시물에 대한 대여 기록이 없습니다.", HttpStatus.BAD_REQUEST);
}
ItemsReviewJpaEntity review=PostsConverter.toItemsReview(user,item,request,postsId);
itemsReivewRepository.save(review);

return PostsConverter.toReviewIdResponse(item,review);
}


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

List<ChatChannelJpaEntity> chatChannel = chatRepository.findAllByItemAndContactUser(item,user);
List<PostsResponse.NoRentalPeriodResponse> contactNoRentalPeriod=chatChannel.stream().map(chat->{
return PostsConverter.toContactNoRentalPeriod(chat);
}).toList();

return PostsConverter.toNoRentalPeriods(ownerNoRentalPeriod,contactNoRentalPeriod);

}












//모듈화 코드
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +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.user.UserJpaEntity;

public interface BorrowHistRepository extends JpaRepository<BorrowHistJapEntity,Long> {
BorrowHistJapEntity findBorrowHistByBorrower(UserJpaEntity borrower);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.jpa.repository.Query;
import site.billbill.apiserver.model.chat.ChatChannelJpaEntity;
import site.billbill.apiserver.model.post.ItemsJpaEntity;
import site.billbill.apiserver.model.user.UserJpaEntity;

public interface ChatRepository extends JpaRepository<ChatChannelJpaEntity,String> {
// List<ChatChannelJpaEntity> findByItemAndCloYnFalseAndDelYnFalse(ItemsJpaEntity item);
Expand All @@ -17,4 +18,11 @@ public interface ChatRepository extends JpaRepository<ChatChannelJpaEntity,Strin
"AND c.startedAt = :startDate " +
"AND c.endedAt = :endDate")
List<ChatChannelJpaEntity> findByItemAndStartAndEndDate(ItemsJpaEntity item, LocalDate startDate, LocalDate endDate);

@Query("SELECT c FROM ChatChannelJpaEntity c "+
"WHERE c.item = :item " +
"AND c.delYn = false " +
"And c.contact = :user "
)
List<ChatChannelJpaEntity> findAllByItemAndContactUser(ItemsJpaEntity item, UserJpaEntity user);
}

0 comments on commit 693461b

Please sign in to comment.