コード例 #1
0
        public async Task <ActionResult> GetResidentsOfPlanetNaboo(CancellationToken cancellationToken, string planetName)
        {
            // TODO: Implement this controller action
            PlanetResidentsViewModel vm = null;

            try
            {
                vm = await _starWarsService.GetResidentsOfPlanetNabooAsync(planetName, cancellationToken);
            }
            catch (NullReferenceException ex)
            {
                if (ex.Message.Contains("not found"))
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.NotFound));
                }

                //potentially log error here and return 500
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
            }
            catch (Exception ex)
            {
                //potentially log error and rethrow error, or, in this case, just return 500
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
            }

            return(View(vm));
        }
コード例 #2
0
        /// <summary>
        /// Gets a view of all the residents of a particular planet
        /// </summary>
        /// <param name="planetName">Name of the planet to get residents</param>
        /// <returns>View of the residents of a planet</returns>
        public PlanetResidentsViewModel GetAllPlanetResidents(string planetName)
        {
            // Search for a matching planet, then take the first match
            var planet = planetsAPI
                         .GetPlanet(planetName)
                         .Planets
                         .FirstOrDefault();

            // Create a view model for Planet Residents
            var planetResidents = new PlanetResidentsViewModel
            {
                Count = planet.Residents.Count
            };

            // Query for and add each resident
            planet.Residents.ForEach(resident =>
            {
                planetResidents.Residents.Add(peopleAPI.GetResidentSummary(resident));
            });

            // Sort the residents alphabetically
            planetResidents.Residents = planetResidents.Residents
                                        .OrderBy(resident => resident.Name)
                                        .ToList();

            return(planetResidents);
        }
コード例 #3
0
        public PlanetResidentsViewModel ToPlanetResidentsViewModel()
        {
            var model = new PlanetResidentsViewModel();

            model.Residents.AddRange(Residents.Select(r => r.ToResidentSummary()).OrderBy(r => r.Name).ToList());

            return(model);
        }
コード例 #4
0
ファイル: HomeController.cs プロジェクト: raecharles/SWAPI
        public ActionResult GetResidentsOfPlanet(string planetname)
        {
            // TODO: Implement this controller action
            List <Person>            planetResidents = _starWarsService.GetResidentsByPlanet(planetname).Result;
            PlanetResidentsViewModel model           = _viewMapperHelper.ResidentsMapper(planetResidents, planetname);

            return(View(model));
        }
コード例 #5
0
        public ActionResult GetResidentsOfPlanetNaboo(string planetname)
        {
            var model = new PlanetResidentsViewModel();

            var planetResidents = StarWars.GetPlanetResidents(planetname);

            return(View(planetResidents.ToPlanetResidentsViewModel()));
        }
コード例 #6
0
        public ActionResult GetResidentsOfPlanetNaboo(string planetname)
        {
            var model = new PlanetResidentsViewModel();

            // TODO: Implement this controller action

            return(View(model));
        }
コード例 #7
0
ファイル: ViewMapperHelper.cs プロジェクト: raecharles/SWAPI
        public PlanetResidentsViewModel ResidentsMapper(List <Person> people, string planet = "")
        {
            var model = new PlanetResidentsViewModel();

            model.Planet = (planet != "") ? planet.First().ToString().ToUpper() + planet.Substring(1) : "Unknown";
            var config = new MapperConfiguration(cfg =>
                                                 cfg.CreateMap <Person, ResidentSummary>()
                                                 .ForMember(dest => dest.Weight, opt => opt.MapFrom(src => src.Mass))
                                                 );

            var mapper = config.CreateMapper();

            model.Residents = mapper.Map <List <ResidentSummary> >(people);
            model.Residents = model.Residents.OrderBy(r => r.Name).ToList();
            return(model);
        }
コード例 #8
0
ファイル: HomeController.cs プロジェクト: mschluper/swapi
        /// <summary>
        /// Retrieve detailed information about a single planet identified by planetname.
        /// </summary>
        /// <param name="planetname">The name of the planet to be retrieved.</param>
        /// <returns>ActionResult</returns>
        public async Task <IActionResult> GetResidentsOfPlanetNaboo(string planetname)
        {
            var model = new PlanetResidentsViewModel();

            model.Residents = (await _StarWarsService.GetPlanetResidentsAsync(planetname))
                              .Select(r => new ResidentSummaryViewModel
            {
                EyeColor  = r.EyeColor,
                Gender    = r.Gender,
                HairColor = r.HairColor,
                Height    = r.Height,
                Name      = r.Name,
                SkinColor = r.SkinColor,
                Weight    = r.Weight
            })
                              .OrderBy(r => r.Name)
                              .ToList();

            return(View(model));
        }
コード例 #9
0
ファイル: SwapiRepo.cs プロジェクト: abflin2/SWAPICodeSample
        public PlanetResidentsViewModel GetResidentsOfPlanet(string planet)
        {
            PlanetResidentsViewModel residents = new PlanetResidentsViewModel();

            List <PersonModel> results = swapiAccessors.RequestResidentsOfPlanet(planet);

            if (results != null)
            {
                residents.Residents.AddRange(results.Select(r => new ResidentSummary()
                {
                    Name      = r.Name,
                    Height    = r.Height,
                    Weight    = r.Mass,
                    Gender    = r.Gender,
                    HairColor = r.HairColor,
                    EyeColor  = r.EyeColor,
                    SkinColor = r.SkinColor
                }).OrderBy(o => o.Name));
            }

            return(residents);
        }
コード例 #10
0
        public async Task <ActionResult> GetResidentsOfPlanetNaboo(string planetname)
        {
            var model = new PlanetResidentsViewModel();

            List <ResidentModel> residents = await starWarsApi.GetResidentsByPlanetNameAsync("Naboo");

            residents = residents.OrderBy(x => x.ResidentName).ToList();
            foreach (ResidentModel r in residents)
            {
                ResidentSummary rs = new ResidentSummary();
                rs.EyeColor  = r.ResidentEyeColor;
                rs.Gender    = r.ResidentGender;
                rs.HairColor = r.ResidentHairColor;
                rs.Height    = r.ResidentHeight;
                rs.Name      = r.ResidentName;
                rs.SkinColor = r.ResidentSkinColor;
                rs.Weight    = r.ResidentWeight;

                model.Residents.Add(rs);
            }
            return(View(model));
        }
コード例 #11
0
        public ActionResult GetResidentsOfPlanetNaboo(string planetname)
        {
            var model = new PlanetResidentsViewModel(planetname);

            return(View(model));
        }