/*
 * Copyright 2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package example.app.books.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import example.app.books.model.Author;
import example.app.books.model.Book;
import example.app.books.model.ISBN;
import example.app.books.repo.BookRepository;

/**
 * The {@link BookService} class is an application {@link Service service} class for managing {@link Book Books}.
 *
 * @author John Blum
 * @see example.app.books.model.Author
 * @see example.app.books.model.Book
 * @see example.app.books.model.ISBN
 * @see example.app.books.repo.BookRepository
 * @see org.springframework.stereotype.Service
 * @since 1.0.0
 */
@Service
@SuppressWarnings("unused")
public class BookService {

	private final BookRepository bookRepository;

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

	protected BookRepository getBookRepository() {

		Assert.state(this.bookRepository != null, "BookRepository was not properly configured");

		return this.bookRepository;
	}

	public List<Book> findByAuthor(Author author) {
		return getBookRepository().findByAuthorOrderByAuthorNameAscTitleAsc(author);
	}

	public Book findByIsbn(ISBN isbn) {
		return getBookRepository().findByIsbn(isbn);
	}

	public Book findByTitle(String title) {
		return getBookRepository().findByTitle(title);
	}

	public Book stock(Book book) {

		if (book.isNew()) {
			book.identifiedBy(ISBN.autoGenerated());
		}

		return getBookRepository().save(book);
	}
}
