Skip to content

Commit 11b4cd6

Browse files
committed
2 parents b4bb936 + 39e5301 commit 11b4cd6

File tree

14 files changed

+278
-36
lines changed

14 files changed

+278
-36
lines changed

src/main/java/com/example/log4u/domain/comment/controller/CommentController.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
import org.springframework.http.ResponseEntity;
44
import org.springframework.web.bind.annotation.DeleteMapping;
5+
import org.springframework.web.bind.annotation.GetMapping;
56
import org.springframework.web.bind.annotation.PathVariable;
67
import org.springframework.web.bind.annotation.PostMapping;
78
import org.springframework.web.bind.annotation.RequestBody;
89
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
911
import org.springframework.web.bind.annotation.RestController;
1012

13+
import com.example.log4u.common.dto.PageResponse;
1114
import com.example.log4u.domain.comment.dto.request.CommentCreateRequestDto;
1215
import com.example.log4u.domain.comment.dto.response.CommentCreateResponseDto;
16+
import com.example.log4u.domain.comment.dto.response.CommentResponseDto;
1317
import com.example.log4u.domain.comment.service.CommentService;
1418

1519
import io.swagger.v3.oas.annotations.tags.Tag;
@@ -39,4 +43,14 @@ public ResponseEntity<Void> deleteComment(@PathVariable Long commentId) {
3943
commentService.deleteComment(userId, commentId);
4044
return ResponseEntity.noContent().build();
4145
}
46+
47+
@GetMapping("/{diaryId}")
48+
public ResponseEntity<PageResponse<CommentResponseDto>> getCommentListByDiary(
49+
@PathVariable Long diaryId,
50+
@RequestParam(required = false) Long cursorCommentId,
51+
@RequestParam(defaultValue = "5") int size
52+
) {
53+
PageResponse<CommentResponseDto> response = commentService.getCommentListByDiary(diaryId, cursorCommentId, size);
54+
return ResponseEntity.ok(response);
55+
}
4256
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example.log4u.domain.comment.dto.response;
2+
3+
import com.example.log4u.domain.comment.entity.Comment;
4+
5+
public record CommentResponseDto(
6+
Long commentId,
7+
String content
8+
) {
9+
public static CommentResponseDto of(Comment comment) {
10+
return new CommentResponseDto(
11+
comment.getCommentId(),
12+
comment.getContent()
13+
);
14+
}
15+
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.example.log4u.domain.comment.repository;
22

3+
import java.time.LocalDateTime;
4+
import java.util.List;
5+
6+
import org.springframework.data.domain.Pageable;
37
import org.springframework.data.jpa.repository.JpaRepository;
8+
import org.springframework.data.jpa.repository.Query;
9+
import org.springframework.data.repository.query.Param;
410

511
import com.example.log4u.domain.comment.entity.Comment;
612

7-
public interface CommentRepository extends JpaRepository<Comment, Long> {
8-
13+
public interface CommentRepository extends JpaRepository<Comment, Long>, CommentRepositoryCustom {
914
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.log4u.domain.comment.repository;
2+
3+
import org.springframework.data.domain.Pageable;
4+
import org.springframework.data.domain.Slice;
5+
6+
import com.example.log4u.domain.comment.entity.Comment;
7+
8+
public interface CommentRepositoryCustom {
9+
10+
Slice<Comment> findByDiaryIdWithCursor(Long diaryId, Long cursorCommentId, Pageable pageable);
11+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.example.log4u.domain.comment.repository;
2+
3+
import static com.example.log4u.domain.comment.entity.QComment.*;
4+
5+
import java.util.List;
6+
7+
import org.springframework.data.domain.Pageable;
8+
import org.springframework.data.domain.Slice;
9+
import org.springframework.data.domain.SliceImpl;
10+
import org.springframework.stereotype.Repository;
11+
12+
import com.example.log4u.domain.comment.entity.Comment;
13+
import com.querydsl.jpa.impl.JPAQueryFactory;
14+
15+
import lombok.RequiredArgsConstructor;
16+
17+
@Repository
18+
@RequiredArgsConstructor
19+
public class CommentRepositoryImpl implements CommentRepositoryCustom {
20+
21+
private final JPAQueryFactory queryFactory;
22+
23+
@Override
24+
public Slice<Comment> findByDiaryIdWithCursor(Long diaryId, Long cursorCommentId, Pageable pageable) {
25+
List<Comment> result = queryFactory
26+
.selectFrom(comment)
27+
.where(
28+
comment.diaryId.eq(diaryId),
29+
cursorCommentId != null ? comment.commentId.lt(cursorCommentId) : null
30+
)
31+
.orderBy(comment.commentId.desc())
32+
.limit(pageable.getPageSize() + 1) // 커서 기반 페이징
33+
.fetch();
34+
35+
boolean hasNext = result.size() > pageable.getPageSize();
36+
List<Comment> content = hasNext ? result.subList(0, pageable.getPageSize()) : result;
37+
38+
return new SliceImpl<>(content, pageable, hasNext);
39+
}
40+
}

src/main/java/com/example/log4u/domain/comment/service/CommentService.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package com.example.log4u.domain.comment.service;
22

3+
import java.util.List;
4+
5+
import org.springframework.data.domain.PageRequest;
6+
import org.springframework.data.domain.Pageable;
7+
import org.springframework.data.domain.Slice;
8+
import org.springframework.data.domain.SliceImpl;
39
import org.springframework.stereotype.Service;
410
import org.springframework.transaction.annotation.Transactional;
511

12+
import com.example.log4u.common.dto.PageResponse;
613
import com.example.log4u.domain.comment.dto.request.CommentCreateRequestDto;
714
import com.example.log4u.domain.comment.dto.response.CommentCreateResponseDto;
15+
import com.example.log4u.domain.comment.dto.response.CommentResponseDto;
816
import com.example.log4u.domain.comment.entity.Comment;
917
import com.example.log4u.domain.comment.exception.NotFoundCommentException;
1018
import com.example.log4u.domain.comment.exception.UnauthorizedAccessException;
@@ -22,7 +30,7 @@ public class CommentService {
2230

2331
@Transactional
2432
public CommentCreateResponseDto addComment(Long userId, CommentCreateRequestDto requestDto) {
25-
checkDiaryExists(requestDto);
33+
checkDiaryExists(requestDto.diaryId());
2634
Comment comment = requestDto.toEntity(userId);
2735
commentRepository.save(comment);
2836
return CommentCreateResponseDto.of(comment);
@@ -35,8 +43,8 @@ public void deleteComment(Long userId, Long commentId) {
3543
commentRepository.delete(comment);
3644
}
3745

38-
private void checkDiaryExists(CommentCreateRequestDto requestDto) {
39-
diaryService.checkDiaryExists(requestDto.diaryId());
46+
private void checkDiaryExists(Long diaryId) {
47+
diaryService.checkDiaryExists(diaryId);
4048
}
4149

4250
private void validateCommentOwner(Long userId, Comment comment) {
@@ -49,4 +57,18 @@ private Comment getComment(Long commentId) {
4957
return commentRepository.findById(commentId)
5058
.orElseThrow(NotFoundCommentException::new);
5159
}
60+
61+
@Transactional(readOnly = true)
62+
public PageResponse<CommentResponseDto> getCommentListByDiary(Long diaryId, Long cursorCommentId, int size) {
63+
checkDiaryExists(diaryId);
64+
Pageable pageable = PageRequest.of(0, size);
65+
Slice<Comment> slice = commentRepository.findByDiaryIdWithCursor(diaryId, cursorCommentId, pageable);
66+
67+
List<CommentResponseDto> dtoList = slice.getContent().stream()
68+
.map(CommentResponseDto::of)
69+
.toList();
70+
71+
Long nextCursor = slice.hasNext() ? dtoList.getLast().commentId() : null;
72+
return PageResponse.of(new SliceImpl<>(dtoList, pageable, slice.hasNext()), nextCursor);
73+
}
5274
}
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package com.example.log4u.domain.reports.entity;
22

3-
import java.time.LocalDateTime;
4-
5-
import org.springframework.data.annotation.CreatedDate;
6-
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
7-
3+
import com.example.log4u.common.entity.BaseEntity;
84
import com.example.log4u.domain.reports.reportTargetType.ReportTargetType;
95
import com.example.log4u.domain.reports.reportType.ReportType;
106

7+
import jakarta.persistence.Column;
118
import jakarta.persistence.Entity;
12-
import jakarta.persistence.EntityListeners;
139
import jakarta.persistence.EnumType;
1410
import jakarta.persistence.Enumerated;
1511
import jakarta.persistence.GeneratedValue;
@@ -27,24 +23,25 @@
2723
@AllArgsConstructor(access = AccessLevel.PACKAGE)
2824

2925
@Entity
30-
@EntityListeners(AuditingEntityListener.class)
31-
public class Report {
26+
public class Report extends BaseEntity {
3227
@Id
3328
@GeneratedValue(strategy = GenerationType.IDENTITY)
3429
private Long id;
3530

31+
@Column(nullable = false)
3632
private Long reporterId;
3733

34+
@Column(nullable = false)
3835
@Enumerated(EnumType.STRING)
3936
private ReportTargetType reportTargetType;
4037

38+
@Column(nullable = false)
4139
@Enumerated(EnumType.STRING)
4240
private ReportType reportType;
4341

42+
@Column(nullable = false)
4443
private Long reportTargetId;
4544

45+
@Column(nullable = false)
4646
private String content;
47-
48-
@CreatedDate
49-
private LocalDateTime createdAt;
5047
}

src/main/java/com/example/log4u/domain/supports/controller/SupportController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public ResponseEntity<Void> createSupport(
3737

3838
@GetMapping
3939
public ResponseEntity<Page<SupportOverviewGetResponseDto>> getSupportOverviewPage(
40-
@RequestParam(required = false) Integer page,
40+
@RequestParam(defaultValue = "1") int page,
4141
@RequestParam(required = false) SupportType supportType
4242
) {
4343
long requesterId = 1L;
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.example.log4u.domain.supports.entity;
22

3-
import java.time.LocalDateTime;
4-
5-
import org.springframework.data.annotation.CreatedDate;
6-
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
7-
3+
import com.example.log4u.common.entity.BaseEntity;
84
import com.example.log4u.domain.supports.supportType.SupportType;
95

6+
import jakarta.persistence.AttributeOverride;
7+
import jakarta.persistence.Column;
108
import jakarta.persistence.Entity;
11-
import jakarta.persistence.EntityListeners;
9+
import jakarta.persistence.EnumType;
10+
import jakarta.persistence.Enumerated;
1211
import jakarta.persistence.GeneratedValue;
1312
import jakarta.persistence.GenerationType;
1413
import jakarta.persistence.Id;
@@ -25,25 +24,26 @@
2524
@AllArgsConstructor(access = AccessLevel.PACKAGE)
2625

2726
@Entity
28-
@EntityListeners(AuditingEntityListener.class)
29-
public class Support {
27+
@AttributeOverride(name = "updatedAt", column = @Column(name = "ANSWERED_AT"))
28+
public class Support extends BaseEntity {
3029
@Id
3130
@GeneratedValue(strategy = GenerationType.IDENTITY)
3231
private Long id;
3332

34-
private long requesterId;
33+
@Column(nullable = false)
34+
private Long requesterId;
3535

36+
@Column(nullable = false)
37+
@Enumerated(value = EnumType.STRING)
3638
private SupportType supportType;
3739

40+
@Column(nullable = false)
3841
private String title;
3942

43+
@Column(nullable = false)
4044
private String content;
4145

42-
@CreatedDate
43-
private LocalDateTime createdAt;
44-
4546
@Setter
47+
@Column(nullable = true)
4648
private String answerContent;
47-
48-
private LocalDateTime answeredAt;
4949
}

src/main/java/com/example/log4u/domain/supports/repository/SupportQuerydsl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public Page<SupportOverviewGetResponseDto> getSupportOverviewGetResponseDtoPage(
4343
support.supportType,
4444
support.title,
4545
support.createdAt,
46-
support.answeredAt.isNotNull() // answered 필드는 answeredAt이 null 이 아니면 true
46+
support.updatedAt.isNotNull() // answered 필드는 answeredAt이 null 이 아니면 true
4747
))
4848
.where(builder)
4949
.orderBy(support.createdAt.desc())
@@ -67,7 +67,7 @@ public SupportGetResponseDto getSupportGetResponseDtoById(
6767
support.content,
6868
support.createdAt,
6969
support.answerContent,
70-
support.answeredAt))
70+
support.updatedAt))
7171
.where(support.id.eq(supportId)
7272
.and(support.requesterId.eq(requesterId)))
7373
.fetchOne();

0 commit comments

Comments
 (0)