Пример #1
0
        public void TestLoadFromDatabase()
        {
            var alvm = new AddressListViewModel();

            var address = new Address
            {
                FirstName = "Bob",
                LastName  = "Newbie",
                Email     = "*****@*****.**"
            };

            using (var db = new MailerEntities())
            {
                db.Addresses.Add(address);
                db.SaveChanges();
            }

            alvm.LoadFromDatabase();

            // Check that it was loaded.
            // Don't check that this is equal to 1, because there might be more
            // from the other tests
            Assert.IsTrue(alvm.AddressListItemViewModels.Count > 0);

            foreach (var addressListItemViewModel in alvm.AddressListItemViewModels)
            {
                addressListItemViewModel.Delete();
            }
        }
Пример #2
0
        public void TestExternalDeletion()
        {
            var alvm = new AddressListViewModel();

            var address = new Address
            {
                FirstName = "Bob",
                LastName  = "Newbie",
                Email     = "*****@*****.**"
            };

            using (var db = new MailerEntities())
            {
                db.Addresses.Add(address);
                db.SaveChanges();
            }

            var alivm1 = new AddressListItemViewModel(address);

            alvm.AddAddressListItemViewModel(alivm1);
            Assert.AreEqual(1, alvm.AddressListItemViewModels.Count);

            alivm1.Delete();
            Assert.AreEqual(0, alvm.AddressListItemViewModels.Count);

            using (var db = new MailerEntities())
            {
                // ensure that it was removed
                Assert.IsFalse(db.Addresses.Any(addr => addr.AddressID == address.AddressID));
            }
        }
        public void TestDelete()
        {
            var testList = new MailingList
            {
                Name             = "a",
                MailingListLines = new Collection <MailingListLine>
                {
                    new MailingListLine
                    {
                        Address = new Address
                        {
                            Email         = "*****@*****.**",
                            FirstName     = "yay",
                            LastName      = "demmetasdfadsf",
                            ReceivedMails = new List <ReceivedMail>()
                        }
                    }
                }
            };

            using (var db = new MailerEntities())
            {
                db.MailingLists.Add(testList);
                db.SaveChanges();
                var mlivmTest = new MailingListItemViewModel(testList, true);

                mlivmTest.Delete();

                Assert.IsFalse(db.MailingLists.Any(ml => ml.ListID == testList.ListID));
            }
        }
Пример #4
0
        /// <summary>
        ///     Add a RecievedMail entity to the database corresponding to the given year and the Address
        ///     entity for which this EditAddressViewModel was created. Throws an InvalidOperationException if
        ///     the given year already exists in the database.
        /// </summary>
        /// <param name="year">The year to create a RecievedMail entity for in the database.</param>
        public void AddYear(int year)
        {
            if (ReceivedMails.Any(rm => rm.Year == year))
            {
                throw new ArgumentException("Year already exists!");
            }

            if (year < 1970)
            {
                throw new ArgumentException("Year too old!");
            }

            if (year > DateTime.Now.Year)
            {
                throw new ArgumentException("Year can't be in the future!");
            }

            using (var db = new MailerEntities())
            {
                db.Addresses.Attach(Address);
                var newRm = new ReceivedMail
                {
                    Year = year
                };
                Address.ReceivedMails.Add(newRm);
                ReceivedMails.Add(newRm);
                db.SaveChangesAsync();
            }
        }
Пример #5
0
        /// <summary>
        ///     Save the address to the database. Note that RecievedMail entities are saved as they are created
        ///     and removed by their correponding methods. This method only saves the address itself - names
        ///     and email.
        /// </summary>
        public void Save()
        {
            if (Address.FirstName == "")
            {
                throw new ArgumentException("First name must not be empty");
            }
            if (Address.Email == "")
            {
                throw new ArgumentException("Email address must not be empty");
            }

            try
            {
                new MailAddress(Address.Email, Address.FirstName);
            }
            catch (Exception)
            {
                throw new ArgumentException("Email address is not in a valid format.");
            }

            using (var db = new MailerEntities())
            {
                db.Addresses.Attach(Address);
                db.Entry(Address).State = EntityState.Modified;
                db.SaveChanges();
            }
        }
Пример #6
0
        /// <summary>
        ///     Add a new Address to the database, and opens a dialog to edit that new address.
        /// </summary>
        public void Add()
        {
            using (var db = new MailerEntities())
            {
                var address = new Address
                {
                    FirstName = "",
                    LastName  = "",
                    Email     = "",
                };
                db.Addresses.Add(address);
                db.SaveChanges();


                var vm = new AddressListItemViewModel(address);
                if (vm.Edit() == true)
                {
                    AddAddressListItemViewModel(vm);
                }
                else
                {
                    db.Addresses.Remove(address);
                    db.SaveChanges();
                }
            }
        }
Пример #7
0
 /// <summary>
 ///     Create a new EditAddressViewModel for the given Address entity. A database connection will be
 ///     established to retrieve any missing data.
 /// </summary>
 /// <param name="address">The Address entity being edited.</param>
 public EditAddressViewModel(Address address)
 {
     using (var db = new MailerEntities())
     {
         Address       = db.Addresses.Find(address.AddressID);
         ReceivedMails = new ObservableCollection <ReceivedMail>(Address.ReceivedMails);
     }
 }
Пример #8
0
 public void Save()
 {
     using (var db = new MailerEntities())
     {
         db.MailingLists.Attach(MailingList);
         db.Entry(MailingList).State = EntityState.Modified;
         db.SaveChanges();
     }
 }
Пример #9
0
 /// <summary>
 ///     Command the ViewModel to load all the addresses from MailerEntities.
 /// </summary>
 public void LoadFromDatabase()
 {
     using (var db = new MailerEntities())
     {
         foreach (var address in db.Addresses)
         {
             AddAddressListItemViewModel(new AddressListItemViewModel(address));
         }
     }
 }
Пример #10
0
        public void TestSave()
        {
            var addr = MakeDatabaseAddress();

            var eavm = new EditAddressViewModel(addr);


            eavm.Address.FirstName = "";
            try
            {
                eavm.Save();
            }
            catch (Exception)
            {
                // expected
            }


            eavm.Address.FirstName = "Bob";


            foreach (var s in new[]
            {
                "",
                "bob",
                "@",
                ".@",
                "123123"
            })
            {
                eavm.Address.Email = s;
                try
                {
                    eavm.Save();
                }
                catch (Exception)
                {
                    // expected
                }
            }

            eavm.Address.Email = "*****@*****.**";

            eavm.Save();

            using (var db = new MailerEntities())
            {
                var addrFromDb = db.Addresses.Find(addr.AddressID);
                Assert.AreEqual("Bob", addrFromDb.FirstName);
                Assert.AreEqual("*****@*****.**", addrFromDb.Email);

                db.Addresses.Remove(addrFromDb);
                db.SaveChanges();
            }
        }
Пример #11
0
        private Address MakeDatabaseAddress()
        {
            using (var db = new MailerEntities())
            {
                var addr = MakeAddress();
                db.Addresses.Add(addr);
                db.SaveChanges();

                return(addr);
            }
        }
Пример #12
0
        /// <summary>
        ///     Remove the RecievedMail entity from the database corresponding to the given year and the
        ///     Address entity for which this EditAddressViewModel was created. Will throw an exception if the
        ///     year is not found in the database.
        /// </summary>
        /// <param name="year">The year to remove the RecievedMail entity for from the database.</param>
        public void RemoveYear(int year)
        {
            using (var db = new MailerEntities())
            {
                db.Addresses.Attach(Address);

                var rmToRemove = ReceivedMails.Single(rm => rm.Year == year);
                Address.ReceivedMails.Remove(rmToRemove);
                ReceivedMails.Remove(rmToRemove);
                db.SaveChangesAsync();
            }
        }
Пример #13
0
        public void AddRecipient(MailingList list)
        {
            using (var db = new MailerEntities())
            {
                list = db.MailingLists.Find(list.ListID);

                foreach (var line in list.MailingListLines)
                {
                    recipients.Add(line.Address);
                }
            }
        }
Пример #14
0
        public void RemoveAddressId(long aid)
        {
            using (var db = new MailerEntities())
            {
                db.MailingLists.Attach(MailingList);

                var mllToRemove = MailingList.MailingListLines.First(mll => mll.AddressID == aid);
                MailingList.MailingListLines.Remove(mllToRemove);
                AvailAddresses.Add(mllToRemove.Address);
                CurrAddresses.Remove(CurrAddresses.Single(addr => addr.AddressID == aid));
                db.SaveChangesAsync();
            }
        }
Пример #15
0
        /// <summary>
        ///     Delete this address from the database, and fire the Deleted event.
        /// </summary>
        public void Delete()
        {
            using (var db = new MailerEntities())
            {
                db.Addresses.Attach(Address);
                db.MailingListLines.RemoveRange(Address.MailingListLines);
                db.ReceivedMails.RemoveRange(Address.ReceivedMails);
                db.Addresses.Remove(Address);
                db.SaveChanges();
            }

            MessagePump.Dispatch(this, "AddressDeleted");
        }
Пример #16
0
        /// <summary>
        ///     Delete the underlying MailingList from the database.
        /// </summary>
        public void Delete()
        {
            if (!CanChange)
            {
                throw new InvalidOperationException("Cannot delete this mailing list");
            }

            using (var db = new MailerEntities())
            {
                db.MailingLists.Attach(MailingList);
                db.MailingListLines.RemoveRange(MailingList.MailingListLines);
                db.MailingLists.Remove(MailingList);
                db.SaveChanges();
            }

            MessagePump.Dispatch(this, "MailingListDeleted");
        }
Пример #17
0
        /// <summary>
        ///     Add a new MailingList to the database, and opens a dialog to edit it.
        /// </summary>
        public void Add()
        {
            using (var db = new MailerEntities())
            {
                var mlist = new MailingList
                {
                    Name = "New Mailing List"
                };
                db.MailingLists.Add(mlist);
                db.SaveChangesAsync();


                var vm = new MailingListItemViewModel(mlist, true);
                AddMailingListItemViewModel(vm);
                vm.Edit();
            }
        }
Пример #18
0
        public EditMailingViewModel(MailingList mlist)
        {
            using (var db = new MailerEntities())
            {
                MailingList = db.MailingLists.Find(mlist.ListID);

                var allAddresses = db.Addresses.ToList();

                CurrAddresses = new ObservableCollection <Address>();

                foreach (var mailingListLine in MailingList.MailingListLines)
                {
                    CurrAddresses.Add(mailingListLine.Address);
                    allAddresses.Remove(mailingListLine.Address);
                }

                AvailAddresses = new ObservableCollection <Address>(allAddresses);
            }
        }
Пример #19
0
        /// <summary>
        ///     Open an EditAddress dialog for editing this address.
        /// </summary>
        /// <returns>The DialogResult of the underlying EditAddress window.</returns>
        public bool?Edit()
        {
            var addrWind = new EditAddress(new EditAddressViewModel(Address));

            addrWind.ShowDialog();

            using (var db = new MailerEntities())
            {
                Address = db.Addresses.Find(Address.AddressID);
            }

            OnPropertyChanged("FirstName");
            OnPropertyChanged("LastName");
            OnPropertyChanged("Email");

            MessagePump.Dispatch(this, "AddressChanged");

            return(addrWind.DialogResult);
        }
Пример #20
0
        /// <summary>
        ///     Update the ViewModel with data from MailerEntities.
        /// </summary>
        public void LoadFromDatabase()
        {
            using (var db = new MailerEntities())
            {
                MailingListItemViewModels.Clear();

                // Add regular mailing lists.
                foreach (var mlist in db.MailingLists)
                {
                    AddMailingListItemViewModel(new MailingListItemViewModel(mlist, true));
                }

                // Add dynamic mailing lists
                var yearMailingLists = db.GetYearMailingLists();
                foreach (var mlist in yearMailingLists)
                {
                    AddMailingListItemViewModel(new MailingListItemViewModel(mlist, false));
                }
            }
        }
Пример #21
0
        public void MessagePumpTest()
        {
            var mlvmTest = new MailingListViewModel();

            using (var db = new MailerEntities())
            {
                var mlist = new MailingList
                {
                    Name = "TestList"
                };
                db.MailingLists.Add(mlist);
                db.SaveChanges();


                var vm = new MailingListItemViewModel(mlist, true);
                mlvmTest.AddMailingListItemViewModel(vm);
                vm.Delete();

                Assert.IsFalse(mlvmTest.MailingListItemViewModels.Contains(vm));
            }
        }
Пример #22
0
        public void AddAddressId(long aid)
        {
            if (CurrAddresses.Any(addr => addr.AddressID == aid))
            {
                throw new InvalidOperationException("Address ID already exists!");
            }

            using (var db = new MailerEntities())
            {
                db.MailingLists.Attach(MailingList);
                var newRm = new MailingListLine
                {
                    AddressID = aid
                };
                MailingList.MailingListLines.Add(newRm);
                var address = db.Addresses.Find(aid);
                CurrAddresses.Add(address);
                AvailAddresses.Remove(AvailAddresses.Single(addr => addr.AddressID == aid));
                db.SaveChangesAsync();
            }
        }
Пример #23
0
        public void SaveTest()
        {
            var testList = new MailingList
            {
                Name = "TestDBSave",
            };

            using (var db = new MailerEntities())
            {
                db.MailingLists.Add(testList);
                db.SaveChanges();
                var TestEavm = new EditMailingViewModel(testList);

                TestEavm.Save();

                Assert.IsTrue(db.MailingLists.Any(ml => testList.Name.Equals(ml.Name) && testList.ListID == ml.ListID));

                db.MailingLists.Remove(testList);
                db.SaveChanges();
            }
        }
Пример #24
0
        public void TestRemoveYear()
        {
            var addr = MakeDatabaseAddress();

            var eavm = new EditAddressViewModel(addr);

            eavm.AddYear(2000);
            Assert.AreEqual(1, eavm.ReceivedMails.Count);

            eavm.RemoveYear(2000);
            Assert.AreEqual(0, eavm.ReceivedMails.Count);

            using (var db = new MailerEntities())
            {
                var addrFromDb = db.Addresses.Find(addr.AddressID);
                Assert.AreEqual(0, addrFromDb.ReceivedMails.Count);

                db.Addresses.Remove(addrFromDb);
                db.SaveChanges();
            }
        }
Пример #25
0
        public void RemoveAddressIdTest()
        {
            var testAddress = new Address
            {
                Email         = "*****@*****.**",
                FirstName     = "yayheythere",
                LastName      = "demmetasdfadsfohyeah",
                ReceivedMails = new List <ReceivedMail>()
            };

            var testList = new MailingList
            {
                Name             = "heythere",
                MailingListLines = new Collection <MailingListLine>()
            };

            using (var db = new MailerEntities())
            {
                db.MailingLists.Add(testList);
                db.Addresses.Add(testAddress);

                db.SaveChanges();

                var testEavm = new EditMailingViewModel(testList);

                testEavm.AddAddressId(testAddress.AddressID);


                Assert.AreEqual(0, testEavm.AvailAddresses.Count(addr => addr.AddressID == testAddress.AddressID));

                testEavm.RemoveAddressId(testAddress.AddressID);

                Assert.AreEqual(0, testEavm.CurrAddresses.Count(addr => addr.AddressID == testAddress.AddressID));
                Assert.AreEqual(1, testEavm.AvailAddresses.Count(addr => addr.AddressID == testAddress.AddressID));

                db.MailingLists.Remove(testList);
                db.SaveChanges();
            }
        }
Пример #26
0
        /// <summary>
        ///     Open a new EditMailingList window to edit the current mailing list.
        /// </summary>
        public void Edit()
        {
            if (!CanChange)
            {
                throw new InvalidOperationException("Cannot edit this mailing list");
            }

            var mailWind = new EditMailingList(new EditMailingViewModel(MailingList));

            mailWind.ShowDialog();

            using (var db = new MailerEntities())
            {
                MailingList    = db.MailingLists.Find(MailingList.ListID);
                countAddresses = MailingList.MailingListLines.Count;
            }

            OnPropertyChanged("Name");
            OnPropertyChanged("Subtext");

            MessagePump.Dispatch(this, "MailingListChanged");
        }
        public void DeleteTest()
        {
            var ml = new MailingList
            {
                Name = "TestMailingList",
            };

            var newAddress = new Address
            {
                FirstName     = "Bob",
                LastName      = "Newbie",
                Email         = "*****@*****.**",
                ReceivedMails = new Collection <ReceivedMail>
                {
                    new ReceivedMail
                    {
                        Year = 2013
                    },
                    new ReceivedMail
                    {
                        Year = 2014
                    },
                }
            };

            var mll = new MailingListLine
            {
                Address     = newAddress,
                MailingList = ml
            };


            using (var db = new MailerEntities())
            {
                db.Addresses.Add(newAddress);
                db.MailingLists.Add(ml);
                db.MailingListLines.Add(mll);
                db.SaveChanges();

                // check that the two receievedmails were added
                Assert.AreEqual(2, db.ReceivedMails.Count(rm => rm.AddressID == newAddress.AddressID));
            }

            var alivm = new AddressListItemViewModel(newAddress);

            var pumpFired = 0;

            MessagePump.OnMessage += (sender, msg) =>
            {
                if (msg == "AddressDeleted" && sender == alivm)
                {
                    pumpFired++;
                }
            };


            alivm.Delete();

            Assert.AreEqual(1, pumpFired);

            using (var db = new MailerEntities())
            {
                // ensure that the database is empty once again
                Assert.IsFalse(db.Addresses.Any(addr => addr.AddressID == newAddress.AddressID));
                Assert.IsFalse(db.MailingListLines.Any(line => line.AddressID == newAddress.AddressID));
                Assert.IsFalse(db.ReceivedMails.Any(rm => rm.AddressID == newAddress.AddressID));

                // delete the mailing list
                db.MailingLists.Attach(ml);
                db.MailingLists.Remove(ml);
                db.SaveChanges();
            }
        }
Пример #28
0
        public void TestAddYear()
        {
            var addr = MakeDatabaseAddress();

            var eavm = new EditAddressViewModel(addr);

            try
            {
                eavm.AddYear(2000);
                eavm.AddYear(2000);
                Assert.Fail("No execption thrown");
            }
            catch (Exception)
            {
                // Expected
            }


            // Year too old
            try
            {
                eavm.AddYear(1969);
                Assert.Fail("No execption thrown");
            }
            catch (Exception)
            {
                // Expected
            }


            // Year in future
            try
            {
                eavm.AddYear(DateTime.Now.Year + 10);
                Assert.Fail("No execption thrown");
            }
            catch (Exception)
            {
                // Expected
            }

            // there should be 1 from the duplicate address test
            Assert.AreEqual(1, eavm.ReceivedMails.Count);

            // and now there will be 2
            eavm.AddYear(2012);
            Assert.AreEqual(2, eavm.ReceivedMails.Count);

            using (var db = new MailerEntities())
            {
                // Check that they're there in the database.
                var addrFromDb = db.Addresses.Find(addr.AddressID);
                Assert.AreEqual(2, addrFromDb.ReceivedMails.Count);

                db.Addresses.Remove(addrFromDb);
                db.SaveChanges();
            }

            eavm.RemoveYear(2000);
            eavm.RemoveYear(2012);
        }