Skip to content

Commit a2503d1

Browse files
authored
Merge pull request #76 from CommitField/feat/#20
Feat/#20
2 parents 57c390f + def638f commit a2503d1

File tree

20 files changed

+331
-93
lines changed

20 files changed

+331
-93
lines changed

โ€Žsrc/main/java/cmf/commitField/domain/chat/chatMessage/controller/ChatController.javaโ€Ž

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package cmf.commitField.domain.chat.chatMessage.controller;
2+
3+
import cmf.commitField.domain.chat.chatMessage.controller.request.ChatMsgRequest;
4+
import cmf.commitField.domain.chat.chatMessage.controller.response.ChatMsgResponse;
5+
import cmf.commitField.domain.chat.chatMessage.dto.ChatMsgDto;
6+
import cmf.commitField.domain.chat.chatMessage.service.ChatMessageService;
7+
import cmf.commitField.domain.user.entity.CustomOAuth2User;
8+
import cmf.commitField.global.error.ErrorCode;
9+
import cmf.commitField.global.globalDto.GlobalResponse;
10+
import cmf.commitField.global.security.LoginCheck;
11+
import jakarta.validation.Valid;
12+
import lombok.RequiredArgsConstructor;
13+
import org.springframework.security.core.Authentication;
14+
import org.springframework.security.core.context.SecurityContextHolder;
15+
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
16+
import org.springframework.web.bind.annotation.*;
17+
18+
import java.util.List;
19+
20+
@RestController
21+
@RequiredArgsConstructor
22+
@RequestMapping("/chat")
23+
public class ChatMessageController {
24+
25+
private final ChatMessageService chatMessageService;
26+
27+
@PostMapping("/msg/{roomId}")
28+
@LoginCheck
29+
public GlobalResponse<Object> sendChat(
30+
@PathVariable Long roomId,
31+
@RequestBody @Valid ChatMsgRequest message) {
32+
33+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
34+
if (!(authentication instanceof OAuth2AuthenticationToken)) {
35+
return GlobalResponse.error(ErrorCode.NOT_AUTHENTICATED);
36+
}
37+
38+
CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal();
39+
Long userId = principal.getId(); // OAuth2 ๋กœ๊ทธ์ธํ•œ ์œ ์ €์˜ ID ๊ฐ€์ ธ์˜ค๊ธฐ
40+
41+
if (message == null || message.getMessage().trim().isEmpty()) {
42+
return GlobalResponse.error(ErrorCode.EMPTY_MESSAGE);
43+
}
44+
45+
ChatMsgResponse response = chatMessageService.sendMessage(message, userId, roomId);
46+
return GlobalResponse.success("์ฑ„ํŒ… ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ ์„ฑ๊ณต", response);
47+
}
48+
49+
@GetMapping("/msg/{roomId}")
50+
@LoginCheck
51+
public GlobalResponse<Object> getChatList(
52+
@PathVariable Long roomId,
53+
@RequestParam(required = false) Long lastId) {
54+
55+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
56+
if (!(authentication instanceof OAuth2AuthenticationToken)) {
57+
return GlobalResponse.error(ErrorCode.NOT_AUTHENTICATED);
58+
}
59+
60+
CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal();
61+
Long userId = principal.getId(); // OAuth2 ๋กœ๊ทธ์ธํ•œ ์œ ์ €์˜ ID ๊ฐ€์ ธ์˜ค๊ธฐ
62+
63+
List<ChatMsgDto> roomChatMsgList = chatMessageService.getRoomChatMsgList(roomId, userId, lastId);
64+
if (roomChatMsgList == null || roomChatMsgList.isEmpty()) {
65+
return GlobalResponse.error(ErrorCode.CHAT_NOT_FOUND);
66+
}
67+
68+
return GlobalResponse.success("ํ•ด๋‹น ์ฑ„ํŒ…๋ฐฉ์˜ ๋ฉ”์‹œ์ง€๋“ค์„ ์กฐํšŒํ•˜์˜€์Šต๋‹ˆ๋‹ค.", roomChatMsgList);
69+
}
70+
}
71+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cmf.commitField.domain.chat.chatMessage.controller.request;
2+
3+
import jakarta.validation.constraints.NotEmpty;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import org.hibernate.validator.constraints.Length;
8+
9+
@Getter
10+
@AllArgsConstructor
11+
@NoArgsConstructor
12+
public class ChatMsgRequest {
13+
@NotEmpty
14+
@Length(max = 300)
15+
private String message;
16+
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cmf.commitField.domain.chat.chatMessage.controller.response;
2+
3+
import lombok.*;
4+
5+
import java.time.LocalDateTime;
6+
7+
@Getter
8+
@Setter
9+
@NoArgsConstructor
10+
@AllArgsConstructor
11+
@Builder
12+
public class ChatMsgResponse {
13+
private Long roomId;
14+
//์‚ฌ์šฉ์ž(user)
15+
private String from;
16+
private String message;
17+
private LocalDateTime sendAt;
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cmf.commitField.domain.chat.chatMessage.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
8+
import java.time.LocalDateTime;
9+
10+
@Getter
11+
@Setter
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
public class ChatMsgDto {
15+
private Long chatMsgId;
16+
private Long userId;
17+
private String nickname;
18+
private String message;
19+
private LocalDateTime sendAt;
20+
}

src/main/java/cmf/commitField/domain/chat/chatMessage/entity/ChatMessage.java renamed to src/main/java/cmf/commitField/domain/chat/chatMessage/entity/ChatMsg.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
@Setter
1717
@NoArgsConstructor(access = AccessLevel.PROTECTED)
1818
@AllArgsConstructor(access = AccessLevel.PROTECTED)
19-
public class ChatMessage extends BaseEntity {
19+
public class ChatMsg extends BaseEntity {
2020

2121
private String message;
2222

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cmf.commitField.domain.chat.chatMessage.repository;
2+
3+
import cmf.commitField.domain.chat.chatMessage.entity.ChatMsg;
4+
import org.springframework.stereotype.Repository;
5+
6+
import java.util.List;
7+
8+
@Repository
9+
public interface ChatMessageCustomRepository {
10+
List<ChatMsg> findChatRoomIdByChatMsg(Long chatMsg, Long lastId);
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package cmf.commitField.domain.chat.chatMessage.repository;
2+
3+
import cmf.commitField.domain.chat.chatMessage.entity.ChatMsg;
4+
import jakarta.persistence.EntityManager;
5+
import jakarta.persistence.TypedQuery;
6+
import lombok.RequiredArgsConstructor;
7+
import org.springframework.stereotype.Repository;
8+
9+
import java.util.List;
10+
11+
@Repository
12+
@RequiredArgsConstructor
13+
public class ChatMessageCustomRepositoryImpl implements ChatMessageCustomRepository {
14+
15+
private final EntityManager entityManager;
16+
17+
@Override
18+
public List<ChatMsg> findChatRoomIdByChatMsg(Long chatMsg, Long lastId) {
19+
String first = "select c from ChatMsg c where c.chatRoom.id =: chatMsg order by c.id asc";
20+
String paging = "select c from ChatMsg c where c.chatRoom.id =: chatMsg and c.id > :lastId order by c.id asc";
21+
22+
TypedQuery<ChatMsg> query = null;
23+
24+
if (lastId == null) {
25+
query = entityManager
26+
.createQuery(first, ChatMsg.class)
27+
.setParameter("chatMsg", chatMsg);
28+
} else {
29+
query = entityManager
30+
.createQuery(paging, ChatMsg.class)
31+
.setParameter("chatMsg", chatMsg)
32+
.setParameter("lastId", lastId);
33+
}
34+
return query
35+
.setMaxResults(10)
36+
.getResultList();
37+
}
38+
39+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package cmf.commitField.domain.chat.chatMessage.repository;
22

3-
import cmf.commitField.domain.chat.chatMessage.entity.ChatMessage;
3+
import cmf.commitField.domain.chat.chatMessage.entity.ChatMsg;
44
import org.springframework.data.jpa.repository.JpaRepository;
55
import org.springframework.stereotype.Repository;
66

77
@Repository
8-
public interface ChatMessageRepository extends JpaRepository<ChatMessage, Long> {
8+
public interface ChatMessageRepository extends JpaRepository<ChatMsg, Long> {
9+
void deleteChatMsgByChatRoom_Id(Long chatRoomId);
910
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cmf.commitField.domain.chat.chatMessage.service;
2+
3+
import cmf.commitField.domain.chat.chatMessage.controller.request.ChatMsgRequest;
4+
import cmf.commitField.domain.chat.chatMessage.controller.response.ChatMsgResponse;
5+
import cmf.commitField.domain.chat.chatMessage.dto.ChatMsgDto;
6+
7+
import java.util.List;
8+
9+
public interface ChatMessageService {
10+
ChatMsgResponse sendMessage(ChatMsgRequest message, Long userId, Long roomId);
11+
12+
List<ChatMsgDto> getRoomChatMsgList(Long roomId, Long userId, Long lastId);
13+
}

0 commit comments

Comments
ย (0)