Skip to content

Commit

Permalink
Merge pull request #78 from billbill-project/feature/borrowPosts
Browse files Browse the repository at this point in the history
Feature/borrow posts
  • Loading branch information
jainefer authored Dec 14, 2024
2 parents 9fad5fb + 693461b commit 73daafe
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 9 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 @@ -123,6 +122,26 @@ public BaseResponse<String> updatePostController(@PathVariable(value="postId",re
return new BaseResponse<>(postsService.UpdatePostService(postId,userId,request));
}

@Operation(summary="리뷰작성",description = "리뷰 작성")
@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 All @@ -109,6 +120,22 @@ public static PostsResponse.saveSearchListResponse toUserSearhList(List<PostsRes
public static String toRecommandSearch(SearchKeywordStatsJpaEntity searchKeywordStats){
return searchKeywordStats.getKeyword();
}
public static ItemsReviewJpaEntity toItemsReview(UserJpaEntity user,ItemsJpaEntity item,PostsRequest.ReviewRequest request ,String reviewId){
return ItemsReviewJpaEntity.builder()
.id(reviewId)
.items(item)
.user(user)
.rating(request.getRating())
.content(request.getContent())
.build();

}
public static PostsResponse.ReviewIdResponse toReviewIdResponse(ItemsJpaEntity item, ItemsReviewJpaEntity review){
return PostsResponse.ReviewIdResponse.builder()
.itemId(item.getId())
.reviewId(review.getId())
.build();
}


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

@Getter
@Setter
@Builder
public static class ReviewRequest{
@Schema(description = "평점", example = "1~5 사이로 입력해주셔야합니다. 그 외엔 오류 처리")
private int rating;
private String content;
}
}
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 @@ -73,4 +72,19 @@ public static class NoRentalPeriodResponse{
private String startDate;
private String endDate;
}
@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public interface PostsService {

PostsResponse.saveSearchListResponse findSearchService(String userId);

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 @@ -37,6 +39,9 @@ public class PostsServiceImpl implements PostsService {
private final ItemsCategoryRepository itemsCategoryRepository;
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 @@ -80,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 @@ -104,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 @@ -194,6 +199,60 @@ public List<String> findRecommandService(){
List<String> result =searchKeywordStats.stream().map(searchKeywordStat-> PostsConverter.toRecommandSearch(searchKeywordStat)).toList();
return result;
}

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);
}
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);

}












//모듈화 코드

private Pageable createPageable(int page, Sort.Direction direction, String orderType) {
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
@@ -0,0 +1,8 @@
package site.billbill.apiserver.repository.borrowPosts;

import org.springframework.data.jpa.repository.JpaRepository;
import site.billbill.apiserver.model.post.ItemsReviewJpaEntity;

public interface ItemsReivewRepository extends JpaRepository<ItemsReviewJpaEntity,String> {

}
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 73daafe

Please sign in to comment.