use of com.aidanwhiteley.books.domain.Owner in project books by aidanwhiteley.
the class BookRepositoryTest method removeCommentFromBook.
@Test
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);
Optional<Book> oBook = bookRepository.findById(savedBook.getId());
Book updatedBook = null;
if (oBook.isPresent()) {
// noinspection OptionalGetWithoutIsPresent
updatedBook = bookRepository.findById(savedBook.getId()).get();
} else {
fail("Optional not expected to be empty");
}
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.Owner in project books by aidanwhiteley.
the class BookSecureController method createBook.
@RequestMapping(value = "/books", method = POST)
public ResponseEntity<Book> createBook(@Valid @RequestBody Book book, Principal principal, HttpServletRequest request) throws MalformedURLException, URISyntaxException {
Optional<User> user = authUtils.extractUserFromPrincipal(principal, false);
if (user.isPresent()) {
book.setCreatedBy(new Owner(user.get()));
// implementation.
if (book.getGoogleBookId() != null && book.getGoogleBookId().length() > 0) {
book.setGoogleBookDetails(googleBooksDaoSync.searchGoogleBooksByGoogleBookId(book.getGoogleBookId()));
}
Book insertedBook = bookRepository.insert(book);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(insertedBook.getId()).toUri();
// Basic GET of book details are not on a secure API
location = new URI(location.toURL().toString().replaceAll("/secure", ""));
return ResponseEntity.created(location).build();
} else {
LOGGER.error("Couldnt create a book as user to own book not found! Principal: {}", principal);
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
}
use of com.aidanwhiteley.books.domain.Owner in project books by aidanwhiteley.
the class BookRepositoryTest method addCommentToBook.
@Test
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());
}
Aggregations