diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/controller/MatchingBoardController.java b/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/controller/MatchingBoardController.java index d6f9aa9..860580f 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/controller/MatchingBoardController.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/controller/MatchingBoardController.java @@ -20,7 +20,7 @@ public class MatchingBoardController { private final MatchingBoardService matchingBoardService; - @GetMapping("/posts") + @GetMapping("/all") @Operation( summary = "매칭 게시판 전체 조회", description = "매칭 게시판의 전체 게시글을 조회합니다", @@ -33,7 +33,24 @@ public MatchingPostPagingResponseDto getAllMatchingPosts( @RequestParam("page") int page ) { MatchingPostPagingRequestDto requestDto = new MatchingPostPagingRequestDto(); - requestDto.setPage(page + 1); // 페이지 번호를 1부터 시작하도록 조정 + requestDto.setPage(page + 1); return matchingBoardService.findAllPosts(requestDto); } + + @GetMapping("/search") + @Operation( + summary = "매칭 게시판 검색", + description = "키워드로 검색하여 결과를 출력합니다", + responses = { + @ApiResponse(responseCode = "200", description = "요청 성공"), + @ApiResponse(responseCode = "403", description = "인증 문제 발생"), + @ApiResponse(responseCode = "500", description = "관리자 문의") + }) + public MatchingPostPagingResponseDto searchByKeyWord(@RequestParam("page") int page, + @RequestParam String keyWord) { + MatchingPostPagingRequestDto requestDto = new MatchingPostPagingRequestDto(); + requestDto.setPage(page + 1); + return matchingBoardService.searchByKeyWord(requestDto, keyWord); + } + } diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/domain/matchingBoard/MatchingBoard.java b/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/domain/matchingBoard/MatchingBoard.java index 0339f98..f200bb8 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/domain/matchingBoard/MatchingBoard.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/domain/matchingBoard/MatchingBoard.java @@ -1,6 +1,12 @@ package com.example.titto_backend.matchingBoard.domain.matchingBoard; -import jakarta.persistence.*; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToOne; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; @@ -13,7 +19,8 @@ @AllArgsConstructor public class MatchingBoard { - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "matchingBoard_id") private Integer matchingBoardId; @@ -22,4 +29,7 @@ public class MatchingBoard { @JoinColumn(name = "matchingPost_id", unique = true, nullable = false) private MatchingPost matchingPost; + // 검색 키워드 + @Column(name = "key_word") + private String keyWord; } diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/repository/matchingBoard/MatchingBoardRepository.java b/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/repository/matchingBoard/MatchingBoardRepository.java deleted file mode 100644 index 92530bd..0000000 --- a/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/repository/matchingBoard/MatchingBoardRepository.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.example.titto_backend.matchingBoard.repository.matchingBoard; - -import com.example.titto_backend.matchingBoard.domain.matchingBoard.MatchingBoard; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface MatchingBoardRepository extends JpaRepository { - Page findAll(Pageable pageable); -} diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/repository/matchingBoard/MatchingPostRepository.java b/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/repository/matchingBoard/MatchingPostRepository.java index c80f063..ef6bd71 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/repository/matchingBoard/MatchingPostRepository.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/repository/matchingBoard/MatchingPostRepository.java @@ -1,9 +1,12 @@ package com.example.titto_backend.matchingBoard.repository.matchingBoard; import com.example.titto_backend.matchingBoard.domain.matchingBoard.MatchingPost; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface MatchingPostRepository extends JpaRepository { + Page findByTitleContaining(String keyword, Pageable pageable); } diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/service/matchingBoard/MatchingBoardService.java b/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/service/matchingBoard/MatchingBoardService.java index 8b1b207..29fc470 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/service/matchingBoard/MatchingBoardService.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/matchingBoard/service/matchingBoard/MatchingBoardService.java @@ -19,13 +19,24 @@ public class MatchingBoardService { @Transactional(readOnly = true) public MatchingPostPagingResponseDto findAllPosts(MatchingPostPagingRequestDto matchingPostPagingRequestDto) { - int page = matchingPostPagingRequestDto.getPage() - 1; // 페이지 번호를 0부터 시작하도록 조정 + int page = matchingPostPagingRequestDto.getPage() - 1; Sort sort = Sort.by(Sort.Direction.DESC, "matchingPostId"); - Pageable pageable = PageRequest.of(page, 10, sort); // 페이지 크기를 고정값인 10으로 설정 + Pageable pageable = PageRequest.of(page, 10, sort); Page matchingPosts = matchingPostRepository.findAll(pageable); return MatchingPostPagingResponseDto.from(matchingPosts); } + + public MatchingPostPagingResponseDto searchByKeyWord(MatchingPostPagingRequestDto matchingPostPagingRequestDto, + String keyword) { + int page = matchingPostPagingRequestDto.getPage() - 1; + + Sort sort = Sort.by(Sort.Direction.DESC, "matchingPostId"); + Pageable pageable = PageRequest.of(page, 10, sort); + Page matchingPosts = matchingPostRepository.findByTitleContaining(keyword, pageable); + return MatchingPostPagingResponseDto.from(matchingPosts); + } + } 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 6640f3c..fef80de 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 @@ -9,17 +9,17 @@ @NoArgsConstructor @AllArgsConstructor public class MessageDTO { + private String title; + private String content; + private Long senderId; + private Long receiverId; - 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() - ); - } - + public static MessageDTO toDto(Message message) { + return new MessageDTO( + message.getTitle(), + message.getContent(), + message.getSender().getId(), + message.getReceiver().getId() + ); + } } 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 37d977e..52b5ada 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 @@ -17,46 +17,45 @@ @RequiredArgsConstructor public class MessageService { - private final MessageRepository messageRepository; - private final UserRepository userRepository; - - @Transactional - public void writeMessage(MessageDTO messageDTO, String email) { - User sender = userRepository.findByEmail(email) - .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); - - User receiver = userRepository.findById(messageDTO.getReceiverId()) - .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); - - messageRepository.save(Message.builder() - .sender(sender) - .receiver(receiver) - .title(messageDTO.getTitle()) - .content(messageDTO.getContent()) - .build()); - } - - @Transactional - public List getMessagesByReceiver(String email) { - User receiver = userRepository.findByEmail(email) - .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); - - List messages = messageRepository.findAllByReceiver(receiver); - return convertMessagesToDTO(messages); - } - - @Transactional - public List getMessagesBySender(String email) { - User sender = userRepository.findByEmail(email) - .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); - - List messages = messageRepository.findAllBySender(sender); - return convertMessagesToDTO(messages); - } - - private List convertMessagesToDTO(List messages) { - return messages.stream().map(MessageDTO::toDto).collect(Collectors.toList()); - } - + private final MessageRepository messageRepository; + private final UserRepository userRepository; + + @Transactional + public void writeMessage(MessageDTO messageDTO, String email) { + User sender = userRepository.findByEmail(email) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + + User receiver = userRepository.findById(messageDTO.getReceiverId()) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + + messageRepository.save(Message.builder() + .sender(sender) + .receiver(receiver) + .title(messageDTO.getTitle()) + .content(messageDTO.getContent()) + .build()); + } + + @Transactional + public List getMessagesByReceiver(String email) { + User receiver = userRepository.findByEmail(email) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + + List messages = messageRepository.findAllByReceiver(receiver); + return convertMessagesToDTO(messages); + } + + @Transactional + public List getMessagesBySender(String email) { + User sender = userRepository.findByEmail(email) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + + List messages = messageRepository.findAllBySender(sender); + return convertMessagesToDTO(messages); + } + + private List convertMessagesToDTO(List messages) { + return messages.stream().map(MessageDTO::toDto).collect(Collectors.toList()); + } }