using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApiExample2.Data.Context;
using WebApiExample2.Data.Model;

namespace WebApiExample2.Business
{
    public class BookService : IBookService
    { 
        public void CreateBook(Book book)
        {
            using (var ctx = new LibraryContext())
            {
                ctx.Books.Add(book);
                ctx.SaveChanges();
            }
        }

        public Book GetBook(int id)
        {
            using (var ctx = new LibraryContext())
            {
                return ctx.Books.Where(b => b.Id == id).SingleOrDefault();               
            }
        }

        public Book[] GetBook()
        {
            using (var ctx = new LibraryContext())
            {
                return ctx.Books.ToArray();
            }
        }
    }
}
