Skip to content

Commit

Permalink
[feat] 리뷰작성
Browse files Browse the repository at this point in the history
  • Loading branch information
jainefer committed Dec 6, 2024
1 parent e634146 commit 0d6b5a4
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ 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));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,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 @@ -73,4 +73,11 @@ public static class NoRentalPeriodResponse{
private String startDate;
private String endDate;
}
@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,7 @@ public interface PostsService {

PostsResponse.saveSearchListResponse findSearchService(String userId);

PostsResponse.ReviewIdResponse DoReviewPostService(String postId,String userId,PostsRequest.ReviewRequest request);
List<String> findRecommandService();

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class PostsServiceImpl implements PostsService {
private final ItemsCategoryRepository itemsCategoryRepository;
private final UserSearchHistRepository userSearchHistRepository;
private final SearchKeywordStatRepository searchKeywordStatRepository;
private final ItemsReivewRepository itemsReivewRepository;
public PostsResponse.UploadResponse uploadPostService(PostsRequest.UploadRequest request,String userId){
//먼저 item 생성,
Optional<UserJpaEntity> isUser=userRepository.findById(userId);
Expand Down Expand Up @@ -194,6 +195,25 @@ 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);
String postsId = ULIDUtil.generatorULID("REVIEW");
if (item == null) {
throw new CustomException(ErrorCode.BadRequest, "올바른 게시물 아이디가 아닙니다.", HttpStatus.BAD_REQUEST);
}else if(request.getRating()>=6||request.getRating()<=0){
throw new CustomException(ErrorCode.BadRequest, "평점이 올바르지 않습니다. 1~5 사이로 입력해주셔야합니다.", HttpStatus.BAD_REQUEST);
}
ItemsReviewJpaEntity review=PostsConverter.toItemsReview(user,item,request,postsId);
itemsReivewRepository.save(review);

return PostsConverter.toReviewIdResponse(item,review);
}




//모듈화 코드

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,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> {

}

0 comments on commit 0d6b5a4

Please sign in to comment.