Skip to content

Commit

Permalink
Merge pull request #46 from Team-Shaka/feat/43
Browse files Browse the repository at this point in the history
✨ Feat : 게시글 신고 API 구현
  • Loading branch information
CYY1007 authored Jun 1, 2024
2 parents af5cd6e + 03dce95 commit 54e88c1
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/treehouse/server/api/post/business/PostMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import treehouse.server.global.entity.member.Member;
import treehouse.server.global.entity.post.Post;
import treehouse.server.global.entity.post.PostImage;
import treehouse.server.global.entity.report.Report;
import treehouse.server.global.entity.treeHouse.TreeHouse;
import treehouse.server.global.feign.dto.PresignedUrlDTO;

Expand Down Expand Up @@ -66,4 +67,13 @@ public static PostResponseDTO.updatePostResult toUpdatePostResult(Post post) {
.postId(post.getId())
.build();
}

public static Report toReport(PostRequestDTO.reportPost request, Post post, Member reporter, Member target){
return Report.builder()
.reason(request.getReason())
.post(post)
.reporterMember(reporter)
.targetMember(target)
.build();
}
}
29 changes: 29 additions & 0 deletions src/main/java/treehouse/server/api/post/business/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
import treehouse.server.api.post.implement.PostQueryAdapter;
import treehouse.server.api.post.presentation.dto.PostRequestDTO;
import treehouse.server.api.post.presentation.dto.PostResponseDTO;
import treehouse.server.api.report.implementation.ReportCommandAdapter;
import treehouse.server.api.treehouse.implementation.TreehouseQueryAdapter;
import treehouse.server.global.constants.Consts;
import treehouse.server.global.entity.User.User;
import treehouse.server.global.entity.member.Member;
import treehouse.server.global.entity.post.Post;
import treehouse.server.global.entity.post.PostImage;
import treehouse.server.global.entity.report.Report;
import treehouse.server.global.entity.treeHouse.TreeHouse;
import treehouse.server.global.exception.GlobalErrorCode;
import treehouse.server.global.exception.ThrowClass.PostException;
Expand All @@ -45,6 +47,8 @@ public class PostService {

private final TreehouseQueryAdapter treehouseQueryAdapter;

private final ReportCommandAdapter reportCommandAdapter;

private final PresignedUrlLambdaClient presignedUrlLambdaClient;

/**
Expand Down Expand Up @@ -153,4 +157,29 @@ public void deletePost(User user, Long treehouseId, Long postId) {

postCommandAdapter.deletePost(post);
}

/**
* 게시글 신고하기
* @param user
* @param treehouseId
* @param postId
* @param request
*/
@Transactional
public void reportPost(User user, Long treehouseId, Long postId,PostRequestDTO.reportPost request) {

TreeHouse treehouse = treehouseQueryAdapter.getTreehouseById(treehouseId);
Member reporter = memberQueryAdapter.findByUserAndTreehouse(user, treehouse);

Post post = postQueryAdapter.findById(postId);

Member target = post.getWriter();

if (reporter.equals(target))
throw new PostException(GlobalErrorCode.POST_SELF_REPORT);

Report report = PostMapper.toReport(request, post, reporter, target);

reportCommandAdapter.createReport(report);
}
}
13 changes: 13 additions & 0 deletions src/main/java/treehouse/server/api/post/presentation/PostApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,17 @@ public CommonResponse deletePost(
postService.deletePost(user, treehouseId, postId);
return CommonResponse.onSuccess(null);
}


@PostMapping("/posts/{postId}/reports")
@Operation(summary = "게시글 신고 ✅ 🔑", description = "게시글을 신고합니다.")
public CommonResponse reportPost(
@PathVariable(name = "treehouseId") Long treehouseId,
@PathVariable(name = "postId") Long postId,
@RequestBody @Validated PostRequestDTO.reportPost request,
@AuthMember @Parameter(hidden = true) User user
){
postService.reportPost(user,treehouseId,postId,request);
return CommonResponse.onSuccess(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,18 @@ public static class updatePost{
@NotBlank(message = "게시글 내용이 필요합니다.")
private String context;
}

@Getter
public static class reportPost{

@JsonProperty("reason")
@Schema(description = "신고 사유", example = "부적절한 게시글")
@NotBlank(message = "게시글 신고 사유가 필요합니다.")
private String reason;

@JsonProperty("targetMemberId")
@Schema(description = "작성자 멤버 아이디", example = "1")
@NotNull(message = "작성자 멤버 아이디는 필수입니다.")
private Long targetMemberId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package treehouse.server.api.report.implementation;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import treehouse.server.api.report.persistent.ReportRepository;
import treehouse.server.global.annotations.Adapter;
import treehouse.server.global.entity.report.Report;

@Adapter
@RequiredArgsConstructor
@Slf4j
public class ReportCommandAdapter {

private final ReportRepository reportRepository;

public Report createReport(Report report){
return reportRepository.save(report);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package treehouse.server.api.report.persistent;

import org.springframework.data.jpa.repository.JpaRepository;
import treehouse.server.global.entity.report.Report;

public interface ReportRepository extends JpaRepository<Report, Long> {
}
44 changes: 44 additions & 0 deletions src/main/java/treehouse/server/global/entity/report/Report.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package treehouse.server.global.entity.report;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import treehouse.server.global.entity.comment.Comment;
import treehouse.server.global.entity.member.Member;
import treehouse.server.global.entity.post.Post;

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Report {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String reason;

@JoinColumn(name = "reporterId")
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
private Member reporterMember;

@JoinColumn(name = "targetId")
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
private Member targetMember;

@JoinColumn(name = "postId")
@ManyToOne(fetch = FetchType.LAZY)
private Post post;

@JoinColumn(name = "commentId")
@ManyToOne(fetch = FetchType.LAZY)
private Comment comment;

public void setReporterMember(Member reporterMember) {
this.reporterMember = reporterMember;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public enum GlobalErrorCode implements BaseErrorCode{

// POST + 401 Unauthorized - 권한 없음
POST_UNAUTHORIZED(UNAUTHORIZED, "POST401_1", "게시글 수정 및 삭제 권한이 없습니다."),

// POST + 403 Forbidden - 금지됨
POST_SELF_REPORT(FORBIDDEN, "POSt403_1", "자신의 게시글은 신고할 수 없습니다."),

// POST + 404 Not Found - 찾을 수 없음
POST_NOT_FOUND(NOT_FOUND, "POST404_1", "존재하지 않는 게시글입니다."),

Expand Down

0 comments on commit 54e88c1

Please sign in to comment.