Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#34/feat/채팅리스트기능 #39

Merged
merged 1 commit into from
Nov 2, 2024
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 @@ -27,26 +27,26 @@ public AuthController(final AuthService authService) {
this.authService = authService;
}

@PostMapping("/register")
@Operation(summary = "회원 가입", description = "가입 요청, 등록")
@PostMapping("/register")
public ResponseEntity<String> register(@RequestBody RegisterRequest authRequest) {
log.info("[가입 요청] 아이디: {} / 이름: {}", authRequest.personId(), authRequest.personName());
authService.register(authRequest);
log.info("[가입 성공] 아이디: {} / 이름: {}", authRequest.personId(), authRequest.personName());
return ResponseEntity.ok("가입 성공");
}

@PostMapping("/login")
@Operation(summary = "로그인", description = "로그인 요청")
@PostMapping("/login")
public LoginResponse login(@RequestBody LoginRequest loginRequest, HttpServletRequest request, HttpServletResponse response) {
log.info("[로그인 요청] 아이디: {}", loginRequest.personId());
LoginResponse loginResponse = authService.login(loginRequest, request, response);
log.info("[로그인 성공] 아이디: {}", loginRequest.personId());
return loginResponse;
}

@PostMapping("/logout")
@Operation(summary = "로그아웃", description = "로그아웃 요청")
@PostMapping("/logout")
public ResponseEntity<String> logout(HttpSession session) {
log.info("[로그아웃 요청] 아이디: {}", session.getAttribute("personId"));
authService.logout(session);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
import com.chat.liveon.chat.dto.request.ChatMessageRequest;
import com.chat.liveon.chat.dto.response.ChatMessageResponse;
import com.chat.liveon.chat.service.ChatMessageService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/api")
Expand All @@ -23,4 +30,11 @@ public ChatMessageResponse sendMessage(@DestinationVariable("roomId") String roo
Long roomIdLong = Long.parseLong(roomId);
return chatMessageService.sendMessage(roomIdLong, message);
}

@Operation(summary = "이전 채팅 메시지 조회 API")
@GetMapping("/chat-room/{roomId}/messages")
public ResponseEntity<List<ChatMessageResponse>> getChatMessages(@PathVariable("roomId") Long roomId, @RequestParam(name = "page", defaultValue = "0") int page, @RequestParam(name = "size", defaultValue = "20") int size) {
List<ChatMessageResponse> messages = chatMessageService.getMessagesByRoomId(roomId, page, size);
return ResponseEntity.ok(messages);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ public class ChatRoomController {

private final ChatRoomService chatRoomService;

@PostMapping("/chat-room")
@Operation(summary = "채팅방 생성 API")
@PostMapping("/chat-room")
public ResponseEntity<ChatRoomResponse> createChatRoom(@RequestBody ChatRoomRequest chatRoomRequest,
HttpSession session) {
String personId = (String) session.getAttribute("personId");
ChatRoomResponse response = chatRoomService.createChatRoom(chatRoomRequest, personId);
return ResponseEntity.ok(response);
}

@GetMapping("/chat-room")
@Operation(summary = "채팅방 조회 API")
@GetMapping("/chat-room")
public ResponseEntity<List<AllChatRoomResponse>> getChatRoom() {
List<AllChatRoomResponse> response = chatRoomService.getAllChatRoom();
return ResponseEntity.ok(response);
Expand Down
9 changes: 0 additions & 9 deletions src/main/java/com/chat/liveon/chat/entity/ChatMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,4 @@ public ChatMessage(Person sender, String message, ChatRoom chatRoom) {
this.chatRoom = chatRoom;
this.sendDate = LocalDateTime.now();
}

public static ChatMessage createChatMessage(Person sender, String message, ChatRoom chatRoom) {
ChatMessage chatMessage = ChatMessage.builder()
.sender(sender)
.message(message)
.chatRoom(chatRoom)
.build();
return chatMessage;
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/chat/liveon/chat/entity/ChatMessageStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.chat.liveon.chat.entity;

import com.chat.liveon.auth.entity.Person;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@NoArgsConstructor
@Getter
public class ChatMessageStatus {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@JoinColumn(name = "message_id", nullable = false)
private ChatMessage message;

@ManyToOne
@JoinColumn(name = "person_id", nullable = false)
private Person person;

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.chat.liveon.chat.repository;

import com.chat.liveon.chat.entity.ChatMessage;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ChatMessageRepository extends JpaRepository<ChatMessage, Long> {
Page<ChatMessage> findByChatRoom_IdOrderBySendDateDesc(Long roomId, Pageable pageable);
}
23 changes: 20 additions & 3 deletions src/main/java/com/chat/liveon/chat/service/ChatMessageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@
import com.chat.liveon.chat.repository.ChatRoomRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Slf4j
@RequiredArgsConstructor
@Service
Expand All @@ -36,16 +42,27 @@ public ChatMessageResponse sendMessage(Long roomId, ChatMessageRequest messageRe
.build();

chatMessage = chatMessageRepository.save(chatMessage);

log.info("[메시지 전송 성공] 채팅방: {}, 사용자: {}, 메시지: {}, 시간: {}", roomId, sender.getPersonId(), messageRequest.message(), chatMessage.getSendDate());
log.info("[메시지 전송 성공] 채팅방: {}, 사용자: {}, 메시지: {}", roomId, sender.getPersonName(), messageRequest.message());

return new ChatMessageResponse(
chatMessage.getChatRoom().getId(),
sender.getPersonId(),
sender.getPersonName(),
chatMessage.getMessage(),
chatMessage.getSendDate()
);
}

@Transactional(readOnly = true)
public List<ChatMessageResponse> getMessagesByRoomId(Long roomId, int page, int size) {
Pageable pageable = PageRequest.of(page, size);
Page<ChatMessage> messagesPage = chatMessageRepository.findByChatRoom_IdOrderBySendDateDesc(roomId, pageable);

return messagesPage.stream()
.map(message -> new ChatMessageResponse(
message.getChatRoom().getId(),
message.getSender().getPersonName(),
message.getMessage(),
message.getSendDate()))
.collect(Collectors.toList());
}
}