diff --git a/src/main/java/com/example/helloworldmvc/converter/ReportConverter.java b/src/main/java/com/example/helloworldmvc/converter/ReportConverter.java new file mode 100644 index 0000000..20daef6 --- /dev/null +++ b/src/main/java/com/example/helloworldmvc/converter/ReportConverter.java @@ -0,0 +1,19 @@ +package com.example.helloworldmvc.converter; + +import com.example.helloworldmvc.domain.CommunityReport; +import com.example.helloworldmvc.domain.enums.ReportStatus; +import com.example.helloworldmvc.domain.enums.ReportType; +import com.example.helloworldmvc.web.dto.ReportResDTO; + +public class ReportConverter { + public static CommunityReport toReport(Long communityId, ReportStatus reportStatus) { + return CommunityReport.builder() + .reportStatus(reportStatus) + .build(); + } + public static ReportResDTO.ReportRes toReportResDTO(CommunityReport communityReport) { + return ReportResDTO.ReportRes.builder() + .communityId(communityReport.getCommunityId()) + .build(); + } +} diff --git a/src/main/java/com/example/helloworldmvc/domain/CommunityReport.java b/src/main/java/com/example/helloworldmvc/domain/CommunityReport.java new file mode 100644 index 0000000..138efb3 --- /dev/null +++ b/src/main/java/com/example/helloworldmvc/domain/CommunityReport.java @@ -0,0 +1,38 @@ +package com.example.helloworldmvc.domain; + +import com.example.helloworldmvc.domain.common.BaseEntity; +import com.example.helloworldmvc.domain.enums.ReportStatus; +import com.example.helloworldmvc.domain.enums.ReportType; +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@Builder +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class CommunityReport extends BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false) + private Long communityId; + + @Enumerated(EnumType.STRING) + @Column(nullable = false) + private ReportStatus reportStatus; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id") + private User user; + + public void setUser(User user) { + if(this.user != null){ + user.getCommunityReportList().remove(this); + } + this.user = user; + user.getCommunityReportList().add(this); + } + +} diff --git a/src/main/java/com/example/helloworldmvc/domain/User.java b/src/main/java/com/example/helloworldmvc/domain/User.java index 821d019..8e2e46b 100644 --- a/src/main/java/com/example/helloworldmvc/domain/User.java +++ b/src/main/java/com/example/helloworldmvc/domain/User.java @@ -9,7 +9,6 @@ import jakarta.validation.constraints.NotNull; import lombok.*; -import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -62,6 +61,9 @@ public class User extends BaseEntity { @OneToMany(mappedBy = "user", cascade = CascadeType.ALL) private List communityList = new ArrayList<>(); + @OneToMany(mappedBy = "user", cascade = CascadeType.ALL) + private List communityReportList = new ArrayList<>(); + public User update(String name) { this.name = name; diff --git a/src/main/java/com/example/helloworldmvc/domain/enums/ReportStatus.java b/src/main/java/com/example/helloworldmvc/domain/enums/ReportStatus.java new file mode 100644 index 0000000..9d397da --- /dev/null +++ b/src/main/java/com/example/helloworldmvc/domain/enums/ReportStatus.java @@ -0,0 +1,5 @@ +package com.example.helloworldmvc.domain.enums; + +public enum ReportStatus { + PENDING, APPROVED, REJECTED +} diff --git a/src/main/java/com/example/helloworldmvc/domain/enums/ReportType.java b/src/main/java/com/example/helloworldmvc/domain/enums/ReportType.java new file mode 100644 index 0000000..cb52909 --- /dev/null +++ b/src/main/java/com/example/helloworldmvc/domain/enums/ReportType.java @@ -0,0 +1,5 @@ +package com.example.helloworldmvc.domain.enums; + +public enum ReportType { + USER, COMMUNITY +} diff --git a/src/main/java/com/example/helloworldmvc/repository/ReportRepository.java b/src/main/java/com/example/helloworldmvc/repository/ReportRepository.java new file mode 100644 index 0000000..a2e1554 --- /dev/null +++ b/src/main/java/com/example/helloworldmvc/repository/ReportRepository.java @@ -0,0 +1,7 @@ +package com.example.helloworldmvc.repository; + +import com.example.helloworldmvc.domain.CommunityReport; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ReportRepository extends JpaRepository { +} diff --git a/src/main/java/com/example/helloworldmvc/service/ReportService.java b/src/main/java/com/example/helloworldmvc/service/ReportService.java new file mode 100644 index 0000000..ce82712 --- /dev/null +++ b/src/main/java/com/example/helloworldmvc/service/ReportService.java @@ -0,0 +1,9 @@ +package com.example.helloworldmvc.service; + + +import com.example.helloworldmvc.web.dto.ReportResDTO; + +public interface ReportService { + public ReportResDTO.ReportRes createReport(String userId, Long communityId); + +} diff --git a/src/main/java/com/example/helloworldmvc/service/ReportServiceImpl.java b/src/main/java/com/example/helloworldmvc/service/ReportServiceImpl.java new file mode 100644 index 0000000..02e4bcf --- /dev/null +++ b/src/main/java/com/example/helloworldmvc/service/ReportServiceImpl.java @@ -0,0 +1,32 @@ +package com.example.helloworldmvc.service; + +import com.example.helloworldmvc.apiPayload.GeneralException; +import com.example.helloworldmvc.apiPayload.code.status.ErrorStatus; +import com.example.helloworldmvc.converter.ReportConverter; +import com.example.helloworldmvc.domain.CommunityReport; +import com.example.helloworldmvc.domain.User; +import com.example.helloworldmvc.domain.enums.ReportStatus; +import com.example.helloworldmvc.repository.ReportRepository; +import com.example.helloworldmvc.repository.UserRepository; +import com.example.helloworldmvc.web.dto.ReportResDTO; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +@Transactional +public class ReportServiceImpl implements ReportService { + private final UserRepository userRepository; + private final ReportRepository reportRepository; + @Override + public ReportResDTO.ReportRes createReport(String userId, Long communityId) { + User user = userRepository.findByEmail(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND)); + + userRepository.findById(communityId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND)); + CommunityReport report = ReportConverter.toReport(communityId, ReportStatus.PENDING); + report.setUser(user); + CommunityReport save = reportRepository.save(report); + return ReportConverter.toReportResDTO(save); + } +} diff --git a/src/main/java/com/example/helloworldmvc/web/controller/ReportController.java b/src/main/java/com/example/helloworldmvc/web/controller/ReportController.java new file mode 100644 index 0000000..625375f --- /dev/null +++ b/src/main/java/com/example/helloworldmvc/web/controller/ReportController.java @@ -0,0 +1,42 @@ +package com.example.helloworldmvc.web.controller; + +import com.example.helloworldmvc.apiPayload.ApiResponse; +import com.example.helloworldmvc.config.auth.JwtTokenProvider; +import com.example.helloworldmvc.service.ReportService; +import com.example.helloworldmvc.web.dto.CommentRequestDTO; +import com.example.helloworldmvc.web.dto.CommentResponseDTO; +import com.example.helloworldmvc.web.dto.ReportResDTO; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.Parameters; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +@SecurityRequirement(name = "JWT Token") +@RestController +@RequiredArgsConstructor +@RequestMapping("/report") +public class ReportController { + private final JwtTokenProvider jwtTokenProvider; + private final ReportService reportService; + + @PostMapping(value = "/community/{community_id}") + @Operation(summary = "커뮤니티 신고 API", description = "커뮤니티 신고 API입니다.") + @ApiResponses({ + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"), + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER4001", description = "사용자를 찾을수 없습니다."), + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMUNITY4001", description = "커뮤니티 글이 존재하지 않습니다.") + }) + @Parameters({ + @Parameter(name = "Authorization", description = "RequestHeader - 로그인한 사용자 토큰"), + @Parameter(name = "community_id ", description = "PathVariable - 커뮤니티 글 아이디"), + }) + public ApiResponse createCommunityReport(@RequestHeader(name = "Authorization") String accessToken, + @PathVariable(name = "community_id") Long targetId){ + String gmail = jwtTokenProvider.getGoogleEmail(accessToken); + return ApiResponse.onSuccess(reportService.createReport(gmail, targetId)); + } +} diff --git a/src/main/java/com/example/helloworldmvc/web/dto/ReportResDTO.java b/src/main/java/com/example/helloworldmvc/web/dto/ReportResDTO.java new file mode 100644 index 0000000..4ce4b70 --- /dev/null +++ b/src/main/java/com/example/helloworldmvc/web/dto/ReportResDTO.java @@ -0,0 +1,18 @@ +package com.example.helloworldmvc.web.dto; + +import com.example.helloworldmvc.domain.enums.ReportStatus; +import com.example.helloworldmvc.domain.enums.ReportType; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +public class ReportResDTO { + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class ReportRes{ + Long communityId; + } +}