/*********************************************************
         #A This method gets a PriceOffer class to send to the user to update
         #B This loads the book with any existing Promotion
         #C I return either the existing Promotion for editing, or create a new one. The important point is to set the BookId, as we need to pass that through to the second stage
         * ******************************************************/

        /// <summary>
        /// This deletes a promotion if the book has one, otherwise is adds a promotion
        /// </summary>
        /// <param name="promotion"></param>
        /// <returns></returns>
        public ValidationResult AddRemovePriceOffer(PriceOffer promotion) //#A
        {
            var book = _context.Books                                     //#B
                       .Include(r => r.Promotion)                         //#B
                       .Single(k => k.BookId                              //#B
                               == promotion.BookId);                      //#B

            if (book.Promotion != null)                                   //#C
            {
                _context.Remove(book.Promotion);                          //#D
                _context.SaveChanges();                                   //#D
                return(null);                                             //#E
            }

            if (string.IsNullOrEmpty(promotion.PromotionalText))            //#F
            {
                return(new ValidationResult(                                //#G
                           "This field cannot be empty",                    //#G
                           new [] { nameof(PriceOffer.PromotionalText) })); //#G
            }

            book.Promotion = promotion;                            //#H
            _context.SaveChanges();                                //#I

            return(null);                                          //#J
        }
        public void TestDeletePriceOffer()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();

                var numPromotions = context.PriceOffers.Count();

                //ATTEMPT
                var promotion = context.PriceOffers //#A
                                .First();           //#A

                context.Remove(promotion);          //#B
                context.SaveChanges();              //#C

                /**********************************************************
                 #A I find the first PriceOffer
                 #B I then remove that PriceOffer from the application's DbContext. The DbContext works what to remove based on its type of its parameter
                 #C The SaveChanges calls DetectChanges which finds a tracked PriceOffer entity which is marked as deleted. It then deletes it from the database
                 * *******************************************************/

                //VERIFY
                context.PriceOffers.Count().ShouldEqual(numPromotions - 1);
            }
        }
        public void TestDeletePriceOffer()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();

                var book = context.Books
                           .First(p => p.Promotion != null);

                //ATTEMPT
                context.Remove(context.PriceOffers.Find(book.Promotion.PriceOfferId));
                context.SaveChanges();

                //VERIFY
                var bookAgain = context.Books
                                .Include(p => p.Promotion)
                                .Single(p => p.BookId == book.BookId);
                bookAgain.Promotion.ShouldBeNull();
                context.PriceOffers.Count().ShouldEqual(0);
            }
        }
Пример #4
0
        public void TestRemoveLinkByDeleteToAuthorOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using var context = new EfCoreContext(options);
            context.Database.EnsureCreated();
            context.SeedDatabaseDummyBooks(1);
            var bookId = context.Books.First().BookId;

            context.ChangeTracker.Clear();

            //ATTEMPT
            var existingBook = context.Books
                               .Include(book => book.AuthorsLink.OrderBy(x => x.Order))
                               .Single(book => book.BookId == bookId);

            var linkToRemove = existingBook.AuthorsLink.Last();

            context.Remove(linkToRemove);
            context.SaveChanges();

            context.ChangeTracker.Clear();

            //VERIFY
            var bookAgain = context.Books
                            .Include(p => p.AuthorsLink).ThenInclude(p => p.Author)
                            .Single(p => p.BookId == bookId);

            bookAgain.AuthorsLink.ShouldNotBeNull();
            bookAgain.AuthorsLink.Count.ShouldEqual(1);
            bookAgain.AuthorsLink.First().Author.Name.ShouldEqual("Author0000");
        }
Пример #5
0
        public void Delete(int id)
        {
            using (var context = new EfCoreContext())
            {
                try
                {
                    var result = context.Players.Where(p => p.Identity.Id == id).SingleOrDefault();

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

                catch (NullReferenceException e)
                {
                    throw new ArgumentException(e.Message + $" Player with {id} was not found");
                }
            }
        }
Пример #6
0
        public void TestDeleteBookMissing()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using var context = new EfCoreContext(options);
            context.Database.EnsureCreated();

            //ATTEMPT
            var book = new Book
            {
                BookId = 123
            };

            context.Remove(book);
            var ex = Assert.Throws <DbUpdateConcurrencyException>(() => context.SaveChanges());

            //VERIFY
            ex.Message.ShouldStartWith("Database operation expected to affect 1 row(s) but actually affected 0 row(s).");
        }
Пример #7
0
        public void TestDeleteBookWithDependentEntityCascadeDeleteOk()
        {
            //SETUP
            int bookId;
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using var context = new EfCoreContext(options);
            context.Database.EnsureCreated();
            var bookSetup = new Book
            {
                Title   = "Test Book",
                Reviews = new List <Review> {
                    new Review()
                }
            };

            context.Add(bookSetup);
            context.SaveChanges();
            bookId = bookSetup.BookId;
            context.Books.Count().ShouldEqual(1);
            context.Set <Review>().Count().ShouldEqual(1);

            context.ChangeTracker.Clear();

            //ATTEMPT
            var book = new Book {
                BookId = bookId
            };

            context.Remove(book);
            context.SaveChanges();

            context.ChangeTracker.Clear();

            //VERIFY

            context.ChangeTracker.Clear();

            context.Books.Count().ShouldEqual(0);
            context.Set <Review>().Count().ShouldEqual(0);
        }
Пример #8
0
        public void TestDeleteBookNoRelationshipsOk()
        {
            //SETUP
            int bookId;
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using var context = new EfCoreContext(options);
            context.Database.EnsureCreated();
            var bookSetup = new Book {
                Title = "Test Book"
            };

            context.Add(bookSetup);
            context.SaveChanges();
            bookId = bookSetup.BookId;


            context.ChangeTracker.Clear();

            //ATTEMPT
            var book = new Book    //#A
            {
                BookId = bookId    //#B
            };

            context.Remove(book);  //#C
            context.SaveChanges(); //#D

            /*****************************************************
             #A This creates the entity class that we want to delete - in this case a Book
             #B This sets the primary key of the entity instance
             #C The call to Remove tells EF Core you want this entity/row to be deleted
             #D Then SaveChanges sends the command to the database to delete that row
             ****************************************************/

            context.ChangeTracker.Clear();

            //VERIFY
            context.Books.Count().ShouldEqual(0);
        }
Пример #9
0
        public void TestDeletePriceOfferQuicklyOk()
        {
            //SETUP
            int priceOfferId;
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using var context = new EfCoreContext(options);
            context.Database.EnsureCreated();
            var book = new Book
            {
                Title     = "Test Book",
                Promotion = new PriceOffer {
                    NewPrice = 1
                }
            };

            context.Add(book);
            context.SaveChanges();
            priceOfferId = book.Promotion.PriceOfferId;
            context.Books.Count().ShouldEqual(1);
            context.PriceOffers.Count().ShouldEqual(1);

            context.ChangeTracker.Clear();

            //ATTEMPT
            var pOfferToDelete = new PriceOffer {
                PriceOfferId = priceOfferId
            };

            context.Remove(pOfferToDelete);
            context.SaveChanges();

            context.ChangeTracker.Clear();

            //VERIFY
            context.Books.Count().ShouldEqual(1);
            context.PriceOffers.Count().ShouldEqual(0);
        }
        public void TestLoadOnlyBookAndDelete()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using var context = new EfCoreContext(options);
            context.Database.EnsureCreated();
            context.SeedDatabaseFourBooks();

            context.ChangeTracker.Clear();

            //ATTEMPT
            var book = context.Books
                       .Single(p => p.Title == "Quantum Networking");

            context.Remove(book);
            context.SaveChanges();

            //VERIFY
            context.Books.Count().ShouldEqual(3);
            context.Set <BookAuthor>().Count().ShouldEqual(3);
            context.Set <Review>().Count().ShouldEqual(0);
        }
Пример #11
0
        public void TestDeletePriceOfferRemoveNullsNavigatinalLinkOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using var context = new EfCoreContext(options);
            context.Database.EnsureCreated();
            var book = new Book
            {
                Title     = "Test Book",
                Promotion = new PriceOffer {
                    NewPrice = 1
                }
            };

            context.Add(book);
            context.SaveChanges();
            context.Books.Count().ShouldEqual(1);
            context.PriceOffers.Count().ShouldEqual(1);

            context.ChangeTracker.Clear();

            //ATTEMPT
            var bookWithPromotion = context.Books
                                    .Include(x => x.Promotion).Single();

            context.Remove(bookWithPromotion.Promotion);
            context.SaveChanges();
            bookWithPromotion.Promotion.ShouldBeNull();

            context.ChangeTracker.Clear();

            //VERIFY
            context.Books.Count().ShouldEqual(1);
            context.PriceOffers.Count().ShouldEqual(0);
        }