Search in sources :

Example 1 with Comment

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());
}
Also used : Comment(com.aidanwhiteley.books.domain.Comment) Owner(com.aidanwhiteley.books.domain.Owner) Book(com.aidanwhiteley.books.domain.Book) Test(org.junit.Test) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Example 2 with Comment

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());
}
Also used : Comment(com.aidanwhiteley.books.domain.Comment) Owner(com.aidanwhiteley.books.domain.Owner) Book(com.aidanwhiteley.books.domain.Book) Test(org.junit.Test) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Example 3 with Comment

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;
    }
}
Also used : Comment(com.aidanwhiteley.books.domain.Comment) User(com.aidanwhiteley.books.domain.User) Book(com.aidanwhiteley.books.domain.Book) AccessForbiddenException(com.aidanwhiteley.books.controller.exceptions.AccessForbiddenException)

Aggregations

Book (com.aidanwhiteley.books.domain.Book)3 Comment (com.aidanwhiteley.books.domain.Comment)3 Owner (com.aidanwhiteley.books.domain.Owner)2 IntegrationTest (com.aidanwhiteley.books.util.IntegrationTest)2 Test (org.junit.Test)2 AccessForbiddenException (com.aidanwhiteley.books.controller.exceptions.AccessForbiddenException)1 User (com.aidanwhiteley.books.domain.User)1