public ActionResult GetStarships(string ids = "") { List <Starship> ships = _starWarsService.GetStarships().Result; StarshipsJsonModel model = _viewMapperHelper.StarshipsMatchedMapper(ships, ids); var json = new JsonResult(model); json.StatusCode = 200; json.ContentType = "application/json"; return(json); }
public StarshipsJsonModel StarshipsMatchedMapper(List <Starship> starships, string ids = "") { List <string> urls = new List <string>(); if (ids != "") { urls = ids.Split(",").Select(u => u.Trim()).ToList(); starships = starships.Where(p => urls.Contains(p.Url)).ToList(); } var model = new StarshipsJsonModel(); var config = new MapperConfiguration(cfg => cfg.CreateMap <Starship, StarshipDetailsJsonModel>() .ForMember(dest => dest.MaxSpeed, opt => opt.MapFrom(src => src.MaxAtmospheringSpeed)) .ForMember(dest => dest.Class, opt => opt.MapFrom(src => src.StarshipClass)) .ForMember(dest => dest.Cost, opt => opt.MapFrom(src => src.CostInCredits)) ); var mapper = config.CreateMapper(); model.Starships = mapper.Map <List <StarshipDetailsJsonModel> >(starships); model.Starships = model.Starships.OrderBy(p => p.Name).ToList(); return(model); }