Exemplo n.º 1
0
        public ActionResult GetVehicles(string ids = "")
        {
            List <Vehicle>    vehicles = _starWarsService.GetVehicles().Result;
            VehiclesJsonModel model    = _viewMapperHelper.VehiclesMatchedMapper(vehicles, ids);
            var json = new JsonResult(model);

            json.StatusCode  = 200;
            json.ContentType = "application/json";
            return(json);
        }
Exemplo n.º 2
0
        public VehiclesJsonModel VehiclesMatchedMapper(List <Vehicle> vehicles, string ids = "")
        {
            List <string> urls = new List <string>();

            if (ids != "")
            {
                urls     = ids.Split(",").Select(u => u.Trim()).ToList();
                vehicles = vehicles.Where(p => urls.Contains(p.Url)).ToList();
            }
            var model  = new VehiclesJsonModel();
            var config = new MapperConfiguration(cfg =>
                                                 cfg.CreateMap <Vehicle, VehicleDetailsJsonModel>()
                                                 //.ForMember(dest => dest.Speed, opt => opt.MapFrom(src => src.MaxSpeed))
                                                 .ForMember(dest => dest.Class, opt => opt.MapFrom(src => src.VehicleClass))
                                                 .ForMember(dest => dest.Cost, opt => opt.MapFrom(src => src.CostInCredits))
                                                 );

            var mapper = config.CreateMapper();

            model.Vehicles = mapper.Map <List <VehicleDetailsJsonModel> >(vehicles);
            model.Vehicles = model.Vehicles.OrderBy(p => p.Name).ToList();
            return(model);
        }