public static Boolean DeleteBook(TempBook book)
        {
            using (var context = new EFDtataAccessLibary.DataAccess.GewertsContext())
            {
                var books = context.Böckers.First(a => a.Isbn == book.ISBN);


                while (context.FörfattareBöckers.Any(a => a.Isbn == book.ISBN))
                {
                    var authorBook = context.FörfattareBöckers.First(a => a.Isbn == book.ISBN);

                    context.Remove(authorBook);
                    context.SaveChanges();
                }

                while (context.LagerSaldos.Any(b => b.Isbn == book.ISBN))
                {
                    var quantity = context.LagerSaldos.First(a => a.Isbn == book.ISBN);
                    context.Remove(quantity);
                    context.SaveChanges();
                }

                context.Remove(books);
                context.SaveChanges();

                return(true);
            }
        }
        public static Boolean AddBook(TempBook book)
        {
            using (var context = new EFDtataAccessLibary.DataAccess.GewertsContext())
            {
                int stores       = context.Butikers.Count();
                var books        = context.Böckers.ToList();
                var booksAuthors = context.FörfattareBöckers.ToList();
                var quantitys    = context.LagerSaldos.ToList();
                EFDtataAccessLibary.Models.Böcker bookToAdd = new EFDtataAccessLibary.Models.Böcker();

                EFDtataAccessLibary.Models.LagerSaldo quantityToAdd = new EFDtataAccessLibary.Models.LagerSaldo();

                for (int i = 0; i < books.Count; i++)
                {
                    if (book.ISBN == books[i].Isbn)
                    {
                        return(false);
                    }
                }

                bookToAdd.Isbn            = book.ISBN;
                bookToAdd.Titel           = book.Title;
                bookToAdd.Språk           = book.Language;
                bookToAdd.Pris            = book.Price;
                bookToAdd.Utgivningsdatum = book.ReleaseDay;
                bookToAdd.BokförlagNamn   = book.Publisher.Namn;
                context.Böckers.Add(bookToAdd);

                for (int i = 0; i < book.NewAuthors.Count; i++)
                {
                    EFDtataAccessLibary.Models.FörfattareBöcker booksAuthorsToAdd = new EFDtataAccessLibary.Models.FörfattareBöcker();
                    booksAuthorsToAdd.Isbn         = book.ISBN;
                    booksAuthorsToAdd.FörfattareId = book.NewAuthors[i].Id;
                    context.FörfattareBöckers.Add(booksAuthorsToAdd);
                    context.SaveChanges();
                }

                for (int i = 0; i < stores; i++)
                {
                    quantityToAdd.Antal    = 0;
                    quantityToAdd.Isbn     = book.ISBN;
                    quantityToAdd.ButiksId = i + 1;

                    context.LagerSaldos.Add(quantityToAdd);
                    context.SaveChanges();
                }

                context.SaveChanges();

                return(true);
            }
        }
 //Ta bort
 public static Boolean DeleteAuthor(int id)
 {
     using (var context = new EFDtataAccessLibary.DataAccess.GewertsContext())
     {
         var author = context.Författares.FirstOrDefault(a => a.Id == id);
         context.Remove(author);
         context.SaveChanges();
         return(true);
     }
 }
        public static Boolean UpdateAuthor(EFDtataAccessLibary.Models.Författare author)
        {
            using (var context = new EFDtataAccessLibary.DataAccess.GewertsContext())
            {
                var authors = context.Författares.FirstOrDefault(a => a.Id == author.Id);

                authors.Förnamn      = author.Förnamn;
                authors.Efternamn    = author.Efternamn;
                authors.Födelsedatum = author.Födelsedatum;

                context.SaveChanges();
            }

            return(true);
        }
        //Skapa
        public static Boolean AddAuthor(EFDtataAccessLibary.Models.Författare author)
        {
            using (var context = new EFDtataAccessLibary.DataAccess.GewertsContext())
            {
                var authors = context.Författares.ToList();

                for (int i = 0; i < authors.Count; i++)
                {
                    if (authors[i].Förnamn.Equals(author.Förnamn) && authors[i].Efternamn.Equals(author.Efternamn))
                    {
                        return(false);
                    }
                }

                context.Författares.Add(author);
                context.SaveChanges();
                return(true);
            }
        }
        //Updatera listor
        public static Boolean UpdateSaldo(int ammount, long isbn, int storeNr)
        {
            using (var context = new EFDtataAccessLibary.DataAccess.GewertsContext())
            {
                var saldo = context.LagerSaldos.FirstOrDefault(a => a.Isbn == isbn && a.ButiksId == storeNr);

                if (saldo != null)
                {
                    saldo.Antal = saldo.Antal + ammount;

                    if (saldo.Antal >= 0)
                    {
                        context.SaveChanges();
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                return(false);
            }
        }
        public static Boolean UpdateBook(TempBook book)
        {
            using (var context = new EFDtataAccessLibary.DataAccess.GewertsContext())
            {
                var books         = context.Böckers.FirstOrDefault(a => a.Isbn == book.ISBN);
                var authorsWBooks = context.FörfattareBöckers.ToList();

                books.Isbn            = book.ISBN;
                books.Titel           = book.Title;
                books.Språk           = book.Language;
                books.Pris            = book.Price;
                books.Utgivningsdatum = book.ReleaseDay;
                books.BokförlagNamn   = book.Publisher.Namn;

                authorsWBooks[0].FörfattareId = book.NewAuthors[0].Id;

                if (book.NewAuthors.Count == 1 && book.Authors.Count > 1)
                {
                    var author = authorsWBooks.First(a => a.FörfattareId == book.Authors[1].Id);
                    context.FörfattareBöckers.Remove(author);

                    if (book.Authors.Count == 3)
                    {
                        var author2 = authorsWBooks.First(a => a.FörfattareId == book.Authors[2].Id);
                        context.FörfattareBöckers.Remove(author2);
                    }
                }

                if (book.NewAuthors.Count >= 2)
                {
                    if (book.Authors.Count >= 2 && authorsWBooks[1].Isbn == book.ISBN)
                    {
                        authorsWBooks[1].FörfattareId = book.NewAuthors[1].Id;

                        if (book.Authors.Count > 2 && book.NewAuthors.Count > 2)
                        {
                            var author = authorsWBooks.First(a => a.FörfattareId == book.Authors[2].Id);
                            context.FörfattareBöckers.Remove(author);
                        }
                    }
                    else
                    {
                        EFDtataAccessLibary.Models.FörfattareBöcker fb = new EFDtataAccessLibary.Models.FörfattareBöcker();
                        fb.Isbn         = book.ISBN;
                        fb.FörfattareId = book.NewAuthors[1].Id;

                        context.FörfattareBöckers.Add(fb);
                    }

                    if (book.NewAuthors.Count == 3)
                    {
                        if (book.Authors.Count >= 3 && authorsWBooks[2].Isbn == book.ISBN)
                        {
                            authorsWBooks[2].FörfattareId = book.NewAuthors[2].Id;
                        }
                        else
                        {
                            EFDtataAccessLibary.Models.FörfattareBöcker fb = new EFDtataAccessLibary.Models.FörfattareBöcker();
                            fb.Isbn         = book.ISBN;
                            fb.FörfattareId = book.NewAuthors[2].Id;

                            context.FörfattareBöckers.Add(fb);
                        }
                    }
                }

                context.SaveChanges();

                return(true);
            }
        }