Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
public interface ApplyRepositoryCustom {
Optional<ApplyInfoDto> getApplyInfoResponse(Long boardId, Long memberId, String delYn);

//지원한 산책의 채팅방 조회
List<ChatRoomListResponseDto> getApplyChatList(Long memberId, MatchingStatus status);

//내가 지원한 산책 리스트 조회
List<MatchingResponseDto> getApplyInfoResponses(Long memberId, WalkMatchingStatus status, String delYn);
List<ApplicantInfoResponseDto> getApplicantList(Long boardId, String delYn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,57 +163,6 @@ else if (status.equals(WalkMatchingStatus.REJECT)) {
.fetch();
}

//지원한 산책의 채팅리스트 조회
@Override
public List<ChatRoomListResponseDto> getApplyChatList(Long memberId, MatchingStatus status) {
QDog dog = QDog.dog;
QMember member = QMember.member;
QBoard board = QBoard.board;
QApply apply = QApply.apply;
QAddress address = QAddress.address;

QChatRoom chatRoom=QChatRoom.chatRoom;
QChat chat= QChat.chat;

//대화 상대의 마지막 메세지 가져오기: chat 테이블에서 Id의 최댓값을 가져온다
SubQueryExpression<Long> lastMessageSubQuery = JPAExpressions.select(chat.chatId.max())
.from(chat)
.leftJoin(chatRoom).on(chat.roomId.eq(chatRoom.roomId))
.where(chat.roomId.eq(chatRoom.roomId)
.and(chat.senderId.ne(memberId)));



List<ChatRoomListResponseDto> chatRoomListResponseDtos=
queryFactory.selectDistinct(
Projections.constructor(ChatRoomListResponseDto.class,
dog.name.as("dogName"),
dog.profile.as("dogProfile"),
board.startTime.as("startTime"),
board.endTime.as("endTime"),
chatRoom.chatOwnerId.as("chatTarget"), //채팅 대상
chat.message.as("lastChat"), //상대의 마지막 채팅
chat.createdAt.as("lastChatTime"), // 상대의 마지막 채팅 내용
Expressions.asString("").as("targetName"),// 상대방 이름을 공백으로 지정
Expressions.asNumber(10).as("notRead"),
chat.roomId.as("roomId"),
board.boardId.as("boardId")

))
.from(board)
.leftJoin(dog).on(dog.dogId.eq(board.dogId))
.leftJoin(apply).on(apply.boardId.eq(board.boardId))
//boardId와 chatParticipantId는 1:1 관계
.leftJoin(chatRoom).on(chatRoom.boardId.eq(board.boardId))
.leftJoin(chat).on(chat.roomId.eq(chatRoom.roomId))
.where(apply.memberId.eq(memberId)
.and(apply.matchingStatus.eq(status))
.and(chat.chatId.eq(lastMessageSubQuery)))
.fetch();

return chatRoomListResponseDtos;
}

/**
* 반려인이 산책 지원자들의 정보를 조회한다
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,6 @@ public List<MatchingResponseDto> getAllAppliedInfoWithStatus(TabStatus tabStatus

}

//지원한 산책의 채팅방 조회
public List<ChatRoomListResponseDto> getAllChatListWithStatus(MatchingStatus status) {
Long memberId = memberService.getMemberFromUserDetail().getMemberId();
log.info("사용자 id {}", memberId);
List<ChatRoomListResponseDto> chatList = applyRepository.getApplyChatList(memberId, status);
return chatList;
}

public List<ApplicantInfoResponseDto> getApplicantList(Long boardId) {
return applyRepository.getApplicantList(boardId,"N");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package org.jullaene.walkmong_back.api.chat.dto.res;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.*;
import org.jullaene.walkmong_back.api.board.domain.Board;
import org.jullaene.walkmong_back.api.chat.domain.Chat;
import org.jullaene.walkmong_back.api.chat.domain.ChatRoom;
import org.jullaene.walkmong_back.api.dog.domain.Dog;
import org.jullaene.walkmong_back.api.member.domain.Member;
import org.jullaene.walkmong_back.common.enums.TabStatus;

import java.time.LocalDateTime;

@Getter
@RequiredArgsConstructor
@AllArgsConstructor
@Setter
public class ChatRoomListResponseDto {
private TabStatus tabStatus;
private String dogName;
private String dogProfile;
private LocalDateTime startTime;
Expand All @@ -24,4 +27,28 @@ public class ChatRoomListResponseDto {
private Long roomId;
private Long boardId;

@Builder
public ChatRoomListResponseDto(Dog dog,
Board board,
Member target,
Chat chat,
ChatRoom chatRoom,
TabStatus tabStatus
) {
this.tabStatus = tabStatus;
this.dogName = dog.getName();
this.dogProfile = dog.getProfile();
this.startTime = board.getStartTime();
this.endTime = board.getEndTime();
this.chatTarget = target.getMemberId();
this.targetName = target.getNickname();
this.notRead = 0;
this.roomId = chatRoom.getRoomId();
this.boardId = board.getBoardId();

if (chat != null) {
this.lastChat = chat.getMessage();
this.lastChatTime = chat.getCreatedAt();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface ChatRepository extends JpaRepository<Chat,Long> {
List<Chat> findAllByRoomId(Long roomId);
Optional<Chat> findFirstByRoomIdAndDelYnOrderByCreatedAtDesc(Long roomId, String delYn);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface ChatRoomRepository extends JpaRepository<ChatRoom,Long> {
Optional<ChatRoom> findByRoomIdAndDelYn(Long chatRoomId, String delYn);

List<ChatRoom> findByChatParticipantIdAndDelYn(Long memberId, String delYn);

List<ChatRoom> findByChatOwnerIdAndDelYn(Long memberId, String delYn);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.jullaene.walkmong_back.api.chat.rest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jullaene.walkmong_back.WalkmongBackApplication;
import org.jullaene.walkmong_back.api.apply.domain.enums.MatchingStatus;
import org.jullaene.walkmong_back.api.apply.dto.enums.WalkMatchingStatus;
import org.jullaene.walkmong_back.api.apply.service.ApplyService;
import org.jullaene.walkmong_back.api.board.service.BoardService;
import org.jullaene.walkmong_back.api.chat.dto.req.ChatMessageRequestDto;
Expand Down Expand Up @@ -53,42 +55,8 @@ public ResponseEntity<BasicResponse<List<ChatHistoryResponseDto>>> getChatHistor
//지원|의뢰 && 매칭상태에 따른 채팅방 리스트 조회
@GetMapping("/list")
public ResponseEntity<BasicResponse<List<ChatRoomListResponseDto>>> getChatRoomList(@RequestParam("record") String record,
@RequestParam("status")MatchingStatus status){
/*
record
applied: 지원한 산책
requested: 의뢰한 산책
all: 전체
*/

/*
<status>
PENDING: 매칭중
CONFIRMED: 매칭확정
REJECTED: 매칭취소
*/
List<ChatRoomListResponseDto> chatRoomListResponseDto=null;
if (record.equals("applied")){
chatRoomListResponseDto=applyService.getAllChatListWithStatus(status);

}else if (record.equals("requested")) {
chatRoomListResponseDto = boardService.getAllChatListWithStatus(status);
}else if (record.equals("all")){
List<ChatRoomListResponseDto> appliedList = applyService.getAllChatListWithStatus(status);
List<ChatRoomListResponseDto> requestedList = boardService.getAllChatListWithStatus(status);

chatRoomListResponseDto = new ArrayList<>();
chatRoomListResponseDto.addAll(appliedList);
chatRoomListResponseDto.addAll(requestedList);

}

//공백으로 설정한 상대방 이름을 실명으로 전환
for (ChatRoomListResponseDto dto:chatRoomListResponseDto){
Long memberId=dto.getChatTarget();
log.info("상대방 아이디 {}",memberId);
dto.setTargetName(memberRepository.findNickNameByMemberId(memberId));
}
@RequestParam("status") WalkMatchingStatus status){
List<ChatRoomListResponseDto> chatRoomListResponseDto = chatRoomService.getChatRoomList(record, status);

return ResponseEntity.ok(BasicResponse.ofSuccess(chatRoomListResponseDto));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
package org.jullaene.walkmong_back.api.chat.service;

import com.google.api.Http;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.IdentifierLoadAccess;
import org.jullaene.walkmong_back.api.apply.dto.enums.WalkMatchingStatus;
import org.jullaene.walkmong_back.api.board.domain.Board;
import org.jullaene.walkmong_back.api.board.domain.enums.WalkingStatus;
import org.jullaene.walkmong_back.api.board.repository.BoardRepository;
import org.jullaene.walkmong_back.api.chat.domain.Chat;
import org.jullaene.walkmong_back.api.chat.domain.ChatRoom;
import org.jullaene.walkmong_back.api.chat.domain.enums.MessageType;
import org.jullaene.walkmong_back.api.chat.dto.req.ChatMessageRequestDto;
import org.jullaene.walkmong_back.api.chat.dto.res.ChatHistoryResponseDto;
import org.jullaene.walkmong_back.api.chat.dto.res.ChatMessageResponseDto;
import org.jullaene.walkmong_back.api.chat.dto.res.ChatRoomListResponseDto;
import org.jullaene.walkmong_back.api.chat.repository.ChatRepository;
import org.jullaene.walkmong_back.api.chat.repository.ChatRoomRepository;

import org.jullaene.walkmong_back.api.dog.domain.Dog;
import org.jullaene.walkmong_back.api.dog.repository.DogRepository;
import org.jullaene.walkmong_back.api.member.domain.Member;
import org.jullaene.walkmong_back.api.member.service.MemberService;
import org.jullaene.walkmong_back.common.enums.TabStatus;
import org.jullaene.walkmong_back.common.exception.CustomException;
import org.jullaene.walkmong_back.common.exception.ErrorType;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


@Service
Expand All @@ -33,6 +43,7 @@ public class ChatRoomService {
private final ChatRepository chatRepository;
private final MemberService memberService;
private final BoardRepository boardRepository;
private final DogRepository dogRepository;

//채팅방 생성
public Long createRoom(Long boardId) {
Expand Down Expand Up @@ -111,4 +122,128 @@ public ChatMessageResponseDto saveMessage(ChatMessageRequestDto message) {
.createdAt(savedChat.getCreatedAt())
.build();
}

public List<ChatRoomListResponseDto> getChatRoomList(String record, WalkMatchingStatus status) {
Member member = memberService.getMemberFromUserDetail();

List<ChatRoomListResponseDto> chatRoomListResponseDtos = new ArrayList<>();

// 지원한 산책
if (record.equals("applied") || record.equals("all")) {
List<ChatRoom> chatRooms = chatRoomRepository.findByChatParticipantIdAndDelYn(member.getMemberId(), "N");

chatRooms.stream()
.filter(chatRoom -> {
log.info("hello");
Board board = boardRepository.findByBoardIdAndDelYn(chatRoom.getBoardId(), "N")
.orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, ErrorType.INVALID_BOARD));

LocalDateTime now = LocalDateTime.now();

WalkMatchingStatus boardStatus = null;
if (board.getWalkingStatus().equals(WalkingStatus.PENDING) && board.getStartTime().isAfter(now)) {
boardStatus = WalkMatchingStatus.PENDING;
}
else if (board.getWalkingStatus().equals(WalkingStatus.BEFORE)) {
boardStatus = WalkMatchingStatus.BEFORE;
}
else if (board.getWalkingStatus().equals(WalkingStatus.ING) || board.getWalkingStatus().equals(WalkingStatus.AFTER)) {
boardStatus = WalkMatchingStatus.AFTER;
}
else if (board.getWalkingStatus().equals(WalkingStatus.PENDING) && board.getStartTime().isBefore(now)) {
boardStatus = WalkMatchingStatus.REJECT;
}

return boardStatus == status;
})
.forEach(chatRoom -> {
Member owner = memberService.getMemberByMemberId(chatRoom.getChatOwnerId())
.orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, ErrorType.INVALID_USER));

Board board = boardRepository.findByBoardIdAndDelYn(chatRoom.getBoardId(), "N")
.orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, ErrorType.INVALID_BOARD));

Dog dog = dogRepository.findByDogIdAndDelYn(board.getDogId(), "N")
.orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, ErrorType.INVALID_DOG));

Chat chat = chatRepository.findFirstByRoomIdAndDelYnOrderByCreatedAtDesc(chatRoom.getRoomId(), "N")
.orElseThrow(null);

ChatRoomListResponseDto chatRoomListResponseDto = ChatRoomListResponseDto.builder()
.tabStatus(TabStatus.APPLY)
.chatRoom(chatRoom)
.board(board)
.dog(dog)
.chat(chat)
.target(owner)
.build();

chatRoomListResponseDtos.add(chatRoomListResponseDto);
});
}

if (record.equals("requested") || record.equals("all")) {
List<ChatRoom> chatRooms = chatRoomRepository.findByChatOwnerIdAndDelYn(member.getMemberId(), "N");

chatRooms.stream()
.filter(chatRoom -> {
log.info("hello1");
Board board = boardRepository.findByBoardIdAndDelYn(chatRoom.getBoardId(), "N")
.orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, ErrorType.INVALID_BOARD));

LocalDateTime now = LocalDateTime.now();

WalkMatchingStatus boardStatus = null;
if (board.getWalkingStatus().equals(WalkingStatus.PENDING) && board.getStartTime().isAfter(now)) {
boardStatus = WalkMatchingStatus.PENDING;
}
else if (board.getWalkingStatus().equals(WalkingStatus.BEFORE)) {
boardStatus = WalkMatchingStatus.BEFORE;
}
else if (board.getWalkingStatus().equals(WalkingStatus.ING) || board.getWalkingStatus().equals(WalkingStatus.AFTER)) {
boardStatus = WalkMatchingStatus.AFTER;
}
else if (board.getWalkingStatus().equals(WalkingStatus.PENDING) && board.getStartTime().isBefore(now)) {
boardStatus = WalkMatchingStatus.REJECT;
}

return boardStatus == status;
})
.forEach(chatRoom -> {

log.info("hello2");
Member walker = memberService.getMemberByMemberId(chatRoom.getChatParticipantId())
.orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, ErrorType.INVALID_USER));


log.info("walker : " + walker.getMemberId());
Board board = boardRepository.findByBoardIdAndDelYn(chatRoom.getBoardId(), "N")
.orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, ErrorType.INVALID_BOARD));


log.info("board : " + board.getBoardId());

Dog dog = dogRepository.findByDogIdAndDelYn(board.getDogId(), "N")
.orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, ErrorType.INVALID_DOG));

Chat chat = chatRepository.findFirstByRoomIdAndDelYnOrderByCreatedAtDesc(chatRoom.getRoomId(), "N")
.orElseThrow(null);

ChatRoomListResponseDto chatRoomListResponseDto = ChatRoomListResponseDto.builder()
.tabStatus(TabStatus.BOARD)
.chatRoom(chatRoom)
.board(board)
.dog(dog)
.chat(chat)
.target(walker)
.build();

chatRoomListResponseDtos.add(chatRoomListResponseDto);
});
}

return chatRoomListResponseDtos.stream()
.sorted(Comparator.comparing(ChatRoomListResponseDto::getStartTime))
.collect(Collectors.toList());
}
}
Loading
Loading