Skip to content

Commit

Permalink
Merge pull request #136 from CSID-DGU/backend/feature/crew
Browse files Browse the repository at this point in the history
BE: [feat] 갤러리 댓글 달기 #83
  • Loading branch information
Seoyoung2222 authored Dec 1, 2024
2 parents cde3fc7 + 0b268b2 commit aa02d01
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package RunningMachines.R2R.domain.crew.post.gallery.comment.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class CrewPostCommentRequestDto {
private String content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package RunningMachines.R2R.domain.crew.post.gallery.comment.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@AllArgsConstructor
public class CrewPostCommentResponseDto {
private Long commentId;
private String content;
private String authorName;
private String authorProfile;
private String createdAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package RunningMachines.R2R.domain.crew.post.gallery.comment.repository;

import RunningMachines.R2R.domain.crew.post.entity.CrewPostComment;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CrewPostCommentRepository extends JpaRepository<CrewPostComment, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package RunningMachines.R2R.domain.crew.post.gallery.comment.service;

import RunningMachines.R2R.domain.crew.common.entity.Crew;
import RunningMachines.R2R.domain.crew.common.repository.CrewRepository;
import RunningMachines.R2R.domain.crew.post.entity.CrewPost;
import RunningMachines.R2R.domain.crew.post.entity.CrewPostComment;
import RunningMachines.R2R.domain.crew.post.gallery.comment.dto.CrewPostCommentRequestDto;
import RunningMachines.R2R.domain.crew.post.gallery.comment.dto.CrewPostCommentResponseDto;
import RunningMachines.R2R.domain.crew.post.gallery.comment.repository.CrewPostCommentRepository;
import RunningMachines.R2R.domain.crew.post.repository.CrewPostRepository;
import RunningMachines.R2R.domain.user.entity.User;
import RunningMachines.R2R.domain.user.service.AuthCommandService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.format.DateTimeFormatter;

@Service
@RequiredArgsConstructor
public class CrewPostCommentService {
private final CrewPostRepository crewPostRepository;
private final CrewPostCommentRepository crewPostCommentRepository;
private final CrewRepository crewRepository;
private final AuthCommandService authCommandService;

@Transactional
public Long createComment(Long crewId, Long postId, CrewPostCommentRequestDto requestDto) {
User currentUser = authCommandService.getCurrentUser();

CrewPost crewPost = crewPostRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException("게시글을 찾을 수 없습니다."));
Crew crew = crewRepository.findById(crewId)
.orElseThrow(() -> new IllegalArgumentException("크루를 찾을 수 없습니다."));

boolean isMember = crew.getCrewUsers().stream()
.anyMatch(crewUser -> crewUser.getUser().equals(currentUser));
if (!isMember) {
throw new IllegalArgumentException("댓글 작성은 크루 유저만 가능합니다.");
}

CrewPostComment comment = CrewPostComment.builder()
.content(requestDto.getContent())
.crewPost(crewPost)
.user(currentUser)
.build();
CrewPostComment savedComment = crewPostCommentRepository.save(comment);

return savedComment.getId();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package RunningMachines.R2R.domain.crew.post.gallery.controller;

import RunningMachines.R2R.domain.crew.post.gallery.comment.dto.CrewPostCommentRequestDto;
import RunningMachines.R2R.domain.crew.post.gallery.comment.dto.CrewPostCommentResponseDto;
import RunningMachines.R2R.domain.crew.post.gallery.comment.repository.CrewPostCommentRepository;
import RunningMachines.R2R.domain.crew.post.gallery.comment.service.CrewPostCommentService;
import RunningMachines.R2R.domain.crew.post.gallery.dto.GalleryPostCreateRequestDto;
import RunningMachines.R2R.domain.crew.post.gallery.dto.GalleryPostDetailResponseDto;
import RunningMachines.R2R.domain.crew.post.gallery.dto.GalleryPreviewResponseDto;
Expand All @@ -21,6 +25,8 @@ public class GalleryPostController {
private final GalleryPostCommandService galleryPostCommandService;
private final GalleryPostQueryService galleryPostQueryService;
private final GalleryPostLikeService galleryPostLikeService;
private final CrewPostCommentRepository crewPostCommentRepository;
private final CrewPostCommentService crewPostCommentService;

@PostMapping
public ResponseEntity<Long> createGalleryPost(@PathVariable Long crewId, @RequestPart("content") GalleryPostCreateRequestDto contentDto, @RequestPart("images") List<MultipartFile> images) {
Expand All @@ -45,4 +51,10 @@ public ResponseEntity<Void> likeGalleryPost(@PathVariable Long crewId, @PathVari
galleryPostLikeService.likeGalleryPost(crewId, crewPostId);
return ResponseEntity.ok().build();
}

@PostMapping("/{postId}/comments")
public ResponseEntity<Long> createComment(@PathVariable Long crewId, @PathVariable Long postId, @RequestBody CrewPostCommentRequestDto requestDto) {
Long commentId = crewPostCommentService.createComment(crewId, postId, requestDto);
return ResponseEntity.ok(commentId);
}
}

0 comments on commit aa02d01

Please sign in to comment.