package com.ryanpmartz.booktrackr.service;

import com.codahale.metrics.annotation.Timed;
import com.ryanpmartz.booktrackr.domain.Book;
import com.ryanpmartz.booktrackr.repository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class BookService {

    private final BookRepository bookRepository;

    @Autowired
    public BookService(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }

    @Timed
    @Transactional(readOnly = true)
    public List<Book> getAllBooksForUser(UUID userId) {
        return bookRepository.findByUserId(userId);
    }

    @Timed
    @Transactional(readOnly = true)
    public Optional<Book> getBook(UUID bookId) {
        return Optional.ofNullable(bookRepository.findOne(bookId));
    }

    @Timed
    @Transactional
    public Book createBook(Book book) {
        return bookRepository.save(book);
    }

    @Timed
    @Transactional
    public void updateBook(Book book) {
        bookRepository.save(book);
    }

    @Timed
    @Transactional
    public void deleteBook(UUID bookId) {
        bookRepository.delete(bookId);
    }
}

@@ -120,69 +120,232 @@
 ckr.
-domain.Book;%0Aimport com.ryanpmartz.booktrackr.repository.Book
+authentication.JwtUtil;%0Aimport com.ryanpmartz.booktrackr.domain.Book;%0Aimport com.ryanpmartz.booktrackr.domain.User;%0Aimport com.ryanpmartz.booktrackr.repository.BookRepository;%0Aimport com.ryanpmartz.booktrackr.repository.User
 Repo
@@ -687,16 +687,65 @@
 ository;
+%0A    private final UserRepository userRepository;
 %0A%0A    @A
@@ -805,16 +805,47 @@
 pository
+, UserRepository userRepository
 ) %7B%0A    
@@ -882,24 +882,70 @@
 Repository;%0A
+        this.userRepository = userRepository;%0A
     %7D%0A%0A    @
@@ -1334,32 +1334,152 @@
 ok(Book book) %7B%0A
+        User user = userRepository.findOne(JwtUtil.tokenFromSecurityContext().getUserId());%0A        book.setUser(user);%0A
         return b

add user to new book