mirror of
https://github.com/usatiuk/y.git
synced 2025-10-28 02:27:49 +01:00
fix chat creating/updating 404
This commit is contained in:
@@ -47,9 +47,13 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
@ExceptionHandler(DataIntegrityViolationException.class)
|
||||
protected ResponseEntity<Object> handleDataIntegrityViolationException(DataIntegrityViolationException ex, WebRequest request) {
|
||||
return handleExceptionInternal(ex,
|
||||
new ErrorTo(List.of("Something is wrong with your request"), HttpStatus.BAD_REQUEST.value()),
|
||||
new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
||||
if (ex.getRootCause() instanceof ConstraintViolationException) {
|
||||
return handleConstraintViolation((ConstraintViolationException) ex.getRootCause(), 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)
|
||||
|
||||
@@ -48,7 +48,11 @@ public class ChatServiceImpl implements ChatService {
|
||||
|
||||
chat.setCreator(entityManager.getReference(Person.class, authentication.getName()));
|
||||
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());
|
||||
chat.setName(chatCreateTo.name());
|
||||
|
||||
@@ -67,7 +71,11 @@ public class ChatServiceImpl implements ChatService {
|
||||
throw new BadInputException("Chat must have members other than its creator");
|
||||
|
||||
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()));
|
||||
chat.setName(chatCreateTo.name());
|
||||
|
||||
|
||||
@@ -44,6 +44,19 @@ public class ChatControllerTest extends DemoDataDbTest {
|
||||
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
|
||||
void shouldGetChat() {
|
||||
for (TokenResponseTo t : new TokenResponseTo[]{person1Auth, person2Auth}) {
|
||||
|
||||
Reference in New Issue
Block a user