Example #1
0
        public void ExampleSeedDatabase()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <BookContext>();

            using (var context = new BookContext(options))
            {
                //2a. make sure you have an empty database
                context.Database.EnsureCreated();
                //2b. read the entities back from the JSON file
                var entities = "ExampleDatabase".ReadSeedDataFromJsonFile <List <Book> >();
                //2c. Optionally “tweak” any specific data in the classes that your unit test needs
                entities.First().Title = "new title";
                //2d. Add the data to the database and save
                context.AddRange(entities);
                context.SaveChanges();

                //ATTEMPT
                //... run your tests here

                //VERIFY
                context.Books.First().Title.ShouldEqual("new title");
                context.Books.Count().ShouldEqual(4);
                context.Authors.Count().ShouldEqual(3);
            }
        }
Example #2
0
 public async Task <ActionResult <IEnumerable <BookItem> > > GetBookItems()
 {
     // if there is no data, add some mock data
     if (_context.BookItems.Count() == 0)
     {
         _context.AddRange(_mockBookRepository.Allbooks);
         _context.SaveChanges();
     }
     return(await _context.BookItems.ToListAsync());
 }
Example #3
0
        private void Seed()
        {
            using (var context = new BookContext(ContextOptions))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                var book1 = new BookItem {
                    Id = 1, Name = "Harry Potter", Author = "J.K Rowling", Description = "Fantasy book about young Wizard named Harry Potter..."
                };
                var book2 = new BookItem {
                    Id = 2, Name = "The Expance", Author = "James S.A. Corey", Description = "Scifi book about space, spacechips, politixs, aliens and relationships in space"
                };
                var book3 = new BookItem {
                    Id = 3, Name = "The Little Pirce", Author = "Antoine de Saint-Exupéry", Description = "One of the most popular childrenbooks.."
                };
                context.AddRange(book1, book2, book3);
                context.SaveChanges();
            }
        }
Example #4
0
        public async Task <IActionResult> Edit(BookCreateViewModel ViewModel)
        {
            if (!ModelState.IsValid)
            {
                try
                {
                    DateTime?PublishDate;
                    if (ViewModel.IsPublish == true && ViewModel.RecentIsPublish == false)
                    {
                        PublishDate = DateTime.Now;
                    }
                    else if (ViewModel.RecentIsPublish == true && ViewModel.IsPublish == false)
                    {
                        PublishDate = null;
                    }

                    else
                    {
                        PublishDate = ViewModel.PublishDate;
                    }

                    BookStor book = new BookStor()
                    {
                        BookId      = ViewModel.BookId,
                        Title       = ViewModel.Title,
                        ISBN        = ViewModel.ISBN,
                        NumOfPages  = ViewModel.NumOfPages,
                        Price       = ViewModel.Price,
                        Stock       = ViewModel.Stock,
                        IsPublish   = ViewModel.IsPublish,
                        LanguageID  = ViewModel.LanguageID,
                        PublisherID = ViewModel.PublisherID,
                        PublishYear = ViewModel.PublishYear,
                        Summary     = ViewModel.Summary,
                        Weight      = ViewModel.Weight,
                        PublishDate = PublishDate,
                        Delete      = false,
                    };

                    _context.Update(book);

                    var RecentAuthors = (from a in _context.Auther_Books
                                         where (a.BookId == ViewModel.BookId)
                                         select a.AutherId).ToArray();

                    var RecentTranslators = (from a in _context.Translator_Books
                                             where (a.BookId == ViewModel.BookId)
                                             select a.TranslaorId).ToArray();



                    var DeletedAuthors     = RecentAuthors.Except(ViewModel.AuthorID);
                    var DeletedTranslators = RecentTranslators.Except(ViewModel.TranslatorID);


                    var AddedAuthors     = ViewModel.AuthorID.Except(RecentAuthors);
                    var AddedTranslators = ViewModel.TranslatorID.Except(RecentTranslators);


                    if (DeletedAuthors.Count() != 0)
                    {
                        _context.RemoveRange(DeletedAuthors.Select(a => new Auther_book {
                            AutherId = a, BookId = ViewModel.BookId
                        }).ToList());
                    }

                    if (DeletedTranslators.Count() != 0)
                    {
                        _context.RemoveRange(DeletedTranslators.Select(a => new Translator_Book {
                            TranslaorId = a, BookId = ViewModel.BookId
                        }).ToList());
                    }


                    if (AddedAuthors.Count() != 0)
                    {
                        _context.AddRange(AddedAuthors.Select(a => new Auther_book {
                            AutherId = a, BookId = ViewModel.BookId
                        }).ToList());
                    }

                    if (AddedTranslators.Count() != 0)
                    {
                        _context.AddRange(AddedTranslators.Select(a => new Translator_Book {
                            TranslaorId = a, BookId = ViewModel.BookId
                        }).ToList());
                    }

                    await _context.SaveChangesAsync();

                    ViewBag.MsgSuccess = "ذخیره تغییرات با موفقیت انجام شد.";
                    return(View(ViewModel));
                }

                catch
                {
                    ViewBag.MsgFailed = "در ذخیره تغییرات خطایی رخ داده است.";
                }
            }

            ViewBag.AuthorID = new SelectList(_context.Authers.Select(r => new ListAuther {
                AutherId = r.AutherId, NameFamily = r.Name + " " + r.LastName
            }), "AutherId", "NameFamily");
            ViewBag.LanguageID   = new SelectList(_context.Languges, "LanguegeName", "LanguegeName");
            ViewBag.PublisherID  = new SelectList(_context.Publishers, "PublisherId", "PublisherName");
            ViewBag.TranslatorID = new SelectList(_context.Translators.Select(r => new ListTranslator {
                TranslatorId = r.TranslaorId, NameFamily = r.Name + " " + r.LastName
            }), "TranslatorId", "NameFamily");
            return(View(ViewModel));
        }