public void LogPrincipalChanged() { Member first; Member second; PersonAddress address; using (var db = new KcsarContext(this.databaseLocation)) { first = new Member { FirstName = "First" }; db.Members.Add(first); second = new Member { FirstName = "Second" }; db.Members.Add(second); address = new PersonAddress { Person = first, Street = "123", City = "Any", State = "WA", Zip = "98765" }; first.Addresses.Add(address); db.SaveChanges(); } var checkpoint = GetCheckpoint(); using (var db = new KcsarContext(this.databaseLocation)) { address = db.Members.Where(f => f.Id == first.Id).SelectMany(f => f.Addresses).Single(); address.Person = db.Members.Single(f => f.Id == second.Id); db.SaveChanges(); } using (var db = new KcsarContext(this.databaseLocation)) { var logs = db.GetLog(checkpoint); Assert.AreEqual(1, logs.Length, "log count"); Assert.AreEqual(string.Format("{0}<br/>{1} => {2}", address, first, second), logs[0].Comment); Assert.AreEqual("Modified", logs[0].Action); } }
public ActionResult CreateAddress(Guid personId, FormCollection fields) { if (!Permissions.IsAdmin && !Permissions.IsSelf(personId)) return this.CreateLoginRedirect(); if (Session["NewAddressGuid"] != null && Session["NewAddressGuid"].ToString() != fields["NewAddressGuid"]) { throw new InvalidOperationException("Invalid operation. Are you trying to re-create a status change?"); } Session.Remove("NewAddressGuid"); ViewData["PageTitle"] = "New Address"; PersonAddress address = new PersonAddress(); address.Person = (from p in this.db.Members where p.Id == personId select p).First(); this.db.PersonAddress.Add(address); return InternalSaveAddress(address, fields); }
public ActionResult CreateAddress(Guid personId) { if (!Permissions.IsAdmin && !Permissions.IsSelf(personId)) return this.CreateLoginRedirect(); ViewData["PageTitle"] = "New Address"; PersonAddress address = new PersonAddress { State = "WA" }; address.Person = (from p in this.db.Members where p.Id == personId select p).First(); Session.Add("NewAddressGuid", address.Id); ViewData["NewAddressGuid"] = Session["NewAddressGuid"]; return InternalEditAddress(address); }
private Member NewEsarTrainee_Internal(FormCollection fields) { Member m = new Member(); TryUpdateModel(m, new[] { "FirstName", "LastName", "MiddleName", "BirthDate", "SheriffApp", "Gender" }); this.db.Members.Add(m); SarUnit esar = (from u in this.db.Units where u.DisplayName == "ESAR" select u).First(); UnitStatus status = (from s in this.db.UnitStatusTypes where s.Unit.Id == esar.Id && s.StatusName == "trainee" select s).First(); if (!string.IsNullOrEmpty(fields["Street"])) { PersonAddress address = new PersonAddress { Person = m, Type = PersonAddressType.Mailing }; TryUpdateModel(address, new[] { "Street", "City", "State" }); GeographyServices.RefineAddressWithGeography(address); if (address.Quality < 8) { try { ModelState.SetModelValue("Zip", new ValueProviderResult(fields["Zip"], fields["Zip"], CultureInfo.CurrentUICulture)); // This is supposed to be UpdateModel, not TryUpdate UpdateModel(address, new[] { "Zip" }); } catch (Exception) { ModelState.AddModelError("Zip", "Can't locate address. ZIP is required"); } } this.db.PersonAddress.Add(address); } foreach (string contact in new[] { "Home", "Work", "Cell" }) { if (string.IsNullOrEmpty(fields[contact + "Phone"])) { continue; } ModelState.SetModelValue(contact + "Phone", new ValueProviderResult(fields[contact + "Phone"], fields[contact + "Phone"], CultureInfo.CurrentUICulture)); PersonContact pc = new PersonContact { Person = m, Type = "phone", Subtype = contact.ToLower(), Value = fields[contact + "Phone"] }; this.db.PersonContact.Add(pc); } if (!string.IsNullOrEmpty(fields["HamCall"])) { ModelState.SetModelValue("HamCall", new ValueProviderResult(fields["HamCall"], fields["HamCall"], CultureInfo.CurrentUICulture)); PersonContact pc = new PersonContact { Person = m, Type = "hamcall", Value = fields["HamCall"] }; this.db.PersonContact.Add(pc); } if (!string.IsNullOrEmpty(fields["Email"])) { ModelState.SetModelValue("Email", new ValueProviderResult(fields["Email"], fields["Email"], CultureInfo.CurrentUICulture)); PersonContact pc = new PersonContact { Person = m, Type = "email", Value = fields["Email"] }; this.db.PersonContact.Add(pc); } if (!string.IsNullOrEmpty(fields["Email2"])) { ModelState.SetModelValue("Email2", new ValueProviderResult(fields["Email2"], fields["Email2"], CultureInfo.CurrentUICulture)); PersonContact pc = new PersonContact { Person = m, Type = "email", Value = fields["Email2"] }; this.db.PersonContact.Add(pc); } DateTime courseDate = new DateTime(1900, 1, 1); ModelState.SetModelValue("CourseDate", new ValueProviderResult(fields["CourseDate"], fields["CourseDate"], CultureInfo.CurrentUICulture)); if (string.IsNullOrEmpty(fields["CourseDate"])) { ModelState.AddModelError("CourseDate", "Required"); return null; } else if (!DateTime.TryParse(fields["CourseDate"], out courseDate)) { ModelState.AddModelError("CourseDate", "Unknown format. Try yyyy-mm-dd"); return null; } courseDate = courseDate.Date; UnitMembership um = new UnitMembership { Person = m, Status = status, Unit = esar, Activated = courseDate }; this.db.UnitMemberships.Add(um); TrainingCourse courseA = (from tc in this.db.TrainingCourses where tc.DisplayName == "Course A" select tc).First(); DateTime nextDate = courseDate.AddDays(1); Training t = (from trn in this.db.Trainings where trn.StartTime >= courseDate && trn.StartTime < nextDate && trn.Title == "Course A" select trn).FirstOrDefault(); if (t == null) { t = new Training(); t.OfferedCourses.Add(courseA); t.StartTime = courseDate.AddHours(19); t.StopTime = courseDate.AddHours(21); t.County = "King"; t.Title = "Course A"; t.Location = "Eastside Fire Headquarters"; this.db.Trainings.Add(t); } TrainingRoster tr = new TrainingRoster { Person = m, TimeIn = courseDate.AddHours(18), TimeOut = courseDate.AddHours(22) }; this.db.TrainingRosters.Add(tr); t.Roster.Add(tr); TrainingAward ta = new TrainingAward(); ta.Completed = courseDate.AddHours(21); if ((courseA.ValidMonths ?? 0) > 0) { ta.Expiry = ta.Completed.AddMonths(courseA.ValidMonths.Value); } ta.Course = courseA; ta.Member = m; this.db.TrainingAward.Add(ta); tr.TrainingAwards.Add(ta); return m; }
private ActionResult InternalSaveAddress(PersonAddress address, FormCollection fields) { TryUpdateModel(address, new string[] { "Street", "City", "State", "Zip", "Type" }); Guid personId = new Guid(fields["Person"]); Member person = (from m in this.db.Members where m.Id == personId select m).First(); address.Person = person; address.Quality = 0; address.Location = null; if (ModelState.IsValid) { this.db.SaveChanges(); TempData["message"] = "Saved"; return RedirectToAction("ClosePopup"); } return InternalEditAddress(address); }
private ActionResult InternalEditAddress(PersonAddress address) { ViewData["State"] = new SelectList(new string[] { "AL", "AK", "AS", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FM", "FL", "GA", "GU", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MH", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "MP", "OH", "OK", "OR", "PW", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VI", "VA", "WA", "WV", "WI", "WY" }, address.State.ToUpperInvariant()); ViewData["Type"] = new SelectList(Enum.GetNames(typeof(PersonAddressType)), address.Type.ToString()); return View("EditAddress", address); }