예제 #1
0
        public async Task<IHttpActionResult> GetCars(string year, string make, string model, string trim)
        {
            var carList = new List<Car>();
            try
            {
                
                //if (trim == null || trim == "")
                //{
                //    carList = db.Database.SqlQuery<Car>("EXEC GetCarsByYearMakeAndModel @year, @make, @model",
                //        new SqlParameter("year", year),
                //        new SqlParameter("make", make),
                //        new SqlParameter("model", model)).ToList();
                //}
                //else
                //{
                carList = db.Database.SqlQuery<Car>("EXEC GetCars @year, @make, @model, @trim",
                    new SqlParameter("year", year ?? ""),
                    new SqlParameter("make", make ?? ""),
                    new SqlParameter("model", model ?? ""),
                    new SqlParameter("trim", trim ?? "")).ToList();
                //}
            }
            catch(Exception ex) 
            {
                var err = ex.ToString();        
            }

            var car = carList[0];
            CarViewModel carView = new CarViewModel();
            carView.Car = car;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://www.nhtsa.gov/");
                try
                {
                    var response = await client.GetAsync("webapi/api/Recalls/vehicle/modelyear/" +
                        car.model_year.ToString() + "/make/" +
                        car.make + "/model/" +
                        car.model_name + "?format=json");
                    carView.Recalls = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());

                }
                catch (Exception e)
                {
                    return InternalServerError(e);
                }
            }

            //Get car images using Bing Search API
            var imageResults = GetImages(year, make, model, trim);
            List<string> images = new List<string>();
            foreach (var result in imageResults)
            {
                images.Add(result.MediaUrl);
            }
            carView.Images = images;
            //carView.Images = JsonConvert.DeserializeObject(images);


            //using (var imageClient = new WebClient())
            //{
            //    try
            //    {
            //        var temp = imageClient.DownloadString(
            //            "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" +
            //            year + "%20" + make + "%20" + model + "%20" + (string.IsNullOrEmpty(trim) ? "" : ("%20" + trim))
            //            );
            //        carView.Images = JsonConvert.DeserializeObject(temp);
            //    }
            //    catch (Exception e)
            //    {
            //        return InternalServerError(e);
            //    }
            //}


            return Ok(carView);

        }
예제 #2
0
        public async Task <IHttpActionResult> GetCars(string year, string make, string model, string trim)
        {
            var carList = new List <Car>();

            try
            {
                //if (trim == null || trim == "")
                //{
                //    carList = db.Database.SqlQuery<Car>("EXEC GetCarsByYearMakeAndModel @year, @make, @model",
                //        new SqlParameter("year", year),
                //        new SqlParameter("make", make),
                //        new SqlParameter("model", model)).ToList();
                //}
                //else
                //{
                carList = db.Database.SqlQuery <Car>("EXEC GetCars @year, @make, @model, @trim",
                                                     new SqlParameter("year", year ?? ""),
                                                     new SqlParameter("make", make ?? ""),
                                                     new SqlParameter("model", model ?? ""),
                                                     new SqlParameter("trim", trim ?? "")).ToList();
                //}
            }
            catch (Exception ex)
            {
                var err = ex.ToString();
            }

            var          car     = carList[0];
            CarViewModel carView = new CarViewModel();

            carView.Car = car;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://www.nhtsa.gov/");
                try
                {
                    var response = await client.GetAsync("webapi/api/Recalls/vehicle/modelyear/" +
                                                         car.model_year.ToString() + "/make/" +
                                                         car.make + "/model/" +
                                                         car.model_name + "?format=json");

                    carView.Recalls = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
                }
                catch (Exception e)
                {
                    return(InternalServerError(e));
                }
            }

            //Get car images using Bing Search API
            var           imageResults = GetImages(year, make, model, trim);
            List <string> images       = new List <string>();

            foreach (var result in imageResults)
            {
                images.Add(result.MediaUrl);
            }
            carView.Images = images;
            //carView.Images = JsonConvert.DeserializeObject(images);


            //using (var imageClient = new WebClient())
            //{
            //    try
            //    {
            //        var temp = imageClient.DownloadString(
            //            "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" +
            //            year + "%20" + make + "%20" + model + "%20" + (string.IsNullOrEmpty(trim) ? "" : ("%20" + trim))
            //            );
            //        carView.Images = JsonConvert.DeserializeObject(temp);
            //    }
            //    catch (Exception e)
            //    {
            //        return InternalServerError(e);
            //    }
            //}


            return(Ok(carView));
        }