//Assigns Sites to a Crew public string Add_Site_To_Crew(int crewId, int siteId) { using (var context = new COESystemContext()) { string units = ""; CrewSite crewSite = (from x in context.CrewSites where x.CrewID == crewId && x.SiteID == siteId select x).FirstOrDefault(); List <string> reasons = new List <string>(); if (crewSite != null) { reasons.Add("This site is already assigned to the current crew"); } else { //Notify user that the site is already assigned to at least another crew List <CrewSite> crewSites = new List <CrewSite>(); crewSites = (from x in context.CrewSites where DbFunctions.TruncateTime(x.Crew.CrewDate) == DbFunctions.TruncateTime(DateTime.Now) && siteId == x.SiteID select x).ToList(); if (crewSites != null) { foreach (CrewSite cs in crewSites) { units += cs.Crew.Unit.UnitNumber + ", "; } } //Create a new CrewSite for the current Crew crewSite = new CrewSite(); crewSite.SiteID = siteId; crewSite.CrewID = crewId; context.CrewSites.Add(crewSite); } if (reasons.Count() > 0) { throw new BusinessRuleException("Adding Site", reasons); } context.SaveChanges(); return(units); } }
public void RemoveCrewSite(int crewSiteId) { using (var context = new COESystemContext()) { List <string> message = new List <string>(); CrewSite cs = context.CrewSites.Find(crewSiteId); List <Grass> grassList = cs.Grasses.Select(x => x).ToList(); List <Watering> wateringList = cs.Waterings.Select(x => x).ToList(); List <Planting> plantinList = cs.Plantings.Select(x => x).ToList(); List <SBM> sBMList = cs.SBMs.Select(x => x).ToList(); List <Mulching> mulchingList = cs.Mulchings.Select(x => x).ToList(); List <Pruning> pruningList = cs.Prunings.Select(x => x).ToList(); List <Uprooting> uprootingList = cs.Uprootings.Select(x => x).ToList(); if (cs == null) { message.Add("This Site was not assigned to the current Crew"); } if (message.Count() > 0) { throw new BusinessRuleException("Removing Site from current Crew Failed", message); } else { foreach (Grass item in grassList) { context.Grasses.Remove(item); } foreach (Watering item in wateringList) { context.Waterings.Remove(item); } foreach (Planting item in plantinList) { context.Plantings.Remove(item); } foreach (SBM item in sBMList) { context.SBMs.Remove(item); } foreach (Mulching item in mulchingList) { context.Mulchings.Remove(item); } foreach (Pruning item in pruningList) { context.Prunings.Remove(item); } foreach (Uprooting item in uprootingList) { context.Uprootings.Remove(item); } context.CrewSites.Remove(cs); context.SaveChanges(); } } }