Skip to content

Commit

Permalink
Merge pull request #53 from BokDoong/feat/report
Browse files Browse the repository at this point in the history
feat: "신고 api완료"
  • Loading branch information
yuntasha authored Nov 15, 2023
2 parents c9aaaa6 + 4a2f398 commit 714e6a4
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum CommunityCode {

POST_NOT_FOUND("CM-001"),
EXISTED_POST("CM-002"),
EXISTED_REPORT("CM-003"),
;

private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public enum ErrorCode {
// COMMUNITY
POST_NOT_FOUND(CommunityCode.POST_NOT_FOUND.getCode(), NOT_FOUND, "존재하지 않는 게시글"),
EXISTED_POST(CommunityCode.EXISTED_POST.getCode(), CONFLICT, "해당 동화에 대한 게시글이 이미 존재할 때"),
EXISTED_REPORT(CommunityCode.EXISTED_REPORT.getCode(), CONFLICT, "이미 신고한 경우")
;

private final String code;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package hanium.englishfairytale.report.controller;

import hanium.englishfairytale.report.dto.PostReportQueryDto;
import hanium.englishfairytale.report.dto.UserReportQueryDto;
import hanium.englishfairytale.report.service.PostReportService;
import hanium.englishfairytale.report.service.UserReportService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/community/report")
@RequiredArgsConstructor
public class ReportController {
private final PostReportService postReportService;
private final UserReportService userReportService;

@PostMapping("post")
public void postReport(@Validated @RequestPart PostReportQueryDto postReportQueryDto) {
postReportService.report(postReportQueryDto);
}
@PostMapping("user")
public void userReport(@Validated @RequestPart UserReportQueryDto userReportQueryDto){
userReportService.report(userReportQueryDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package hanium.englishfairytale.report.dto;

import lombok.Data;

import javax.validation.constraints.NotNull;

@Data
public class PostReportQueryDto {
@NotNull
private Long taleId;
@NotNull
private Long memberId;

public PostReportQueryDto(Long taleId, Long memberId) {
this.taleId = taleId;
this.memberId = memberId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package hanium.englishfairytale.report.dto;

import lombok.Data;

import javax.validation.constraints.NotNull;
@Data
public class UserReportQueryDto {
@NotNull
private Long reportedId;
@NotNull
private Long reporterId;

public UserReportQueryDto(Long reportedId, Long reporterId) {
this.reportedId = reportedId;
this.reporterId = reporterId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package hanium.englishfairytale.report.entity;

import hanium.englishfairytale.member.domain.Member;
import hanium.englishfairytale.tale.domain.Tale;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.time.LocalDateTime;

@Entity
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class PostReport {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "tale_id")
private Tale tale;
@OneToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "reporter_id")
private Member member;
@Column(name = "created_date")
private LocalDateTime createdTime;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package hanium.englishfairytale.report.entity;

import hanium.englishfairytale.member.domain.Member;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.time.LocalDateTime;

@Entity
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class UserReport {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "reported_id")
private Member reported;
@OneToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "reporter_id")
private Member reporter;
@Column(name = "created_date")
private LocalDateTime createdTime;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package hanium.englishfairytale.report.repository;

import hanium.englishfairytale.member.domain.Member;
import hanium.englishfairytale.report.entity.PostReport;
import hanium.englishfairytale.tale.domain.Tale;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface PostReportRepository extends JpaRepository<PostReport, Long> {
// public Long save(PostReport postReport);
Optional<PostReport> findByMemberAndTale(Member member, Tale tale);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package hanium.englishfairytale.report.repository;

import hanium.englishfairytale.report.entity.UserReport;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserReportRepository extends JpaRepository<UserReport, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package hanium.englishfairytale.report.service;

import hanium.englishfairytale.member.domain.Member;
import hanium.englishfairytale.report.dto.PostReportQueryDto;
import hanium.englishfairytale.tale.domain.Tale;

public interface PostReportService {
void report(PostReportQueryDto postReportQueryDto);
void createPostReport(Member member, Tale tale);
Member verifyAndGetMember(Long member_id);
Tale verifyAndGetTale(Long tale_id);
void verifyExistedMember(Member member, Tale tale);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package hanium.englishfairytale.report.service;

import hanium.englishfairytale.exception.BusinessException;
import hanium.englishfairytale.exception.NotFoundException;
import hanium.englishfairytale.exception.code.ErrorCode;
import hanium.englishfairytale.member.domain.Member;
import hanium.englishfairytale.member.domain.MemberRepository;
import hanium.englishfairytale.report.dto.PostReportQueryDto;
import hanium.englishfairytale.report.entity.PostReport;
import hanium.englishfairytale.report.repository.PostReportRepository;
import hanium.englishfairytale.tale.domain.Tale;
import hanium.englishfairytale.tale.domain.TaleRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class PostReportServiceImpl implements PostReportService{
private final PostReportRepository postReportRepository;
private final MemberRepository memberRepository;
private final TaleRepository taleRepository;


@Override
@Transactional
public void report(PostReportQueryDto postReportQueryDto){ // 더 해야할 것 자기자신 신고 예외 처리, 포스트 되지 않은 게시물 예외처리
Member member = verifyAndGetMember(postReportQueryDto.getMemberId());
Tale tale = verifyAndGetTale(postReportQueryDto.getTaleId());
verifyExistedMember(member, tale);
createPostReport(member, tale);
}
@Override
@Transactional
public void createPostReport(Member member, Tale tale) {
PostReport postReport = PostReport.builder()
.id(1L)
.member(member)
.tale(tale)
.build();
postReportRepository.save(postReport);
}

@Override
@Transactional
public Member verifyAndGetMember(Long member_id) {
return memberRepository.findMemberById(member_id).orElseThrow(() -> new NotFoundException(ErrorCode.MEMBER_NOT_FOUND));
}
@Override
@Transactional
public Tale verifyAndGetTale(Long tale_id) {
return taleRepository.findTaleByTaleId(tale_id).orElseThrow(() -> new NotFoundException(ErrorCode.TALE_NOT_FOUND));
}

@Override
@Transactional
public void verifyExistedMember(Member member, Tale tale) {
if ( postReportRepository.findByMemberAndTale(member, tale).isPresent()) {
throw new BusinessException(ErrorCode.EXISTED_REPORT);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package hanium.englishfairytale.report.service;

import hanium.englishfairytale.member.domain.Member;
import hanium.englishfairytale.report.dto.UserReportQueryDto;

public interface UserReportService {
void report(UserReportQueryDto userReportQueryDto);
Member getMember(Long member_id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package hanium.englishfairytale.report.service;

import hanium.englishfairytale.exception.NotFoundException;
import hanium.englishfairytale.exception.code.ErrorCode;
import hanium.englishfairytale.member.domain.Member;
import hanium.englishfairytale.member.domain.MemberRepository;
import hanium.englishfairytale.report.dto.UserReportQueryDto;
import hanium.englishfairytale.report.entity.UserReport;
import hanium.englishfairytale.report.repository.UserReportRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class UserReportServiceImpl implements UserReportService {
private final UserReportRepository userReportRepository;
private final MemberRepository memberRepository;

@Override
@Transactional
public void report(UserReportQueryDto userReportQueryDto) {
Member reporterUser = getMember(userReportQueryDto.getReporterId());
Member reportedUser = getMember(userReportQueryDto.getReportedId());

UserReport userReport = UserReport.builder()
.reported(reportedUser)
.reporter(reporterUser)
.build();
userReportRepository.save(userReport);
}
@Override
@Transactional
public Member getMember(Long member_id) {
return memberRepository.findMemberById(member_id).orElseThrow(() -> new NotFoundException(ErrorCode.MEMBER_NOT_FOUND));
}
}

0 comments on commit 714e6a4

Please sign in to comment.