public static Registration CreateNew(Registration newreg, campdata ctx) { var reg = ctx.Registrations.Create(); reg.ContactName = newreg.ContactName; reg.ContactEmail = newreg.ContactEmail; reg.ContactPhone = newreg.ContactPhone; reg.Camps = new List <Camp>(); foreach (var camp in newreg.Camps) { reg.Camps.Add(ctx.Camps.Find(camp.Id)); } reg.Campers = new List <Camper>(); foreach (var camper in newreg.Campers) { Camper c; if (camper.Id > 100) { c = ctx.Campers.Find(camper.Id); } else { c = ctx.Campers.Create(); ctx.Campers.Add(c); } c.Name = camper.Name; c.Email = camper.Email; c.Parentname = (string.IsNullOrWhiteSpace(camper.Parentname)) ? newreg.ContactName : camper.Parentname; c.Phone = camper.Phone; c.RiderLevel = camper.RiderLevel; c.Notes = camper.Notes; reg.Campers.Add(c); } return(reg); }
public void Update(Registration updreg, campdata ctx) { if (this.RegistrationId != updreg.RegistrationId) { throw new Exception("Attempt to update registration " + this.RegistrationId + " with data for " + updreg.RegistrationId); } this.ContactName = updreg.ContactName; this.ContactEmail = updreg.ContactEmail; this.ContactPhone = updreg.ContactPhone; for (var i = 0; i < this.Camps.Count; i++) { var camp = this.Camps.ElementAt(i); if (updreg.Camps.All(c => c.Id != camp.Id)) { this.Camps.Remove(camp); i--; } } for (var i = 0; i < updreg.Camps.Count; i++) { var camp = updreg.Camps.ElementAt(i); if (this.Camps.All(c => c.Id != camp.Id)) { this.Camps.Add(ctx.Camps.Find(camp.Id)); } } for (var i = 0; i < this.Campers.Count; i++) { var camper = this.Campers.ElementAt(i); if (updreg.Campers.All(c => c.Id != camper.Id)) { this.Campers.Remove(camper); i--; } } for (var i = 0; i < updreg.Campers.Count; i++) { var camper2 = updreg.Campers.ElementAt(i); if (camper2.Id == 0) { var c = ctx.Campers.Create(); c.Name = camper2.Name; c.Parentname = (string.IsNullOrWhiteSpace(camper2.Parentname)) ? updreg.ContactName : camper2.Parentname; c.Phone = camper2.Phone; c.Email = camper2.Email; c.RiderLevel = camper2.RiderLevel; c.Notes = camper2.Notes; ctx.Campers.Add(c); this.Campers.Add(c); } else { var camper = this.Campers.FirstOrDefault(c => c.Id == camper2.Id); if (camper == null) { camper = ctx.Campers.Find(camper2.Id); this.Campers.Add(camper); } camper.Name = camper2.Name; camper.Parentname = (string.IsNullOrWhiteSpace(camper2.Parentname)) ? updreg.ContactName : camper2.Parentname; camper.Email = camper2.Email; camper.Phone = camper2.Phone; camper.RiderLevel = camper2.RiderLevel; camper.Notes = camper2.Notes; } } }