Skip to content
Open
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ out/

### VS Code ###
.vscode/

### Security ###
/src/main/resources/application-aws.properties
/src/main/resources/application.properties
27 changes: 27 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,41 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.jsonwebtoken:jjwt-api:0.12.3' //jwt
implementation 'io.jsonwebtoken:jjwt-impl:0.12.3' //jwt
implementation 'io.jsonwebtoken:jjwt-jackson:0.12.3' //jwt
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"

//test 용 데이터베이스
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'com.h2database:h2'

//swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.7.0'

//s3
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
}

tasks.named('test') {
useJUnitPlatform()
}

clean {
delete file('src/main/generated')
}

2 changes: 2 additions & 0 deletions src/main/java/apptive/devlog/DevlogApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@SpringBootApplication
@EnableJpaAuditing
public class DevlogApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package apptive.devlog.comment.controller;

import apptive.devlog.comment.dto.CommentRequest;
import apptive.devlog.comment.dto.CommentResponse;
import apptive.devlog.comment.service.CommentService;
import apptive.devlog.member.dto.MemberDetails;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/users/me")
public class CommentController {

private final CommentService commentService;

@PostMapping("/post/{id}/comment")
public ResponseEntity<CommentResponse> createComment(@Valid @RequestBody CommentRequest comment,
@PathVariable Long id,
@AuthenticationPrincipal MemberDetails member) {
CommentResponse response = commentService.saveComment(comment, id, member.getUsername());


return ResponseEntity.status(HttpStatus.CREATED).body(response);
}

@PostMapping("/post/{postId}/comment/{commentId}")
public ResponseEntity<CommentResponse> createReComment(@Valid @RequestBody CommentRequest comment,
@PathVariable Long postId, @PathVariable Long commentId,
@AuthenticationPrincipal MemberDetails member) {
CommentResponse response = commentService.saveReComment(comment, postId, commentId, member.getUsername());

return ResponseEntity.status(HttpStatus.CREATED).body(response);
}

@DeleteMapping("/comment/{id}")
public ResponseEntity<Map<String, String>> deleteComment(@PathVariable Long id,
@AuthenticationPrincipal MemberDetails member) {
commentService.deleteComment(id, member.getUsername());

return ResponseEntity.status(HttpStatus.NO_CONTENT).body(Map.of("message", "댓글이 삭제되었습니다."));
}


@PatchMapping("/comment/{id}")
public ResponseEntity<Map<String, String>> updateComment(@Valid @RequestBody CommentRequest request, @PathVariable Long id,
@AuthenticationPrincipal MemberDetails member) {
commentService.updateComment(request, id, member.getUsername());

return ResponseEntity.status(HttpStatus.OK).body(Map.of("message", "댓글이 수정되었습니다"));
}
}
18 changes: 18 additions & 0 deletions src/main/java/apptive/devlog/comment/dto/CommentRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package apptive.devlog.comment.dto;

import jakarta.validation.constraints.NotBlank;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CommentRequest {

@NotBlank
private String content;


}
49 changes: 49 additions & 0 deletions src/main/java/apptive/devlog/comment/dto/CommentResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package apptive.devlog.comment.dto;


import apptive.devlog.domain.Comment;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class CommentResponse {

private Long id;
private String author;
private String content;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private List<ReCommentResponse> reComments = new ArrayList<>();
private Boolean isDeleted;



public CommentResponse(String author, Comment comment, List<ReCommentResponse> reComments) {
this.author = author;
this.id = comment.getId();
this.content = comment.getContent();
this.createdAt = comment.getCreatedAt();
this.updatedAt = comment.getUpdatedAt();
this.isDeleted = comment.isDeleted();
this.reComments = reComments;
}

public CommentResponse(String author, Comment comment) {
this.author = author;
this.id = comment.getId();
this.content = comment.getContent();
this.createdAt = comment.getCreatedAt();
this.updatedAt = comment.getUpdatedAt();
this.isDeleted = comment.isDeleted();
}

}

22 changes: 22 additions & 0 deletions src/main/java/apptive/devlog/comment/dto/ReCommentResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package apptive.devlog.comment.dto;

import apptive.devlog.domain.Comment;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class ReCommentResponse {

private Long id;
private String author;
private String content;
private Long parentId;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package apptive.devlog.comment.exception;

public class BadCommentRequestException extends RuntimeException {
public BadCommentRequestException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package apptive.devlog.comment.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Map;

@RestControllerAdvice
public class CommentExceptionHandler {

@ExceptionHandler(BadCommentRequestException.class)
public ResponseEntity<Map<String,String>> handleBadComment(BadCommentRequestException ex) {

return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(Map.of("message", ex.getMessage()));
}

@ExceptionHandler(NotFoundCommentException.class)
public ResponseEntity<Map<String,String>> handleNotFoundComment(NotFoundCommentException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Map.of("message", ex.getMessage()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package apptive.devlog.comment.exception;

public class NotFoundCommentException extends RuntimeException {
public NotFoundCommentException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package apptive.devlog.comment.repository;

import apptive.devlog.comment.dto.ReCommentResponse;
import apptive.devlog.domain.Comment;
import apptive.devlog.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.time.LocalDateTime;
import java.util.List;

public interface CommentRepository extends JpaRepository<Comment, Long> {

@Query("select count (c) from Comment c where c.parent = :parent")
long countChild(Comment parent);

@Query("select new apptive.devlog.comment.dto.ReCommentResponse " +
"(c.id, c.member.nickname, c.content,c.parent.id, c.createdAt, c.updatedAt) from Comment c where c.parent in :parents")
List<ReCommentResponse> findReComments(List<Comment> parents);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package apptive.devlog.comment.repository;


import apptive.devlog.domain.Comment;
import apptive.devlog.domain.QComment;
import apptive.devlog.domain.QMember;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

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

import static apptive.devlog.domain.QComment.*;
import static apptive.devlog.domain.QMember.*;

@Repository
public class QCommentRepository {

private final JPAQueryFactory queryFactory;

public QCommentRepository(EntityManager em) {
queryFactory = new JPAQueryFactory(em);
}

public Page<Comment> findParentComment(Long id, Pageable pageable) {
List<Comment> comments = queryFactory
.selectFrom(comment)
.join(comment.member, member).fetchJoin()
.where(comment.post.id.eq(id).and(comment.parent.id.isNull()))
.orderBy(comment.createdAt.asc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

long count = Optional.ofNullable(queryFactory
.select(comment.count())
.from(comment)
.where(comment.post.id.eq(id).and(comment.parent.id.isNull()))
.fetchOne()).orElse(0L);

return new PageImpl<>(comments, pageable, count);
}
}
Loading