Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] 댓글 생성 api #669

Merged
merged 4 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions gg-data/src/main/java/gg/data/party/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,28 @@ public class Comment extends BaseTimeEntity {
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
@JoinColumn(name = "user_id", nullable = false)
private User user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_room_id")
@JoinColumn(name = "user_room_id", nullable = false)
private UserRoom userRoom;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "room_id")
@JoinColumn(name = "room_id", nullable = false)
private Room room;

@Column(name = "content", length = 100)
@Column(name = "content", length = 100, nullable = false)
private String content;

@Column(name = "is_hidden")
@Column(name = "is_hidden", nullable = false)
private Boolean isHidden;

public Comment(User user, UserRoom userRoom, Room room, String content) {
this.user = user;
this.userRoom = userRoom;
this.room = room;
this.content = content;
this.isHidden = false;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
package gg.pingpong.api.party.user.comment.controller;
package gg.party.api.user.comment.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
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 gg.party.api.user.comment.controller.request.CommentCreateReqDto;
import gg.party.api.user.comment.service.CommentService;
import gg.pingpong.api.global.utils.argumentresolver.Login;
import gg.pingpong.api.user.user.dto.UserDto;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/party/rooms/{roomId}/comments")
public class CommentController {

private final CommentService commentService;

/**
* 댓글 생성
* @param reqDto 댓글 정보
* @param roomId 방 번호
* @return 생성 성공 여부
*/
@PostMapping
public ResponseEntity<Void> createComment(@PathVariable Long roomId, @RequestBody CommentCreateReqDto reqDto,
@Login UserDto user) {
commentService.createComment(roomId, reqDto, user.getId());
return new ResponseEntity<>(HttpStatus.CREATED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gg.party.api.user.comment.controller.request;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class CommentCreateReqDto {
private String content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package gg.party.api.user.comment.service;

import java.time.LocalDateTime;

import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import gg.data.party.Comment;
import gg.data.party.Room;
import gg.data.party.UserRoom;
import gg.data.user.User;
import gg.party.api.user.comment.controller.request.CommentCreateReqDto;
import gg.repo.party.CommentRepository;
import gg.repo.party.RoomRepository;
import gg.repo.party.UserRoomRepository;
import gg.repo.user.UserRepository;
import gg.utils.exception.ErrorCode;
import gg.utils.exception.party.CommentNotValidException;
import gg.utils.exception.party.RoomNotFoundException;
import gg.utils.exception.party.RoomUpdateException;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class CommentService {
private final CommentRepository commentRepository;
private final RoomRepository roomRepository;
private final UserRepository userRepository;
private final UserRoomRepository userRoomRepository;

/**
* 댓글 생성
* @param roomId 방 번호
* @param reqDto 댓글 정보
*/
@Transactional
public void createComment(Long roomId, CommentCreateReqDto reqDto, Long userId) {
Room room = roomRepository.findById(roomId)
.orElseThrow(() -> new RoomNotFoundException(ErrorCode.ROOM_NOT_FOUND));
if (LocalDateTime.now().isAfter(room.getDueDate())) {
throw new RoomUpdateException(ErrorCode.ROOM_FINISHED);
}
if (reqDto.getContent().length() > 100) {
throw new CommentNotValidException(ErrorCode.COMMENT_TOO_LONG);
}
AreSain marked this conversation as resolved.
Show resolved Hide resolved
User user = userRepository.findById(userId).get();
UserRoom userRoom = userRoomRepository.findByUserAndRoom(user, room)
.orElseThrow(() -> new RoomUpdateException(ErrorCode.USER_NOT_IN_ROOM));
if (!userRoom.getIsExist()) {
throw new RoomUpdateException(ErrorCode.USER_NOT_IN_ROOM);
}
AreSain marked this conversation as resolved.
Show resolved Hide resolved
Comment comment = new Comment(user, userRoom, room, reqDto.getContent());
commentRepository.save(comment);
}

}
9 changes: 9 additions & 0 deletions gg-repo/src/main/java/gg/repo/party/CommentRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gg.repo.party;

import org.springframework.data.jpa.repository.JpaRepository;

import gg.data.party.Comment;

public interface CommentRepository extends JpaRepository<Comment, Long> {

}
5 changes: 4 additions & 1 deletion gg-repo/src/main/java/gg/repo/party/UserRoomRepository.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package gg.repo.party;

import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import gg.data.party.Room;
import gg.data.party.UserRoom;
import gg.data.party.type.RoomType;
import gg.data.user.User;
import io.lettuce.core.dynamic.annotation.Param;

public interface UserRoomRepository extends JpaRepository<UserRoom, Long> {
List<UserRoom> findByUserId(Long userId);

Optional<UserRoom> findByUserAndRoom(User user, Room room);

@Query("SELECT ur.room FROM UserRoom ur WHERE ur.user.id = :userId AND ur.isExist = true "
+ "AND ur.room.status = :status AND ur.room.dueDate IS NOT NULL ORDER BY ur.room.dueDate ASC")
List<Room> findFinishRoomsByUserId(@Param("userId") Long userId, @Param("status") RoomType status);
Expand Down
8 changes: 6 additions & 2 deletions gg-utils/src/main/java/gg/utils/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,12 @@ public enum ErrorCode {
TOURNAMENT_GAME_EXCEED(500, "TN016", "토너먼트 게임 최대 사이즈를 초과하였습니다!"),
TOURNAMENT_IS_BEFORE(403, "TN017", "before인 토너먼트에서 점수 수정할 수 없습니다."),

// PARTY
CATEGORY_NOT_FOUND(404, "PT101", "유효하지 않은 카테고리 입니다.");
// Party
COMMENT_TOO_LONG(400, "PT201", "댓글은 100자 이하로 작성해 주세요."),
ROOM_FINISHED(403, "PT501", "마감된 방입니다."),
CATEGORY_NOT_FOUND(404, "PT101", "유효하지 않은 카테고리입니다."),
ROOM_NOT_FOUND(404, "PT102", "존재하지 않는 방입니다."),
USER_NOT_IN_ROOM(404, "PT103", "방에 존재하지 않는 유저입니다.");

private final int status;
private final String errCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gg.utils.exception.party;

import gg.utils.exception.ErrorCode;
import gg.utils.exception.custom.InvalidParameterException;

public class CommentNotValidException extends InvalidParameterException {
public CommentNotValidException(ErrorCode errorCode) {
super(errorCode.getMessage(), errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gg.utils.exception.party;

import gg.utils.exception.ErrorCode;
import gg.utils.exception.custom.NotExistException;

public class RoomNotFoundException extends NotExistException {
public RoomNotFoundException(ErrorCode errorCode) {
super(errorCode.getMessage(), errorCode);
}
}
AreSain marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gg.utils.exception.party;

import gg.utils.exception.ErrorCode;
import gg.utils.exception.custom.ForbiddenException;

public class RoomUpdateException extends ForbiddenException {
public RoomUpdateException(ErrorCode errorCode) {
super(errorCode.getMessage(), errorCode);
}
}
Loading