Skip to content

Commit

Permalink
Feat: CHAT-223-채팅-조회-API (#28)
Browse files Browse the repository at this point in the history
* Feat: 채팅 조회 기능 구현

* Refactor: 채팅 10개 반환
  • Loading branch information
Kjiw0n authored Jan 30, 2024
1 parent ea2c38c commit 101254c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/java/com/kuit/chatdiary/controller/ChatController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.kuit.chatdiary.controller;

import com.kuit.chatdiary.dto.chat.ChatGetResponseDTO;
import com.kuit.chatdiary.service.ChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/chat")
public class ChatController {

@Autowired
private ChatService chatService;

@GetMapping("/get")
public ResponseEntity<List<ChatGetResponseDTO>> getChats(@RequestParam Long chatId) {
List<ChatGetResponseDTO> chats = chatService.getChats(chatId);
if (chats == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Collections.emptyList());
}
return ResponseEntity.ok(chatService.getChats(chatId));
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/kuit/chatdiary/dto/chat/ChatGetResponseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.kuit.chatdiary.dto.chat;

import com.kuit.chatdiary.domain.Chat;
import com.kuit.chatdiary.domain.ChatType;
import com.kuit.chatdiary.domain.Sender;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
public class ChatGetResponseDTO {

private Long chatId;
private LocalDateTime createAt;
private String content;
private ChatType chatType;
private Sender sender;

public ChatGetResponseDTO(Chat chat) {
this.chatId = chat.getChatId();
this.createAt = chat.getCreateAt();
this.content = chat.getContent();
this.chatType = chat.getChatType();
this.sender = chat.getSender();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
public interface ChatRepository extends JpaRepository<Chat, Long> {

List<Chat> findTop10ByMember_UserIdOrderByChatIdDesc(Long userId);
List<Chat> findTop10ByChatIdGreaterThan(Long lastChatId);
}
14 changes: 14 additions & 0 deletions src/main/java/com/kuit/chatdiary/service/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.kuit.chatdiary.domain.*;
import com.kuit.chatdiary.dto.chat.ChatGetResponseDTO;
import com.kuit.chatdiary.repository.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ChatService {
Expand Down Expand Up @@ -66,4 +70,14 @@ public String extractGptResponse(String jsonResponse) throws JsonProcessingExcep
return "";
}

public List<ChatGetResponseDTO> getChats(Long chatId) {
List<Chat> chats = chatRepository.findTop10ByChatIdGreaterThan(chatId);
if (chats.isEmpty()) {
return null;
}
return chats.stream()
.map(ChatGetResponseDTO::new)
.collect(Collectors.toList());
}

}

0 comments on commit 101254c

Please sign in to comment.