diff --git a/src/main/java/com/example/moyeothon/Controller/MessageController.java b/src/main/java/com/example/moyeothon/Controller/MessageController.java index a84426d..52e2098 100644 --- a/src/main/java/com/example/moyeothon/Controller/MessageController.java +++ b/src/main/java/com/example/moyeothon/Controller/MessageController.java @@ -20,16 +20,16 @@ public class MessageController { // 쪽지 전송 @Operation(summary = "쪽지 전송") - @PostMapping("/{uid}") - public ResponseEntity createMessage(@PathVariable String uid, @RequestBody MessageDTO messageDTO, @AuthenticationPrincipal UserDetails userDetails) { - return ResponseEntity.ok(messageService.createMessage(uid, messageDTO, userDetails)); + @PostMapping("/{uid}/{bucketListId}") + public ResponseEntity createMessage(@PathVariable String uid, @PathVariable Long bucketListId, @RequestBody MessageDTO messageDTO, @AuthenticationPrincipal UserDetails userDetails) { + return ResponseEntity.ok(messageService.createMessage(uid, bucketListId, messageDTO, userDetails)); } // 쪽지 답장 @Operation(summary = "쪽지 답장") - @PostMapping("/reply/{messageId}/{uid}") - public ResponseEntity replyMessage(@PathVariable Long messageId, @PathVariable String uid, @RequestBody MessageDTO messageDTO, @AuthenticationPrincipal UserDetails userDetails) { - return ResponseEntity.ok(messageService.replyMessage(messageId, uid, messageDTO, userDetails)); + @PostMapping("/reply/{uid}/{messageId}/{bucketListId}") + public ResponseEntity replyMessage(@PathVariable Long messageId, @PathVariable String uid, @PathVariable Long bucketListId, @RequestBody MessageDTO messageDTO, @AuthenticationPrincipal UserDetails userDetails) { + return ResponseEntity.ok(messageService.replyMessage(messageId, uid, bucketListId, messageDTO, userDetails)); } // 쪽지 읽음 처리 diff --git a/src/main/java/com/example/moyeothon/DTO/BucketDto/ResponseDto.java b/src/main/java/com/example/moyeothon/DTO/BucketDto/ResponseDto.java index 1671954..6c8d43c 100644 --- a/src/main/java/com/example/moyeothon/DTO/BucketDto/ResponseDto.java +++ b/src/main/java/com/example/moyeothon/DTO/BucketDto/ResponseDto.java @@ -1,6 +1,6 @@ package com.example.moyeothon.DTO.BucketDto; - +import com.example.moyeothon.DTO.UserDTO; import com.example.moyeothon.Entity.BucketlistEntity; import lombok.AllArgsConstructor; import lombok.Getter; @@ -16,13 +16,17 @@ public class ResponseDto { private String title; private String content; private boolean isPublic; + private Long userId; + private UserDTO user; - public static ResponseDto entityToDTO(BucketlistEntity bucketlistEntity){ + public static ResponseDto entityToDto(BucketlistEntity bucketlistEntity){ return new ResponseDto( bucketlistEntity.getId(), bucketlistEntity.getTitle(), bucketlistEntity.getContent(), - bucketlistEntity.isPublic() + bucketlistEntity.isPublic(), + bucketlistEntity.getUser().getId(), + UserDTO.entityToDto(bucketlistEntity.getUser()) ); } @@ -31,8 +35,7 @@ public ResponseDto(BucketlistEntity bucket) { this.title = bucket.getTitle(); this.content = bucket.getContent(); this.isPublic = bucket.isPublic(); - - + this.userId = bucket.getUser().getId(); + this.user = new UserDTO(); } - } diff --git a/src/main/java/com/example/moyeothon/DTO/MessageDTO.java b/src/main/java/com/example/moyeothon/DTO/MessageDTO.java index 03dd9a1..bc6d787 100644 --- a/src/main/java/com/example/moyeothon/DTO/MessageDTO.java +++ b/src/main/java/com/example/moyeothon/DTO/MessageDTO.java @@ -1,5 +1,8 @@ package com.example.moyeothon.DTO; +import com.example.moyeothon.DTO.BucketDto.RequestDto; +import com.example.moyeothon.DTO.BucketDto.ResponseDto; +import com.example.moyeothon.Entity.BucketlistEntity; import com.example.moyeothon.Entity.MessageEntity; import com.example.moyeothon.Entity.UserEntity; import com.example.moyeothon.Enum.MessageStatus; @@ -21,6 +24,8 @@ public class MessageDTO { private UserDTO sender; private UserDTO receiver; private MessageStatus status; + private Long bucketListId; + private ResponseDto bucketList; public static MessageDTO entityToDTO(MessageEntity messageEntity) { return new MessageDTO( @@ -31,11 +36,13 @@ public static MessageDTO entityToDTO(MessageEntity messageEntity) { messageEntity.getReceiver().getId(), UserDTO.entityToDto(messageEntity.getSender()), UserDTO.entityToDto(messageEntity.getReceiver()), - messageEntity.getStatus() + messageEntity.getStatus(), + messageEntity.getBucketList().getId(), + ResponseDto.entityToDto(messageEntity.getBucketList()) ); } - public MessageEntity dtoToEntity(UserEntity sender, UserEntity receiver, MessageStatus status) { - return new MessageEntity(id, content, createTime, sender, receiver, status); + public MessageEntity dtoToEntity(UserEntity sender, UserEntity receiver, BucketlistEntity bucketList) { + return new MessageEntity(id, content, createTime, sender, receiver, bucketList, status); } } diff --git a/src/main/java/com/example/moyeothon/Entity/MessageEntity.java b/src/main/java/com/example/moyeothon/Entity/MessageEntity.java index 6c7f840..c267881 100644 --- a/src/main/java/com/example/moyeothon/Entity/MessageEntity.java +++ b/src/main/java/com/example/moyeothon/Entity/MessageEntity.java @@ -26,6 +26,10 @@ public class MessageEntity { @ManyToOne @JoinColumn(name="receiver_id") private UserEntity receiver; + + @ManyToOne + @JoinColumn(name="bucketList_id") + private BucketlistEntity bucketList; @Enumerated(EnumType.STRING) private MessageStatus status; diff --git a/src/main/java/com/example/moyeothon/Service/BucketService.java b/src/main/java/com/example/moyeothon/Service/BucketService.java index eb783ce..e09b0ce 100644 --- a/src/main/java/com/example/moyeothon/Service/BucketService.java +++ b/src/main/java/com/example/moyeothon/Service/BucketService.java @@ -31,8 +31,8 @@ public ResponseDto addBucket(RequestDto requestDto, String uid, UserDetails user throw new RuntimeException("인증되지 않은 유저입니다."); } UserEntity user = userRepository.findByUid(uid); - BucketlistEntity bucket = bucketRepository.save(new BucketlistEntity(requestDto, user)); - return new ResponseDto(bucket); + BucketlistEntity bucketList = bucketRepository.save(new BucketlistEntity(requestDto, user)); + return ResponseDto.entityToDto(bucketList); } // id로 버킷리스트 조회 @@ -44,7 +44,7 @@ public ResponseDto getBucket(Long id, String uid, UserDetails userDetails){ if (!bucketList.isPublic() && !bucketList.getUser().getUid().equals(uid)) { throw new RuntimeException("해당 버킷리스트에 접근 권한이 없습니다."); } - return new ResponseDto(bucketList); + return ResponseDto.entityToDto(bucketList); } // 버킷리스트 삭제 @@ -57,7 +57,7 @@ public ResponseDto deleteBucket(Long id, String uid, UserDetails userDetails){ throw new AccessDeniedException("권한이 없는 유저입니다."); } bucketRepository.deleteById(id); - return new ResponseDto(bucketList); + return ResponseDto.entityToDto(bucketList); } // 버킷리스트 수정 @@ -70,7 +70,7 @@ public ResponseDto updateBucket(Long id, String uid, RequestDto requestDto, User throw new AccessDeniedException("권환이 없는 유저입니다."); } bucketList.update(requestDto); - return new ResponseDto(bucketList); + return ResponseDto.entityToDto(bucketList); } // 해당 유저 버킷리스트 전체 조회 @@ -108,7 +108,7 @@ public List searchTitleAndContent(String keyword, String uid, UserD } return bucketRepository.findByTitleContainingIgnoreCaseOrContentContainingIgnoreCase(keyword, keyword) .stream() - .map(ResponseDto::entityToDTO) + .map(ResponseDto::entityToDto) .collect(Collectors.toList()); } } diff --git a/src/main/java/com/example/moyeothon/Service/MessageService.java b/src/main/java/com/example/moyeothon/Service/MessageService.java index 095dc97..2c5da9d 100644 --- a/src/main/java/com/example/moyeothon/Service/MessageService.java +++ b/src/main/java/com/example/moyeothon/Service/MessageService.java @@ -1,9 +1,11 @@ package com.example.moyeothon.Service; import com.example.moyeothon.DTO.MessageDTO; +import com.example.moyeothon.Entity.BucketlistEntity; import com.example.moyeothon.Entity.MessageEntity; import com.example.moyeothon.Entity.UserEntity; import com.example.moyeothon.Enum.MessageStatus; +import com.example.moyeothon.Repository.BucketRepository; import com.example.moyeothon.Repository.MessageRepository; import com.example.moyeothon.Repository.UserRepository; import lombok.RequiredArgsConstructor; @@ -23,33 +25,36 @@ public class MessageService { private static final Logger logger = LoggerFactory.getLogger(MessageService.class); private final MessageRepository messageRepository; private final UserRepository userRepository; + private final BucketRepository bucketRepository; // 쪽지 전송 - public MessageDTO createMessage(String uid, MessageDTO messageDTO, UserDetails userDetails) { + public MessageDTO createMessage(String uid, Long bucketListId, MessageDTO messageDTO, UserDetails userDetails) { if (!userDetails.getUsername().equals(uid)) { throw new RuntimeException("인증되지 않은 유저입니다."); } UserEntity sender = userRepository.findByUid(uid); UserEntity receiver = userRepository.findById(messageDTO.getReceiverId()).orElseThrow(); - MessageEntity messageEntity = messageDTO.dtoToEntity(sender, receiver, MessageStatus.안읽음); + BucketlistEntity bucketList = bucketRepository.findById(bucketListId).orElseThrow(); + MessageEntity messageEntity = messageDTO.dtoToEntity(sender, receiver, bucketList); messageEntity.setCreateTime(LocalDateTime.now()); + messageEntity.setStatus(MessageStatus.안읽음); logger.info("쪽지 전송 성공!"); - logger.info("전송된 쪽지 내용 : " + messageDTO.getContent() + ", 송신인 ID : " + sender.getId() + ", 수신인 ID : " + receiver.getId()); return MessageDTO.entityToDTO(messageRepository.save(messageEntity)); } // 쪽지 답장 - public MessageDTO replyMessage(Long messageId, String uid, MessageDTO messageDTO, UserDetails userDetails) { + public MessageDTO replyMessage(Long messageId, String uid, Long bucketListId, MessageDTO messageDTO, UserDetails userDetails) { if (!userDetails.getUsername().equals(uid)) { throw new RuntimeException("인증되지 않은 유저입니다."); } MessageEntity originalMessage = messageRepository.findById(messageId).orElseThrow(); UserEntity sender = userRepository.findByUid(uid); UserEntity receiver = originalMessage.getSender(); - MessageEntity messageEntity = messageDTO.dtoToEntity(sender, receiver, MessageStatus.안읽음); + BucketlistEntity bucketList = bucketRepository.findById(bucketListId).orElseThrow(); + MessageEntity messageEntity = messageDTO.dtoToEntity(sender, receiver, bucketList); messageEntity.setCreateTime(LocalDateTime.now()); - logger.info("쪽지 전송 성공!"); - logger.info("전송된 쪽지 내용 : " + messageDTO.getContent() + ", 송신인 ID : " + sender.getId() + ", 수신인 ID : " + receiver.getId()); + messageEntity.setStatus(MessageStatus.안읽음); + logger.info("쪽지 답장 성공!"); return MessageDTO.entityToDTO(messageRepository.save(messageEntity)); } @@ -77,8 +82,7 @@ public MessageDTO deleteMessage(Long messageId, String uid, UserDetails userDeta if (!messageEntity.getSender().getUid().equals(uid)) { throw new RuntimeException("해당 유저의 쪽지가 아닙니다."); } - messageEntity.setStatus(MessageStatus.삭제됨); - messageRepository.save(messageEntity); + messageRepository.delete(messageEntity); logger.info("쪽지 삭제 성공!"); return MessageDTO.entityToDTO(messageEntity); }