コード例 #1
0
        //[Route("{id}")]
        public HttpResponseMessage GetCarById(int id)
        {
            try
            {
                ApplicationMessage appmessage  = new ApplicationMessage();
                CarDetailsResponse carResponse = new CarDetailsResponse();
                CarProductDetails  carresults  = new CarProductDetails();

                List <CarsDetailsDto> carlst = new List <CarsDetailsDto>();
                carresults.appCarDetails = CarsRepository.GetProductById(id);
                carResponse.results      = carresults;
                carresults.apiMessage    = appmessage.carproductSuccessfully;
                carResponse.status       = true;
                carResponse.message      = appmessage.executedSuccessfully;

                List <CarImages>    lstimages   = new List <CarImages>();
                List <CarImagesDto> lstimages2  = new List <CarImagesDto>();
                CarImagesDto        carImagesdd = new CarImagesDto();
                lstimages = CarsRepository.GetAllImages();



                if (carresults.appCarDetails != null)
                {
                    string imgPath = ConfigurationManager.AppSettings["imgPath"];
                    foreach (var item in carresults.appCarDetails)
                    {
                        CarsDetailsDto carsProduct = new CarsDetailsDto();
                        carsProduct.CarsProductId          = item.CarsProductId;
                        carsProduct.CarsProductName1       = item.CarsProductName1;
                        carsProduct.CarsProductDescription = item.CarsProductDescription;
                        carsProduct.CarsPrice      = item.CarsPrice;
                        carsProduct.CarsColor      = item.CarsColor;
                        carsProduct.CarsKilometers = item.CarsKilometers;
                        carsProduct.CarsDoors      = item.CarsDoors;
                        foreach (var mg in lstimages)
                        {
                            if (mg.SessionId == item.CarsProductId)
                            {
                                carImagesdd.ProductImages = imgPath + mg.CarsImages;
                                lstimages2.Add(carImagesdd);
                            }

                            //carsProduct.CarsImages = imgPath + item.CarsImages;
                        }
                        carsProduct.CarsImagess = lstimages2;
                        carlst.Add(carsProduct);
                    }
                    carresults.appCarDetails = carlst;
                    //carresponse.results = carlst;
                    var response = this.Request.CreateResponse(HttpStatusCode.OK);
                    response.Content = new StringContent(JsonConvert.SerializeObject(carResponse, Formatting.None), Encoding.UTF8, "application/json");
                    return(response);
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not found"));
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }