Skip to content

Commit

Permalink
Merge pull request #77 from YogitTeam/feat/report
Browse files Browse the repository at this point in the history
Feat/report (보드, 클립보드, 코멘트 신고 구현)
  • Loading branch information
xhaktmchl authored Nov 29, 2022
2 parents a41fae8 + 33337f7 commit 595818a
Show file tree
Hide file tree
Showing 39 changed files with 838 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.yogit.server.block.controller;

import com.yogit.server.block.dto.req.CreateBlockReq;
import com.yogit.server.block.dto.res.BlockRes;
import com.yogit.server.block.service.BlockService;
import com.yogit.server.global.dto.ApplicationResponse;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
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 javax.validation.Valid;

@Slf4j
@RestController
@RequiredArgsConstructor // private final DI의존주입
@RequestMapping("/blocks")
public class BlockController {

private final BlockService blockService;

/**
* 유저 차단 생성
* @author 토마스
*/
@ApiOperation(value = "유저 차단 생성", notes = "유저 차단 생성 요청.")
@ApiResponses({
@ApiResponse(code= 201, message = "요청에 성공하였습니다."),
@ApiResponse(code= 404, message = "존재하지 않는 유저입니다."),
@ApiResponse(code = 4000 , message = "서버 오류입니다.")
})
@PostMapping
public ApplicationResponse<BlockRes> createBlock(@RequestBody @Validated CreateBlockReq dto){
return blockService.createBlock(dto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.yogit.server.block.dto.req;

import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class CreateBlockReq {

@ApiModelProperty(example = "1")
@ApiParam(value = "차단 생성하는 유저 ID", required = true)
private Long blockingUserId;

@ApiModelProperty(example = "2")
@ApiParam(value = "차단 받는 유저 ID", required = true)
private Long blockedUserId;
}
43 changes: 43 additions & 0 deletions server/src/main/java/com/yogit/server/block/dto/res/BlockRes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.yogit.server.block.dto.res;

import com.yogit.server.block.entity.Block;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class BlockRes {

@ApiModelProperty(example = "1")
@ApiParam(value = "차단 객체 ID")
private Long blockId;

@ApiModelProperty(example = "1")
@ApiParam(value = "차단 생성하는 유저 ID")
private Long blockingUserId;

@ApiModelProperty(example = "2")
@ApiParam(value = "차단 받는 유저 ID")
private Long blockedUserId;

/*
연관관계 편의 메서드
*/
@Builder
public BlockRes(Long blockId, Long blockingUserId, Long blockedUserId) {
this.blockId = blockId;
this.blockingUserId = blockingUserId;
this.blockedUserId = blockedUserId;
}

public static BlockRes toDto(Block block){
return BlockRes.builder()
.blockId(block.getId())
.blockingUserId(block.getBlockingUser().getId())
.blockedUserId(block.getBlockedUser().getId())
.build();
}
}
33 changes: 33 additions & 0 deletions server/src/main/java/com/yogit/server/block/entity/Block.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.yogit.server.block.entity;

import com.yogit.server.config.domain.BaseEntity;
import com.yogit.server.user.entity.User;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class Block extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "block_id")
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "blocking_user_id")
private User blockingUser; // 차단을 생성하는 유저

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "blocked_user_id")
private User blockedUser; // 차단을 받는 상대 유저

public Block(User blockingUser, User blockedUser) {
this.blockingUser = blockingUser;
this.blockedUser = blockedUser;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.yogit.server.block.exception;

import static com.yogit.server.block.exception.BlockExceptionList.*;

public class AlreadyBlockingException extends BlockException{

public AlreadyBlockingException(){
super(ALREADY_BLOCKING.getCODE(), ALREADY_BLOCKING.getHTTPSTATUS(), ALREADY_BLOCKING.getMESSAGE());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.yogit.server.block.exception;

import com.yogit.server.global.exception.ApplicationException;
import org.springframework.http.HttpStatus;

public class BlockException extends ApplicationException {

protected BlockException(String errorCode, HttpStatus httpStatus, String message) {
super(errorCode, httpStatus, message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.yogit.server.block.exception;

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

@Getter
@RequiredArgsConstructor
public enum BlockExceptionList {

ALREADY_BLOCKING("BL0001", HttpStatus.BAD_REQUEST, "이미 차단하였습니다.");

private final String CODE;
private final HttpStatus HTTPSTATUS;
private final String MESSAGE;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.yogit.server.block.repository;

import com.yogit.server.block.entity.Block;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface BlockRepository extends JpaRepository<Block, Long> {

@Query("select bl from Block bl where bl.blockingUser.id = :blockingUserId and bl.blockedUser.id = :blockedUserId and bl.status = 'ACTIVE'")
List<Block> findByBlockingUserIdAndBlockedUserId(@Param("blockingUserId") Long blockingUserId, @Param("blockedUserId") Long blockedUserId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.yogit.server.block.service;

import com.yogit.server.block.dto.req.CreateBlockReq;
import com.yogit.server.block.dto.res.BlockRes;
import com.yogit.server.global.dto.ApplicationResponse;

public interface BlockService {

ApplicationResponse<BlockRes> createBlock(CreateBlockReq createBlockReq);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.yogit.server.block.service;

import com.yogit.server.block.dto.req.CreateBlockReq;
import com.yogit.server.block.dto.res.BlockRes;
import com.yogit.server.block.entity.Block;
import com.yogit.server.block.exception.AlreadyBlockingException;
import com.yogit.server.block.repository.BlockRepository;
import com.yogit.server.global.dto.ApplicationResponse;
import com.yogit.server.user.entity.User;
import com.yogit.server.user.exception.NotFoundUserException;
import com.yogit.server.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class BlockServiceImpl implements BlockService{

private final BlockRepository blockRepository;
private final UserRepository userRepository;

@Override
@Transactional(readOnly = false)
public ApplicationResponse<BlockRes> createBlock(CreateBlockReq dto){

// 차단 생성하는 유저 조회
User blockingUser = userRepository.findById(dto.getBlockingUserId())
.orElseThrow(() -> new NotFoundUserException());
// 차단 받는 유저 조회
User blockedUser = userRepository.findById(dto.getBlockedUserId())
.orElseThrow(() -> new NotFoundUserException());

//Validation: 중복 차단(이미 차단했는지 검증)
//TODO: 1.차단 리파지토리로 조회 or 2.유저의 차단 리스트 참조 조회 중 좋은 방법 선택해야 됨.
if (!blockRepository.findByBlockingUserIdAndBlockedUserId(dto.getBlockingUserId(), dto.getBlockedUserId()).isEmpty()) {
throw new AlreadyBlockingException();
}

// 차단 엔티티 새성, 저장
Block block = new Block(blockingUser, blockedUser);
blockRepository.save(block);

BlockRes res = BlockRes.toDto(block);
return ApplicationResponse.create("자단하였습니다.", res);
}
}
7 changes: 7 additions & 0 deletions server/src/main/java/com/yogit/server/board/entity/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class Board extends BaseEntity {
@OneToMany(mappedBy = "board")
private List<BoardReport> boardReports;

private Integer reportedCnt;

// 생성자
@Builder
public Board(Long id, City city, User host, String title, String address, String addressDetail, float longitute, float latitude, LocalDateTime date, String notice, String introduction, String kindOfPerson, int currentMember, int totalMember, List<BoardUser> boardUsers, List<BoardImage> boardImages, Category category, List<BookMark> bookMarks, List<ClipBoard> clipBoards) {
Expand Down Expand Up @@ -121,6 +123,7 @@ public Board(CreateBoardReq dto, User host, City city, Category category){
this.category = category;
// this.bookMarks = bookMarks;
// this.clipBoards = clipBoards;
this.reportedCnt = 0;
}

/*
Expand Down Expand Up @@ -169,4 +172,8 @@ public List<String> getBoardImagesUUids(){
public void addCurrentMember(){
this.currentMember+=1;
}

public void changeReportedCnt(){
this.reportedCnt+=1;
}
}
12 changes: 12 additions & 0 deletions server/src/main/java/com/yogit/server/board/entity/ClipBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import com.yogit.server.board.dto.request.clipboard.PatchClipBoardReq;
import com.yogit.server.config.domain.BaseEntity;
import com.yogit.server.config.domain.BaseStatus;
import com.yogit.server.report.entity.ClipBoardReport;
import com.yogit.server.user.entity.User;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
Expand All @@ -37,11 +39,17 @@ public class ClipBoard extends BaseEntity {

private String content;

@OneToMany(mappedBy = "clipBoard")
private List<ClipBoardReport> clipBoardReports = new ArrayList<>();

private Integer reportedCnt;

public ClipBoard(CreateClipBoardReq dto, User user, Board board) {
this.user = user;
this.board = board;
this.title = dto.getTitle();
this.content = dto.getContent();
this.reportedCnt = 0;
}

/*
Expand All @@ -55,4 +63,8 @@ public void updateClipBoard(PatchClipBoardReq dto){
this.title = dto.getTitle();
this.content = dto.getContent();
}

public void changeReportedCnt(){
this.reportedCnt+=1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ public class Comment extends BaseEntity {
@JoinColumn(name = "clip_board_id")
private ClipBoard clipBoard;

private Integer reportedCnt;

/*
연관관계 편의 메소드
*/

public Comment(CreateCommentReq dto, User user, ClipBoard clipBoard) {
this.content = dto.getContent();
this.user = user;
this.clipBoard = clipBoard;
this.reportedCnt = 0;
}

public void deleteComment(){
Expand All @@ -47,4 +49,8 @@ public void deleteComment(){
public void updateComment(PatchCommentReq dto){
this.content = dto.getContent();
}

public void changeReportedCnt(){
this.reportedCnt +=1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ public class BoardReportController {

private final BoardReportService boardReportService;

// /**
// * 게시글 신고 생성
// * @author 토마스
// */
// @ApiOperation(value = "게시글 신고 생성", notes = "게시글 신고 생성 요청.")
// @ApiResponses({
// @ApiResponse(code= 201, message = "요청에 성공하였습니다."),
// @ApiResponse(code= 404, message = "존재하지 않는 유저입니다."),
// @ApiResponse(code= 404, message = "존재하지 않는 보드입니다."),
// @ApiResponse(code = 4000 , message = "서버 오류입니다.")
// })
// @PostMapping
// public ApplicationResponse<BoardReportRes> createBoardReport(@RequestBody @Validated CreateBoardReportReq dto){
// return boardReportService.createBoardReport(dto);
// }
/**
* 게시글 신고 생성
* @author 토마스
*/
@ApiOperation(value = "게시글 신고 생성", notes = "게시글 신고 생성 요청.")
@ApiResponses({
@ApiResponse(code= 201, message = "요청에 성공하였습니다."),
@ApiResponse(code= 404, message = "존재하지 않는 유저입니다."),
@ApiResponse(code= 404, message = "존재하지 않는 보드입니다."),
@ApiResponse(code = 4000 , message = "서버 오류입니다.")
})
@PostMapping
public ApplicationResponse<BoardReportRes> createBoardReport(@RequestBody @Validated CreateBoardReportReq dto){
return boardReportService.createBoardReport(dto);
}
}
Loading

0 comments on commit 595818a

Please sign in to comment.