diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/auth/repository/UserRepository.java b/Titto_Backend/src/main/java/com/example/titto_backend/auth/repository/UserRepository.java index 97033b2..c906b7d 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/auth/repository/UserRepository.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/auth/repository/UserRepository.java @@ -9,6 +9,8 @@ public interface UserRepository extends JpaRepository { Optional findByEmail(String email); + Optional findByNickname(String nickname); + boolean existsByNickname(String nickname); boolean existsByStudentNo(String studentNo); diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/message/controller/MessageController.java b/Titto_Backend/src/main/java/com/example/titto_backend/message/controller/MessageController.java index 2da8c50..6d2e64d 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/message/controller/MessageController.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/message/controller/MessageController.java @@ -35,10 +35,9 @@ public class MessageController { @ApiResponse(responseCode = "400", description = "잘못된 요청"), @ApiResponse(responseCode = "500", description = "관리자 문의") }) - public ResponseEntity writeMessage(@RequestBody MessageDTO messageDTO, + public ResponseEntity writeMessage(@RequestBody MessageDTO.Request request, @AuthenticationPrincipal UserDetails userDetails) { - messageService.writeMessage(messageDTO, userDetails.getUsername()); - return new ResponseEntity<>(HttpStatus.CREATED); + return ResponseEntity.status(HttpStatus.CREATED).body(messageService.writeMessage(request, userDetails.getUsername())); } @GetMapping("/receiver") @@ -50,7 +49,7 @@ public ResponseEntity writeMessage(@RequestBody MessageDTO messageDT @ApiResponse(responseCode = "400", description = "잘못된 요청"), @ApiResponse(responseCode = "500", description = "관리자 문의") }) - public ResponseEntity> getMessagesByReceiver(@AuthenticationPrincipal UserDetails userDetails) { + public ResponseEntity> getMessagesByReceiver(@AuthenticationPrincipal UserDetails userDetails) { return new ResponseEntity<>(messageService.getMessagesByReceiver(userDetails.getUsername()), HttpStatus.OK); } @@ -63,9 +62,13 @@ public ResponseEntity> getMessagesByReceiver(@AuthenticationPri @ApiResponse(responseCode = "400", description = "잘못된 요청"), @ApiResponse(responseCode = "500", description = "관리자 문의") }) - public ResponseEntity> getMessagesBySender(@AuthenticationPrincipal UserDetails userDetails) { + public ResponseEntity> getMessagesBySender(@AuthenticationPrincipal UserDetails userDetails) { return new ResponseEntity<>(messageService.getMessagesBySender(userDetails.getUsername()), HttpStatus.OK); } + //메세지 삭제 (sender인 유저가 receiver유저와 한 대화내용을 삭제) + //쪽지함 전체 조회 + //쪽지함 세부 조회 ( 주고 받은 사용자와의 대화 내용을 뿌려줄 수 있는 api) + } diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/message/domain/Message.java b/Titto_Backend/src/main/java/com/example/titto_backend/message/domain/Message.java index a9e2238..e4870fb 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/message/domain/Message.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/message/domain/Message.java @@ -19,7 +19,6 @@ import lombok.NoArgsConstructor; import lombok.Setter; -//TODO: 메세지 삭제 기능 추가 할거임? @Entity @Table(name = "messages") @Getter @@ -33,30 +32,33 @@ public class Message extends BaseEntity { @Column(name = "message_id") private Long id; - // FetchType.LAZY로 바꿔야할지 고민중 - @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) + @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name = "sender_id") private User sender; - @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) + @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name = "receiver_id") private User receiver; - @Column(name = "title", nullable = false, length = 100) - private String title; - @Column(name = "content", columnDefinition = "TEXT", nullable = false) private String content; @Column(name = "sent_time") private LocalDateTime sentAt; + @Column(name = "sender_nickname") + private String senderNickname; + + @Column(name = "receiver_nickname") + private String receiverNickname; + @Builder - public Message(User sender, User receiver, String title, String content) { + public Message(User sender, User receiver, String content, String senderNickname, String receiverNickname) { this.sender = sender; this.receiver = receiver; - this.title = title; this.content = content; + this.senderNickname = senderNickname; + this.receiverNickname = receiverNickname; this.sentAt = LocalDateTime.now(); } -} +} \ No newline at end of file diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/message/dto/MessageDTO.java b/Titto_Backend/src/main/java/com/example/titto_backend/message/dto/MessageDTO.java index ce460cf..2f85f32 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/message/dto/MessageDTO.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/message/dto/MessageDTO.java @@ -1,23 +1,64 @@ package com.example.titto_backend.message.dto; import com.example.titto_backend.message.domain.Message; +import com.fasterxml.jackson.annotation.JsonInclude; +import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; +import lombok.Getter; import lombok.NoArgsConstructor; -@Data -@NoArgsConstructor -@AllArgsConstructor +// response 임! public class MessageDTO { - private String title; - private String content; - private Long receiverId; - - public static MessageDTO toDto(Message message) { - return new MessageDTO( - message.getTitle(), - message.getContent(), - message.getReceiver().getId() - ); + + @Data + @Builder + @NoArgsConstructor + @AllArgsConstructor + @Getter + @Schema(description = "메시지 작성") + public static class Request { + @Schema(description = "내용") + private String content; + @Schema(description = "받는 사람 닉네임") + private String receiverNickname; + } + + @Data + @JsonInclude(JsonInclude.Include.NON_NULL) + @Schema(description = "메시지 조회") + public static class Response { + @Schema(description = "메시지 ID") + private Long id; + + @Schema(description = "내용") + private String content; + + @Schema(description = "보낸 사람 ID") + private Long senderId; + + @Schema(description = "받는 사람 ID") + private Long receiverId; + + @Schema(description = "보낸 시간") + private String sentAt; + + @Schema(description = "받는 사람 닉네임") + private String receiverNickname; + + @Schema(description = "보낸 사람 닉네임") + private String senderNickname; + + public Response(Message message) { + this.id = message.getId(); + this.content = message.getContent(); + this.senderId = message.getSender().getId(); + this.receiverId = message.getReceiver().getId(); + this.sentAt = message.getSentAt().toString(); + this.receiverNickname = message.getReceiverNickname(); + this.senderNickname = message.getSenderNickname(); + } } } + diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/message/service/MessageService.java b/Titto_Backend/src/main/java/com/example/titto_backend/message/service/MessageService.java index 1fda423..9cd1ffb 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/message/service/MessageService.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/message/service/MessageService.java @@ -6,6 +6,7 @@ import com.example.titto_backend.common.exception.ErrorCode; import com.example.titto_backend.message.domain.Message; import com.example.titto_backend.message.dto.MessageDTO; +import com.example.titto_backend.message.dto.MessageDTO.Response; import com.example.titto_backend.message.repository.MessageRepository; import java.util.List; import lombok.RequiredArgsConstructor; @@ -20,23 +21,26 @@ public class MessageService { private final UserRepository userRepository; @Transactional - public void writeMessage(MessageDTO messageDTO, String email) { + public String writeMessage(MessageDTO.Request request, String email) throws CustomException { User sender = userRepository.findByEmail(email) .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); - User receiver = userRepository.findById(messageDTO.getReceiverId()) + User receiver = userRepository.findByNickname(request.getReceiverNickname()) .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); messageRepository.save(Message.builder() .sender(sender) .receiver(receiver) - .title(messageDTO.getTitle()) - .content(messageDTO.getContent()) + .senderNickname(sender.getNickname()) + .receiverNickname(receiver.getNickname()) + .content(request.getContent()) .build()); + + return "메시지 전송 성공"; } @Transactional - public List getMessagesByReceiver(String email) { + public List getMessagesByReceiver(String email) { User receiver = userRepository.findByEmail(email) .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); @@ -45,7 +49,7 @@ public List getMessagesByReceiver(String email) { } @Transactional - public List getMessagesBySender(String email) { + public List getMessagesBySender(String email) { User sender = userRepository.findByEmail(email) .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); @@ -53,9 +57,9 @@ public List getMessagesBySender(String email) { return convertMessagesToDTO(messages); } - private List convertMessagesToDTO(List messages) { + private List convertMessagesToDTO(List messages) { return messages.stream() - .map(MessageDTO::toDto) + .map(MessageDTO.Response::new) .toList(); } } diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/dto/QuestionDTO.java b/Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/dto/QuestionDTO.java index 58d7679..a2c36c6 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/dto/QuestionDTO.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/dto/QuestionDTO.java @@ -31,10 +31,6 @@ public static class Request { @NotBlank private String content; - // 아직 미완성 상태 - @Schema(description = "이미지") - private List imageList; - @Schema(description = "카테고리", example = "HUMANITIES, MANAGEMENT, SOCIETY, MEDIA_CONTENT, FUTURE_FUSION, SOFTWARE") @NotBlank private String department; diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/service/QuestionService.java b/Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/service/QuestionService.java index 264b3b5..0cfd20a 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/service/QuestionService.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/service/QuestionService.java @@ -88,26 +88,6 @@ public Page findByCategory(int page, String category) { .map(QuestionDTO.Response::new); } - /*// Keyword Search - private Specification searchByKeyword(String keyword) { - return new Specification<>() { - @Serial - private static final long serialVersionUID = 1L; - - @Override - public Predicate toPredicate(Root q, CriteriaQuery query, CriteriaBuilder cb) { - query.distinct(true); - Join u1 = q.join("author", JoinType.LEFT); - Join a = q.join("answers", JoinType.LEFT); - Join u2 = a.join("author", JoinType.LEFT); - return cb.or( - cb.like(q.get("title"), "%" + keyword + "%"), - cb.like(q.get("content"), "%" + keyword + "%"), - cb.like(u1.get("nickname"), "%" + keyword + "%"), - cb.like(u2.get("nickname"), "%" + keyword + "%") - ); - } - };*/ @Transactional(readOnly = true) public Page searchByKeyword(String keyWord, int page) { Pageable pageable = Pageable.ofSize(10).withPage(page);