Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.back.domain.cocktail.comment.controller;

import com.back.domain.cocktail.comment.dto.CocktailCommentCreateRequestDto;
import com.back.domain.cocktail.comment.dto.CocktailCommentResponseDto;
import com.back.domain.cocktail.comment.dto.CocktailCommentUpdateRequestDto;
import com.back.domain.cocktail.comment.service.CocktailCommentService;
import com.back.global.rsData.RsData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/cocktails/{cocktailId}/comments")
@Tag(name = "ApiCocktailCommentController", description = "API 칵테일댓글 컨트롤러")
@RequiredArgsConstructor
public class CocktailCommentController {

private final CocktailCommentService cocktailCommentService;

/**
* 칵테일댓글 작성 API
*
* @param cocktailId 칵테일댓글을 작성할 칵테일 ID
* @param reqBody 칵테일댓글 작성 요청 DTO
* @return 작성된 칵테일댓글 정보
*/
@PostMapping
@Operation(summary = "칵테일댓글 작성")
public RsData<CocktailCommentResponseDto> createCocktailComment(
@PathVariable Long cocktailId,
@Valid @RequestBody CocktailCommentCreateRequestDto reqBody
) {
return RsData.successOf(cocktailCommentService.createCocktailComment(cocktailId, reqBody)); // code=200, message="success"
}

/**
* 칵테일댓글 다건조회 API
*
* @param cocktailId 칵테일댓글 작성된 게시글 ID
* @param lastId 마지막으로 조회한 칵테일댓글 ID (페이징 처리용, optional)
* @return 칵테일댓글 목록
*/
@GetMapping
@Operation(summary = "댓글 다건 조회")
public RsData<List<CocktailCommentResponseDto>> getCocktailComments(
@PathVariable Long cocktailId,
@RequestParam(required = false) Long lastId
) {
return RsData.successOf(cocktailCommentService.getCocktailComments(cocktailId, lastId)); // code=200, message="success"
}

/**
* 칵테일댓글 단건 조회 API
*
* @param cocktailId 칵테일댓글이 작성된 칵테일 ID
* @param cocktailCommentId 조회할 칵테일댓글 ID
* @return 해당 ID의 칵테일댓글 정보
*/
@GetMapping("/{cocktailCommentId}")
@Operation(summary = "칵테일 댓글 단건 조회")
public RsData<CocktailCommentResponseDto> getCocktailComment(
@PathVariable Long cocktailId,
@PathVariable Long cocktailCommentId
) {
return RsData.successOf(cocktailCommentService.getCocktailComment(cocktailId, cocktailCommentId)); // code=200, message="success"
}

/**
* 칵테일댓글 수정 API
*
* @param cocktailId 칵테일댓글 작성된 칵테일 ID
* @param cocktailCommentId 수정할 칵테일댓글 ID
* @param reqBody 칵테일댓글 수정 요청 DTO
* @return 수정된 칵테일댓글 정보
*/
@PatchMapping("/{cocktailCommentId}")
@Operation(summary = "칵테일댓글 수정")
public RsData<CocktailCommentResponseDto> updateComment(
@PathVariable Long cocktailId,
@PathVariable Long cocktailCommentId,
@Valid @RequestBody CocktailCommentUpdateRequestDto reqBody
) {
return RsData.successOf(cocktailCommentService.updateCocktailComment(cocktailId, cocktailCommentId, reqBody)); // code=200, message="success"
}

/**
* 칵테일댓글 삭제 API
*
* @param cocktailId 칵테일댓글 작성된 칵테일 ID
* @param cocktailCommentId 삭제할 칵테일댓글 ID
* @return 삭제 성공 메시지
*/
@DeleteMapping("/{cocktailCommentId}")
@Operation(summary = "댓글 삭제")
public RsData<Void> deleteComment(
@PathVariable Long cocktailId,
@PathVariable Long cocktailCommentId
) {
cocktailCommentService.deleteCocktailComment(cocktailId, cocktailCommentId);
return RsData.successOf(null); // code=200, message="success"
}
}

Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.back.domain.cocktail.comment.repository;

import com.back.domain.post.comment.entity.Comment;
import com.back.domain.cocktail.comment.entity.CocktailComment;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CocktailCommentRepository extends JpaRepository<Comment, Long> {
import java.util.List;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

순수한 질문이 있습니다.
@repository 어노테이션이 없어도 상관없나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인감사합니다 제가 빼먹은것 같아욥
수정해서 다시 PR 올리겠습니다


public interface CocktailCommentRepository extends JpaRepository<CocktailComment, Long> {

List<CocktailComment> findTop10ByCocktailIdOrderByIdDesc(Long cocktailId);

List<CocktailComment> findTop10ByCocktailIdAndIdLessThanOrderByIdDesc(Long cocktailId, Long lastId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.back.domain.cocktail.comment.service;

import com.back.domain.cocktail.comment.dto.CocktailCommentCreateRequestDto;
import com.back.domain.cocktail.comment.dto.CocktailCommentResponseDto;
import com.back.domain.cocktail.comment.dto.CocktailCommentUpdateRequestDto;
import com.back.domain.cocktail.comment.entity.CocktailComment;
import com.back.domain.cocktail.comment.repository.CocktailCommentRepository;
import com.back.domain.cocktail.entity.Cocktail;
import com.back.domain.cocktail.repository.CocktailRepository;
import com.back.domain.post.comment.enums.CommentStatus;
import com.back.domain.user.entity.User;
import com.back.global.rq.Rq;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CocktailCommentService {
private final CocktailCommentRepository cocktailCommentRepository;
private final CocktailRepository cocktailRepository;
private final Rq rq;

// 칵테일 댓글 작성 로직
@Transactional
public CocktailCommentResponseDto createCocktailComment(Long cocktailId, CocktailCommentCreateRequestDto reqBody) {
User user = rq.getActor();

Cocktail cocktail = cocktailRepository.findById(cocktailId)
.orElseThrow(() -> new IllegalArgumentException("칵테일이 존재하지 않습니다. id=" + cocktailId));

CocktailComment cocktailComment = CocktailComment.builder()
.cocktail(cocktail)
.user(user)
.content(reqBody.content())
.build();

return new CocktailCommentResponseDto(cocktailCommentRepository.save(cocktailComment));
}

// 칵테일 댓글 다건 조회 로직 (무한스크롤)
@Transactional(readOnly = true)
public List<CocktailCommentResponseDto> getCocktailComments(Long cocktailId, Long lastId) {
if (lastId == null) {
return cocktailCommentRepository.findTop10ByCocktailIdOrderByIdDesc(cocktailId)
.stream()
.map(CocktailCommentResponseDto::new)
.toList();
} else {
return cocktailCommentRepository.findTop10ByCocktailIdAndIdLessThanOrderByIdDesc(cocktailId, lastId)
.stream()
.map(CocktailCommentResponseDto::new)
.toList();
}
}

// 칵테일 댓글 단건 조회 로직
@Transactional(readOnly = true)
public CocktailCommentResponseDto getCocktailComment(Long cocktailId, Long cocktailCommentId) {
CocktailComment cocktailComment = findByIdAndValidateCocktail(cocktailId, cocktailCommentId);

return new CocktailCommentResponseDto(cocktailComment);
}

// 칵테일댓글 ID로 찾고, 칵테일과의 관계를 검증
private CocktailComment findByIdAndValidateCocktail(Long cocktailId, Long cocktailCommentId) {
CocktailComment cocktailComment = cocktailCommentRepository.findById(cocktailCommentId)
.orElseThrow(() -> new IllegalArgumentException("댓글이 존재하지 않습니다. id=" + cocktailCommentId));

if (!cocktailComment.getCocktail().getId().equals(cocktailId)) {
throw new IllegalStateException("댓글이 해당 게시글에 속하지 않습니다.");
}
return cocktailComment;
}

// 칵테일댓글 수정 로직
@Transactional
public CocktailCommentResponseDto updateCocktailComment(Long cocktailId, Long cocktailCommentId, CocktailCommentUpdateRequestDto requestDto) {
User user = rq.getActor();

CocktailComment cocktailComment = findByIdAndValidateCocktail(cocktailId, cocktailCommentId);

if (!cocktailComment.getUser().getId().equals(user.getId())) {
throw new IllegalStateException("본인의 댓글만 수정할 수 있습니다.");
}

cocktailComment.updateContent(requestDto.content());
return new CocktailCommentResponseDto(cocktailComment);
}

// 칵테일댓글 삭제 로직
@Transactional
public void deleteCocktailComment(Long cocktailId, Long cocktailCommentId) {
User user = rq.getActor();

CocktailComment cocktailComment = findByIdAndValidateCocktail(cocktailId, cocktailCommentId);

if (!cocktailComment.getUser().getId().equals(user.getId())) {
throw new IllegalStateException("본인의 댓글만 삭제할 수 있습니다.");
}

cocktailComment.updateStatus(CommentStatus.DELETED); // soft delete 사용.
}
}






Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.back.domain.cocktail.service.CocktailService;
import com.back.global.rsData.RsData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
Expand All @@ -15,6 +16,7 @@

@RestController
@RequestMapping("api/cocktails")
@Tag(name = "ApiCocktailController", description = "API 칵테일 컨트롤러")
@RequiredArgsConstructor
public class CocktailController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class Cocktail {
@Id
@GeneratedValue(strategy = IDENTITY)
private long id;
private Long id;

private String cocktailName;

Expand Down