Skip to content

Commit

Permalink
Merge pull request #48 from dnd-side-project/feature/#42
Browse files Browse the repository at this point in the history
피드백 작성자가 피드백 제출 시 예외처리 #42
  • Loading branch information
woo0doo authored Feb 24, 2024
2 parents c363cc3 + 65333da commit a6556ad
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.hibernate.annotations.SQLDelete;

import java.time.LocalDate;
import java.util.Objects;

@Entity
@Getter
Expand All @@ -33,6 +34,7 @@ public class Feedback extends BaseEntity {
private Project project;
private String title;
private String linkUrl;
@Column(length = 500)
private String content;
private String rewardMessage;
private LocalDate startedAt;
Expand Down Expand Up @@ -78,4 +80,8 @@ public void updateIsFinished() {
this.isFinished = true;
}

public boolean isAuthor(final User user) {
return Objects.equals(this.user, user);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
public enum FeedbackExceptionType implements ExceptionType {

NOT_FOUND_FEEDBACK(3000, "요청한 피드백을 찾을 수 없습니다."),
DUPLICATE_FEEDBACK_SUBMIT(3010, "중복된 유저가 피드백을 제출했습니다.");
DUPLICATE_FEEDBACK_SUBMIT(3010, "중복된 유저가 피드백을 제출했습니다."),
REJECT_SUBMIT_FEEDBACK_BY_AUTHOR(3020, "작성자 본인은 본인 피드백에 제출할 수 없습니다.");
private final int statusCode;
private final String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

import java.util.List;

import static com.sendback.domain.feedback.exception.FeedbackExceptionType.DUPLICATE_FEEDBACK_SUBMIT;
import static com.sendback.domain.feedback.exception.FeedbackExceptionType.NOT_FOUND_FEEDBACK;
import static com.sendback.domain.feedback.exception.FeedbackExceptionType.*;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -89,6 +88,8 @@ public SubmitFeedbackResponseDto submitFeedback(Long userId, Long feedbackId, Mu
User loginUser = userService.getUserById(userId);
Feedback feedback = getFeedback(feedbackId);

validateIsAuthor(loginUser, feedback);

validateAlreadySubmit(loginUser, feedback);

String screenShotUrl = imageService.uploadImage(file, "feedback");
Expand All @@ -102,6 +103,12 @@ public SubmitFeedbackResponseDto submitFeedback(Long userId, Long feedbackId, Mu
return new SubmitFeedbackResponseDto(loginUser.getLevel().getName(), isLevelUp, remainCount);
}

private void validateIsAuthor(User loginUser, Feedback feedback) {
boolean isAuthor = feedback.isAuthor(loginUser);
if (isAuthor)
throw new BadRequestException(REJECT_SUBMIT_FEEDBACK_BY_AUTHOR);
}

private boolean isLevelUpUserLevelBySubmitting(User user, Long submitCount) {
Level level = Level.getLevelByFeedbackSubmitCount(submitCount);
if (user.getLevel().equals(level))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public static GetProjectsResponseDto of(Project project, boolean isScrapped) {
project.getFieldName().getName(),
project.getCreatedAt(),
project.getProjectPull().getPullUpCnt(),
project.getLikes().stream().map(like -> !like.isDeleted()).count(),
project.getComments().stream().map(comment -> !comment.isDeleted()).count(),
project.getLikes().stream().filter(like -> !like.isDeleted()).count(),
project.getComments().stream().filter(comment -> !comment.isDeleted()).count(),
isScrapped
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class Project extends BaseEntity {

private String title;

@Column(length = 500)
private String content;

private String summary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
import java.util.List;
import java.util.Optional;

import static com.sendback.domain.feedback.exception.FeedbackExceptionType.DUPLICATE_FEEDBACK_SUBMIT;
import static com.sendback.domain.feedback.exception.FeedbackExceptionType.NOT_FOUND_FEEDBACK;
import static com.sendback.domain.feedback.exception.FeedbackExceptionType.*;
import static com.sendback.domain.feedback.fixture.FeedbackFixture.*;
import static com.sendback.domain.project.exception.ProjectExceptionType.NOT_PROJECT_AUTHOR;
import static com.sendback.domain.project.fixture.ProjectFixture.createDummyProject;
Expand Down Expand Up @@ -197,12 +196,27 @@ public void success() throws Exception {
@DisplayName("피드백을 제출했을 때")
class submitFeedback {

@Test
@DisplayName("작성자가 피드백을 제출하면 예외를 일으킨다.")
public void fail_isAuthor() throws Exception {
//given
given(userService.getUserById(anyLong())).willReturn(user);
given(feedbackRepository.findById(anyLong())).willReturn(Optional.of(feedback));
given(feedback.isAuthor(any(User.class))).willReturn(true);

//when - then
assertThatThrownBy(() -> feedbackService.submitFeedback(1L, 1L, mockingMultipartFile("screenShot")))
.isInstanceOf(BadRequestException.class)
.hasMessage(REJECT_SUBMIT_FEEDBACK_BY_AUTHOR.getMessage());
}

@Test
@DisplayName("이미 제출했던 유저면 예외를 발생한다.")
public void fail_alreadySubmit() throws Exception {
//given
given(userService.getUserById(anyLong())).willReturn(user);
given(feedbackRepository.findById(anyLong())).willReturn(Optional.of(feedback));
given(feedback.isAuthor(any(User.class))).willReturn(false);
given(feedbackSubmitRepository.existsByUserAndFeedbackAndIsDeletedIsFalse(any(User.class), any(Feedback.class))).willReturn(true);

//when - then
Expand All @@ -217,6 +231,7 @@ public void success_keep() throws Exception {
//given
given(userService.getUserById(anyLong())).willReturn(user);
given(feedbackRepository.findById(anyLong())).willReturn(Optional.of(feedback));
given(feedback.isAuthor(any(User.class))).willReturn(false);
given(feedbackSubmitRepository.existsByUserAndFeedbackAndIsDeletedIsFalse(any(User.class), any(Feedback.class))).willReturn(false);
given(imageService.uploadImage(any(), anyString())).willReturn("sendback1.jpg");
given(feedbackSubmitRepository.save(any(FeedbackSubmit.class))).willReturn(feedbackSubmit);
Expand All @@ -238,6 +253,7 @@ public void success_levelUp() throws Exception {
//given
given(userService.getUserById(anyLong())).willReturn(user);
given(feedbackRepository.findById(anyLong())).willReturn(Optional.of(feedback));
given(feedback.isAuthor(any(User.class))).willReturn(false);
given(feedbackSubmitRepository.existsByUserAndFeedbackAndIsDeletedIsFalse(any(User.class), any(Feedback.class))).willReturn(false);
given(imageService.uploadImage(any(), anyString())).willReturn("sendback1.jpg");
given(feedbackSubmitRepository.save(any(FeedbackSubmit.class))).willReturn(feedbackSubmit);
Expand Down

0 comments on commit a6556ad

Please sign in to comment.