private void delete(String bookId) { User user = ProfileController.getAuthenticatedUser(); user.getBooks().remove(bookId); userService.save(user); ProfileController.authenticateUser(user); }
// lazy removing private void updateUsersBooks(List<Book> books) { List<String> changedList = new ArrayList<String>(); for (Book b : books) changedList.add(b.getId()); User user = ProfileController.getAuthenticatedUser(); user.setBooks(changedList); userService.save(user); ProfileController.authenticateUser(user); }
@RequestMapping(value = "/library/add", method = RequestMethod.GET) public String addBookToLibrary(@RequestParam(required = true) String id) { User user = (User) ProfileController.getAuthenticatedUser(); if (user.getBooks() == null || !user.getBooks().contains(id)) user.addBook(id); userService.save(user); ProfileController.authenticateUser(user); return "redirect:/library"; }
public List<Book> getUsersBooks() { List<Book> books = new ArrayList<Book>(); User user = ProfileController.getAuthenticatedUser(); List<String> usersBooks = user.getBooks(); if (usersBooks == null) return books; int sizeBefore = usersBooks.size(); for (Iterator<String> bookIter = usersBooks.iterator(); bookIter.hasNext(); ) { String bookId = bookIter.next(); if (bookService.get(bookId) != null && !bookService.get(bookId).equals("")) books.add(bookService.get(bookId)); else bookIter.remove(); } if (sizeBefore != usersBooks.size()) updateUsersBooks(books); return books; }