예제 #1
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();
                }
            }
        }
예제 #2
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();
            }
        }
        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
        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));
            }
        }
예제 #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
 public void Save()
 {
     using (var db = new MailerEntities())
     {
         db.MailingLists.Attach(MailingList);
         db.Entry(MailingList).State = EntityState.Modified;
         db.SaveChanges();
     }
 }
예제 #7
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();
            }
        }
예제 #8
0
        private Address MakeDatabaseAddress()
        {
            using (var db = new MailerEntities())
            {
                var addr = MakeAddress();
                db.Addresses.Add(addr);
                db.SaveChanges();

                return(addr);
            }
        }
예제 #9
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();
            }
        }
예제 #10
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();
            }
        }
예제 #11
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");
        }
예제 #12
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");
        }
예제 #13
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();
            }
        }
예제 #14
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));
            }
        }
예제 #15
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);
        }
        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();
            }
        }