Skip to content

Commit

Permalink
Merge pull request #21 from goormthon-Univ/feature/11
Browse files Browse the repository at this point in the history
[Feat] 댓글 기능 구현
  • Loading branch information
Ahyun0326 committed Mar 20, 2024
2 parents 787b026 + be9df95 commit 92881f2
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.beotkkot.qtudy.controller.comment;

import com.beotkkot.qtudy.dto.request.comments.CommentsRequestDto;
import com.beotkkot.qtudy.dto.response.comments.CommentsResponseDto;
import com.beotkkot.qtudy.dto.response.comments.GetCommentsAllResponseDto;
import com.beotkkot.qtudy.dto.response.posts.PostsResponseDto;
import com.beotkkot.qtudy.service.auth.AuthService;
import com.beotkkot.qtudy.service.comments.CommentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@Slf4j
public class CommentController {

private final CommentService commentService;
private final AuthService authService;

// 전체 댓글 조회
@GetMapping("/posts/comments/all")
public ResponseEntity<? super GetCommentsAllResponseDto> getAllComment(@RequestParam("postId") Long postId, @RequestParam("page") int page) {
ResponseEntity<? super GetCommentsAllResponseDto> response = commentService.getAllComment(postId, page);
return response;
}

// 댓글 저장
@PostMapping("/posts/comments")
public ResponseEntity<? super CommentsResponseDto> save(@RequestParam("postId") Long postId, @RequestHeader("Authorization") String token, @RequestBody CommentsRequestDto requestDto) {

Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null) {
return PostsResponseDto.noAuthentication();
}
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}

ResponseEntity<? super CommentsResponseDto> response = commentService.saveComment(postId, kakao_uid, requestDto);
return response;
}

// 댓글 수정
@PatchMapping("/posts/comments")
public ResponseEntity<? super CommentsResponseDto> patchComment(@RequestParam("postId") Long postId, @RequestParam("commentId") Long commentId,
@RequestHeader("Authorization") String token, @RequestBody CommentsRequestDto requestDto) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null) {
return PostsResponseDto.noAuthentication();
}
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}

ResponseEntity<? super CommentsResponseDto> response = commentService.patchComment(postId, commentId, kakao_uid, requestDto);
return response;
}

// 댓글 삭제
@DeleteMapping("/posts/comments")
public ResponseEntity<? super CommentsResponseDto> deleteComment(@RequestParam("postId") Long postId, @RequestParam("commentId") Long commentId, @RequestHeader("Authorization") String token) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null) {
return PostsResponseDto.noAuthentication();
}
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}

ResponseEntity<? super CommentsResponseDto> response = commentService.deleteComment(postId, commentId, kakao_uid);
return response;
}
}
20 changes: 13 additions & 7 deletions src/main/java/com/beotkkot/qtudy/domain/comments/Comments.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.beotkkot.qtudy.domain.comments;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import jakarta.persistence.*;
import lombok.*;

@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
Expand All @@ -17,5 +14,14 @@ public class Comments {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long commentId;

@Column(nullable = false)
private Long postId;

@Column(nullable = false)
private Long userUid;

@Column(columnDefinition = "TEXT")
private String content;

private String createdAt;
}
26 changes: 26 additions & 0 deletions src/main/java/com/beotkkot/qtudy/dto/object/CommentListItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.beotkkot.qtudy.dto.object;

import com.beotkkot.qtudy.domain.comments.Comments;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class CommentListItem {
private Long commentId;
private String content;
private String createdAt;

public static CommentListItem of(Comments comment) {

return CommentListItem.builder()
.commentId(comment.getCommentId())
.content(comment.getContent())
.createdAt(comment.getCreatedAt())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.beotkkot.qtudy.dto.request.comments;

import com.beotkkot.qtudy.domain.comments.Comments;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;

@NoArgsConstructor
@Getter
public class CommentsRequestDto {
private String content;
private Long userUid;

public Comments toEntity(Long postId) {
Date now = Date.from(Instant.now());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String writeDatetime = simpleDateFormat.format(now);

return Comments.builder()
.userUid(userUid)
.postId(postId)
.content(content)
.createdAt(writeDatetime)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.beotkkot.qtudy.dto.response.comments;

import com.beotkkot.qtudy.common.ResponseCode;
import com.beotkkot.qtudy.common.ResponseMessage;
import com.beotkkot.qtudy.dto.response.ResponseDto;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@Getter
public class CommentsResponseDto extends ResponseDto{
public CommentsResponseDto() {
super(ResponseCode.SUCCESS, ResponseMessage.SUCCESS);
}

public static ResponseEntity<CommentsResponseDto> success() {
CommentsResponseDto result = new CommentsResponseDto();
return ResponseEntity.status(HttpStatus.OK).body(result);
}

public static ResponseEntity<ResponseDto> notExistedPost(){
ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_POST, ResponseMessage.NOT_EXISTED_POST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
}

public static ResponseEntity<ResponseDto> notExistedUser() {
ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_USER, ResponseMessage.NOT_EXISTED_USER);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
}

public static ResponseEntity<? super CommentsResponseDto> notExistedComment() {
ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_COMMENT, ResponseMessage.NOT_EXISTED_COMMENT);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.beotkkot.qtudy.dto.response.comments;

import com.beotkkot.qtudy.common.ResponseCode;
import com.beotkkot.qtudy.common.ResponseMessage;
import com.beotkkot.qtudy.dto.object.CommentListItem;
import com.beotkkot.qtudy.dto.response.ResponseDto;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.List;


@Getter
public class GetCommentsAllResponseDto extends ResponseDto {
private int page;
private List<CommentListItem> commentList;

public GetCommentsAllResponseDto(List<CommentListItem> commentListItem, int page) {
super(ResponseCode.SUCCESS, ResponseMessage.SUCCESS);
this.page = page;
this.commentList = commentListItem;
}

public static ResponseEntity<GetCommentsAllResponseDto> success(List<CommentListItem> commentListItem, int page) {
GetCommentsAllResponseDto result = new GetCommentsAllResponseDto(commentListItem, page);
return ResponseEntity.status(HttpStatus.OK).body(result);
}

public static ResponseEntity<ResponseDto> notExistedPost(){
ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_POST, ResponseMessage.NOT_EXISTED_POST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
}
}



Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.beotkkot.qtudy.repository.comments;

import com.beotkkot.qtudy.domain.comments.Comments;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional;

public interface CommentsRepository extends JpaRepository<Comments, Long> {
// int countByPostId(Long postid);

// @Transactional
// void deleteByPostId(Long postId);

Page<Comments> findAllByPostId(Long postId, Pageable pageable);

int countByPostId(Long postid);

@Transactional
void deleteByPostId(Long postId);
}
Loading

0 comments on commit 92881f2

Please sign in to comment.