Skip to content

Commit

Permalink
Merge pull request #36 from YAPP-Github/feature/PC-272-api-report
Browse files Browse the repository at this point in the history
[PC-272] 유저 신고 기능 구현
  • Loading branch information
devchlee12 authored Jan 28, 2025
2 parents bd0ee93 + 48443f7 commit db8e128
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.yapp.domain.report.application;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.yapp.domain.report.Report;
import org.yapp.domain.report.dao.ReportRepository;
import org.yapp.domain.report.dto.request.UserReportRequest;
import org.yapp.domain.user.User;
import org.yapp.domain.user.application.UserService;

@Service
@RequiredArgsConstructor
public class ReportService {

private final ReportRepository reportRepository;
private final UserService userService;

/**
* 유저 신고 기능
*
* @param reporterId 신고하는 유저의 아이디
* @param userReportRequest 신고 요청
* @return 신고 객체
*/
public Report reportUser(Long reporterId, UserReportRequest userReportRequest) {
User reporter = userService.getUserById(reporterId);
User reportedUser = userService.getUserById(userReportRequest.getReportedUserId());
Report report = Report.builder().reporter(reporter).reportedUser(reportedUser)
.reason(userReportRequest.getReason()).build();
return reportRepository.save(report);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.yapp.domain.report.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yapp.domain.report.application.ReportService;
import org.yapp.domain.report.dto.request.UserReportRequest;
import org.yapp.util.CommonResponse;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/reports")
public class ReportController {

public final ReportService reportService;

@PostMapping()
@Operation(summary = "유저 신고", description = "유저를 신고합니다.", tags = {
"신고"})
@ApiResponse(responseCode = "200", description = "유저를 성공적으로 신고하였습니다.")
public ResponseEntity<CommonResponse<Void>> reportUser(@AuthenticationPrincipal Long userId,
@RequestBody @Valid UserReportRequest request) {
reportService.reportUser(userId, request);
return ResponseEntity.ok(CommonResponse.createSuccessWithNoContent());
}
}
10 changes: 10 additions & 0 deletions api/src/main/java/org/yapp/domain/report/dao/ReportRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.yapp.domain.report.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.yapp.domain.report.Report;
import org.yapp.domain.user.User;

public interface ReportRepository extends JpaRepository<Report, Long> {

boolean existsReportByReporterAndReportedUser(User reporter, User reportedUser);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.yapp.domain.report.dto.request;

import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;

@NoArgsConstructor
@Getter
public class UserReportRequest {

@NotNull
private Long reportedUserId;
@NotNull
@Length(min = 1, max = 100)
private String reason;
}
15 changes: 15 additions & 0 deletions common/src/main/java/org/yapp/error/dto/ReportErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.yapp.error.dto;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

@Getter
@RequiredArgsConstructor
public enum ReportErrorCode implements ErrorCode {
ALREADY_REPORTED(HttpStatus.BAD_REQUEST, "이미 신고한 유저입니다."),
;

private final HttpStatus httpStatus;
private final String message;
}

0 comments on commit db8e128

Please sign in to comment.