-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/join
- Loading branch information
Showing
21 changed files
with
453 additions
and
97 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/main/java/com/wooyeon/yeon/chat/controller/ChatController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.wooyeon.yeon.chat.controller; | ||
|
||
import com.wooyeon.yeon.chat.dto.ChatDto; | ||
import com.wooyeon.yeon.chat.service.ChatService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/chat") | ||
public class ChatController { | ||
|
||
private final ChatService chatService; | ||
|
||
@GetMapping("/list") | ||
public ChatDto.Response getChatList(@RequestParam Long matchId) { | ||
return chatService.getChatList(matchId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.wooyeon.yeon.chat.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
import javax.validation.constraints.NotNull; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
public class ChatDto { | ||
|
||
@Getter | ||
@Builder | ||
public static class Request { | ||
@NotNull | ||
private Long matchId; | ||
} | ||
|
||
@Getter | ||
@Builder | ||
public static class Response { | ||
private List<ChatResponse> chatData; | ||
} | ||
|
||
@Getter | ||
@Builder | ||
public static class ChatResponse { | ||
private String message; | ||
private LocalDateTime sendTime; | ||
private String sender; | ||
private Boolean isChecked; | ||
private Boolean isSender; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,74 @@ | ||
package com.wooyeon.yeon.chat.dto; | ||
|
||
|
||
import com.wooyeon.yeon.chat.domain.Chat; | ||
import com.wooyeon.yeon.user.domain.Profile; | ||
import com.wooyeon.yeon.user.domain.ProfilePhoto; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
public class RoomDto { | ||
|
||
@Getter | ||
@Builder | ||
public static class RoomResponse { | ||
private List<ChatResponse> chatRoomList; | ||
} | ||
|
||
@Getter | ||
@Builder | ||
public static class ChatResponse { | ||
private Long matchId; | ||
private Long profilePhoto; | ||
private String profilePhoto; | ||
private String name; | ||
private LocalDateTime lastTime; | ||
private String lastMessage; | ||
private boolean pinToTop; | ||
private int unReadChatCount; | ||
private Long unReadChatCount; | ||
} | ||
|
||
@Getter | ||
public static class SearchRoomRequest { | ||
@NonNull | ||
private String SearchWord; | ||
} | ||
|
||
@Getter | ||
@Builder | ||
public static class SearchRoomResponse { | ||
private Long matchId; | ||
private Long profilePhoto; | ||
private String profilePhoto; | ||
private String name; | ||
} | ||
|
||
public static ChatResponse updateChatInfo(ChatResponse response, Chat chat) { | ||
response.lastMessage = chat.getMessage(); | ||
response.lastTime = chat.getSendTime(); | ||
|
||
return response; | ||
} | ||
|
||
public static ChatResponse updateProfilePhoto(ChatResponse response, ProfilePhoto profilePhoto) { | ||
response.profilePhoto = profilePhoto.getPhotoUrl(); | ||
return response; | ||
} | ||
|
||
public static SearchRoomResponse updateProfilePhoto(SearchRoomResponse response, ProfilePhoto profilePhoto) { | ||
response.profilePhoto = profilePhoto.getPhotoUrl(); | ||
return response; | ||
} | ||
|
||
public static SearchRoomResponse updateProfile(SearchRoomResponse response, Profile profile) { | ||
response.name = profile.getNickname(); | ||
return response; | ||
} | ||
|
||
public static ChatResponse updateProfile(ChatResponse response, Profile profile) { | ||
response.name = profile.getNickname(); | ||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 72 additions & 1 deletion
73
src/main/java/com/wooyeon/yeon/chat/service/ChatService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,103 @@ | ||
package com.wooyeon.yeon.chat.service; | ||
|
||
import com.wooyeon.yeon.chat.domain.Chat; | ||
import com.wooyeon.yeon.chat.dto.ChatDto; | ||
import com.wooyeon.yeon.chat.dto.StompDto; | ||
import com.wooyeon.yeon.chat.repository.ChatRepository; | ||
import com.wooyeon.yeon.common.security.SecurityService; | ||
import com.wooyeon.yeon.exception.ExceptionMessage; | ||
import com.wooyeon.yeon.profileChoice.domain.UserMatch; | ||
import com.wooyeon.yeon.profileChoice.repository.MatchRepository; | ||
import com.wooyeon.yeon.user.domain.Profile; | ||
import com.wooyeon.yeon.user.domain.User; | ||
import com.wooyeon.yeon.user.repository.ProfileRepository; | ||
import com.wooyeon.yeon.user.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.socket.WebSocketSession; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ChatService { | ||
|
||
private final ChatRepository chatRepository; | ||
private final MatchRepository matchRepository; | ||
private final SecurityService securityService; | ||
private final UserRepository userRepository; | ||
private final ProfileRepository profileRepository; | ||
|
||
private final List<WebSocketSession> sessions = new CopyOnWriteArrayList<>(); | ||
|
||
@Transactional | ||
public void saveChat(StompDto stompDto) { | ||
UserMatch userMatch = matchRepository.findById(stompDto.getRoomId()) | ||
.orElseThrow(() -> new IllegalArgumentException()); | ||
.orElseThrow(() -> new IllegalArgumentException(ExceptionMessage.USER_MATCH_NOT_FOUND.toString())); | ||
|
||
Chat chat = Chat.builder() | ||
.message(stompDto.getMessage()) | ||
.sendTime(LocalDateTime.now()) | ||
.userMatch(userMatch) | ||
.sender(getLoginUserNickName()) | ||
// .isChecked() //stomp 연결되어 있으면 check | ||
.build(); | ||
|
||
chatRepository.save(chat); | ||
} | ||
|
||
public ChatDto.Response getChatList(Long matchId) { | ||
|
||
UserMatch userMatch = matchRepository.findById(matchId) | ||
.orElseThrow(() -> new IllegalArgumentException(ExceptionMessage.USER_MATCH_NOT_FOUND.toString())); | ||
|
||
List<Chat> chatList = chatRepository.findAllByUserMatchOrderBySendTime(userMatch); | ||
List<ChatDto.ChatResponse> responseList = new ArrayList<>(); | ||
|
||
String userName = getLoginUserNickName(); | ||
|
||
for (Chat chat : chatList) { | ||
responseList.add(makeResponse(chat, userName)); | ||
} | ||
|
||
return ChatDto.Response.builder() | ||
.chatData(responseList) | ||
.build(); | ||
} | ||
|
||
public ChatDto.ChatResponse makeResponse(Chat chat, String userName) { | ||
return ChatDto.ChatResponse.builder() | ||
.message(chat.getMessage()) | ||
.sender(chat.getSender()) | ||
.sendTime(chat.getSendTime()) | ||
.isChecked(chat.isChecked()) | ||
.isSender(chat.getSender().equals(userName)) | ||
.build(); | ||
} | ||
|
||
public String getLoginUserNickName() { | ||
User loginUser = userRepository.findOptionalByEmail(securityService.getCurrentUserEmail()) | ||
.orElseThrow(() -> new IllegalArgumentException(ExceptionMessage.LOGIN_USER_NOT_FOUND.toString())); | ||
|
||
Optional<Profile> profile; | ||
String loginUserNickname = null; | ||
|
||
if (null != loginUser.getUserProfile()) { | ||
profile = profileRepository.findById(loginUser.getUserProfile().getId()); | ||
loginUserNickname = profile.get().getNickname(); | ||
} | ||
|
||
return loginUserNickname; | ||
} | ||
|
||
public int calculateUserCount() { | ||
// 중복된 사용자를 고려하여 사용자 수를 계산 | ||
return (int) sessions.stream().map(s -> s.getAttributes().get("userId")).distinct().count(); | ||
} | ||
|
||
} |
Oops, something went wrong.