use of com.aidanwhiteley.books.domain.Comment in project books by aidanwhiteley.
the class BookRepositoryTest method removeCommentFromBook.
@Test
public void removeCommentFromBook() {
// Set up a couple of comments
Book book = createTestBook();
Book savedBook = bookRepository.insert(book);
Comment comment = new Comment(A_COMMENT, new Owner());
bookRepository.addCommentToBook(savedBook.getId(), comment);
comment = new Comment(ANOTHER_COMMENT, new Owner());
bookRepository.addCommentToBook(savedBook.getId(), comment);
// noinspection ConstantConditions
Book updatedBook = bookRepository.findById(savedBook.getId()).get();
assertEquals(2, updatedBook.getComments().size());
// Returned Book holds just the updated comments
updatedBook = bookRepository.removeCommentFromBook(savedBook.getId(), updatedBook.getComments().get(0).getId(), COMMENT_REMOVER);
// There should still be two comments but the first should now be "marked" as deleted
assertEquals(2, updatedBook.getComments().size());
assertEquals("", updatedBook.getComments().get(0).getCommentText());
assertTrue(updatedBook.getComments().get(0).isDeleted());
assertEquals(COMMENT_REMOVER, updatedBook.getComments().get(0).getDeletedBy());
}
use of com.aidanwhiteley.books.domain.Comment in project books by aidanwhiteley.
the class BookRepositoryTest method addCommentToBook.
@Test
public void addCommentToBook() {
Book book = createTestBook();
Book savedBook = bookRepository.insert(book);
Comment comment = new Comment(A_COMMENT, new Owner());
// Returned book holds just the Book's comments - no other data other than the book id.
Book updatedBook = bookRepository.addCommentToBook(savedBook.getId(), comment);
assertEquals(1, updatedBook.getComments().size());
assertEquals(A_COMMENT, updatedBook.getComments().get(0).getCommentText());
}
use of com.aidanwhiteley.books.domain.Comment in project books by aidanwhiteley.
the class BookSecureController method removeCommentFromBook.
@RequestMapping(value = "/books/{id}/comments/{commentId}", method = DELETE)
public Book removeCommentFromBook(@PathVariable("id") String id, @PathVariable("commentId") String commentId, Principal principal) {
Optional<User> user = authUtils.extractUserFromPrincipal(principal, false);
if (user.isPresent()) {
Book currentBook = bookRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Unable to find book to delete comment from"));
Comment comment = currentBook.getComments().stream().filter(c -> c.getId().equals(commentId)).findFirst().orElse(null);
if (comment == null) {
throw new IllegalArgumentException("Unknown commentId supplied");
}
if (comment.isOwner(user.get()) || user.get().getRoles().contains(User.Role.ROLE_ADMIN)) {
return bookRepository.removeCommentFromBook(id, commentId, user.get().getFullName());
} else {
throw new AccessForbiddenException("Not owner of comment or admin");
}
} else {
return null;
}
}
Aggregations