Skip to content

Commit

Permalink
new api
Browse files Browse the repository at this point in the history
Marc Gorzala committed Dec 15, 2023

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
1 parent 8efd185 commit 5d450e9
Showing 4 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/dancier/dancer/chat/ChatController.java
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ public ResponseEntity<ChatDto> postChat(
log.info("Creating a new chat for user {}.", authenticatedUser.getUserId());

ChatDto createdChat = chatService.createChat(authenticatedUser.getDancerIdOrThrow(), createChatDto);

log.info("this chat was created " + createdChat);
URI location = ServletUriComponentsBuilder
.fromCurrentContextPath()
.path("/chats/" + createdChat.getChatId())
10 changes: 5 additions & 5 deletions src/main/java/net/dancier/dancer/chat/ChatService.java
Original file line number Diff line number Diff line change
@@ -35,15 +35,15 @@ public ChatDto createChat(UUID dancerId, CreateChatDto createChatDto) {
public ChatDto getChat(UUID chatId, UUID dancerId) {
ChatDto chat = chatServiceClient.getChat(chatId);

throwIfDancerIsNotInChat(chat.getDancerIds(), dancerId);
throwIfDancerIsNotInChat(chat.getParticipantIds(), dancerId);

return chat;
}

public MessagesDto getMessages(UUID chatId, UUID dancerId, Optional<UUID> lastMessageId) {
ChatDto chat = chatServiceClient.getChat(chatId);

throwIfDancerIsNotInChat(chat.getDancerIds(), dancerId);
throwIfDancerIsNotInChat(chat.getParticipantIds(), dancerId);

MessagesDto messages = chatServiceClient.getMessages(chatId, dancerId, lastMessageId);
return messages;
@@ -52,7 +52,7 @@ public MessagesDto getMessages(UUID chatId, UUID dancerId, Optional<UUID> lastMe
public Void createMessage(UUID chatId, UUID dancerId, CreateMessageDto createMessageDto) {
ChatDto chat = chatServiceClient.getChat(chatId);

throwIfDancerIsNotInChat(chat.getDancerIds(), dancerId);
throwIfDancerIsNotInChat(chat.getParticipantIds(), dancerId);

RemoteCreateMessageDto remoteCreateMessageDto = new RemoteCreateMessageDto.RemoteCreateMessageDtoBuilder()
.fromCreateMessageDto(createMessageDto)
@@ -62,8 +62,8 @@ public Void createMessage(UUID chatId, UUID dancerId, CreateMessageDto createMes
return chatServiceClient.createMessage(chatId, remoteCreateMessageDto);
}

private void throwIfDancerIsNotInChat(List<UUID> dancerIds, UUID currentDancerId) {
if (!dancerIds.contains(currentDancerId)) {
private void throwIfDancerIsNotInChat(List<UUID> participantIds, UUID currentDancerId) {
if (!participantIds.contains(currentDancerId)) {
throw new BusinessException("Current dancer must be part of the chat");
}
}
2 changes: 1 addition & 1 deletion src/main/java/net/dancier/dancer/chat/dto/ChatDto.java
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
@Data
public class ChatDto {
private UUID chatId;
private List<UUID> dancerIds;
private List<UUID> participantIds;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
private OffsetDateTime lastActivity;
@Enumerated(EnumType.STRING)
18 changes: 9 additions & 9 deletions src/test/java/net/dancier/dancer/chat/ChatControllerTest.java
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ public class GetChats {
void getChatsShouldReturnChats() throws Exception {

ChatDto chat = new ChatDto();
chat.setDancerIds(List.of(dancerId, UUID.randomUUID()));
chat.setParticipantIds(List.of(dancerId, UUID.randomUUID()));
chat.setChatId(chatId);
ChatsDto chats = new ChatsDto();
chats.setChats(List.of(chat));
@@ -81,7 +81,7 @@ void postChatShouldReturnTheChat() throws Exception {
chat.setParticipantIds(dancerIds);

ChatDto createdChat = new ChatDto();
createdChat.setDancerIds(dancerIds);
createdChat.setParticipantIds(dancerIds);
createdChat.setChatId(UUID.randomUUID());

when(chatServiceClient.createChat(chat)).thenReturn(createdChat);
@@ -92,7 +92,7 @@ void postChatShouldReturnTheChat() throws Exception {
.andExpect(status().isCreated())
.andExpect(header().exists("Location"));

result.andExpect(jsonPath("$.dancerIds").isNotEmpty());
result.andExpect(jsonPath("$.participantIds").isNotEmpty());
result.andExpect(jsonPath("$.chatId").isNotEmpty());
}

@@ -121,7 +121,7 @@ void getChatShouldReturnTheChat() throws Exception {
UUID dancerId = dancerRepository.findByUserId(userId).get().getId();

ChatDto chat = new ChatDto();
chat.setDancerIds(List.of(dancerId, UUID.randomUUID()));
chat.setParticipantIds(List.of(dancerId, UUID.randomUUID()));
chat.setChatId(chatId);

when(chatServiceClient.getChat(chatId)).thenReturn(chat);
@@ -137,7 +137,7 @@ void getChatShouldReturnTheChat() throws Exception {
@WithUserDetails("[email protected]")
void getChatShouldNotReturnTheChatIfUserIsNotPartOfIt() throws Exception {
ChatDto chat = new ChatDto();
chat.setDancerIds(List.of(UUID.randomUUID(), UUID.randomUUID()));
chat.setParticipantIds(List.of(UUID.randomUUID(), UUID.randomUUID()));
chat.setChatId(chatId);

when(chatServiceClient.getChat(chatId)).thenReturn(chat);
@@ -156,7 +156,7 @@ public class GetMessages {
@WithUserDetails("[email protected]")
void getMessagesShouldNotReturnMessagesIfUserIsNotInChat() throws Exception {
ChatDto chat = new ChatDto();
chat.setDancerIds(List.of(UUID.randomUUID(), UUID.randomUUID()));
chat.setParticipantIds(List.of(UUID.randomUUID(), UUID.randomUUID()));

when(chatServiceClient.getChat(chatId)).thenReturn(chat);

@@ -172,7 +172,7 @@ void getMessagesShouldReturnMessagesIfUserIsInChat() throws Exception {
UUID dancerId = dancerRepository.findByUserId(userId).get().getId();

ChatDto chat = new ChatDto();
chat.setDancerIds(List.of(dancerId, UUID.randomUUID()));
chat.setParticipantIds(List.of(dancerId, UUID.randomUUID()));

MessagesDto messages = new MessagesDto();
MessageDto message = new MessageDto();
@@ -199,7 +199,7 @@ public class PostMessages {
@WithUserDetails("[email protected]")
void postMessagesShouldNotCreateTheMessageIfUserIsNotInTheChat() throws Exception {
ChatDto chat = new ChatDto();
chat.setDancerIds(List.of(UUID.randomUUID(), UUID.randomUUID()));
chat.setParticipantIds(List.of(UUID.randomUUID(), UUID.randomUUID()));

CreateMessageDto message = new CreateMessageDto();
message.setText("Hallo");
@@ -219,7 +219,7 @@ void postMessagesShouldNotCreateTheMessageIfUserIsNotInTheChat() throws Exceptio
@WithUserDetails("[email protected]")
void postMessagesShouldCreateAMessage() throws Exception {
ChatDto chat = new ChatDto();
chat.setDancerIds(List.of(dancerId, UUID.randomUUID()));
chat.setParticipantIds(List.of(dancerId, UUID.randomUUID()));

CreateMessageDto message = new CreateMessageDto();
message.setText("Hallo");

0 comments on commit 5d450e9

Please sign in to comment.