Skip to content

Commit

Permalink
Merge pull request #37 from BudgetBuddiesTeam/feat/#33
Browse files Browse the repository at this point in the history
[feat] 댓글 Paging기능 추가
  • Loading branch information
wnd01jun authored Jul 25, 2024
2 parents 236e32a + 975962d commit b9e08f5
Show file tree
Hide file tree
Showing 13 changed files with 428 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
@Getter
@SuperBuilder
@SoftDelete // boolean 타입의 deleted 필드가 추가
@Getter
public abstract class BaseEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,89 +1,83 @@
package com.bbteam.budgetbuddies.domain.comment.controller;


import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto;
import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto;
import com.bbteam.budgetbuddies.domain.comment.service.CommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class CommentController {

private final CommentService commentService;

// user, discountInfo 인증 어노테이션 추후 추가 예정
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

public interface CommentController {
@Operation(summary = "[User] 특정 할인 정보 게시글에 댓글달기", description = "특정 할인 정보 게시글에 댓글을 다는 API입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
})
@Parameters({
@Parameter(name = "userId", description = "현재 댓글을 다는 유저 id입니다."),
@Parameter(name = "discountInfoId", description = "댓글을 다는 할인 정보 게시글 id입니다."),
@Parameter(name = "content", description = "댓글 내용입니다."),
@Parameter(name = "userId", description = "현재 댓글을 다는 유저 id입니다. parameter"),
@Parameter(name = "discountInfoId", description = "댓글을 다는 할인 정보 게시글 id입니다. requestBody"),
@Parameter(name = "content", description = "댓글 내용입니다. requestBody"),
})
@PostMapping("/discounts/comments/{userId}/add")
public ResponseEntity<CommentResponseDto.DiscountInfoSuccessDto> saveDiscountInfoComment(
@RequestParam("userId") Long userId,
@RequestBody CommentRequestDto.DiscountInfoCommentDto discountInfoCommentDto){
CommentResponseDto.DiscountInfoSuccessDto dto = commentService.saveDiscountComment(userId, discountInfoCommentDto);
return ResponseEntity.ok(dto);
}
ResponseEntity<CommentResponseDto.DiscountInfoSuccessDto> saveDiscountInfoComment(
Long userId,
CommentRequestDto.DiscountInfoCommentDto discountInfoCommentDto);


@Operation(summary = "[User] 특정 할인 정보 게시글의 댓글 조회하기", description = "특정 할인 정보 게시글의 댓글을 가져오는 API입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
})
@Parameters({
@Parameter(name = "discountInfoId", description = "댓글을 가져올 할인 정보 게시글 id입니다."),
@Parameter(name = "discountInfoId", description = "댓글을 가져올 할인 정보 게시글 id입니다. parameter"),
@Parameter(name = "page", description = "페이징을 위한 페이지 번호입니다. 0부터 시작합니다. parameter"),
@Parameter(name = "size", description = "페이징을 위한 페이지 사이즈입니다. default는 20입니다. parameter")
})
@GetMapping("/discounts/comments/get/{discountInfoId}")
public ResponseEntity<List<CommentResponseDto.DiscountInfoCommentDto>> findAllByDiscountInfo(
@RequestParam("discountInfoId") Long discountInfoId){
List<CommentResponseDto.DiscountInfoCommentDto> result = commentService.findByDiscountInfo(discountInfoId);
return ResponseEntity.ok(result);
}
ResponseEntity<Page<CommentResponseDto.DiscountInfoCommentDto>> findAllByDiscountInfo(
Long discountInfoId,
Pageable pageable);

@Operation(summary = "[User] 특정 지원 정보 게시글에 댓글달기", description = "특정 지원 정보 게시글에 댓글을 다는 API입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
})
@Parameters({
@Parameter(name = "userId", description = "현재 댓글을 다는 유저 id입니다."),
@Parameter(name = "supportInfoId", description = "댓글을 다는 지원 정보 게시글 id입니다."),
@Parameter(name = "content", description = "댓글 내용입니다."),
@Parameter(name = "userId", description = "현재 댓글을 다는 유저 id입니다. parameter"),
@Parameter(name = "supportInfoId", description = "댓글을 다는 지원 정보 게시글 id입니다. requestBody"),
@Parameter(name = "content", description = "댓글 내용입니다. requestBody"),
})
@PostMapping("/supports/comments/{userId}/add")
public ResponseEntity<CommentResponseDto.SupportInfoSuccessDto> saveSupportInfoComment(
@RequestParam("userId") Long userId,
@RequestBody CommentRequestDto.SupportInfoCommentDto supportInfoCommentDto){
CommentResponseDto.SupportInfoSuccessDto dto = commentService.saveSupportComment(userId, supportInfoCommentDto);
return ResponseEntity.ok(dto);
}
ResponseEntity<CommentResponseDto.SupportInfoSuccessDto> saveSupportInfoComment(
Long userId,
CommentRequestDto.SupportInfoCommentDto supportInfoCommentDto);

@Operation(summary = "[User] 특정 지원 정보 게시글의 댓글 조회하기", description = "특정 지원 정보 게시글의 댓글을 가져오는 API입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
})
@Parameters({
@Parameter(name = "supportInfoId", description = "댓글을 가져올 지원 정보 게시글 id입니다."),
})
@GetMapping("/supports/comments/get/{supportInfoId}")
public ResponseEntity<List<CommentResponseDto.SupportInfoCommentDto>> findAllBySupportInfo(
@RequestParam("supportInfoId") Long supportInfoId){
List<CommentResponseDto.SupportInfoCommentDto> result = commentService.findBySupportInfo(supportInfoId);
return ResponseEntity.ok(result);
}

@Parameter(name = "supportInfoId", description = "댓글을 가져올 지원 정보 게시글 id입니다. parameter"),
@Parameter(name = "page", description = "페이징을 위한 페이지 번호입니다. 0부터 시작합니다. parameter"),
@Parameter(name = "size", description = "페이징을 위한 페이지 사이즈입니다. default는 20입니다. parameter")


})
ResponseEntity<Page<CommentResponseDto.SupportInfoCommentDto>> findAllBySupportInfo(
Long supportInfoId,
Pageable pageable);

@Operation(summary = "[User] 특정 댓글 삭제하기", description = "특정 댓글을 삭제하는 API입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
})
@Parameters({
@Parameter(name = "commentId", description = "삭제할 댓글 id 입니다. parameter")
})
@GetMapping("/comments/delete")
ResponseEntity<String> deleteComment(Long commentId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.bbteam.budgetbuddies.domain.comment.controller;

import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto;
import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto;
import com.bbteam.budgetbuddies.domain.comment.service.CommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
public class CommentControllerImpl implements CommentController{

private final CommentService commentService;

@PostMapping("/discounts/comments")
public ResponseEntity<CommentResponseDto.DiscountInfoSuccessDto> saveDiscountInfoComment(
@RequestParam("userId") Long userId,
@RequestBody CommentRequestDto.DiscountInfoCommentDto discountInfoCommentDto){
CommentResponseDto.DiscountInfoSuccessDto dto = commentService.saveDiscountComment(userId, discountInfoCommentDto);
return ResponseEntity.ok(dto);
}


@GetMapping("/discounts/comments")
public ResponseEntity<Page<CommentResponseDto.DiscountInfoCommentDto>> findAllByDiscountInfo(
@RequestParam("discountInfoId") Long discountInfoId,
@PageableDefault(size = 20, page = 0) Pageable pageable){
Page<CommentResponseDto.DiscountInfoCommentDto> result = commentService.findByDiscountInfoWithPaging(discountInfoId, pageable);
return ResponseEntity.ok(result);
}


@PostMapping("/supports/comments")
public ResponseEntity<CommentResponseDto.SupportInfoSuccessDto> saveSupportInfoComment(
@RequestParam("userId") Long userId,
@RequestBody CommentRequestDto.SupportInfoCommentDto supportInfoCommentDto){
CommentResponseDto.SupportInfoSuccessDto dto = commentService.saveSupportComment(userId, supportInfoCommentDto);
return ResponseEntity.ok(dto);
}


@GetMapping("/supports/comments")
public ResponseEntity<Page<CommentResponseDto.SupportInfoCommentDto>> findAllBySupportInfo(
@RequestParam("supportInfoId") Long supportInfoId,
@PageableDefault(size = 20, page = 0)Pageable pageable){
Page<CommentResponseDto.SupportInfoCommentDto> result = commentService.findBySupportInfoWithPaging(supportInfoId, pageable);
return ResponseEntity.ok(result);
}


public ResponseEntity<String> deleteComment(@RequestParam("commentId") Long commentId) {
commentService.deleteComment(commentId);
return ResponseEntity.ok("ok");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,53 @@
import com.bbteam.budgetbuddies.domain.supportinfo.entity.SupportInfo;
import com.bbteam.budgetbuddies.domain.user.entity.User;

import java.util.HashMap;

public class CommentConverter {

public static Comment toDiscountComment(CommentRequestDto.DiscountInfoCommentDto dto, User user, DiscountInfo discountInfo) {
public static Comment toDiscountComment(CommentRequestDto.DiscountInfoCommentDto dto, User user, DiscountInfo discountInfo,
Integer anonymousNumber) {
return Comment.builder()
.user(user)
.discountInfo(discountInfo)
.content(dto.getContent())
.anonymousNumber(anonymousNumber)
.build();
}

public static Comment toSupportComment(CommentRequestDto.SupportInfoCommentDto dto, User user, SupportInfo supportInfo) {
public static Comment toSupportComment(CommentRequestDto.SupportInfoCommentDto dto, User user, SupportInfo supportInfo,
Integer anonymousNumber) {
return Comment.builder()
.user(user)
.supportInfo(supportInfo)
.content(dto.getContent())
.anonymousNumber(anonymousNumber)
.build();
}

public static CommentResponseDto.DiscountInfoCommentDto toDiscountInfoCommentDto(Comment comment,
HashMap<Long, Long> anonymousMapping){
public static CommentResponseDto.DiscountInfoCommentDto toDiscountInfoCommentDto(Comment comment){
return CommentResponseDto.DiscountInfoCommentDto.builder()
.commentId(comment.getId())
.discountInfoId(comment.getDiscountInfo().getId())
.userId(comment.getUser().getId())
.content(comment.getContent())
.anonymousNumber(anonymousMapping.get(comment.getUser().getId()))
.anonymousNumber(comment.getAnonymousNumber())
.build();

}

public static CommentResponseDto.SupportInfoCommentDto toSupportInfoCommentDto(Comment comment,
HashMap<Long, Long> anonymousMapping){
public static CommentResponseDto.SupportInfoCommentDto toSupportInfoCommentDto(Comment comment){
return CommentResponseDto.SupportInfoCommentDto.builder()
.commentId(comment.getId())
.supportInfoId(comment.getSupportInfo().getId())
.userId(comment.getUser().getId())
.content(comment.getContent())
.anonymousNumber(anonymousMapping.get(comment.getUser().getId()))
.anonymousNumber(comment.getAnonymousNumber())
.build();

}

public static CommentResponseDto.DiscountInfoSuccessDto toDiscountInfoSuccessDto(Comment comment){
return CommentResponseDto.DiscountInfoSuccessDto.builder()
.commentId(comment.getId())
.discountInfoId(comment.getDiscountInfo().getId())
.userId(comment.getUser().getId())
.content(comment.getContent())
Expand All @@ -59,6 +62,7 @@ public static CommentResponseDto.DiscountInfoSuccessDto toDiscountInfoSuccessDto

public static CommentResponseDto.SupportInfoSuccessDto toSupportInfoSuccessDto(Comment comment){
return CommentResponseDto.SupportInfoSuccessDto.builder()
.commentId(comment.getId())
.supportInfoId(comment.getSupportInfo().getId())
.userId(comment.getUser().getId())
.content(comment.getContent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,27 @@ public class CommentResponseDto {
@Getter
@Builder
public static class DiscountInfoCommentDto{
private Long commentId;
private Long userId;
private Long discountInfoId;
private String content;
private Long anonymousNumber;
private Integer anonymousNumber;
}

@Getter
@Builder
public static class SupportInfoCommentDto{
private Long commentId;
private Long userId;
private Long supportInfoId;
private String content;
private Long anonymousNumber;
private Integer anonymousNumber;
}

@Getter
@Builder
public static class DiscountInfoSuccessDto{
private Long commentId;
private Long userId;
private Long discountInfoId;
private String content;
Expand All @@ -35,6 +38,7 @@ public static class DiscountInfoSuccessDto{
@Getter
@Builder
public static class SupportInfoSuccessDto{
private Long commentId;
private Long userId;
private Long supportInfoId;
private String content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.Where;
import org.springframework.lang.Nullable;

@Entity
@Getter
Expand All @@ -34,4 +35,7 @@ public class Comment extends BaseEntity {
@JoinColumn(name = "support_info_id")
private SupportInfo supportInfo;

@Column(nullable = false)
private Integer anonymousNumber;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,36 @@
import com.bbteam.budgetbuddies.domain.comment.entity.Comment;
import com.bbteam.budgetbuddies.domain.discountinfo.entity.DiscountInfo;
import com.bbteam.budgetbuddies.domain.supportinfo.entity.SupportInfo;
import com.bbteam.budgetbuddies.domain.user.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

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

public interface CommentRepository extends JpaRepository<Comment, Long> {
@Query("select c from Comment c where c.discountInfo.id = :discountInfoId" +
" order by c.createdAt asc")
" order by c.createdAt asc") // 익명번호 부여용
List<Comment> findByDiscountInfo(@Param("discountInfoId")Long discountInfoId);

@Query("select c from Comment c where c.discountInfo.id = :discountInfoId" +
" order by c.createdAt asc")
Page<Comment> findByDiscountInfoWithPaging(@Param("discountInfoId")Long discountInfoId,
Pageable pageable);

@Query("select c from Comment c where c.supportInfo.id = :supportInfoId" +
" order by c.createdAt asc")
List<Comment> findBySupportInfo(@Param("supportInfoId")Long supportInfoId);

@Query("select c from Comment c where c.supportInfo.id = :supportInfoId" +
" order by c.createdAt asc")
Page<Comment> findBySupportInfoWithPaging(@Param("supportInfoId")Long supportInfoId,
Pageable pageable);

Optional<Comment> findTopByUserAndDiscountInfo(User user, DiscountInfo discountInfo);
Optional<Comment> findTopByUserAndSupportInfo(User user, SupportInfo supportInfo);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto;
import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

Expand All @@ -26,6 +28,12 @@ public interface CommentService {
*/
List<CommentResponseDto.SupportInfoCommentDto> findBySupportInfo(Long supportInfoId);

Page<CommentResponseDto.DiscountInfoCommentDto> findByDiscountInfoWithPaging(Long discountInfoId, Pageable pageable);
Page<CommentResponseDto.SupportInfoCommentDto> findBySupportInfoWithPaging(Long supportInfoId, Pageable pageable);

void deleteComment(Long commentId);





Expand Down
Loading

0 comments on commit b9e08f5

Please sign in to comment.