Skip to content
Open
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 @@ -13,6 +13,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

@RestController
Expand Down Expand Up @@ -41,11 +42,12 @@ public ResponseEntity<Object> createRoom(@RequestBody ChatRoomRegisterDto chatRo
@ApiResponse(code = 400, message = "Bad Request!"),
@ApiResponse(code = 500, message = "Server Error")
})
@PostMapping("{leftUserId}/chatroom/left/")
@PostMapping("/chatroom/left/")
public ResponseEntity<Object> leftChatRoom(
@RequestBody ChatRoomLeftDto chatRoomLeftDto,
@PathVariable String leftUserId) throws JsonProcessingException{

Principal principal
) throws JsonProcessingException{
String leftUserId = principal.getName();
ChatRoomResponseDto chatRoomResponseDto = chatService.leftChatRoom(Long.valueOf(leftUserId), chatRoomLeftDto);

return new ResponseHandler().generateResponse("OK", HttpStatus.OK, chatRoomResponseDto);
Expand Down Expand Up @@ -102,11 +104,12 @@ public ResponseEntity<Object> getDetailChatRoom(
@ApiResponse(code = 200, message = "OK!"),
@ApiResponse(code = 500, message = "Server Error")
})
@GetMapping("/chatroom/{userId}}")
@GetMapping("/chatroom}")
public ResponseEntity<Object> getAllChatRoom(
@PathVariable Long userId
Principal principal
) throws JsonProcessingException {
List<ChatRoomResponseDto> chatRoomResponseDtoList = chatService.getAllChatRoom(userId.toString());
String memberId = principal.getName();
List<ChatRoomResponseDto> chatRoomResponseDtoList = chatService.getAllChatRoom(memberId);

return new ResponseHandler().generateResponse("OK", HttpStatus.OK, chatRoomResponseDtoList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
public interface FollowRepository extends JpaRepository<Follow, Long> {
List<Follow> findAllByFollowerAndFollowStatus(Member follower, FollowStatus followStatus);
Follow findByFollowingAndFollowStatus(Member following, FollowStatus followStatus);

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public interface ChatRoomRepository extends MongoRepository<ChatRoom, String> {

@Override
Optional<ChatRoom> findById(String s);


ChatRoom findByChatRoomId(List<Long> participantsId);
}
37 changes: 34 additions & 3 deletions src/main/java/com/haltebogen/gittalk/service/chat/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,41 @@ public class ChatService {
@Transactional
public ChatRoomResponseDto createChatRoom(ChatRoomRegisterDto chatRoomRegisterDto) throws JsonProcessingException {
String chatRoomId = UUID.randomUUID().toString();
List<ChatMessage> messageList = new ArrayList<>();

if (isExistChatRoom(chatRoomRegisterDto)){
ChatRoom chatRoom = chatRoomRepository.findByChatRoomId(chatRoomRegisterDto.getParticipantsId());
return new ChatRoomResponseDto(chatRoom);
}
else {
List<ChatMessage> messageList = new ArrayList<>();
ChatRoom chatRoom = new ChatRoom(
chatRoomId,
chatRoomRegisterDto.getRoomName(),
messageList,
chatRoomRegisterDto.getParticipantsId(),
LocalDateTime.now()
);
chatRoomRepository.save(chatRoom);
return new ChatRoomResponseDto(chatRoom);
}

}
@Transactional
public ChatRoomResponseDto createChatRoomBySearch(ChatRoomRegisterDto chatRoomRegisterDto) throws JsonProcessingException {
String chatRoomId = UUID.randomUUID().toString();
List<ChatMessage> messageList = new ArrayList<>();
ChatRoom chatRoom = new ChatRoom(
chatRoomId,
chatRoomRegisterDto.getRoomName(),
messageList,
chatRoomRegisterDto.getParticipantsId(),
LocalDateTime.now()
);

chatRoomRepository.save(chatRoom);

return new ChatRoomResponseDto(chatRoom);
}


@Transactional
public ChatRoomResponseDto leftChatRoom(Long leftUserId, ChatRoomLeftDto chatRoomLeftDto) throws JsonGenerationException {
ChatRoom chatRoom = chatRoomRepository.findById(chatRoomLeftDto.getChatRoomId()).get();
Expand Down Expand Up @@ -152,4 +172,15 @@ public List<ChatRoomResponseDto> getAllChatRoom(String userId) {
return chatRoomResponseDtoList;
}

private Boolean isExistChatRoom(ChatRoomRegisterDto chatRoomRegisterDto) {
List<ChatRoom> chatRoomList = chatRoomRepository.findAll();
for (int i=0; i<chatRoomList.size(); i++) {
if(chatRoomList.get(i).getParticipantsId().equals(chatRoomRegisterDto.getParticipantsId())){
return true;
}
}
return false;
}


}