/// <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(); } }
/// <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(); } }
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(); } }
/// <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(); } }
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(); } }