public static int SeedAsync(GuestbookDbContext context)
        {
            // Use the Migrate method to automatically create the database and migrat if needed
            context.Database.EnsureCreated();

            if (context.Guestbooks.Count() == 0)
            {
                // we could also check for some specific instances and behave accordingly to the result.
                Guestbook guestbook = new Guestbook {
                    Name = "Novia guestbook"
                };
                IGuestbookEntry firstEntry = new GuestbookEntry
                {
                    Message = "Welcome to Novia!"
                };
                guestbook.AddEntry(firstEntry);
                IGuestbookEntry secondEntry = new GuestbookEntry
                {
                    Message = "IT profile salutes you."
                };
                guestbook.AddEntry(secondEntry);

                context.Guestbooks.Add(guestbook);
                return(context.SaveChanges());
            }
            return(0);
        }
        public IActionResult Index()
        {
            if (!_repository.List <Guestbook>().Any())
            {
                var newGuestbook = new Guestbook {
                    Name = "My Guestbook"
                };
                newGuestbook.AddEntry(new GuestbookEntry {
                    EmailAddress = "*****@*****.**", Message = "Hi!", DateTimeCreated = DateTime.UtcNow.AddHours(-2)
                });
                newGuestbook.AddEntry(new GuestbookEntry {
                    EmailAddress = "*****@*****.**", Message = "Hi again!", DateTimeCreated = DateTime.UtcNow.AddHours(-1)
                });
                newGuestbook.AddEntry(new GuestbookEntry {
                    EmailAddress = "*****@*****.**", Message = "Hello!"
                });
                newGuestbook.Events.Clear();
                _repository.Add(newGuestbook);
            }

            var guestbook = _repository.GetById <Guestbook>(1); // hardcoded for this sample; could support multi-tenancy in real app
            var viewModel = new HomePageViewModel();

            viewModel.GuestbookName = guestbook.Name;
            viewModel.PreviousEntries.AddRange(guestbook.Entries
                                               .Select(e => new GuestbookEntryDTO
            {
                DateTimeCreated = e.DateTimeCreated,
                EmailAddress    = e.EmailAddress,
                Id      = e.Id,
                Message = e.Message
            }));
            return(View(viewModel));
        }
Esempio n. 3
0
        private void PopulateTestData(IApplicationBuilder app)
        {
            var dbContext = app.ApplicationServices.GetService <AppDbContext>();

            // reset the database
            dbContext.Database.EnsureDeleted();

            dbContext.ToDoItems.Add(new ToDoItem()
            {
                Title       = "Test Item 1",
                Description = "Test Description One"
            });
            dbContext.ToDoItems.Add(new ToDoItem()
            {
                Title       = "Test Item 2",
                Description = "Test Description Two"
            });
            dbContext.SaveChanges();

            // add Guestbook test data; specify Guestbook ID for use in tests
            var guestbook = new Guestbook()
            {
                Name = "Test Guestbook", Id = 1
            };

            dbContext.Guestbooks.Add(guestbook);
            guestbook.AddEntry(new GuestbookEntry()
            {
                EmailAddress = "*****@*****.**",
                Message      = "Test message"
            });
            dbContext.SaveChanges();
        }
Esempio n. 4
0
        public static void PopulateTestData(AppDbContext dbContext)
        {
            foreach (var item in dbContext.ToDoItems)
            {
                dbContext.Remove(item);
            }
            dbContext.SaveChanges();
            dbContext.ToDoItems.Add(ToDoItem1);
            dbContext.ToDoItems.Add(ToDoItem2);
            dbContext.ToDoItems.Add(ToDoItem3);

            dbContext.SaveChanges();

            // add Guestbook test data; specify Guestbook ID for use in tests
            var guestbook = new Guestbook()
            {
                Name = "Test Guestbook", Id = 1
            };

            dbContext.Guestbooks.Add(guestbook);
            guestbook.AddEntry(new GuestbookEntry
            {
                EmailAddress = "*****@*****.**",
                Message      = "Test message"
            });
            guestbook.Events.Clear();
            dbContext.SaveChanges();
        }
Esempio n. 5
0
        public static void PopulateTestData(AppDbContext dbContext)
        {
            var toDos = dbContext.ToDoItems;

            foreach (var item in toDos)
            {
                dbContext.Remove(item);
            }
            dbContext.SaveChanges();
            dbContext.ToDoItems.Add(new ToDoItem()
            {
                Title       = "Test Item 1",
                Description = "Test Description One"
            });
            dbContext.ToDoItems.Add(new ToDoItem()
            {
                Title       = "Test Item 2",
                Description = "Test Description Two"
            });

            var createdGuestbook = new Guestbook();

            createdGuestbook.Name = "My Guestbook";

            createdGuestbook.AddEntry(new GuestBookEntry()
            {
                EmailAddress = "*****@*****.**", Message = "hello world", DateTimeCreated = DateTimeOffset.UtcNow.AddHours(-2)
            });
            dbContext.Guestbooks.Add(createdGuestbook);

            dbContext.SaveChanges();
        }
Esempio n. 6
0
        public static void PopulateTestData(AppDbContext dbContext)
        {
            var toDos = dbContext.ToDoItems;

            foreach (var item in toDos)
            {
                dbContext.Remove(item);
            }
            dbContext.SaveChanges();
            dbContext.ToDoItems.Add(new ToDoItem()
            {
                Title       = "Test Item 1",
                Description = "Test Description One"
            });
            dbContext.ToDoItems.Add(new ToDoItem()
            {
                Title       = "Test Item 2",
                Description = "Test Description Two"
            });
            dbContext.SaveChanges();

            // add Guestbook test data; specify Guestbook ID for use in tests
            var guestbook = new Guestbook {
                Name = "Test Guestbook", Id = 1
            };

            dbContext.Guestbooks.Add(guestbook);
            guestbook.AddEntry(new GuestbookEntry
            {
                EmailAddress = "*****@*****.**",
                Message      = "Test message"
            });
            guestbook.Events.Clear();
            dbContext.SaveChanges();
        }
Esempio n. 7
0
        public IActionResult Index()
        {
            if (!_repository.List <Guestbook>().Any())
            {
                var createdGuestbook = new Guestbook();
                createdGuestbook.Name = "My Guestbook";

                createdGuestbook.AddEntry(new GuestBookEntry()
                {
                    EmailAddress = "*****@*****.**", Message = "hello world", DateTimeCreated = DateTimeOffset.UtcNow.AddHours(-2)
                });
                createdGuestbook.AddEntry(new GuestBookEntry()
                {
                    EmailAddress = "*****@*****.**", Message = "hello world again", DateTimeCreated = DateTimeOffset.UtcNow.AddHours(-3)
                });
                createdGuestbook.AddEntry(new GuestBookEntry()
                {
                    EmailAddress = "*****@*****.**", Message = "hello from Karen"
                });
                createdGuestbook.Events.Clear();
                _repository.Add(createdGuestbook);
            }

            var guestbook        = _repository.GetById <Guestbook>(1);
            var guestbookEntries = _repository.List <GuestBookEntry>();

            guestbookEntries.Clear();
            foreach (var entry in guestbookEntries)
            {
                guestbook.AddEntry(entry);
            }

            var vm = new HomePageViewModel();

            vm.GuestBookName = guestbook.Name;
            vm.PreviousEntries.AddRange(guestbook.Entries);

            return(View(vm));
        }
Esempio n. 8
0
        public void RecordEntry(Guestbook guestbook, GuestbookEntry entry)
        {
            guestbook.AddEntry(entry);
            _guestbookRepository.Update(guestbook);

            // send updates to previous entries made within last day
            var emailsToNotify = guestbook.Entries
                                 .Where(e => e.DateTimeCreated > DateTimeOffset.UtcNow.AddDays(-1))
                                 .Select(e => e.EmailAddress);

            foreach (var emailAddress in emailsToNotify)
            {
                string messageBody = "{entry.EmailAddress} left new message {entry.Message}";
                _messageSender.SendGuestbookNotificationEmail(emailAddress, messageBody);
            }
        }
Esempio n. 9
0
 private void InitializeData()
 {
     if (!_guestbookRepository.List().Any())
     {
         var newGuestbook = new Guestbook()
         {
             Name = "My Guestbook"
         };
         newGuestbook.AddEntry(new GuestbookEntry()
         {
             EmailAddress = "*****@*****.**",
             Message      = "Hi!"
         });
         _guestbookRepository.Add(newGuestbook);
     }
 }
Esempio n. 10
0
        // [Fact] -- Ignoring for now; need to test against real db
        public void NotIncludeRelatedEntriesWithoutSpec()
        {
            // add data
            var    repository = GetRepository <Guestbook>();
            string name       = Guid.NewGuid().ToString();
            var    guestbook  = new Guestbook()
            {
                Name = name
            };

            repository.Add(guestbook);
            guestbook.AddEntry(new GuestbookEntry()
            {
                EmailAddress = "*****@*****.**", Message = "test 1"
            });
            repository.Update(guestbook);

            var result = repository.List().FirstOrDefault(g => g.Name == name);

            Assert.NotNull(result);
            Assert.Equal(0, result.Entries.Count());
        }