Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import com.be.sportizebe.domain.post.entity.Post;
import com.be.sportizebe.domain.post.exception.PostErrorCode;
import com.be.sportizebe.domain.post.repository.PostRepository;
import com.be.sportizebe.domain.user.entity.User;
import com.be.sportizebe.global.exception.CustomException;
import com.be.sportizebe.global.response.BaseResponse;
import com.be.sportizebe.global.cache.dto.UserAuthInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -33,12 +33,12 @@ public class NoteController {
@Operation(summary = "1대1 채팅방 생성", description = "게시글 작성자와 1대1 쪽지 채팅방을 생성합니다. 채팅방 이름은 게시글 제목으로 설정됩니다. 이미 채팅방이 존재하면 기존 채팅방을 반환합니다.")
public ResponseEntity<BaseResponse<ChatRoomResponse>> createChatRoom(
@Parameter(description = "게시글 ID") @PathVariable Long postId,
@AuthenticationPrincipal User user) {
@AuthenticationPrincipal UserAuthInfo userAuthInfo) {

Post post = postRepository.findById(postId)
.orElseThrow(() -> new CustomException(PostErrorCode.POST_NOT_FOUND));

ChatRoom room = chatRoomService.createNote(post, user);
ChatRoom room = chatRoomService.createNote(post, userAuthInfo.getId());

return ResponseEntity.status(HttpStatus.CREATED)
.body(BaseResponse.success("채팅방 생성 성공", ChatRoomResponse.from(room)));
Expand All @@ -47,9 +47,9 @@ public ResponseEntity<BaseResponse<ChatRoomResponse>> createChatRoom(
@GetMapping("/rooms")
@Operation(summary = "내 쪽지 채팅방 목록 조회", description = "현재 사용자가 참여한 모든 쪽지 채팅방 목록을 조회합니다.")
public ResponseEntity<BaseResponse<List<ChatRoomResponse>>> getMyChatRooms(
@AuthenticationPrincipal User user) {
@AuthenticationPrincipal UserAuthInfo userAuthInfo) {

List<ChatRoomResponse> rooms = chatRoomService.findMyNoteRooms(user).stream()
List<ChatRoomResponse> rooms = chatRoomService.findMyNoteRooms(userAuthInfo.getId()).stream()
.map(ChatRoomResponse::from)
.toList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.be.sportizebe.domain.chat.entity.ChatRoom;
import com.be.sportizebe.domain.post.entity.Post;
import com.be.sportizebe.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
Expand All @@ -11,8 +10,8 @@
public interface ChatRoomRepository extends JpaRepository<ChatRoom, Long> {

// 특정 게시글에 대해 두 사용자 간의 1대1 채팅방이 이미 존재하는지 확인
Optional<ChatRoom> findByPostAndGuestUser(Post post, User guestUser);
Optional<ChatRoom> findByPostAndGuestUserId(Post post, Long guestUserId);

// 사용자가 참여한 1대1 채팅방 목록 조회 (게시글 작성자 또는 채팅 요청자로 참여)
List<ChatRoom> findByPost_UserOrGuestUser(User postUser, User guestUser);
List<ChatRoom> findByPost_UserIdOrGuestUserId(Long postUserId, Long guestUserId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.be.sportizebe.domain.club.entity.Club;
import com.be.sportizebe.domain.post.entity.Post;
import com.be.sportizebe.domain.user.entity.User;
import com.be.sportizebe.domain.user.exception.UserErrorCode;
import com.be.sportizebe.domain.user.repository.UserRepository;
import com.be.sportizebe.global.exception.CustomException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -18,6 +20,7 @@
@Transactional(readOnly = true)
public class ChatRoomService {
private final ChatRoomRepository chatRoomRepository;
private final UserRepository userRepository;

@Transactional
public ChatRoom createGroup(Club club) {
Expand All @@ -29,17 +32,19 @@ public ChatRoom createGroup(Club club) {
}

@Transactional
public ChatRoom createNote(Post post, User guestUser) {
public ChatRoom createNote(Post post, Long guestUserId) {
User hostUser = post.getUser(); // 게시글 등록자

// 자기 자신에게 채팅 불가
if (hostUser.getId() == guestUser.getId()) {
if (hostUser.getId() == guestUserId) {
throw new CustomException(ChatErrorCode.SELF_CHAT_NOT_ALLOWED);
}

// 이미 존재하는 채팅방이 있으면 반환
return chatRoomRepository.findByPostAndGuestUser(post, guestUser)
return chatRoomRepository.findByPostAndGuestUserId(post, guestUserId)
.orElseGet(() -> {
User guestUser = userRepository.findById(guestUserId)
.orElseThrow(() -> new CustomException(UserErrorCode.USER_NOT_FOUND));
ChatRoom room = ChatRoom.builder()
.chatRoomType(ChatRoom.ChatRoomType.NOTE)
.post(post)
Expand All @@ -50,8 +55,8 @@ public ChatRoom createNote(Post post, User guestUser) {
}

// 사용자가 참여한 1대1 채팅방 목록 조회
public List<ChatRoom> findMyNoteRooms(User user) {
return chatRoomRepository.findByPost_UserOrGuestUser(user, user);
public List<ChatRoom> findMyNoteRooms(Long userId) {
return chatRoomRepository.findByPost_UserIdOrGuestUserId(userId, userId);
}

public List<ChatRoom> findAll() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.be.sportizebe.domain.club.dto.response.ClubImageResponse;
import com.be.sportizebe.domain.club.dto.response.ClubResponse;
import com.be.sportizebe.domain.club.service.ClubServiceImpl;
import com.be.sportizebe.domain.user.entity.User;
import com.be.sportizebe.global.response.BaseResponse;
import com.be.sportizebe.global.cache.dto.UserAuthInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -32,8 +32,8 @@ public class ClubController {
public ResponseEntity<BaseResponse<ClubResponse>> createClub(
@RequestPart("request") @Valid ClubCreateRequest request,
@RequestPart(value = "image", required = false) MultipartFile image,
@AuthenticationPrincipal User user) {
ClubResponse response = clubService.createClub(request, image, user);
@AuthenticationPrincipal UserAuthInfo userAuthInfo) {
ClubResponse response = clubService.createClub(request, image, userAuthInfo.getId());
return ResponseEntity.status(HttpStatus.CREATED)
.body(BaseResponse.success("동호회 생성 성공", response));
}
Expand All @@ -43,8 +43,8 @@ public ResponseEntity<BaseResponse<ClubResponse>> createClub(
public ResponseEntity<BaseResponse<ClubResponse>> updateClub(
@PathVariable Long clubId,
@RequestBody @Valid ClubUpdateRequest request,
@AuthenticationPrincipal User user) {
ClubResponse response = clubService.updateClub(clubId, request, user);
@AuthenticationPrincipal UserAuthInfo userAuthInfo) {
ClubResponse response = clubService.updateClub(clubId, request, userAuthInfo.getId());
return ResponseEntity.ok(BaseResponse.success("동호회 수정 성공", response));
}

Expand All @@ -53,8 +53,8 @@ public ResponseEntity<BaseResponse<ClubResponse>> updateClub(
public ResponseEntity<BaseResponse<ClubImageResponse>> updateClubImage(
@Parameter(description = "동호회 ID") @PathVariable Long clubId,
@RequestPart("image") MultipartFile image,
@AuthenticationPrincipal User user) {
ClubImageResponse response = clubService.updateClubImage(clubId, image, user);
@AuthenticationPrincipal UserAuthInfo userAuthInfo) {
ClubImageResponse response = clubService.updateClubImage(clubId, image, userAuthInfo.getId());
return ResponseEntity.ok(BaseResponse.success("동호회 사진 수정 성공", response));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import com.be.sportizebe.domain.club.dto.request.ClubUpdateRequest;
import com.be.sportizebe.domain.club.dto.response.ClubImageResponse;
import com.be.sportizebe.domain.club.dto.response.ClubResponse;
import com.be.sportizebe.domain.user.entity.User;
import org.springframework.web.multipart.MultipartFile;

public interface ClubService {
ClubResponse createClub(ClubCreateRequest request, MultipartFile image, User user); // 동호회 생성
ClubResponse createClub(ClubCreateRequest request, MultipartFile image, Long userId); // 동호회 생성

ClubResponse updateClub(Long clubId, ClubUpdateRequest request, User user); // 동호회 수정
ClubResponse updateClub(Long clubId, ClubUpdateRequest request, Long userId); // 동호회 수정

ClubImageResponse updateClubImage(Long clubId, MultipartFile image, User user); // 동호회 사진 수정
ClubImageResponse updateClubImage(Long clubId, MultipartFile image, Long userId); // 동호회 사진 수정
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.be.sportizebe.domain.club.repository.ClubMemberRepository;
import com.be.sportizebe.domain.club.repository.ClubRepository;
import com.be.sportizebe.domain.user.entity.User;
import com.be.sportizebe.domain.user.exception.UserErrorCode;
import com.be.sportizebe.domain.user.repository.UserRepository;
import com.be.sportizebe.global.exception.CustomException;
import com.be.sportizebe.global.s3.enums.PathName;
import com.be.sportizebe.global.s3.service.S3Service;
Expand All @@ -27,11 +29,15 @@ public class ClubServiceImpl implements ClubService {
private final ClubRepository clubRepository;
private final ChatRoomService chatRoomService;
private final ClubMemberRepository clubMemberRepository;
private final UserRepository userRepository;
private final S3Service s3Service;

@Override
@Transactional
public ClubResponse createClub(ClubCreateRequest request, MultipartFile image, User user) {
public ClubResponse createClub(ClubCreateRequest request, MultipartFile image, Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(UserErrorCode.USER_NOT_FOUND));

if (clubRepository.existsByName(request.name())) {
throw new CustomException(ClubErrorCode.CLUB_NAME_DUPLICATED);
}
Expand Down Expand Up @@ -62,12 +68,12 @@ public ClubResponse createClub(ClubCreateRequest request, MultipartFile image, U

@Override
@Transactional
public ClubResponse updateClub(Long clubId, ClubUpdateRequest request, User user) {
public ClubResponse updateClub(Long clubId, ClubUpdateRequest request, Long userId) {
Club club = clubRepository.findById(clubId)
.orElseThrow(() -> new CustomException(ClubErrorCode.CLUB_NOT_FOUND));

// 동호회 방장만 수정 가능하도록 검증
if (club.getLeader().getId() != user.getId()) {
if (club.getLeader().getId() != userId) {
throw new CustomException(ClubErrorCode.CLUB_UPDATE_DENIED);
}

Expand All @@ -86,12 +92,12 @@ public ClubResponse updateClub(Long clubId, ClubUpdateRequest request, User user

@Override
@Transactional
public ClubImageResponse updateClubImage(Long clubId, MultipartFile image, User user) {
public ClubImageResponse updateClubImage(Long clubId, MultipartFile image, Long userId) {
Club club = clubRepository.findById(clubId)
.orElseThrow(() -> new CustomException(ClubErrorCode.CLUB_NOT_FOUND));

// 동호회 방장만 수정 가능하도록 검증
if (club.getLeader().getId() != user.getId()) {
if (club.getLeader().getId() != userId) {
throw new CustomException(ClubErrorCode.CLUB_UPDATE_DENIED);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.be.sportizebe.domain.comment.dto.response.CommentListResponse;
import com.be.sportizebe.domain.comment.dto.response.CommentResponse;
import com.be.sportizebe.domain.comment.service.CommentService;
import com.be.sportizebe.domain.user.entity.User;
import com.be.sportizebe.global.response.BaseResponse;
import com.be.sportizebe.global.cache.dto.UserAuthInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -16,8 +16,6 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/posts/{postId}/comments")
Expand All @@ -31,8 +29,8 @@ public class CommentController {
public ResponseEntity<BaseResponse<CommentResponse>> createComment(
@Parameter(description = "게시글 ID") @PathVariable Long postId,
@RequestBody @Valid CreateCommentRequest request,
@AuthenticationPrincipal User user) {
CommentResponse response = commentService.createComment(postId, request, user);
@AuthenticationPrincipal UserAuthInfo userAuthInfo) {
CommentResponse response = commentService.createComment(postId, request, userAuthInfo.getId());
return ResponseEntity.status(HttpStatus.CREATED)
.body(BaseResponse.success("댓글 생성 성공", response));
}
Expand All @@ -50,8 +48,8 @@ public ResponseEntity<BaseResponse<CommentListResponse>> getComments(
public ResponseEntity<BaseResponse<Void>> deleteComment(
@Parameter(description = "게시글 ID") @PathVariable Long postId,
@Parameter(description = "댓글 ID") @PathVariable Long commentId,
@AuthenticationPrincipal User user) {
commentService.deleteComment(postId, commentId, user);
@AuthenticationPrincipal UserAuthInfo userAuthInfo) {
commentService.deleteComment(postId, commentId, userAuthInfo.getId());
return ResponseEntity.ok(BaseResponse.success("댓글 삭제 성공", null));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@
import com.be.sportizebe.domain.comment.dto.request.CreateCommentRequest;
import com.be.sportizebe.domain.comment.dto.response.CommentListResponse;
import com.be.sportizebe.domain.comment.dto.response.CommentResponse;
import com.be.sportizebe.domain.user.entity.User;

import java.util.List;

public interface CommentService {

// 댓글 생성 (대댓글 포함)
CommentResponse createComment(Long postId, CreateCommentRequest request, User user);
CommentResponse createComment(Long postId, CreateCommentRequest request, Long userId);

// 게시글의 댓글 목록 조회
CommentListResponse getCommentsByPostId(Long postId);

// 댓글 삭제
void deleteComment(Long postId, Long commentId, User user);
void deleteComment(Long postId, Long commentId, Long userId);

// 게시글의 댓글 수 조회
long getCommentCount(Long postId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.be.sportizebe.domain.post.exception.PostErrorCode;
import com.be.sportizebe.domain.post.repository.PostRepository;
import com.be.sportizebe.domain.user.entity.User;
import com.be.sportizebe.domain.user.exception.UserErrorCode;
import com.be.sportizebe.domain.user.repository.UserRepository;
import com.be.sportizebe.global.exception.CustomException;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,15 +29,20 @@ public class CommentServiceImpl implements CommentService {

private final CommentRepository commentRepository;
private final PostRepository postRepository;
private final UserRepository userRepository;

@Override
@CacheEvict(cacheNames = {"commentList", "commentCount"}, key = "#postId")
@Transactional
public CommentResponse createComment(Long postId, CreateCommentRequest request, User user) {
public CommentResponse createComment(Long postId, CreateCommentRequest request, Long userId) {
// 게시글 조회
Post post = postRepository.findById(postId)
.orElseThrow(() -> new CustomException(PostErrorCode.POST_NOT_FOUND));

// 사용자 조회
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(UserErrorCode.USER_NOT_FOUND));

// 부모 댓글 조회 (대댓글인 경우)
Comment parent = null; // null로 초기화
if (request.parentId() != null) { // parentId가 null이 아니면 대댓글이기 때문에 부모 댓글 조회로 검증 필요함
Expand Down Expand Up @@ -79,12 +86,12 @@ public CommentListResponse getCommentsByPostId(Long postId) {
@Override
@CacheEvict(cacheNames = {"commentList", "commentCount"}, key = "#postId")
@Transactional
public void deleteComment(Long postId, Long commentId, User user) {
public void deleteComment(Long postId, Long commentId, Long userId) {
Comment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new CustomException(CommentErrorCode.COMMENT_NOT_FOUND));

// 작성자 확인
if (comment.getUser().getId() != user.getId()) {
if (comment.getUser().getId() != userId) {
throw new CustomException(CommentErrorCode.COMMENT_DELETE_DENIED);
}

Expand Down
Loading