private void DeserializeAstronomers() { var astronomerDtos = JsonFileOperations.DeserializeJsonCollection <AstronomerDto>(Constants.AstronomerJsonFile); using (PlanetHuntersContext ctx = new PlanetHuntersContext()) { AstronomerRepo repo = new AstronomerRepo(ctx); foreach (var dto in astronomerDtos) { if (dto.FirstName != null && dto.LastName != null) { var a = new Astronomer(); Mapper.Map(dto, a); repo.Insert(a); this.DebugLog.AppendLine($"Record {dto.FirstName} {dto.LastName} successfully imported."); } else { this.DebugLog.AppendLine(Constants.InvalidDataFormat); } } DbUtil.ExecTransaction(ctx); } }
private void DeserializeDiscoveries() { var xmlDocument = XDocument.Load(Constants.DiscoveryXmlFile).Root.Elements(); using (PlanetHuntersContext ctx = new PlanetHuntersContext()) { var starRepo = new StarRepo(ctx); var discoveryRepo = new DiscoveryRepo(ctx); var planetRepo = new PlanetRepo(ctx); var telescopeRepo = new TelescopeRepo(ctx); var astronomerRepo = new AstronomerRepo(ctx); foreach (var discoveryElem in xmlDocument) { bool errorFlag = false; var discovery = new Discovery(); DateTime date = DateTime.Parse(discoveryElem.Attribute("DateMade").Value); string telescopeName = discoveryElem.Attribute("Telescope").Value; discovery.DateMade = date; var telescope = telescopeRepo.SearchFor(t => t.Name == telescopeName).FirstOrDefault(); if (telescope == null) { errorFlag = true; } else { discovery.Telescope = telescope; } var starsElement = discoveryElem.Elements("Stars"); if (starsElement.Elements().Any()) { foreach (var sElem in starsElement) { string starName = sElem.Value; var star = starRepo.SearchFor(s => s.Name == starName).FirstOrDefault(); if (star == null) { errorFlag = true; break; } discovery.Stars.Add(star); } } var planetsElement = discoveryElem.Elements("Planets"); if (planetsElement.Elements().Any()) { foreach (var pElem in planetsElement) { string planetName = pElem.Value; var planet = planetRepo.SearchFor(p => p.Name == planetName).FirstOrDefault(); if (planet == null) { errorFlag = true; break; } discovery.Planets.Add(planet); } } var pioneerElement = discoveryElem.Elements("Pioneers").Elements(); if (pioneerElement.Any()) { foreach (var pElem in pioneerElement) { string[] pName = pElem.Value.Split(','); string firstName = pName[0].Trim(); string lastName = pName[1].Trim(); var pioneer = astronomerRepo .GetAll() .Where(p => p.FirstName.Equals(firstName) && p.LastName.Equals(lastName)) .FirstOrDefault(); if (pioneer == null) { errorFlag = true; break; } discovery.Pioneers.Add(pioneer); } } var observersElement = discoveryElem.Elements("Observers"); if (observersElement.Elements().Any()) { foreach (var oElem in observersElement) { string[] oName = oElem.Value.Split(','); string firstName = oName[0].Trim(); string lastName = oName[1].Trim(); var observer = astronomerRepo .SearchFor(p => p.FirstName == firstName && p.LastName == lastName) .FirstOrDefault(); if (observer == null) { errorFlag = true; break; } discovery.Observers.Add(observer); } } if (errorFlag == false) { discoveryRepo.Insert(discovery); ctx.SaveChanges(); } } } }