Skip to content

Commit

Permalink
Merge pull request #136 from ootd-zip/feature/report
Browse files Browse the repository at this point in the history
신고항목 여러개 선택할 수 있도록 변경
  • Loading branch information
kkmin223 authored Mar 7, 2024
2 parents 2fcbb3d + ff783c0 commit c037fb0
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package zip.ootd.ootdzip.report.controller.request;

import java.util.List;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Positive;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -15,8 +18,8 @@
@AllArgsConstructor
public class ReportReq {

@Positive(message = "신고 항목 선택은 필수입니다.")
private Long reportId;
@NotEmpty(message = "신고항목은 필수입니다.")
private List<@Positive(message = "신고 항목 ID는 양수입니다.") Long> reportIds;

@Positive(message = "신고할 ootd 선택은 필수입니다.")
private Long targetId;
Expand All @@ -26,7 +29,7 @@ public class ReportReq {

public ReportSvcReq toServiceReq() {
return ReportSvcReq.builder()
.reportId(this.reportId)
.reportIds(this.reportIds)
.targetId(this.targetId)
.reportType(this.reportType)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package zip.ootd.ootdzip.report.service.request;

import java.util.List;

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

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

private final Long reportId;
private final Long targetId;
private final ReportType reportType;

@Builder
private ReportSvcReq(Long reportId, Long targetId, ReportType reportType) {
this.reportId = reportId;
this.targetId = targetId;
this.reportType = reportType;
}
private List<Long> reportIds;
private Long targetId;
private ReportType reportType;

public static ReportSvcReq of(Long reportId, Long targetId, ReportType reportType) {
public static ReportSvcReq of(List<Long> reportIds, Long targetId, ReportType reportType) {
return ReportSvcReq.builder()
.reportId(reportId)
.reportIds(reportIds)
.targetId(targetId)
.reportType(reportType)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package zip.ootd.ootdzip.report.service.strategy;

import java.util.List;

import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
Expand All @@ -26,7 +28,7 @@ public class ReportClothesStrategy implements ReportStrategy {
@Override
public ReportResultRes report(User reporter, ReportSvcReq request) {

Report report = validateReportId(request.getReportId(), reportRepository);
List<Report> reports = validateReportId(request.getReportIds(), reportRepository);

Clothes clothes = clothesRepository.findById(request.getTargetId())
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_CLOTHES_ID));
Expand All @@ -37,8 +39,10 @@ public ReportResultRes report(User reporter, ReportSvcReq request) {
throw new CustomException(ErrorCode.NOT_DUPLICATE_REPORT);
}

ReportClothes reportClothes = ReportClothes.of(report, clothes, reporter);
reportClothesRepository.save(reportClothes);
List<ReportClothes> reportClothes = reports.stream()
.map((report) -> ReportClothes.of(report, clothes, reporter))
.toList();
reportClothesRepository.saveAll(reportClothes);

clothes.increaseReportCount();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static zip.ootd.ootdzip.common.exception.code.ErrorCode.*;

import java.util.List;

import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
Expand All @@ -28,7 +30,7 @@ public class ReportCommentStrategy implements ReportStrategy {
@Override
public ReportResultRes report(User reporter, ReportSvcReq request) {

Report report = validateReportId(request.getReportId(), reportRepository);
List<Report> reports = validateReportId(request.getReportIds(), reportRepository);

Comment comment = commentRepository.findById(request.getTargetId())
.orElseThrow(() -> new CustomException(NOT_FOUNT_COMMENT_ID));
Expand All @@ -39,8 +41,10 @@ public ReportResultRes report(User reporter, ReportSvcReq request) {
throw new CustomException(ErrorCode.NOT_DUPLICATE_REPORT);
}

ReportComment reportComment = ReportComment.of(report, comment, reporter);
reportCommentRepository.save(reportComment);
List<ReportComment> reportComments = reports.stream()
.map((report) -> ReportComment.of(report, comment, reporter))
.toList();
reportCommentRepository.saveAll(reportComments);

comment.increaseReportCount();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package zip.ootd.ootdzip.report.service.strategy;

import java.util.List;

import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
Expand All @@ -26,7 +28,7 @@ public class ReportOotdStrategy implements ReportStrategy {
@Override
public ReportResultRes report(User reporter, ReportSvcReq request) {

Report report = validateReportId(request.getReportId(), reportRepository);
List<Report> reports = validateReportId(request.getReportIds(), reportRepository);

Ootd ootd = ootdRepository.findById(request.getTargetId())
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_OOTD_ID));
Expand All @@ -37,8 +39,10 @@ public ReportResultRes report(User reporter, ReportSvcReq request) {
throw new CustomException(ErrorCode.NOT_DUPLICATE_REPORT);
}

ReportOotd reportOotd = ReportOotd.of(report, ootd, reporter);
reportOotdRepository.save(reportOotd);
List<ReportOotd> reportOotds = reports.stream()
.map((report) -> ReportOotd.of(report, ootd, reporter))
.toList();
reportOotdRepository.saveAll(reportOotds);

ootd.increaseReportCount();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package zip.ootd.ootdzip.report.service.strategy;

import java.util.List;

import zip.ootd.ootdzip.common.exception.CustomException;
import zip.ootd.ootdzip.common.exception.code.ErrorCode;
import zip.ootd.ootdzip.report.controller.response.ReportResultRes;
Expand All @@ -13,13 +15,19 @@ public interface ReportStrategy {

ReportResultRes report(final User reporter, final ReportSvcReq request);

default Report validateReportId(final Long reportId, final ReportRepository reportRepository) {
return reportRepository.findById(reportId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_REPORT_ID));
default List<Report> validateReportId(final List<Long> reportIds, final ReportRepository reportRepository) {

List<Report> findReports = reportRepository.findAllById(reportIds);

if (findReports.size() != reportIds.size()) {
throw new CustomException(ErrorCode.NOT_FOUND_REPORT_ID);
}

return findReports;
}

default void checkReporterAndWriter(final User reporter, final User writer) {
if (reporter.getId().equals(writer.getId())) {
if (reporter.equals(writer)) {
throw new CustomException(ErrorCode.CANT_MY_REPORT);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void findClothesByUser() throws Exception {
@Test
void report() throws Exception {
// given
ReportReq request = new ReportReq(1L, 1L, ReportType.OOTD);
ReportReq request = new ReportReq(List.of(1L), 1L, ReportType.OOTD);

when(reportService.report(any(), any())).thenReturn(ReportResultRes.of(1L, 1));

Expand All @@ -52,11 +52,29 @@ void report() throws Exception {
.andExpect(MockMvcResultMatchers.jsonPath("$.result").exists());
}

@DisplayName("신고할 때 신고 ID는 필수이다.")
@Test
void reportWithoutReportId() throws Exception {
// given
ReportReq request = new ReportReq(null, 1L, ReportType.OOTD);

// when & then
mockMvc.perform(post("/api/v1/report")
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
)
.andDo(print())
.andExpect(status().isNotFound())
.andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(404))
.andExpect(MockMvcResultMatchers.jsonPath("$.errors[0].field").value("reportIds"))
.andExpect(MockMvcResultMatchers.jsonPath("$.errors[0].reason").value("신고항목은 필수입니다."));
}

@DisplayName("신고할 때 신고 ID는 양수이다.")
@Test
void reportWithZeroReportId() throws Exception {
// given
ReportReq request = new ReportReq(0L, 1L, ReportType.OOTD);
ReportReq request = new ReportReq(List.of(0L), 1L, ReportType.OOTD);

// when & then
mockMvc.perform(post("/api/v1/report")
Expand All @@ -66,14 +84,15 @@ void reportWithZeroReportId() throws Exception {
.andDo(print())
.andExpect(status().isNotFound())
.andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(404))
.andExpect(MockMvcResultMatchers.jsonPath("$.errors").isArray());
.andExpect(MockMvcResultMatchers.jsonPath("$.errors[0].field").value("reportIds[0]"))
.andExpect(MockMvcResultMatchers.jsonPath("$.errors[0].reason").value("신고 항목 ID는 양수입니다."));
}

@DisplayName("신고할 때 target ID는 양수이다.")
@Test
void reportWithZeroTargetId() throws Exception {
// given
ReportReq request = new ReportReq(1L, 0L, ReportType.OOTD);
ReportReq request = new ReportReq(List.of(1L), 0L, ReportType.OOTD);

// when & then
mockMvc.perform(post("/api/v1/report")
Expand All @@ -83,14 +102,15 @@ void reportWithZeroTargetId() throws Exception {
.andDo(print())
.andExpect(status().isNotFound())
.andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(404))
.andExpect(MockMvcResultMatchers.jsonPath("$.errors").isArray());
.andExpect(MockMvcResultMatchers.jsonPath("$.errors[0].field").value("targetId"))
.andExpect(MockMvcResultMatchers.jsonPath("$.errors[0].reason").value("신고할 ootd 선택은 필수입니다."));
}

@DisplayName("신고할 때 신고유형은 필수이다.")
@Test
void reportWithoutReportType() throws Exception {
// given
ReportReq request = new ReportReq(1L, 1L, null);
ReportReq request = new ReportReq(List.of(1L), 1L, null);

// when & then
mockMvc.perform(post("/api/v1/report")
Expand All @@ -100,7 +120,8 @@ void reportWithoutReportType() throws Exception {
.andDo(print())
.andExpect(status().isNotFound())
.andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(404))
.andExpect(MockMvcResultMatchers.jsonPath("$.errors").isArray());
.andExpect(MockMvcResultMatchers.jsonPath("$.errors[0].field").value("reportType"))
.andExpect(MockMvcResultMatchers.jsonPath("$.errors[0].reason").value("유효하지 않은 신고타입입니다."));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void reportOotd() {

Report report = createReportBy("신고항목1");

ReportSvcReq request = ReportSvcReq.of(report.getId(), savedOotd.getId(), ReportType.OOTD);
ReportSvcReq request = ReportSvcReq.of(List.of(report.getId()), savedOotd.getId(), ReportType.OOTD);

// when & then
ReportResultRes result = reportService.report(request, reportUser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void reportClothes() {

Clothes clothes = createClothesBy(user, true, "1");

ReportSvcReq request = ReportSvcReq.of(report.getId(), clothes.getId(), ReportType.CLOTHES);
ReportSvcReq request = ReportSvcReq.of(List.of(report.getId()), clothes.getId(), ReportType.CLOTHES);

// when
ReportResultRes result = reportClothesStrategy.report(reporter, request);
Expand All @@ -93,7 +93,7 @@ void reportClothesWithInvalidClothesId() {
User user = createUserBy("유저1");
User reporter = createUserBy("신고자1");

ReportSvcReq request = ReportSvcReq.of(report.getId(), 1L, ReportType.CLOTHES);
ReportSvcReq request = ReportSvcReq.of(List.of(report.getId()), 1L, ReportType.CLOTHES);

// when & then
assertThatThrownBy(() -> reportClothesStrategy.report(user, request))
Expand All @@ -111,7 +111,7 @@ void reportClothesWithInvalidReportId() {

Clothes clothes = createClothesBy(user, true, "1");

ReportSvcReq request = ReportSvcReq.of(1L, clothes.getId(), ReportType.CLOTHES);
ReportSvcReq request = ReportSvcReq.of(List.of(1L), clothes.getId(), ReportType.CLOTHES);

// when & then
assertThatThrownBy(() -> reportClothesStrategy.report(user, request))
Expand All @@ -131,7 +131,7 @@ void reportClothesWithDuplicateUserAndClothes() {

Clothes clothes = createClothesBy(user, true, "1");

ReportSvcReq request = ReportSvcReq.of(report.getId(), clothes.getId(), ReportType.CLOTHES);
ReportSvcReq request = ReportSvcReq.of(List.of(report.getId()), clothes.getId(), ReportType.CLOTHES);
reportClothesStrategy.report(reporter, request);

// when & then
Expand All @@ -151,7 +151,7 @@ void reportMyClothes() {

Clothes clothes = createClothesBy(user, true, "1");

ReportSvcReq request = ReportSvcReq.of(report.getId(), clothes.getId(), ReportType.CLOTHES);
ReportSvcReq request = ReportSvcReq.of(List.of(report.getId()), clothes.getId(), ReportType.CLOTHES);

// when & then
assertThatThrownBy(() -> reportClothesStrategy.report(user, request))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.assertj.core.api.Assertions.*;

import java.util.List;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -50,7 +52,7 @@ void reportComment() {

Comment comment = createCommentBy(writer, ootd);

ReportSvcReq request = ReportSvcReq.of(report.getId(), comment.getId(), ReportType.COMMENT);
ReportSvcReq request = ReportSvcReq.of(List.of(report.getId()), comment.getId(), ReportType.COMMENT);

// when
ReportResultRes result = reportCommentStrategy.report(reporter, request);
Expand All @@ -75,7 +77,7 @@ void reportCommentWithInvalidCommentId() {
User writer = createUserBy("작성자1");
User reporter = createUserBy("신고자1");

ReportSvcReq request = ReportSvcReq.of(report.getId(), 1L, ReportType.COMMENT);
ReportSvcReq request = ReportSvcReq.of(List.of(report.getId()), 1L, ReportType.COMMENT);

//when & then
assertThatThrownBy(() -> reportCommentStrategy.report(reporter, request))
Expand All @@ -95,7 +97,7 @@ void reportCommentWithInvalidReportId() {

Comment comment = createCommentBy(writer, ootd);

ReportSvcReq request = ReportSvcReq.of(1L, comment.getId(), ReportType.COMMENT);
ReportSvcReq request = ReportSvcReq.of(List.of(1L), comment.getId(), ReportType.COMMENT);

//when & then
assertThatThrownBy(() -> reportCommentStrategy.report(reporter, request))
Expand All @@ -117,7 +119,7 @@ void reportCommentWithDuplicateUserAndComment() {

Comment comment = createCommentBy(writer, ootd);

ReportSvcReq request = ReportSvcReq.of(report.getId(), comment.getId(), ReportType.COMMENT);
ReportSvcReq request = ReportSvcReq.of(List.of(report.getId()), comment.getId(), ReportType.COMMENT);

ReportResultRes result = reportCommentStrategy.report(reporter, request);

Expand All @@ -140,7 +142,7 @@ void reportMyComment() {

Comment comment = createCommentBy(writer, ootd);

ReportSvcReq request = ReportSvcReq.of(report.getId(), comment.getId(), ReportType.COMMENT);
ReportSvcReq request = ReportSvcReq.of(List.of(report.getId()), comment.getId(), ReportType.COMMENT);

// when&then
assertThatThrownBy(() -> reportCommentStrategy.report(writer, request))
Expand Down
Loading

0 comments on commit c037fb0

Please sign in to comment.