Skip to content

Commit

Permalink
Merge pull request #83 from dongkyun0713/dongkyun
Browse files Browse the repository at this point in the history
๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ ๋ฐ ์ชฝ์ง€ ์ˆ˜์ •
  • Loading branch information
dongkyun0713 authored Feb 24, 2024
2 parents 25c0080 + 9e3daa6 commit 8ef63b9
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MatchingBoardController {

private final MatchingBoardService matchingBoardService;

@GetMapping("/posts")
@GetMapping("/all")
@Operation(
summary = "๋งค์นญ ๊ฒŒ์‹œํŒ ์ „์ฒด ์กฐํšŒ",
description = "๋งค์นญ ๊ฒŒ์‹œํŒ์˜ ์ „์ฒด ๊ฒŒ์‹œ๊ธ€์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค",
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<MatchingPost, Long> {
Page<MatchingPost> findByTitleContaining(String keyword, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<MatchingPost> 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<MatchingPost> matchingPosts = matchingPostRepository.findByTitleContaining(keyword, pageable);
return MatchingPostPagingResponseDto.from(matchingPosts);
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<MessageDTO> getMessagesByReceiver(String email) {
User receiver = userRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

List<Message> messages = messageRepository.findAllByReceiver(receiver);
return convertMessagesToDTO(messages);
}

@Transactional
public List<MessageDTO> getMessagesBySender(String email) {
User sender = userRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

List<Message> messages = messageRepository.findAllBySender(sender);
return convertMessagesToDTO(messages);
}

private List<MessageDTO> convertMessagesToDTO(List<Message> 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<MessageDTO> getMessagesByReceiver(String email) {
User receiver = userRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

List<Message> messages = messageRepository.findAllByReceiver(receiver);
return convertMessagesToDTO(messages);
}

@Transactional
public List<MessageDTO> getMessagesBySender(String email) {
User sender = userRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

List<Message> messages = messageRepository.findAllBySender(sender);
return convertMessagesToDTO(messages);
}

private List<MessageDTO> convertMessagesToDTO(List<Message> messages) {
return messages.stream().map(MessageDTO::toDto).collect(Collectors.toList());
}
}

0 comments on commit 8ef63b9

Please sign in to comment.