mirror of
https://github.com/usatiuk/y.git
synced 2025-10-28 10:37:47 +01:00
fix chat creating/updating 404
This commit is contained in:
@@ -47,9 +47,13 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
|
|||||||
|
|
||||||
@ExceptionHandler(DataIntegrityViolationException.class)
|
@ExceptionHandler(DataIntegrityViolationException.class)
|
||||||
protected ResponseEntity<Object> handleDataIntegrityViolationException(DataIntegrityViolationException ex, WebRequest request) {
|
protected ResponseEntity<Object> handleDataIntegrityViolationException(DataIntegrityViolationException ex, WebRequest request) {
|
||||||
return handleExceptionInternal(ex,
|
if (ex.getRootCause() instanceof ConstraintViolationException) {
|
||||||
new ErrorTo(List.of("Something is wrong with your request"), HttpStatus.BAD_REQUEST.value()),
|
return handleConstraintViolation((ConstraintViolationException) ex.getRootCause(), request);
|
||||||
new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
} else {
|
||||||
|
return handleExceptionInternal(ex,
|
||||||
|
new ErrorTo(List.of("Something is wrong with your request"), HttpStatus.BAD_REQUEST.value()),
|
||||||
|
new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(AuthenticationException.class)
|
@ExceptionHandler(AuthenticationException.class)
|
||||||
|
|||||||
@@ -48,7 +48,11 @@ public class ChatServiceImpl implements ChatService {
|
|||||||
|
|
||||||
chat.setCreator(entityManager.getReference(Person.class, authentication.getName()));
|
chat.setCreator(entityManager.getReference(Person.class, authentication.getName()));
|
||||||
chat.setMembers(Arrays.stream(chatCreateTo.memberUuids()).map(
|
chat.setMembers(Arrays.stream(chatCreateTo.memberUuids()).map(
|
||||||
p -> entityManager.getReference(Person.class, p)
|
p -> {
|
||||||
|
if (entityManager.find(Person.class, p) == null)
|
||||||
|
throw new NotFoundException("User with uuid " + p + " not found");
|
||||||
|
return entityManager.getReference(Person.class, p);
|
||||||
|
}
|
||||||
).toList());
|
).toList());
|
||||||
chat.setName(chatCreateTo.name());
|
chat.setName(chatCreateTo.name());
|
||||||
|
|
||||||
@@ -67,7 +71,11 @@ public class ChatServiceImpl implements ChatService {
|
|||||||
throw new BadInputException("Chat must have members other than its creator");
|
throw new BadInputException("Chat must have members other than its creator");
|
||||||
|
|
||||||
chat.setMembers(new ArrayList<>(Arrays.stream(chatCreateTo.memberUuids()).map(
|
chat.setMembers(new ArrayList<>(Arrays.stream(chatCreateTo.memberUuids()).map(
|
||||||
p -> entityManager.getReference(Person.class, p)
|
p -> {
|
||||||
|
if (entityManager.find(Person.class, p) == null)
|
||||||
|
throw new NotFoundException("User with uuid " + p + " not found");
|
||||||
|
return entityManager.getReference(Person.class, p);
|
||||||
|
}
|
||||||
).toList()));
|
).toList()));
|
||||||
chat.setName(chatCreateTo.name());
|
chat.setName(chatCreateTo.name());
|
||||||
|
|
||||||
|
|||||||
@@ -44,6 +44,19 @@ public class ChatControllerTest extends DemoDataDbTest {
|
|||||||
Assertions.assertTrue(chatRepository.findByName("chatnew").isPresent());
|
Assertions.assertTrue(chatRepository.findByName("chatnew").isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void chatCreateWithBadUuid404() {
|
||||||
|
var response = restTemplate.exchange(addr + "/chat", HttpMethod.POST,
|
||||||
|
new HttpEntity<>(new ChatCreateTo("chatnew", new String[]{person1.getUuid(), "asdfasdfasdf"}), createAuthHeaders(person1Auth)),
|
||||||
|
ErrorTo.class);
|
||||||
|
|
||||||
|
Assertions.assertNotNull(response);
|
||||||
|
Assertions.assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
|
|
||||||
|
var toResponse = response.getBody();
|
||||||
|
Assertions.assertNotNull(toResponse);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldGetChat() {
|
void shouldGetChat() {
|
||||||
for (TokenResponseTo t : new TokenResponseTo[]{person1Auth, person2Auth}) {
|
for (TokenResponseTo t : new TokenResponseTo[]{person1Auth, person2Auth}) {
|
||||||
|
|||||||
Reference in New Issue
Block a user