Skip to content

Commit

Permalink
new api
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Gorzala committed Dec 15, 2023
1 parent 8efd185 commit 5d450e9
Show file tree
Hide file tree
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
Expand Up @@ -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())
Expand Down
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
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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");
}
}
Expand Down
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
Expand Up @@ -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)
Expand Down
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
Expand Up @@ -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));
Expand All @@ -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);
Expand All @@ -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());
}

Expand Down Expand Up @@ -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);
Expand All @@ -137,7 +137,7 @@ void getChatShouldReturnTheChat() throws Exception {
@WithUserDetails("user-with-a-profile@dancier.net")
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);
Expand All @@ -156,7 +156,7 @@ public class GetMessages {
@WithUserDetails("user-with-a-profile@dancier.net")
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);

Expand All @@ -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();
Expand All @@ -199,7 +199,7 @@ public class PostMessages {
@WithUserDetails("user-with-a-profile@dancier.net")
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");
Expand All @@ -219,7 +219,7 @@ void postMessagesShouldNotCreateTheMessageIfUserIsNotInTheChat() throws Exceptio
@WithUserDetails("user-with-a-profile@dancier.net")
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");
Expand Down

0 comments on commit 5d450e9

Please sign in to comment.