public async Task <SingleProductDto> GetSingleProduct(string userId, string productUri) { var product = await _context.Products .Where(p => p.Uri.Equals(productUri)) .Include(p => p.Images) .Include(p => p.Reviews) .Include(p => p.Seller) .FirstOrDefaultAsync(); if (product == null) { throw new KeyNotFoundException("Product not found"); } return(SingleProductDto.Create(userId, product)); }
public IActionResult SingleProduct([FromRoute] string ProductNameUrl) { var locale = Request.HttpContext.Features.Get <IRequestCultureFeature>(); string culture = locale.RequestCulture.UICulture.ToString(); string comingUrl1 = ProductNameUrl; string x = Request.Headers["Referer"]; var url = _httpContextAccessor.HttpContext?.Request?.GetDisplayUrl(); string[] urlParts2 = x.Split("/"); string[] urlParts3 = url.Split("/"); string comingUrl2 = urlParts2[urlParts2.Length - 1]; string comingUrl3 = urlParts3[urlParts3.Length - 1]; string finalProNameUrl = !comingUrl1.Equals("placeholder.gif") ? comingUrl1 : !comingUrl2.Equals("placeholder.gif") ? comingUrl2 : comingUrl3; try { var product = _productRepo.GetProductWithNameUrl(finalProNameUrl, "tr") ?? _productRepo.GetProductWithNameUrl(finalProNameUrl, "en"); if (product == null) { HttpContext.Session.SetString("alert", _localizer["Ürün bulunamadı."]); return(LocalRedirect("/")); } ViewBag.metaTitle = culture.Equals("tr") ? product.HeadTitleTR : culture.Equals("ru") ? product.HeadTitleRU : product.HeadTitleEN; ViewBag.metaDesc = culture.Equals("tr") ? product.HeadDescriptionTR : culture.Equals("ru") ? product.HeadDescriptionRU : product.HeadDescriptionEN; bool favPros = false; if (HttpContext.Session.GetObject <SessionDto>("customerInfo") != null) { var customer = _customerRepo.GetEntityById(HttpContext.Session.GetObject <SessionDto>("customerInfo").Id); favPros = _fpRepo.AnyEntity(x => x.CustomerId == customer.Id && x.ProductId == product.Id); } SingleProductDto dto = new SingleProductDto(); dto.Id = product.Id; dto.Name = culture.Equals("tr") ? product.NameTR : culture.Equals("ru") ? product.NameRU : product.NameEN; dto.ProductDescription = culture.Equals("tr") ? product.ProductDescriptionTR : culture.Equals("ru") ? product.ProductDescriptionRU : product.ProductDescriptionEN; dto.MainPhotoFileName = product.MainPhotoFileName; dto.PhotoAltTag = culture.Equals("tr") ? product.PhotoAltTagTR : culture.Equals("ru") ? product.PhotoAltTagRU : product.PhotoAltTagEN; dto.ProductCode = product.ProductCode; dto.DiscountRate = product.DiscountRate; dto.HasNewBadge = product.HasNewBadge; dto.IsFreeShipping = product.IsFreeShipping; dto.HeadTitle = culture.Equals("tr") ? product.HeadTitleTR : culture.Equals("ru") ? product.HeadTitleRU : product.HeadTitleEN; dto.HeadDescription = culture.Equals("tr") ? product.HeadDescriptionTR : culture.Equals("ru") ? product.HeadDescriptionRU : product.HeadDescriptionEN; dto.BrandName = product.Brand.BrandName; dto.TopCategoryName = culture.Equals("tr") ? product.TopCategory.NameTR : culture.Equals("ru") ? product.TopCategory.NameRU : product.TopCategory.NameEN; dto.TopCategoryNameUrl = culture.Equals("tr") ? product.TopCategory.TopCategoryNameUrlTR : product.TopCategory.TopCategoryNameUrlEN; dto.MiddleCategoryName = culture.Equals("tr") ? product.MiddleCategory?.NameTR : culture.Equals("ru") ? product.MiddleCategory?.NameRU : product.MiddleCategory?.NameEN; dto.MiddleCategoryNameUrl = culture.Equals("tr") ? product.MiddleCategory?.MiddleCategoryNameUrlTR : product.MiddleCategory?.MiddleCategoryNameUrlEN; dto.SubCategoryName = culture.Equals("tr") ? product.SubCategory?.NameTR : culture.Equals("ru") ? product.SubCategory?.NameRU : product.SubCategory?.NameEN; dto.SubCategoryNameUrl = culture.Equals("tr") ? product.SubCategory?.SubCategoryNameUrlTR : product.SubCategory?.SubCategoryNameUrlEN; dto.ProductOfferType = product.ProductOfferType; dto.IsFavorite = favPros; dto.PhotoAltTag = culture.Equals("tr") ? product.PhotoAltTagTR : culture.Equals("ru") ? product.PhotoAltTagRU : product.PhotoAltTagEN; foreach (var item in product.ProductPhotos.Where(x => !x.IsMainPhoto)) { SingleProductPhotoDto photoDto = new SingleProductPhotoDto(); photoDto.Id = item.Id; photoDto.PhotoFileName = item.PhotoFileName; dto.ProductPhotoDtos.Add(photoDto); } return(View(dto)); } catch (Exception e) { _errorRepo.CreateEntity(new ErrorLog { ErrorLocation = "SingleProduct", ErrorDetail = e.Message + " , " + e.InnerException + $"comingUrl1:{comingUrl1}, comingUrl2:{comingUrl2}, comingUrl3:{comingUrl3}, finalProNameUrl:{finalProNameUrl}", ErrorUrl = HttpContext.Request.Path }); HttpContext.Session.SetString("alert", _localizer["Hata oluştu ve kayıt altına alındı."]); return(LocalRedirect("/")); } }