Skip to content

Commit

Permalink
Merge pull request #29 from gaaji/GM-257
Browse files Browse the repository at this point in the history
Gm 257
  • Loading branch information
ansm6358 authored Feb 22, 2023
2 parents a27b3db + 6e9747d commit 3125640
Show file tree
Hide file tree
Showing 18 changed files with 212 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.gaaji.auth.exception.EqualsSellerAndPurchaserException;
import com.gaaji.auth.exception.NoMatchIdException;
import com.gaaji.auth.exception.NoReviewException;
import com.gaaji.auth.exception.NoTownException;
import com.gaaji.auth.exception.NonexistentTargetException;
import com.gaaji.auth.repository.ReviewRepository;

Expand All @@ -42,6 +43,10 @@ private void saveEntity(Review review) {
}

private Review createReviewEntity(ReviewCreateRequest dto, MultipartFile multipartFile, String authId) {
if(dto.getTown() == null) {
throw new NoTownException();
}

if((dto.getPurchaserId() == null) || dto.getSellerId() == null) {
throw new NonexistentTargetException();
}
Expand All @@ -60,12 +65,12 @@ private Review createReviewEntity(ReviewCreateRequest dto, MultipartFile multipa
if (dto.getPurchaserId().equals(authId)) {
return Review.of(ReviewId.of(this.reviewRepository.nextId()), PostId.of(dto.getPostId()), AuthId.of(authId),
AuthId.of(dto.getSellerId()), goodManners, badManners,
Comment.of(pictureUrl, dto.getContents(), true));
Comment.of(pictureUrl, dto.getContents(), dto.getTown(), true));

} else if (dto.getSellerId().equals(authId)) {
return Review.of(ReviewId.of(this.reviewRepository.nextId()), PostId.of(dto.getPostId()), AuthId.of(authId),
AuthId.of(dto.getPurchaserId()), goodManners, badManners,
Comment.of(pictureUrl, dto.getContents(), false));
Comment.of(pictureUrl, dto.getContents(), dto.getTown(), false));
} else {
throw new NoMatchIdException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.gaaji.auth.applicationservice;

import java.util.List;

import com.gaaji.auth.controller.dto.CommentRetrieveResponse;
import com.gaaji.auth.controller.dto.ReviewRetrieveResponse;

public interface ReviewRetriveService {

ReviewRetrieveResponse retriveMyReview(String authId, String postId);

List<CommentRetrieveResponse> retriveComment(String authId);

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.gaaji.auth.applicationservice;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gaaji.auth.controller.dto.CommentRetrieveResponse;
import com.gaaji.auth.controller.dto.ReviewRetrieveResponse;
import com.gaaji.auth.domain.Auth;
import com.gaaji.auth.domain.AuthId;
import com.gaaji.auth.domain.PostId;
import com.gaaji.auth.domain.Review;
import com.gaaji.auth.exception.NoSearchReviewException;
import com.gaaji.auth.repository.AuthRepository;
import com.gaaji.auth.repository.ReviewRepository;

import lombok.RequiredArgsConstructor;
Expand All @@ -18,12 +25,30 @@
public class ReviewRetriveServiceImpl implements ReviewRetriveService{

private final ReviewRepository reviewRepository;
private final AuthRepository authRepository;

@Override
public ReviewRetrieveResponse retriveMyReview(String authId, String postId) {
Review review = this.reviewRepository.findByPostIdAndSenderId(PostId.of(postId), AuthId.of(authId)).orElseThrow(NoSearchReviewException::new);
return ReviewRetrieveResponse.of(review.getReviewId().getId(), review.getComment().getContents(), review.getComment().getPictureUrl(), review.getGoodManners(), review.getBadManners());
}

@Override
public List<CommentRetrieveResponse> retriveComment(String authId) {
List<Review> reviewList = this.reviewRepository.findByReceiverIdAndComment_ContentsIsNotNullOrderByComment_CreatedAtDesc(AuthId.of(authId));
List<CommentRetrieveResponse> commentList = new ArrayList<CommentRetrieveResponse>();
for(Review review : reviewList) {

Auth auth = this.authRepository.findById(review.getSenderId().getId()).orElse(Auth.signUp(null, null, null));
if(auth.getAuthIdForToken()==null) {
auth.registerNickname(null);
}
commentList.add(CommentRetrieveResponse.of(review.getSenderId().getId(), auth.getNickname(), auth.getProfilePictureUrl(), review.getComment().getTown(), review.getComment().getContents(), review.getComment().getPictureUrl(), review.getComment().isIspurchaser(), review.getComment().getCreatedAt()));

}

return commentList;
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.gaaji.auth.controller;

import java.util.List;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -9,6 +11,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gaaji.auth.applicationservice.ReviewRetriveService;
import com.gaaji.auth.controller.dto.CommentRetrieveResponse;
import com.gaaji.auth.controller.dto.ReviewRetrieveResponse;
import com.gaaji.auth.domain.Review;

Expand All @@ -22,8 +25,15 @@ public class ReviewRetriveController {
private final ReviewRetriveService reviewRetriveService;

@GetMapping("/my")
private ResponseEntity<ReviewRetrieveResponse> createReview(@RequestHeader(HttpHeaders.AUTHORIZATION) String authId, @RequestBody String postId) {
private ResponseEntity<ReviewRetrieveResponse> retriveMyReview(@RequestHeader(HttpHeaders.AUTHORIZATION) String authId, @RequestBody String postId) {
ReviewRetrieveResponse dto = this.reviewRetriveService.retriveMyReview(authId, postId);
return ResponseEntity.ok(dto);
}

@GetMapping("/comment")
private ResponseEntity<List<CommentRetrieveResponse>> retriveComments(@RequestHeader(HttpHeaders.AUTHORIZATION) String authId, @RequestBody String postId) {
List<CommentRetrieveResponse> dto = this.reviewRetriveService.retriveComment(authId);
return ResponseEntity.ok(dto);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.gaaji.auth.controller.dto;

import java.time.LocalDateTime;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor
public class CommentRetrieveResponse {

private String senderId;
private String nickname;
private String profilePictureUrl;
private String town;
private String contents;
private String pictureUrl;
private boolean ispurchaser;
private LocalDateTime createdAt;

public static CommentRetrieveResponse of(String senderId, String nickname, String profilePictureUrl, String town, String contents, String pictureUrl, boolean ispurchaser, LocalDateTime createdAt) {
return new CommentRetrieveResponse(senderId, nickname, profilePictureUrl, town, contents, pictureUrl, ispurchaser, createdAt);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class ReviewCreateRequest {
private String postId;
private String sellerId;
private String purchaserId;
private String town;
private List<String> goodManners;
private List<String> badManners;
private String contents;
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/gaaji/auth/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ public class Comment {

private String pictureUrl;
private String contents;
private String town;
private boolean ispurchaser;
private LocalDateTime createdAt;

public Comment(String pictureUrl, String contents, boolean ispurchaser) {
public Comment(String pictureUrl, String contents, String town, boolean ispurchaser) {
this.pictureUrl = pictureUrl;
this.contents = contents;
this.ispurchaser = ispurchaser;
this.town = town;
this.createdAt = LocalDateTime.now();
}

public static Comment of(String pictureUrl, String contents, boolean ispurchaser) {
return new Comment(pictureUrl, contents, ispurchaser);
public static Comment of(String pictureUrl, String contents, String town, boolean ispurchaser) {
return new Comment(pictureUrl, contents, town, ispurchaser);

}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/gaaji/auth/domain/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static Review of(ReviewId reviewId, PostId postId, AuthId senderId, AuthI
public void modify(String pictureUrl, List<GoodManner> goodManners, List<BadManner> badManners, String contents) {
this.goodManners = goodManners;
this.badManners = badManners;
this.comment = Comment.of(pictureUrl, contents, this.comment.isIspurchaser());
this.comment = Comment.of(pictureUrl, contents, this.comment.getTown(), this.comment.isIspurchaser());
}


Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/gaaji/auth/exception/AuthErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum AuthErrorCode implements ErrorCode{
Equals_Seller_And_Purchaser(HttpStatus.BAD_REQUEST, "r-0006","판매자와 구매자가 동일합니다."),
No_Search_Review(HttpStatus.BAD_REQUEST, "r-0007","해당 후기를 찾지 못했습니다."),
No_Match_Sender(HttpStatus.BAD_REQUEST, "r-0008","해당 후기 작성자와 정보가 맞지 않습니다."),
No_Town(HttpStatus.BAD_REQUEST, "r-0009","해당 지역을 알 수 없습니다."),
;

private final HttpStatus httpStatus;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/gaaji/auth/exception/NoTownException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.gaaji.auth.exception;

import static com.gaaji.auth.exception.AuthErrorCode.No_Town;

public class NoTownException extends AbstractApiException {

public NoTownException() {
super(No_Town);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gaaji.auth.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -13,4 +14,6 @@ public interface JpaReviewRepository extends JpaRepository<Review, ReviewId>{

Optional<Review> findByPostIdAndSenderId(PostId postId, AuthId senderId);

List<Review> findByReceiverIdAndComment_ContentsIsNotNullOrderByComment_CreatedAtDesc(AuthId receiverId);

}
3 changes: 3 additions & 0 deletions src/main/java/com/gaaji/auth/repository/ReviewRepository.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gaaji.auth.repository;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

Expand All @@ -20,4 +21,6 @@ default String nextId(){

Optional<Review> findByPostIdAndSenderId(PostId postId, AuthId senderId);

List<Review> findByReceiverIdAndComment_ContentsIsNotNullOrderByComment_CreatedAtDesc(AuthId receiverId);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gaaji.auth.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.stereotype.Repository;
Expand Down Expand Up @@ -32,4 +33,9 @@ public Optional<Review> findByPostIdAndSenderId(PostId postId, AuthId senderId)
return this.jpaReviewRepository.findByPostIdAndSenderId(postId, senderId);
}

@Override
public List<Review> findByReceiverIdAndComment_ContentsIsNotNullOrderByComment_CreatedAtDesc(AuthId receiverId) {
return this.jpaReviewRepository.findByReceiverIdAndComment_ContentsIsNotNullOrderByComment_CreatedAtDesc(receiverId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void afterEach() {
good.add(GoodManner.gm1);
List<BadManner> bad = new ArrayList<BadManner>();
bad.add(BadManner.bm2);
Review review = Review.of(ReviewId.of("review"), PostId.of("post"), AuthId.of("sender"), AuthId.of("receiver"), good, bad, Comment.of("사진", "내용", true));
Review review = Review.of(ReviewId.of("review"), PostId.of("post"), AuthId.of("sender"), AuthId.of("receiver"), good, bad, Comment.of("사진", "내용", "남가좌동", true));

this.jpaReviewRepository.save(review);

Expand All @@ -51,6 +51,7 @@ void afterEach() {

assertThat(newReview.getComment().getPictureUrl()).isEqualTo("사진");
assertThat(newReview.getComment().getContents()).isEqualTo("내용");
assertThat(newReview.getComment().getTown()).isEqualTo("남가좌동");
assertThat(newReview.getComment().isIspurchaser()).isEqualTo(true);

}
Expand Down
36 changes: 34 additions & 2 deletions src/test/java/com/gaaji/auth/repository/ReviewRetriveJpaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ void afterEach() {
}

@Test
void 조회테스트() {
void 내조회테스트() {
List<GoodManner> good = new ArrayList<GoodManner>();
good.add(GoodManner.gm1);
List<BadManner> bad = new ArrayList<BadManner>();
bad.add(BadManner.bm2);
Review review = Review.of(ReviewId.of("review"), PostId.of("post"), AuthId.of("sender"), AuthId.of("receiver"), good, bad, Comment.of("사진", "내용", true));
Review review = Review.of(ReviewId.of("review"), PostId.of("post"), AuthId.of("sender"), AuthId.of("receiver"), good, bad, Comment.of("사진", "내용", "남가좌동", true));

this.jpaReviewRepository.save(review);

Expand All @@ -52,6 +52,38 @@ void afterEach() {

assertThat(newReview.getComment().getPictureUrl()).isEqualTo("사진");
assertThat(newReview.getComment().getContents()).isEqualTo("내용");
assertThat(newReview.getComment().getTown()).isEqualTo("남가좌동");
assertThat(newReview.getComment().isIspurchaser()).isEqualTo(true);

}

@Test
void 후기조회테스트() {
List<GoodManner> good = new ArrayList<GoodManner>();
good.add(GoodManner.gm1);
List<BadManner> bad = new ArrayList<BadManner>();
bad.add(BadManner.bm2);
Review review = Review.of(ReviewId.of("review"), PostId.of("post"), AuthId.of("sender"), AuthId.of("receiver"), good, bad, Comment.of("사진", "내용", "남가좌동", true));
Review review1 = Review.of(ReviewId.of("review1"), PostId.of("post1"), AuthId.of("sender"), AuthId.of("receiver"), good, bad, Comment.of("사진", null, "남가좌동", true));

this.jpaReviewRepository.save(review);
this.jpaReviewRepository.save(review1);

List<Review> reviewList = this.jpaReviewRepository.findByReceiverIdAndComment_ContentsIsNotNullOrderByComment_CreatedAtDesc(AuthId.of("receiver"));

assertThat(reviewList.size()).isEqualTo(1);
Review newReview = reviewList.get(0);
assertThat(newReview.getReviewId().getId()).isEqualTo("review");
assertThat(newReview.getPostId().getId()).isEqualTo("post");

assertThat(newReview.getSenderId().getId()).isEqualTo("sender");
assertThat(newReview.getReceiverId().getId()).isEqualTo("receiver");
assertThat(newReview.getGoodManners().get(0)).isEqualTo(GoodManner.gm1);
assertThat(newReview.getBadManners().get(0)).isEqualTo(BadManner.bm2);

assertThat(newReview.getComment().getPictureUrl()).isEqualTo("사진");
assertThat(newReview.getComment().getContents()).isEqualTo("내용");
assertThat(newReview.getComment().getTown()).isEqualTo("남가좌동");
assertThat(newReview.getComment().isIspurchaser()).isEqualTo(true);

}
Expand Down
Loading

0 comments on commit 3125640

Please sign in to comment.