mirror of
https://github.com/usatiuk/y.git
synced 2025-10-28 18:37:47 +01:00
find chats by member
This commit is contained in:
@@ -15,6 +15,7 @@ import org.springframework.web.server.ResponseStatusException;
|
||||
import java.security.Principal;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/chat", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
@@ -55,5 +56,10 @@ public class ChatController {
|
||||
return chatMapper.makeDto(chat);
|
||||
}
|
||||
|
||||
@GetMapping(path = "/my")
|
||||
public Stream<ChatTo> getMy(Principal principal) {
|
||||
return chatService.readByMember(principal.getName()).stream().map(chatMapper::makeDto);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -3,8 +3,11 @@ package com.usatiuk.tjv.y.server.repository;
|
||||
import com.usatiuk.tjv.y.server.entity.Chat;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ChatRepository extends CrudRepository<Chat, Long> {
|
||||
Optional<Chat> findByName(String name);
|
||||
|
||||
Collection<Chat> findByMembers_uuid(String memberUuid);
|
||||
}
|
||||
|
||||
@@ -2,5 +2,8 @@ package com.usatiuk.tjv.y.server.service;
|
||||
|
||||
import com.usatiuk.tjv.y.server.entity.Chat;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public interface ChatService extends CrudService<Chat, Long> {
|
||||
Collection<Chat> readByMember(String memberUuid);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import com.usatiuk.tjv.y.server.repository.ChatRepository;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@Service
|
||||
public class ChatServiceImpl extends CrudServiceImpl<Chat, Long> implements ChatService {
|
||||
|
||||
@@ -18,4 +20,9 @@ public class ChatServiceImpl extends CrudServiceImpl<Chat, Long> implements Chat
|
||||
protected CrudRepository<Chat, Long> getRepository() {
|
||||
return chatRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Chat> readByMember(String memberUuid) {
|
||||
return chatRepository.findByMembers_uuid(memberUuid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ChatControllerTest extends DemoDataDbTest {
|
||||
@@ -60,6 +61,21 @@ public class ChatControllerTest extends DemoDataDbTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldGetChatsUserIsMemberOf() {
|
||||
var response = restTemplate.exchange(addr + "/chat/my", HttpMethod.GET,
|
||||
new HttpEntity<>(createAuthHeaders(person2Auth)),
|
||||
ChatTo[].class);
|
||||
|
||||
Assertions.assertNotNull(response);
|
||||
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
|
||||
var toResponse = response.getBody();
|
||||
Assertions.assertNotNull(toResponse);
|
||||
|
||||
Assertions.assertIterableEquals(Stream.of(chat1, chat2).map(chatMapper::makeDto).toList(), Arrays.asList(toResponse));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotChatUnauthorized() {
|
||||
var response = restTemplate.exchange(addr + "/chat/by-id/" + chat1.getId(), HttpMethod.GET,
|
||||
|
||||
Reference in New Issue
Block a user