mirror of
https://github.com/usatiuk/y.git
synced 2025-10-28 18:37:47 +01:00
get chat
This commit is contained in:
@@ -46,10 +46,11 @@ public class ChatController {
|
||||
return chatMapper.makeDto(chat);
|
||||
}
|
||||
|
||||
@PostMapping(path = "/by-id/:id")
|
||||
@GetMapping(path = "/by-id/{id}")
|
||||
public ChatTo get(Principal principal, @PathVariable Long id) {
|
||||
var chat = chatService.readById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Chat not found"));
|
||||
if (!chat.getMembers().contains(entityManager.getReference(Person.class, principal.getName())))
|
||||
var userRef = entityManager.getReference(Person.class, principal.getName());
|
||||
if (!chat.getMembers().contains(userRef))
|
||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User isn't member of the chat");
|
||||
return chatMapper.makeDto(chat);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.usatiuk.tjv.y.server.dto;
|
||||
|
||||
public record ChatTo(Long id, String name, String creatorUuid, PersonTo[] memberUuids, MessageTo[] messages) {
|
||||
import java.util.Collection;
|
||||
|
||||
public record ChatTo(Long id, String name, String creatorUuid, Collection<PersonTo> members,
|
||||
Collection<MessageTo> messages) {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.usatiuk.tjv.y.server.dto.converters;
|
||||
|
||||
import com.usatiuk.tjv.y.server.dto.ChatTo;
|
||||
import com.usatiuk.tjv.y.server.dto.MessageTo;
|
||||
import com.usatiuk.tjv.y.server.dto.PersonTo;
|
||||
import com.usatiuk.tjv.y.server.entity.Chat;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -19,7 +17,7 @@ public class ChatMapper {
|
||||
|
||||
public ChatTo makeDto(Chat chat) {
|
||||
return new ChatTo(chat.getId(), chat.getName(), chat.getCreator().getUuid(),
|
||||
chat.getMembers().stream().map(personMapper::makeDto).toArray(PersonTo[]::new),
|
||||
chat.getMessages().stream().map(messageMapper::makeDto).toArray(MessageTo[]::new));
|
||||
chat.getMembers().stream().map(personMapper::makeDto).toList(),
|
||||
chat.getMessages().stream().map(messageMapper::makeDto).toList());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,10 @@ public class Chat implements EntityWithId<Long> {
|
||||
@OneToMany(mappedBy = "chat")
|
||||
private Collection<Message> messages = new ArrayList<>();
|
||||
|
||||
@ManyToMany(mappedBy = "chats")
|
||||
@ManyToMany
|
||||
@JoinTable(name = "chat_person",
|
||||
joinColumns = @JoinColumn(name = "chat"),
|
||||
inverseJoinColumns = @JoinColumn(name = "person"))
|
||||
private Collection<Person> members = new ArrayList<>();
|
||||
|
||||
@ManyToOne
|
||||
|
||||
@@ -52,10 +52,7 @@ public class Person implements EntityWithId<String> {
|
||||
@ManyToMany(mappedBy = "following")
|
||||
private Collection<Person> followers;
|
||||
|
||||
@ManyToMany
|
||||
@JoinTable(name = "person_chat",
|
||||
joinColumns = @JoinColumn(name = "person"),
|
||||
inverseJoinColumns = @JoinColumn(name = "chat"))
|
||||
@ManyToMany(mappedBy = "members")
|
||||
private Collection<Chat> chats;
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,6 +2,9 @@ package com.usatiuk.tjv.y.server.controller;
|
||||
|
||||
import com.usatiuk.tjv.y.server.dto.ChatCreateTo;
|
||||
import com.usatiuk.tjv.y.server.dto.ChatTo;
|
||||
import com.usatiuk.tjv.y.server.dto.ErrorTo;
|
||||
import com.usatiuk.tjv.y.server.dto.TokenResponseTo;
|
||||
import com.usatiuk.tjv.y.server.dto.converters.ChatMapper;
|
||||
import com.usatiuk.tjv.y.server.dto.converters.PersonMapper;
|
||||
import com.usatiuk.tjv.y.server.repository.ChatRepository;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
@@ -11,7 +14,6 @@ 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 {
|
||||
@@ -19,6 +21,8 @@ public class ChatControllerTest extends DemoDataDbTest {
|
||||
@Autowired
|
||||
private PersonMapper personMapper;
|
||||
@Autowired
|
||||
private ChatMapper chatMapper;
|
||||
@Autowired
|
||||
private ChatRepository chatRepository;
|
||||
|
||||
@Test
|
||||
@@ -35,7 +39,52 @@ public class ChatControllerTest extends DemoDataDbTest {
|
||||
|
||||
Assertions.assertEquals("chatnew", toResponse.name());
|
||||
Assertions.assertEquals(person1.getUuid(), toResponse.creatorUuid());
|
||||
Assertions.assertIterableEquals(Stream.of(person1, person2).map(personMapper::makeDto).toList(), Arrays.asList(toResponse.memberUuids()));
|
||||
Assertions.assertIterableEquals(Stream.of(person1, person2).map(personMapper::makeDto).toList(), toResponse.members());
|
||||
Assertions.assertTrue(chatRepository.findByName("chatnew").isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldGetChat() {
|
||||
for (TokenResponseTo t : new TokenResponseTo[]{person1Auth, person2Auth}) {
|
||||
var response = restTemplate.exchange(addr + "/chat/by-id/" + chat1.getId(), HttpMethod.GET,
|
||||
new HttpEntity<>(createAuthHeaders(t)),
|
||||
ChatTo.class);
|
||||
|
||||
Assertions.assertNotNull(response);
|
||||
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
|
||||
var toResponse = response.getBody();
|
||||
Assertions.assertNotNull(toResponse);
|
||||
|
||||
Assertions.assertEquals(chatMapper.makeDto(chat1), toResponse);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotChatUnauthorized() {
|
||||
var response = restTemplate.exchange(addr + "/chat/by-id/" + chat1.getId(), HttpMethod.GET,
|
||||
new HttpEntity<>(createAuthHeaders(person3Auth)),
|
||||
ErrorTo.class);
|
||||
|
||||
Assertions.assertNotNull(response);
|
||||
Assertions.assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
|
||||
|
||||
var toResponse = response.getBody();
|
||||
Assertions.assertNotNull(toResponse);
|
||||
|
||||
Assertions.assertEquals(HttpStatus.FORBIDDEN.value(), toResponse.code());
|
||||
|
||||
response = restTemplate.exchange(addr + "/chat/by-id/" + chat1.getId(), HttpMethod.GET,
|
||||
HttpEntity.EMPTY,
|
||||
ErrorTo.class);
|
||||
|
||||
Assertions.assertNotNull(response);
|
||||
Assertions.assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
|
||||
|
||||
toResponse = response.getBody();
|
||||
Assertions.assertNotNull(toResponse);
|
||||
|
||||
Assertions.assertEquals(HttpStatus.UNAUTHORIZED.value(), toResponse.code());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ public abstract class DemoDataDbTest {
|
||||
@AfterEach
|
||||
void erase() {
|
||||
assert !TestTransaction.isActive();
|
||||
JdbcTestUtils.deleteFromTables(jdbcTemplate, "person_follows", "person_chat", "post", "chat", "message", "person");
|
||||
JdbcTestUtils.deleteFromTables(jdbcTemplate, "person_follows", "chat_person", "post", "chat", "message", "person");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user