private List<PlanetViewModel> MapPlanetsToViewModels(List<Planet> planets) { List<PlanetViewModel> vms = new List<PlanetViewModel>(); PlanetViewModel vm; foreach (Planet p in planets) { vm = new PlanetViewModel(); vm.Id = p.Id.ToString(); vm.Name = p.Name; vm.ImagePath = p.ImagePath; vm.Mass = p.Mass; vm.Diameter = p.Diameter; vm.Gravity = p.Gravity; vm.LengthOfDay = p.LengthOfDay; vm.DistanceFromSun = p.DistanceFromSun; vm.OrbitalPeriod = p.OrbitalPeriod; vm.MeanTemperature = p.MeanTemperature; vm.NumberOfMoons = p.NumberOfMoons; vms.Add(vm); } return vms; }
/// <summary> /// Add new Planet then write all planets to Local Folder planets.json /// </summary> private async void ExecuteAddCommand() { SelectedPlanetVM = new PlanetViewModel(); LocalDataAddDialog addDialog = new LocalDataAddDialog(); this.SelectedPlanetVM.TextBoxesAreEnabled = true; addDialog.DataContext = this.SelectedPlanetVM; try { var result = await addDialog.ShowAsync(); if (result == Windows.UI.Xaml.Controls.ContentDialogResult.Primary) { if (PlanetVMs == null) { PlanetVMs = new ObservableCollection<PlanetViewModel>(); } PlanetVMs.Add(SelectedPlanetVM); List<Planet> planets = MapViewModelsToPlanets(PlanetVMs.ToList()); var json = planetRepository.PlanetsToJson(planets); jsonFileService.AddJsonObject(JSON_FILENAME, json); } else { Results = "Operation canceled"; } } catch (Exception x) { Results = x.Message; } }