Skip to content

Commit

Permalink
✅ 쪽지 버킷리스트 외래키 연결, 버킷리스트 생성 시 유저 정보 전체 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdals4716 committed Oct 31, 2024
1 parent 4989227 commit c18b1e6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ public class MessageController {

// 쪽지 전송
@Operation(summary = "쪽지 전송")
@PostMapping("/{uid}")
public ResponseEntity<MessageDTO> createMessage(@PathVariable String uid, @RequestBody MessageDTO messageDTO, @AuthenticationPrincipal UserDetails userDetails) {
return ResponseEntity.ok(messageService.createMessage(uid, messageDTO, userDetails));
@PostMapping("/{uid}/{bucketListId}")
public ResponseEntity<MessageDTO> 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<MessageDTO> 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<MessageDTO> 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));
}

// 쪽지 읽음 처리
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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())
);
}

Expand All @@ -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();
}

}
13 changes: 10 additions & 3 deletions src/main/java/com/example/moyeothon/DTO/MessageDTO.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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(
Expand All @@ -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);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/example/moyeothon/Entity/MessageEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/example/moyeothon/Service/BucketService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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로 버킷리스트 조회
Expand All @@ -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);
}

// 버킷리스트 삭제
Expand All @@ -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);
}

// 버킷리스트 수정
Expand All @@ -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);
}

// 해당 유저 버킷리스트 전체 조회
Expand Down Expand Up @@ -108,7 +108,7 @@ public List<ResponseDto> searchTitleAndContent(String keyword, String uid, UserD
}
return bucketRepository.findByTitleContainingIgnoreCaseOrContentContainingIgnoreCase(keyword, keyword)
.stream()
.map(ResponseDto::entityToDTO)
.map(ResponseDto::entityToDto)
.collect(Collectors.toList());
}
}
22 changes: 13 additions & 9 deletions src/main/java/com/example/moyeothon/Service/MessageService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
}

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit c18b1e6

Please sign in to comment.