using Library.Domain.Abstract;
using System.Collections.Generic;
using Library.Domain.Entities;
using System;

namespace Library.Domain.Concrete
{
    public class EFBookRepository : IBookRepository
    {
        EFDbContext context = new EFDbContext();

        public IEnumerable<Book> Books
        {
            get { return context.Books; }
        }

        public void SaveBook(Book book)
        {
            if (book.ID == 0)
            {
                context.Books.Add(book);
            }
            else
            {
                Book dbItem = context.Books.Find(book.ID);
                if (dbItem != null)
                {
                    dbItem.Name = book.Name;
                    dbItem.Author = book.Author;
                    dbItem.Genre = book.Genre;
                    dbItem.Subgenre = book.Subgenre;
                    dbItem.Description = book.Description;
                    dbItem.IsLent = book.IsLent;
                }
            }
            context.SaveChanges();
        }

        public string DeleteBook(int bookID)
        {
            Book dbItem = context.Books.Find(bookID);
            if (dbItem != null)
            {
                context.Books.Remove(dbItem);
                context.SaveChanges();
            }
            return dbItem.Name;
        }
    }
}
